-
Notifications
You must be signed in to change notification settings - Fork 34
feat: support --format text for dump ui command #382
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
+151
−4
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package types | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
||
| // FormatText renders a ref-annotated tree as indented lines, one element per | ||
| // line, e.g. ` @e7 [Button] "Add" #add-button (disabled)`. It is far more | ||
| // compact than JSON, which matters when the tree is fed to a model. | ||
| // Call AttachRefs first, otherwise refs render empty. | ||
| func FormatText(elements []ScreenElement) string { | ||
| var builder strings.Builder | ||
| writeElementLines(&builder, elements, 0) | ||
| return builder.String() | ||
| } | ||
|
|
||
| func writeElementLines(builder *strings.Builder, elements []ScreenElement, depth int) { | ||
| for _, element := range elements { | ||
| builder.WriteString(strings.Repeat(" ", depth)) | ||
| builder.WriteString(formatElementLine(element)) | ||
| builder.WriteString("\n") | ||
| writeElementLines(builder, element.Children, depth+1) | ||
| } | ||
| } | ||
|
|
||
| func formatElementLine(element ScreenElement) string { | ||
| parts := []string{fmt.Sprintf("@%s [%s]", element.Ref, element.Type)} | ||
|
|
||
| if description := elementDescription(element); description != "" { | ||
| parts = append(parts, fmt.Sprintf("%q", description)) | ||
| } | ||
|
|
||
| // The identifier is what a caller would write a selector against, so it stays | ||
| // on the line even when a label is already shown. | ||
| if element.Identifier != nil && *element.Identifier != "" { | ||
| parts = append(parts, "#"+*element.Identifier) | ||
| } | ||
|
|
||
| if states := elementStates(element); states != "" { | ||
| parts = append(parts, states) | ||
| } | ||
|
|
||
| return strings.Join(parts, " ") | ||
| } | ||
|
|
||
| // elementDescription is the first non-empty human-readable field, in the order | ||
| // an agent would care about. | ||
| func elementDescription(element ScreenElement) string { | ||
| for _, candidate := range []*string{element.Text, element.Label, element.Name, element.Value, element.Placeholder} { | ||
| if candidate != nil && *candidate != "" { | ||
| return *candidate | ||
| } | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| func elementStates(element ScreenElement) string { | ||
| var states []string | ||
| if element.Enabled != nil && !*element.Enabled { | ||
| states = append(states, "disabled") | ||
| } | ||
| if element.Checked != nil && *element.Checked { | ||
| states = append(states, "checked") | ||
| } | ||
| if element.Selected != nil && *element.Selected { | ||
| states = append(states, "selected") | ||
| } | ||
| if element.Focused != nil && *element.Focused { | ||
| states = append(states, "focused") | ||
| } | ||
| if len(states) == 0 { | ||
| return "" | ||
| } | ||
| return "(" + strings.Join(states, ", ") + ")" | ||
| } |
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,54 @@ | ||
| package types | ||
|
|
||
| import "testing" | ||
|
|
||
| func stringPtr(value string) *string { | ||
| return &value | ||
| } | ||
|
|
||
| func boolPtr(value bool) *bool { | ||
| return &value | ||
| } | ||
|
|
||
| func TestFormatTextIndentsTreeAndShowsRefsLabelsAndStates(t *testing.T) { | ||
| elements := []ScreenElement{ | ||
| { | ||
| Type: "Window", | ||
| Children: []ScreenElement{ | ||
| { | ||
| Type: "Button", | ||
| Label: stringPtr("Add"), | ||
| Identifier: stringPtr("add-button"), | ||
| Enabled: boolPtr(false), | ||
| }, | ||
| { | ||
| Type: "TextField", | ||
| Placeholder: stringPtr("First name"), | ||
| Focused: boolPtr(true), | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| AttachRefs(elements) | ||
|
|
||
| got := FormatText(elements) | ||
| want := "@e1 [Window]\n" + | ||
| " @e2 [Button] \"Add\" #add-button (disabled)\n" + | ||
| " @e3 [TextField] \"First name\" (focused)\n" | ||
|
|
||
| if got != want { | ||
| t.Fatalf("got:\n%s\nwant:\n%s", got, want) | ||
| } | ||
| } | ||
|
|
||
| func TestFormatTextPrefersTextOverOtherLabels(t *testing.T) { | ||
| elements := []ScreenElement{ | ||
| {Type: "StaticText", Text: stringPtr("Hello"), Label: stringPtr("ignored")}, | ||
| } | ||
| AttachRefs(elements) | ||
|
|
||
| want := "@e1 [StaticText] \"Hello\"\n" | ||
| if got := FormatText(elements); got != want { | ||
| t.Fatalf("got %q, want %q", got, want) | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Print empty text dumps as text output.
When
--format textreturns an empty UI tree,FormatTextreturns"". Line 32 then falls back toprintJson, so the command returns JSON instead of the requested text format. Select the direct-output path from the requested format, not from whether the formatted text is non-empty.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents