💄 feat(ui): Finally fucking truncate list

This commit is contained in:
punkfairie 2024-09-07 21:13:35 -07:00
parent 6db2cbae9f
commit c156ff321b

50
main.go
View file

@ -4,18 +4,18 @@ import (
"fmt" "fmt"
"os" "os"
"sort" "sort"
"strings"
"github.com/charmbracelet/bubbles/help" "github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/spinner" "github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
gloss "github.com/charmbracelet/lipgloss" gloss "github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/list" "github.com/charmbracelet/lipgloss/list"
"golang.org/x/term" "golang.org/x/term"
) )
var width, height, _ = term.GetSize(int(os.Stdout.Fd()))
type menu struct { type menu struct {
order []string order []string
current int current int
@ -24,6 +24,9 @@ type menu struct {
inputStyle gloss.Style inputStyle gloss.Style
spinner spinner.Model spinner spinner.Model
quitting bool quitting bool
viewport viewport.Model
width int
height int
} }
const ( const (
@ -35,13 +38,16 @@ func initialModel() menu {
s := spinner.New() s := spinner.New()
s.Spinner = spinner.MiniDot s.Spinner = spinner.MiniDot
s.Style = gloss.NewStyle().Foreground(gloss.Color("3")) s.Style = gloss.NewStyle().Foreground(gloss.Color("3"))
width, height, _ := term.GetSize(int(os.Stdout.Fd()))
return menu{ return menu{
current: 0, current: 150,
keys: keys, keys: keys,
help: help.New(), help: help.New(),
spinner: s, spinner: s,
quitting: false, quitting: false,
width: width,
height: height,
} }
} }
@ -86,11 +92,10 @@ func (m menu) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case softwareListMsg: case softwareListMsg:
m.order = msg m.order = msg
return m, nil
case tea.KeyMsg: case tea.KeyMsg:
switch msg.String() { switch {
case "ctrl+c", "q": case key.Matches(msg, keys.Quit):
m.quitting = true m.quitting = true
return m, tea.Quit return m, tea.Quit
} }
@ -98,6 +103,9 @@ func (m menu) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case spinner.TickMsg: case spinner.TickMsg:
m.spinner, cmd = m.spinner.Update(msg) m.spinner, cmd = m.spinner.Update(msg)
cmds = append(cmds, cmd) cmds = append(cmds, cmd)
case tea.WindowSizeMsg:
m.width, m.height, _ = term.GetSize(int(os.Stdout.Fd()))
} }
return m, tea.Batch(cmds...) return m, tea.Batch(cmds...)
@ -110,13 +118,17 @@ func (m menu) View() string {
Padding(0, 1) Padding(0, 1)
mainStyle := borderStyle. mainStyle := borderStyle.
Width(int(float64(width) * 0.65)) Width(int(float64(m.width) * 0.65))
sidebarStyle := borderStyle. sidebarStyle := borderStyle.
Width(int(float64(width) * 0.3)) Width(int(float64(m.width) * 0.3))
topPadding := 1
mainContent := "" mainContent := ""
helpView := m.help.View(m.keys)
softwareListEnumerator := func(l list.Items, i int) string { softwareListEnumerator := func(l list.Items, i int) string {
if m.current == i { if m.current == i {
return m.spinner.View() return m.spinner.View()
@ -128,8 +140,19 @@ func (m menu) View() string {
software := list.New().Enumerator(softwareListEnumerator) software := list.New().Enumerator(softwareListEnumerator)
for _, item := range m.order { sidebarHeight := m.height - 3 - gloss.Height(helpView) - topPadding
software.Item(item)
if len(m.order) > 0 {
start := max(m.current-10, 0)
end := min(start+sidebarHeight, len(m.order))
if (end - start) < sidebarHeight {
start = (len(m.order) - sidebarHeight)
}
for _, item := range m.order[start:end] {
software.Item(item)
}
} }
sidebarContent := software.String() sidebarContent := software.String()
@ -139,16 +162,15 @@ func (m menu) View() string {
content := gloss.JoinHorizontal(gloss.Top, main, sidebar) content := gloss.JoinHorizontal(gloss.Top, main, sidebar)
helpView := m.help.View(m.keys) top := strings.Repeat("\n", topPadding)
last := "" last := ""
if m.quitting { if m.quitting {
last = "\n" last = "\n"
} }
page := gloss.JoinVertical(gloss.Left, content, helpView, last) page := gloss.JoinVertical(gloss.Left, top, content, helpView, last)
return gloss.PlaceHorizontal(width, gloss.Center, page) return gloss.PlaceHorizontal(m.width, gloss.Center, page)
} }
func main() { func main() {