Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{cut,list}: add header #43

Merged
merged 1 commit into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions internal/cut/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"os"
"regexp"
"sort"
"strings"
"text/tabwriter"
Expand All @@ -21,9 +22,11 @@ import (

type Command struct {
printStandard bool
noAlign bool
format string
exclude string

noAlign bool
header string
format string
}

func (*Command) Name() string { return "cut" }
Expand All @@ -40,9 +43,11 @@ func (*Command) Usage() string {

func (cmd *Command) SetFlags(f *flag.FlagSet) {
f.BoolVar(&cmd.printStandard, "std", false, "print std packages")
f.BoolVar(&cmd.noAlign, "noalign", false, "disable aligning tabs")
f.StringVar(&cmd.format, "f", "{{.ID}}\tin:{{.InDegree}}\tpkgs:{{.Cut.PackageCount}}\tsize:{{.Cut.AllFiles.Size}}\tloc:{{.Cut.Go.Lines}}", "info formatting")
f.StringVar(&cmd.exclude, "exclude", "", "package expr to exclude from output")

f.BoolVar(&cmd.noAlign, "noalign", false, "disable aligning tabs")
f.StringVar(&cmd.header, "h", "", "header for the table\nautomatically derives from format, when empty, use \"-\" to skip")
f.StringVar(&cmd.format, "f", "{{.ID}}\t{{.InDegree}}\t{{.Cut.PackageCount}}\t{{.Cut.AllFiles.Size}}\t{{.Cut.Go.Lines}}", "info formatting")
}

func (cmd *Command) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
Expand Down Expand Up @@ -127,6 +132,13 @@ func (cmd *Command) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface
if !cmd.noAlign {
w = tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
}
if cmd.header != "-" {
if cmd.header == "" {
rx := regexp.MustCompile(`(\{\{\s*\.?|\s*\}\})`)
cmd.header = rx.ReplaceAllString(cmd.format, "")
}
fmt.Fprintln(w, cmd.header)
}
for _, node := range nodelist {
if _, exclude := excluded[node.ID]; exclude {
continue
Expand Down
16 changes: 14 additions & 2 deletions internal/list/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"os"
"regexp"
"text/tabwriter"

"github.com/google/subcommands"
Expand All @@ -17,8 +18,10 @@ import (

type Command struct {
printStandard bool
noAlign bool
format string

noAlign bool
header string
format string
}

func (*Command) Name() string { return "list" }
Expand All @@ -34,7 +37,9 @@ func (*Command) Usage() string {

func (cmd *Command) SetFlags(f *flag.FlagSet) {
f.BoolVar(&cmd.printStandard, "std", false, "print std packages")

f.BoolVar(&cmd.noAlign, "noalign", false, "disable aligning tabs")
f.StringVar(&cmd.header, "h", "", "header for the table\nautomatically derives from format, when empty, use \"-\" to skip")
f.StringVar(&cmd.format, "f", "{{.ID}}", "formatting")
}

Expand Down Expand Up @@ -64,6 +69,13 @@ func (cmd *Command) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface
if !cmd.noAlign {
w = tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
}
if cmd.header != "-" {
if cmd.header == "" {
rx := regexp.MustCompile(`(\{\{\s*\.?|\s*\}\})`)
cmd.header = rx.ReplaceAllString(cmd.format, "")
}
fmt.Fprintln(w, cmd.header)
}
for _, p := range graph.Sorted {
err := t.Execute(w, p)
fmt.Fprintln(w)
Expand Down