-
Notifications
You must be signed in to change notification settings - Fork 6
Wrap logs correctly at terminal width #361
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
Open
jw2
wants to merge
3
commits into
main
Choose a base branch
from
jw2/log-full-terminal-width
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+234
−7
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,101 @@ | ||
| package ui | ||
|
|
||
| import ( | ||
| "regexp" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/charmbracelet/lipgloss" | ||
| "github.com/localstack/lstk/internal/output" | ||
| "github.com/localstack/lstk/internal/ui/styles" | ||
| "github.com/localstack/lstk/internal/ui/wrap" | ||
| "github.com/muesli/termenv" | ||
| ) | ||
|
|
||
| var ansiRegexp = regexp.MustCompile(`\x1b\[[0-9;]*m`) | ||
|
|
||
| func stripANSI(s string) string { | ||
| return ansiRegexp.ReplaceAllString(s, "") | ||
| } | ||
|
|
||
| func TestRenderLogLineWrapsPlainTextAtAvailableWidth(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| got := stripANSI(renderLogLine("abcdefghij", output.LogLevelInfo, 4, 0)) | ||
| want := "abcd\nefgh\nij" | ||
| if got != want { | ||
| t.Fatalf("expected %q, got %q", want, got) | ||
| } | ||
| } | ||
|
|
||
| func TestRenderLogLineUsesVisiblePrefixWidth(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| prefix := styles.Secondary.Render("container | ") | ||
| prefixWidth := lipgloss.Width(prefix) | ||
|
|
||
| got := stripANSI(prefix + renderLogLine("abcdefghijklmnop", output.LogLevelInfo, 20-prefixWidth, prefixWidth)) | ||
| lines := strings.Split(got, "\n") | ||
| if len(lines) != 2 { | ||
| t.Fatalf("expected 2 lines, got %d: %q", len(lines), got) | ||
| } | ||
| if lines[0] != "container | abcdefgh" { | ||
| t.Fatalf("unexpected first line: %q", lines[0]) | ||
| } | ||
| if lines[1] != " ijklmnop" { | ||
| t.Fatalf("unexpected continuation line: %q", lines[1]) | ||
| } | ||
| } | ||
|
|
||
| func TestRenderLogLineWrapsMetaAndMessage(t *testing.T) { | ||
| // Asserts on the raw styled output, so it forces a TrueColor profile. That | ||
| // mutates the global lipgloss color profile, so it must not run in parallel. | ||
| original := lipgloss.ColorProfile() | ||
| lipgloss.SetColorProfile(termenv.TrueColor) | ||
| t.Cleanup(func() { lipgloss.SetColorProfile(original) }) | ||
|
|
||
| line := "abcd : WXYZ" | ||
| got := renderLogLine(line, output.LogLevelWarn, 4, 0) | ||
| want := wrap.HardWrap(line, 4) | ||
|
|
||
| if stripANSI(got) != want { | ||
| t.Fatalf("expected %q, got %q", want, stripANSI(got)) | ||
| } | ||
|
|
||
| lines := strings.Split(got, "\n") | ||
| if len(lines) != 3 { | ||
| t.Fatalf("expected 3 wrapped lines, got %d: %q", len(lines), got) | ||
| } | ||
| if lines[0] != styles.Secondary.Render("abcd") { | ||
| t.Fatalf("unexpected first line styling: %q", lines[0]) | ||
| } | ||
| if lines[1] != styles.Secondary.Render(" : ")+styles.Warning.Render("W") { | ||
| t.Fatalf("unexpected mixed meta/message styling: %q", lines[1]) | ||
| } | ||
| if lines[2] != styles.Warning.Render("XYZ") { | ||
| t.Fatalf("unexpected final message styling: %q", lines[2]) | ||
| } | ||
| } | ||
|
|
||
| func TestRenderLogLineIndentsContinuationLines(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| got := stripANSI(renderLogLine("abcdefghij", output.LogLevelInfo, 4, 3)) | ||
| want := "abcd\n efgh\n ij" | ||
| if got != want { | ||
| t.Fatalf("expected %q, got %q", want, got) | ||
| } | ||
| } | ||
|
|
||
| func TestRenderLogLineZeroWidthPassesThrough(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| got := stripANSI(renderLogLine("meta : payload", output.LogLevelWarn, 0, 12)) | ||
| want := "meta : payload" | ||
| if got != want { | ||
| t.Fatalf("expected %q, got %q", want, got) | ||
| } | ||
| if strings.Contains(got, "\n") { | ||
| t.Fatalf("expected zero-width passthrough, got %q", got) | ||
| } | ||
| } |
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,4 @@ | ||
| container | abcdefgh | ||
| ijklmnop | ||
| qrstuvwx | ||
| yz |
22 changes: 22 additions & 0 deletions
22
openspec/changes/logs-follow-full-terminal-width/proposal.md
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,22 @@ | ||
| ## Why | ||
|
|
||
| `lstk logs --follow` renders log lines narrower than the terminal because `HardWrap` counts ANSI escape code characters as visible width. Styled prefixes (e.g. `styles.Secondary.Render("container | ")`) embed invisible escape sequences that are included in the rune count, so wrapping fires before the visible content reaches the terminal edge. | ||
|
|
||
| ## What Changes | ||
|
|
||
| - Log lines are wrapped using ANSI-aware width calculation so they fill the full terminal width. | ||
| - The `renderLogLine` rendering path in `app.go` is updated to measure visible prefix width and wrap the message content independently before re-combining with the styled prefix. | ||
|
|
||
| ## Capabilities | ||
|
|
||
| ### New Capabilities | ||
|
|
||
| - `logs-full-width-rendering`: Log lines in `--follow` mode wrap at the correct terminal width by accounting for invisible ANSI sequences in styled prefixes. | ||
|
|
||
| ### Modified Capabilities | ||
|
|
||
| ## Impact | ||
|
|
||
| - `internal/ui/logrender.go` — `renderLogLine` signature or callers updated to accept a `width` parameter | ||
| - `internal/ui/app.go` — `output.LogLineEvent` handler uses ANSI-aware wrapping | ||
| - `internal/ui/wrap/` — may need a new ANSI-aware wrap helper | ||
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.
issue: can you remove this spec? We don't usually add any of those. Only Peter commited a couple when he worked on bigger features, but this is just a small bug fix.