Skip to content

Commit

Permalink
cli/list: print listings as tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Worm committed May 18, 2024
1 parent 0b6a23a commit a485910
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 37 deletions.
47 changes: 25 additions & 22 deletions flow/cmd/list.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package cmd

import (
"fmt"
"sort"
"github.com/codemodus/kace"
"github.com/olekukonko/tablewriter"

"github.com/hofstadter-io/hof/cmd/hof/flags"
"github.com/hofstadter-io/hof/lib/yagu"
)

func List(args []string, rflags flags.RootPflagpole, cflags flags.FlowPflagpole) error {
Expand All @@ -13,25 +14,27 @@ func List(args []string, rflags flags.RootPflagpole, cflags flags.FlowPflagpole)
return err
}

fmt.Println("Available Generators")
flows := make([]string, 0, len(R.Workflows))
for _, G := range R.Workflows {
fmt.Println(" ", G.Hof.Flow.Name)
flows = append(flows, G.Hof.Flow.Name)
}
sort.Strings(flows)
return yagu.PrintAsTable(
[]string{"Name", "Path", "ID"},
func(table *tablewriter.Table) ([][]string, error) {
var rows = make([][]string, 0, len(R.Workflows))
// fill with data
for _, wf := range R.Workflows {
id := wf.Hof.Metadata.ID
if id == "" {
id = kace.Snake(wf.Hof.Metadata.Name) + " (auto)"
}

name := wf.Hof.Flow.Name
if name == "" {
name = "(anon)"
}
path := wf.Hof.Path

// TODO...
// 1. use table printer
// 2. move this command up, large blocks of this ought
//flows := make([]string, 0, len(R.Workflows))
//for _, G := range R.Workflows {
// flows = append(flows, G.Hof.Flow.Name)
//}
//sort.Strings(flows)
//fmt.Printf("Available Generators\n ")
//fmt.Println(strings.Join(flows, "\n "))

// print gens
return nil
row := []string{name, path, id}
rows = append(rows, row)
}
return rows, nil
},
)
}
57 changes: 42 additions & 15 deletions lib/gen/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package cmd

import (
"fmt"
"strings"

"github.com/codemodus/kace"
"github.com/olekukonko/tablewriter"

"github.com/hofstadter-io/hof/cmd/hof/flags"
"github.com/hofstadter-io/hof/lib/yagu"
)

func List(args []string, rflags flags.RootPflagpole, gflags flags.GenFlagpole) error {
Expand All @@ -13,19 +16,43 @@ func List(args []string, rflags flags.RootPflagpole, gflags flags.GenFlagpole) e
return err
}

// TODO...
// 1. use table printer
// 2. move this command up, large blocks of this ought
gens := make([]string, 0, len(R.Generators))
for _, G := range R.Generators {
gens = append(gens, G.Hof.Metadata.Name)
}
if len(gens) == 0 {
return fmt.Errorf("no generators found")
}
fmt.Printf("Available Generators\n ")
fmt.Println(strings.Join(gens, "\n "))
return yagu.PrintAsTable(
[]string{"Name", "Path", "ID", "Creator"},
func(table *tablewriter.Table) ([][]string, error) {
var rows = make([][]string, 0, len(R.Generators))
// fill with data
for _, gen := range R.Generators {
id := gen.Hof.Metadata.ID
if id == "" {
id = kace.Snake(gen.Hof.Metadata.Name) + " (auto)"
}

name := gen.Hof.Gen.Name
if name == "" {
name = "(anon)"
}
path := gen.Hof.Path

row := []string{name, path, id, fmt.Sprint(gen.Hof.Gen.Creator)}
rows = append(rows, row)
}
return rows, nil
},
)

// // TODO...
// // 1. use table printer
// // 2. move this command up, large blocks of this ought
// gens := make([]string, 0, len(R.Generators))
// for _, G := range R.Generators {
// gens = append(gens, G.Hof.Metadata.Name)
// }
// if len(gens) == 0 {
// return fmt.Errorf("no generators found")
// }
// fmt.Printf("Available Generators\n ")
// fmt.Println(strings.Join(gens, "\n "))

// print gens
return nil
// // print gens
// return nil
}
4 changes: 4 additions & 0 deletions lib/hof/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func upgradeAttrs[T any](node *Node[T], label string) bool {
case "gen":
node.Hof.Gen.Root = true
node.Hof.Gen.Name = ac
c := val.LookupPath(cue.ParsePath("Create"))
if c.Exists() {
node.Hof.Gen.Creator = true
}

// this doesnt handle empty @flow()
case "flow":
Expand Down
42 changes: 42 additions & 0 deletions lib/yagu/table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package yagu

import (
"os"

"github.com/olekukonko/tablewriter"
)

func DefaultTableFormat(table *tablewriter.Table) {
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetTablePadding(" ")
table.SetNoWhiteSpace(true)
}

type DataPrinter func(table *tablewriter.Table) ([][]string, error)

func PrintAsTable(headers []string, printer DataPrinter) error {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(headers)

DefaultTableFormat(table)

rows, err := printer(table)
if err != nil {
return err
}

table.AppendBulk(rows)

// render
table.Render()

return nil
}

0 comments on commit a485910

Please sign in to comment.