Skip to content

Commit

Permalink
Display tables using the less terminal pager (#25)
Browse files Browse the repository at this point in the history
* Display tables using the less terminal pager

* Review adjustments

* Fix lint

* Use command out instead of std out

* Rename variable
  • Loading branch information
joaopalet authored Nov 24, 2023
1 parent bfe8432 commit 53fe3cb
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 8 deletions.
5 changes: 4 additions & 1 deletion internal/cmd/dns/record-set/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ func NewCmd() *cobra.Command {
rs := recordSets[i]
table.AddRow(*rs.Id, *rs.Name, *rs.Type, *rs.State)
}
table.Render(cmd)
err = table.Render(cmd)
if err != nil {
return fmt.Errorf("render table: %w", err)
}

return nil
},
Expand Down
5 changes: 4 additions & 1 deletion internal/cmd/dns/zone/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ func NewCmd() *cobra.Command {
z := zones[i]
table.AddRow(*z.Id, *z.Name, *z.DnsName, *z.State)
}
table.Render(cmd)
err = table.Render(cmd)
if err != nil {
return fmt.Errorf("render table: %w", err)
}

return nil
},
Expand Down
5 changes: 4 additions & 1 deletion internal/cmd/postgresql/credential/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ func NewCmd() *cobra.Command {
c := credentials[i]
table.AddRow(*c.Id)
}
table.Render(cmd)
err = table.Render(cmd)
if err != nil {
return fmt.Errorf("render table: %w", err)
}

return nil
},
Expand Down
5 changes: 4 additions & 1 deletion internal/cmd/postgresql/instance/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ func NewCmd() *cobra.Command {
instance := instances[i]
table.AddRow(*instance.InstanceId, *instance.Name, *instance.LastOperation.Type, *instance.LastOperation.State)
}
table.Render(cmd)
err = table.Render(cmd)
if err != nil {
return fmt.Errorf("render table: %w", err)
}

return nil
},
Expand Down
5 changes: 4 additions & 1 deletion internal/cmd/postgresql/offering/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ func NewCmd() *cobra.Command {
table.AddSeparator()
}
table.EnableAutoMergeOnColumns(1)
table.Render(cmd)
err = table.Render(cmd)
if err != nil {
return fmt.Errorf("render table: %w", err)
}

return nil
},
Expand Down
5 changes: 4 additions & 1 deletion internal/cmd/ske/cluster/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ func NewCmd() *cobra.Command {
c := clusters[i]
table.AddRow(*c.Name, *c.Status.Aggregated)
}
table.Render(cmd)
err = table.Render(cmd)
if err != nil {
return fmt.Errorf("render table: %w", err)
}

return nil
},
Expand Down
20 changes: 18 additions & 2 deletions internal/pkg/tables/tables.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package tables

import (
"fmt"
"os/exec"
"strings"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -42,11 +46,23 @@ func (t *Table) EnableAutoMergeOnColumns(columns ...int) {
}

// Renders the table
func (t *Table) Render(cmd *cobra.Command) {
func (t *Table) Render(cmd *cobra.Command) error {
t.table.SetStyle(table.StyleLight)
t.table.Style().Options.DrawBorder = false
t.table.Style().Options.SeparateRows = false
t.table.Style().Options.SeparateColumns = true
t.table.Style().Options.SeparateHeader = true
cmd.Printf("\n%s\n\n", t.table.Render())

renderedTable := fmt.Sprintf("\n%s\n\n", t.table.Render())

lessCmd := exec.Command("less", "-F", "-S", "-w")
lessCmd.Stdin = strings.NewReader(renderedTable)
lessCmd.Stdout = cmd.OutOrStdout()

err := lessCmd.Run()
if err != nil {
return fmt.Errorf("run less command: %w", err)
}

return nil
}

0 comments on commit 53fe3cb

Please sign in to comment.