From 943f941253666a69995d59051be0ef72eec5765a Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Mon, 13 Sep 2021 12:43:03 -0400 Subject: [PATCH] Truncate long repos names in the menu --- tui/bubble.go | 2 ++ tui/bubbles/selection/bubble.go | 20 ++++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/tui/bubble.go b/tui/bubble.go index edc5f28cd038adc4d018025a1e5abb460c9ddaca..9cd72030148074d864ae08c9cee94fb904328797 100644 --- a/tui/bubble.go +++ b/tui/bubble.go @@ -140,6 +140,7 @@ func (b *Bubble) viewForBox(i int) string { isActive := i == b.activeBox switch box := b.boxes[i].(type) { case *selection.Bubble: + // Menu var s lipgloss.Style s = b.styles.Menu if isActive { @@ -147,6 +148,7 @@ func (b *Bubble) viewForBox(i int) string { } return s.Render(box.View()) case *repo.Bubble: + // Repo details box.Active = isActive return box.View() default: diff --git a/tui/bubbles/selection/bubble.go b/tui/bubbles/selection/bubble.go index d669007269ab744123ae8fa41e39fa8e8be3b16a..bd985657a2ec33c6e67cc42b222e721b055fee06 100644 --- a/tui/bubbles/selection/bubble.go +++ b/tui/bubbles/selection/bubble.go @@ -2,8 +2,11 @@ package selection import ( "soft-serve/tui/style" + "strings" tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" + "github.com/muesli/reflow/truncate" ) type SelectedMsg struct { @@ -34,19 +37,24 @@ func (b *Bubble) Init() tea.Cmd { } func (b Bubble) View() string { - s := "" + s := strings.Builder{} + repoNameMaxWidth := b.styles.Menu.GetWidth() - // menu width + b.styles.Menu.GetHorizontalPadding() - // menu padding + lipgloss.Width(b.styles.MenuCursor.String()) - // cursor + b.styles.MenuItem.GetHorizontalFrameSize() // menu item gaps for i, item := range b.Items { + item := truncate.StringWithTail(item, uint(repoNameMaxWidth), "…") if i == b.SelectedItem { - s += b.styles.MenuCursor.String() - s += b.styles.SelectedMenuItem.Render(item) + s.WriteString(b.styles.MenuCursor.String()) + s.WriteString(b.styles.SelectedMenuItem.Render(item)) } else { - s += b.styles.MenuItem.Render(item) + s.WriteString(b.styles.MenuItem.Render(item)) } if i < len(b.Items)-1 { - s += "\n" + s.WriteRune('\n') } } - return s + return s.String() } func (b *Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) {