943f941253666a69995d59051be0ef72eec5765a

Author
Christian Rocha <christian@rocha.is>
Committer
Christian Rocha <christian@rocha.is>
Date

Message

Truncate long repos names in the menu

Diff

 1diff --git a/tui/bubble.go b/tui/bubble.go
 2index edc5f28cd038adc4d018025a1e5abb460c9ddaca..9cd72030148074d864ae08c9cee94fb904328797 100644
 3--- a/tui/bubble.go
 4+++ b/tui/bubble.go
 5@@ -140,6 +140,7 @@ func (b *Bubble) viewForBox(i int) string {
 6 	isActive := i == b.activeBox
 7 	switch box := b.boxes[i].(type) {
 8 	case *selection.Bubble:
 9+		// Menu
10 		var s lipgloss.Style
11 		s = b.styles.Menu
12 		if isActive {
13@@ -147,6 +148,7 @@ func (b *Bubble) viewForBox(i int) string {
14 		}
15 		return s.Render(box.View())
16 	case *repo.Bubble:
17+		// Repo details
18 		box.Active = isActive
19 		return box.View()
20 	default:
21diff --git a/tui/bubbles/selection/bubble.go b/tui/bubbles/selection/bubble.go
22index d669007269ab744123ae8fa41e39fa8e8be3b16a..bd985657a2ec33c6e67cc42b222e721b055fee06 100644
23--- a/tui/bubbles/selection/bubble.go
24+++ b/tui/bubbles/selection/bubble.go
25@@ -2,8 +2,11 @@ package selection
26 
27 import (
28 	"soft-serve/tui/style"
29+	"strings"
30 
31 	tea "github.com/charmbracelet/bubbletea"
32+	"github.com/charmbracelet/lipgloss"
33+	"github.com/muesli/reflow/truncate"
34 )
35 
36 type SelectedMsg struct {
37@@ -34,19 +37,24 @@ func (b *Bubble) Init() tea.Cmd {
38 }
39 
40 func (b Bubble) View() string {
41-	s := ""
42+	s := strings.Builder{}
43+	repoNameMaxWidth := b.styles.Menu.GetWidth() - // menu width
44+		b.styles.Menu.GetHorizontalPadding() - // menu padding
45+		lipgloss.Width(b.styles.MenuCursor.String()) - // cursor
46+		b.styles.MenuItem.GetHorizontalFrameSize() // menu item gaps
47 	for i, item := range b.Items {
48+		item := truncate.StringWithTail(item, uint(repoNameMaxWidth), "…")
49 		if i == b.SelectedItem {
50-			s += b.styles.MenuCursor.String()
51-			s += b.styles.SelectedMenuItem.Render(item)
52+			s.WriteString(b.styles.MenuCursor.String())
53+			s.WriteString(b.styles.SelectedMenuItem.Render(item))
54 		} else {
55-			s += b.styles.MenuItem.Render(item)
56+			s.WriteString(b.styles.MenuItem.Render(item))
57 		}
58 		if i < len(b.Items)-1 {
59-			s += "\n"
60+			s.WriteRune('\n')
61 		}
62 	}
63-	return s
64+	return s.String()
65 }
66 
67 func (b *Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) {