Render stash view with a strings.Builder

This commit is contained in:
Christian Rocha 2020-05-26 12:36:14 -04:00 committed by Christian Muehlhaeuser
commit 21b1a2822f
2 changed files with 19 additions and 13 deletions

View file

@ -389,9 +389,9 @@ func stashView(m stashModel) string {
var s string
switch m.state {
case stashStateInit:
s += spinner.View(m.spinner) + " Loading stash..."
s += " " + spinner.View(m.spinner) + " Loading stash..."
case stashStateLoadingDocument:
s += spinner.View(m.spinner) + " Loading document..."
s += " " + spinner.View(m.spinner) + " Loading document..."
case stashStateReady:
fallthrough
case stashStateSettingNote:
@ -454,15 +454,17 @@ func stashEmtpyView(m stashModel) string {
}
func stashPopulatedView(m stashModel) string {
var s string
var b strings.Builder
start, end := m.paginator.GetSliceBounds(len(m.markdowns))
docs := m.markdowns[start:end]
for i, md := range docs {
s += stashItemView(m, i, md) + "\n\n"
stashItemView(&b, m, i, md)
if i != len(docs)-1 {
fmt.Fprintf(&b, "\n\n")
}
}
s = strings.TrimRight(s, "\n")
// If there aren't enough items to fill up this page (always the last page)
// then we need to add some newlines to fill up the space to push the
@ -470,10 +472,12 @@ func stashPopulatedView(m stashModel) string {
itemsOnPage := m.paginator.ItemsOnPage(len(m.markdowns))
if itemsOnPage < m.paginator.PerPage {
n := (m.paginator.PerPage - itemsOnPage) * stashViewItemHeight
s += strings.Repeat("\n", n)
for i := 0; i < n; i++ {
fmt.Fprint(&b, "\n")
}
}
return s
return b.String()
}
func stashHelpView(m stashModel) string {

View file

@ -2,6 +2,7 @@ package ui
import (
"fmt"
"strings"
"github.com/charmbracelet/boba/textinput"
"github.com/charmbracelet/charm/ui/common"
@ -19,13 +20,13 @@ var (
green = common.NewColorPair("#04B575", "#04B575")
dullFuchsia = common.NewColorPair("#AD58B4", "#F793FF")
dullYellow = common.NewColorPair("#9BA92F", "#6BCB94") // renders light green on light backgrounds
warmGray = common.NewColorPair("#918892", "#847A85")
warmGray = common.NewColorPair("#979797", "#847A85")
subtleIndigo = common.NewColorPair("#514DC1", "#7D79F6")
)
func stashItemView(m stashModel, index int, md *markdown) string {
truncateTo := m.terminalWidth - stashViewHorizontalPadding*2
func stashItemView(b *strings.Builder, m stashModel, index int, md *markdown) *strings.Builder {
truncateTo := m.terminalWidth - stashViewHorizontalPadding*2
gutter := " "
title := md.Note
date := relativeTime(*md.CreatedAt)
@ -76,7 +77,8 @@ func stashItemView(m stashModel, index int, md *markdown) string {
}
}
s := fmt.Sprintf("%s %s\n", gutter, title)
s += fmt.Sprintf("%s %s", gutter, date)
return s
fmt.Fprintf(b, "%s %s\n", gutter, title)
fmt.Fprintf(b, "%s %s", gutter, date)
return b
}