From b3768042e49529cc9fb58e9abd9f4add14ea4386 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Thu, 28 Apr 2022 22:21:07 +0200 Subject: [PATCH] Auto resize progress bars --- main.go | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 503dcf6..54659ed 100644 --- a/main.go +++ b/main.go @@ -143,6 +143,16 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.wordsViewport.Height = msg.Height - verticalMarginHeight } + lineWidth := lipgloss.Width(fmt.Sprintf( + "* %s %s %s %4d ", + strings.Repeat(" ", m.maxLength), + "-00.00", + " ", + 42, + )) + m.progressBar.Width = msg.Width - lineWidth + m.wordsViewport.SetContent(m.wordsView()) + case errMsg: m.err = msg return m, nil @@ -168,11 +178,16 @@ func (m model) InputWord() (model, tea.Cmd) { w := word{input, 200*rand.Float64() - 100, rand.Intn(1000)} m.words = append(m.words, w) - var content string - for _, w := range m.words { - content += w.View(m) - } - m.wordsViewport.SetContent(content) + lineWidth := lipgloss.Width(fmt.Sprintf( + "* %s %s %s %4d ", + strings.Repeat(" ", m.maxLength), + "-00.00", + " ", + 42, + )) + m.progressBar.Width = m.wordsViewport.Width - lineWidth + + m.wordsViewport.SetContent(m.wordsView()) m.textInput.SetValue("") m.textInput.CursorStart() @@ -209,7 +224,7 @@ func (w word) View(m model) string { } return fmt.Sprintf( - "* %s\t%s %s\t%4d %s\n", + "* %s %s %s %4d %s\n", w.content+strings.Repeat(" ", m.maxLength-len(w.content)), distStr, emoji, @@ -240,6 +255,14 @@ func (m model) headerView() string { return msg } +func (m model) wordsView() string { + var content string + for _, w := range m.words { + content += w.View(m) + } + return content +} + func (m model) footerView() string { info := infoStyle.Render(fmt.Sprintf("%3.f%%", m.wordsViewport.ScrollPercent()*100)) line := strings.Repeat("─", int(math.Max(0, float64(m.wordsViewport.Width-lipgloss.Width(info))))) @@ -248,7 +271,7 @@ func (m model) footerView() string { func (m model) View() string { if !m.ready { - return "\n\tInitializing..." + return "\n\tInitialisation…" } return fmt.Sprintf("%s\n%s\n%s", m.headerView(), m.wordsViewport.View(), m.footerView())