-
Notifications
You must be signed in to change notification settings - Fork 0
fix(api): prevent excessive padding from blank lines and large lists #113
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c7ca800
feat(task): pluggable LiveRenderer for custom live/final rendering
moshloop a792e3d
fix(api): prevent blank lines in tree labels and large description li…
moshloop f855c0b
feat(docs): add docs generation command with CLI reference and UI cat…
moshloop dc3a3ee
refactor(docs): simplify docs generation to write flat markdown files…
moshloop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "regexp" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/charmbracelet/x/ansi" | ||
| ) | ||
|
|
||
| func TestNormalizeTreeLabel(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input string | ||
| expected string | ||
| }{ | ||
| {"single line", "hello", "hello"}, | ||
| {"clean multiline", "a\nb\nc", "a\nb\nc"}, | ||
| {"leading blank", "\nhello", "hello"}, | ||
| {"trailing blank", "hello\n", "hello"}, | ||
| {"internal blank dropped", "a\n\nb", "a\nb"}, | ||
| {"doubled internal blank dropped", "a\n\n\nb", "a\nb"}, | ||
| {"trailing spaces trimmed", "a \nb\t", "a\nb"}, | ||
| {"line of only spaces dropped", "a\n \nb", "a\nb"}, | ||
| {"ansi-only line dropped", "a\n\x1b[1;38;2;8;145;178m\x1b[0m\nb", "a\nb"}, | ||
| {"ansi content kept", "\x1b[1mTitle\x1b[0m\nbody", "\x1b[1mTitle\x1b[0m\nbody"}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if got := normalizeTreeLabel(tt.input); got != tt.expected { | ||
| t.Errorf("normalizeTreeLabel(%q) = %q, want %q", tt.input, got, tt.expected) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // treeNodeStub returns a Pretty() whose first node is short but whose children | ||
| // carry multi-line content with a deliberate blank-line separator — mirroring a | ||
| // gavel Test node wrapping an activity-trace detail. | ||
| type treeNodeStub struct{} | ||
|
|
||
| func (treeNodeStub) Pretty() Text { | ||
| return Text{Content: "✗ trace"}. | ||
| NewLine().Add(Text{Content: "Scheme: GL Scheme"}). | ||
| NewLine().NewLine().Add(Text{Content: "Activity 123 Foo"}) | ||
| } | ||
| func (treeNodeStub) GetChildren() []TreeNode { return nil } | ||
|
|
||
| var gutterOnly = regexp.MustCompile(`^[\s│├╰└─]*$`) | ||
|
|
||
| // TestTextTreeRender_NoBlankGutterLines is the regression guard for stray | ||
| // "│"-gutter lines: a multi-line node label with an internal blank separator | ||
| // must not produce a bare gutter line in either the plain or ANSI tree render. | ||
| func TestTextTreeRender_NoBlankGutterLines(t *testing.T) { | ||
| root := &SimpleTreeNode{ | ||
| Label: "suite", | ||
| Children: []TreeNode{ | ||
| treeNodeStub{}, | ||
| &SimpleTreeNode{Label: "sibling"}, | ||
| }, | ||
| } | ||
| tree := NewTree[TreeNode](root) | ||
|
|
||
| for _, tc := range []struct { | ||
| name string | ||
| out string | ||
| }{ | ||
| {"String", tree.String()}, | ||
| {"ANSI", tree.ANSI()}, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| for i, line := range strings.Split(tc.out, "\n") { | ||
| if gutterOnly.MatchString(ansi.Strip(line)) { | ||
| t.Errorf("blank gutter line at index %d:\n%s", i, tc.out) | ||
| } | ||
| } | ||
| for _, want := range []string{"trace", "Scheme: GL Scheme", "Activity 123 Foo", "sibling"} { | ||
| if !strings.Contains(tc.out, want) { | ||
| t.Errorf("expected %q in:\n%s", want, tc.out) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package docs | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/pflag" | ||
| ) | ||
|
|
||
| // walkCommands invokes fn for cmd and every descendant, depth-first. | ||
| func walkCommands(cmd *cobra.Command, fn func(*cobra.Command)) { | ||
| fn(cmd) | ||
| for _, sub := range cmd.Commands() { | ||
| walkCommands(sub, fn) | ||
| } | ||
| } | ||
|
|
||
| // isRunnable reports whether the command does work (has a Run/RunE) and is not | ||
| // the root. Grouping commands (no Run) are skipped in the reference body. | ||
| func isRunnable(cmd *cobra.Command) bool { | ||
| return cmd.Parent() != nil && (cmd.Run != nil || cmd.RunE != nil) | ||
| } | ||
|
|
||
| // depthBelow returns how many levels cmd sits below ancestor: 0 if cmd is the | ||
| // ancestor itself, 1 for a direct child, and so on. | ||
| func depthBelow(ancestor, cmd *cobra.Command) int { | ||
| depth := 0 | ||
| for c := cmd; c != nil && c != ancestor; c = c.Parent() { | ||
| depth++ | ||
| } | ||
| return depth | ||
| } | ||
|
|
||
| // commandPath returns the space-delimited path below the root, e.g. "stack create". | ||
| func commandPath(cmd *cobra.Command) string { | ||
| var parts []string | ||
| for c := cmd; c.Parent() != nil; c = c.Parent() { | ||
| parts = append([]string{c.Name()}, parts...) | ||
| } | ||
| return strings.Join(parts, " ") | ||
| } | ||
|
|
||
| func flagDocs(cmd *cobra.Command) []FlagDoc { | ||
| var flags []FlagDoc | ||
| seen := map[string]bool{} | ||
| collect := func(flag *pflag.Flag) { | ||
| if flag.Hidden || seen[flag.Name] { | ||
| return | ||
| } | ||
| seen[flag.Name] = true | ||
| flags = append(flags, FlagDoc{ | ||
| Name: flag.Name, | ||
| Shorthand: flag.Shorthand, | ||
| Type: flag.Value.Type(), | ||
| Default: nonEmptyDefault(flag), | ||
| Required: flagRequired(flag), | ||
| Usage: flag.Usage, | ||
| }) | ||
| } | ||
| cmd.LocalFlags().VisitAll(collect) | ||
| cmd.InheritedFlags().VisitAll(collect) | ||
| return flags | ||
| } | ||
|
|
||
| func nonEmptyDefault(flag *pflag.Flag) interface{} { | ||
| if flag.DefValue == "" || (flag.DefValue == "false" && flag.Value.Type() == "bool") { | ||
| return nil | ||
| } | ||
| return flag.DefValue | ||
| } | ||
|
|
||
| func flagRequired(flag *pflag.Flag) bool { | ||
| if flag.Annotations == nil { | ||
| return false | ||
| } | ||
| _, ok := flag.Annotations["cobra_annotation_bash_completion_one_required_flag"] | ||
| return ok | ||
| } | ||
|
|
||
| func title(root *cobra.Command, cfg *DocsConfig) string { | ||
| if cfg != nil && cfg.Title != "" { | ||
| return cfg.Title | ||
| } | ||
| return root.Name() | ||
| } | ||
|
|
||
| func description(root *cobra.Command, cfg *DocsConfig) string { | ||
| if cfg != nil && cfg.Description != "" { | ||
| return cfg.Description | ||
| } | ||
| if root.Long != "" { | ||
| return root.Long | ||
| } | ||
| return root.Short | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.