From 7212aa31f4d56e106c819d03317cb371fd245074 Mon Sep 17 00:00:00 2001 From: punkfairie <23287005+punkfairie@users.noreply.github.com> Date: Sat, 7 Sep 2024 22:48:11 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=84=20fix(ui):=20Properly=20calc=20mai?= =?UTF-8?q?n=20dimensions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 56 ++++++++++++++++++++++++++++++++------------------------ test.sh | 7 ++++--- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/main.go b/main.go index 2ee3813..95902b7 100644 --- a/main.go +++ b/main.go @@ -102,10 +102,6 @@ func (m menu) setDimensions() { m.width, m.height, _ = term.GetSize(int(os.Stdout.Fd())) } -func calcMainWidth(width int) int { - return int(float64(width) * 0.65) -} - func (m menu) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var cmd tea.Cmd var cmds []tea.Cmd @@ -136,7 +132,8 @@ func (m menu) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case tea.WindowSizeMsg: m.setDimensions() - m.viewport.Width = calcMainWidth(m.width) + m.viewport.Width = gloss.Width(m.mainView()) + m.viewport.Height = gloss.Height(m.mainView()) case errMsg: m.appendOutput("Error: " + msg.Error()) @@ -148,23 +145,33 @@ func (m menu) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, tea.Batch(cmds...) } -func (m menu) View() string { - mainWidth := calcMainWidth(m.width) - borderStyle := gloss.NewStyle(). - BorderStyle(gloss.RoundedBorder()). - BorderForeground(gloss.Color("5")). - Padding(0, 1) +var borderStyle = gloss.NewStyle(). + BorderStyle(gloss.RoundedBorder()). + BorderForeground(gloss.Color("5")). + Padding(0, 1) + +var topPadding = 1 + +func (m menu) mainView() string { + mainWidth := int(float64(m.width) * 0.65) mainStyle := borderStyle. - Width(mainWidth) + Width(mainWidth). + Height(m.calcInnerSidebarHeight() - 2) + mainContent := m.viewport.View() + + return mainStyle.Render(mainContent) +} + +func (m menu) calcInnerSidebarHeight() int { + return m.height - 3 - gloss.Height(m.helpView()) - topPadding +} + +func (m menu) sidebarView() string { sidebarStyle := borderStyle. Width(int(float64(m.width) * 0.3)) - topPadding := 1 - - helpView := m.help.View(m.keys) - softwareListEnumerator := func(l list.Items, i int) string { if m.current == i { return m.spinner.View() @@ -176,7 +183,7 @@ func (m menu) View() string { software := list.New().Enumerator(softwareListEnumerator) - sidebarHeight := m.height - 3 - gloss.Height(helpView) - topPadding + sidebarHeight := m.calcInnerSidebarHeight() if len(m.order) > 0 { start := max(m.current-10, 0) @@ -193,14 +200,15 @@ func (m menu) View() string { sidebarContent := software.String() - m.viewport.Height = sidebarHeight - m.viewport.Width = mainWidth - mainContent := m.viewport.View() + return sidebarStyle.Render(sidebarContent) +} - main := mainStyle.Render(mainContent) - sidebar := sidebarStyle.Render(sidebarContent) +func (m menu) helpView() string { + return m.help.View(m.keys) +} - content := gloss.JoinHorizontal(gloss.Top, main, sidebar) +func (m menu) View() string { + content := gloss.JoinHorizontal(gloss.Top, m.mainView(), m.sidebarView()) top := strings.Repeat("\n", topPadding) last := "" @@ -208,7 +216,7 @@ func (m menu) View() string { last = "\n" } - page := gloss.JoinVertical(gloss.Left, top, content, helpView, last) + page := gloss.JoinVertical(gloss.Left, top, content, m.helpView(), last) return gloss.PlaceHorizontal(m.width, gloss.Center, page) } diff --git a/test.sh b/test.sh index f0b59e9..059c7de 100755 --- a/test.sh +++ b/test.sh @@ -2,7 +2,8 @@ i=0 -while [ $i -le 100 ]; do - printf "line\n" - sleep 1 +while [ $i -le 200 ]; do + printf '%s line\n' "$i" + i=$((i + 1)) + sleep .1 done