Auto resize progress bars

This commit is contained in:
Yohann D'ANELLO 2022-04-28 22:21:07 +02:00
parent f68970ea6f
commit b3768042e4
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
1 changed files with 30 additions and 7 deletions

37
main.go
View File

@ -143,6 +143,16 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.wordsViewport.Height = msg.Height - verticalMarginHeight 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: case errMsg:
m.err = msg m.err = msg
return m, nil return m, nil
@ -168,11 +178,16 @@ func (m model) InputWord() (model, tea.Cmd) {
w := word{input, 200*rand.Float64() - 100, rand.Intn(1000)} w := word{input, 200*rand.Float64() - 100, rand.Intn(1000)}
m.words = append(m.words, w) m.words = append(m.words, w)
var content string lineWidth := lipgloss.Width(fmt.Sprintf(
for _, w := range m.words { "* %s %s %s %4d ",
content += w.View(m) strings.Repeat(" ", m.maxLength),
} "-00.00",
m.wordsViewport.SetContent(content) " ",
42,
))
m.progressBar.Width = m.wordsViewport.Width - lineWidth
m.wordsViewport.SetContent(m.wordsView())
m.textInput.SetValue("") m.textInput.SetValue("")
m.textInput.CursorStart() m.textInput.CursorStart()
@ -209,7 +224,7 @@ func (w word) View(m model) string {
} }
return fmt.Sprintf( 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)), w.content+strings.Repeat(" ", m.maxLength-len(w.content)),
distStr, distStr,
emoji, emoji,
@ -240,6 +255,14 @@ func (m model) headerView() string {
return msg 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 { func (m model) footerView() string {
info := infoStyle.Render(fmt.Sprintf("%3.f%%", m.wordsViewport.ScrollPercent()*100)) 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))))) 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 { func (m model) View() string {
if !m.ready { if !m.ready {
return "\n\tInitializing..." return "\n\tInitialisation…"
} }
return fmt.Sprintf("%s\n%s\n%s", m.headerView(), m.wordsViewport.View(), m.footerView()) return fmt.Sprintf("%s\n%s\n%s", m.headerView(), m.wordsViewport.View(), m.footerView())