From e0c3011f1dce081e741eb29d48a48fb1f21df6d1 Mon Sep 17 00:00:00 2001 From: everettraven Date: Sun, 3 Dec 2023 11:05:33 -0500 Subject: [PATCH] (bugfix): fix word wrapping on logs to prevent weird munging of the log output when scrolling vertically in the viewport Signed-off-by: everettraven --- go.mod | 2 +- pkg/charm/models/panels/logs.go | 23 ++++------------------- pkg/charm/models/tabber.go | 5 ++++- 3 files changed, 9 insertions(+), 21 deletions(-) diff --git a/go.mod b/go.mod index 50557a0..a5a6760 100644 --- a/go.mod +++ b/go.mod @@ -55,7 +55,7 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70 // indirect github.com/muesli/cancelreader v0.2.2 // indirect - github.com/muesli/reflow v0.3.0 // indirect + github.com/muesli/reflow v0.3.0 github.com/muesli/termenv v0.15.1 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/rivo/uniseg v0.2.0 // indirect diff --git a/pkg/charm/models/panels/logs.go b/pkg/charm/models/panels/logs.go index b752387..762724e 100644 --- a/pkg/charm/models/panels/logs.go +++ b/pkg/charm/models/panels/logs.go @@ -12,6 +12,7 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" "github.com/everettraven/buoy/pkg/charm/styles" + "github.com/muesli/reflow/wrap" "github.com/sahilm/fuzzy" ) @@ -77,8 +78,9 @@ func NewLogs(keys LogsKeyMap, name string) *Logs { searchbar := textinput.New() searchbar.Prompt = "> " searchbar.Placeholder = "search term" + vp := viewport.New(10, 10) return &Logs{ - viewport: viewport.New(10, 10), + viewport: vp, searchbar: searchbar, name: name, mutex: &sync.Mutex{}, @@ -250,22 +252,5 @@ func matched(index int, matches []int) bool { } func wrapLogs(logs string, maxWidth int) string { - splitLogs := strings.Split(logs, "\n") - var logsBuilder strings.Builder - for _, log := range splitLogs { - if len(log) > maxWidth { - segs := (len(log) / maxWidth) - for seg := 0; seg < segs; seg++ { - logsBuilder.WriteString(log[:maxWidth]) - logsBuilder.WriteString("\n") - log = log[maxWidth:] - } - //write any leftovers - logsBuilder.WriteString(log) - } else { - logsBuilder.WriteString(log) - } - logsBuilder.WriteString("\n") - } - return logsBuilder.String() + return wrap.String(logs, maxWidth) } diff --git a/pkg/charm/models/tabber.go b/pkg/charm/models/tabber.go index 4bf6301..e1858bd 100644 --- a/pkg/charm/models/tabber.go +++ b/pkg/charm/models/tabber.go @@ -38,7 +38,6 @@ func (t *Tabber) Init() tea.Cmd { } func (t *Tabber) Update(msg tea.Msg) (*Tabber, tea.Cmd) { - var cmd tea.Cmd switch msg := msg.(type) { case tea.KeyMsg: switch { @@ -47,14 +46,17 @@ func (t *Tabber) Update(msg tea.Msg) (*Tabber, tea.Cmd) { if t.selected > len(t.tabs)-1 { t.selected = 0 } + return t, tea.ClearScreen case key.Matches(msg, t.keyMap.TabLeft): t.selected-- if t.selected < 0 { t.selected = len(t.tabs) - 1 } + return t, tea.ClearScreen } case tea.WindowSizeMsg: t.width = msg.Width + var cmd tea.Cmd for i := range t.tabs { var tempCmd tea.Cmd t.tabs[i].Model, tempCmd = t.tabs[i].Model.Update(msg) @@ -63,6 +65,7 @@ func (t *Tabber) Update(msg tea.Msg) (*Tabber, tea.Cmd) { return t, cmd } + var cmd tea.Cmd t.tabs[t.selected].Model, cmd = t.tabs[t.selected].Model.Update(msg) return t, cmd }