Skip to content

Commit

Permalink
(bugfix): fix word wrapping on logs (#37)
Browse files Browse the repository at this point in the history
to prevent weird munging of the log output
when scrolling vertically in the viewport

Signed-off-by: everettraven <everettraven@gmail.com>
  • Loading branch information
everettraven committed Dec 3, 2023
1 parent a32effc commit 0269b93
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 21 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 4 additions & 19 deletions pkg/charm/models/panels/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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{},
Expand Down Expand Up @@ -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)
}
5 changes: 4 additions & 1 deletion pkg/charm/models/tabber.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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
}
Expand Down

0 comments on commit 0269b93

Please sign in to comment.