Render stash items

This commit is contained in:
Christian Rocha 2020-05-13 19:02:39 -04:00 committed by Christian Muehlhaeuser
parent bb6904afe2
commit 796504238f
3 changed files with 60 additions and 6 deletions

2
go.mod
View file

@ -9,6 +9,8 @@ require (
github.com/charmbracelet/tea v0.3.0
github.com/charmbracelet/teaparty v0.0.0-20200504225426-da64445a0e0d
github.com/dlclark/regexp2 v1.2.0 // indirect
github.com/muesli/reflow v0.1.0
github.com/muesli/termenv v0.5.2
github.com/spf13/cobra v0.0.7
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 // indirect

View file

@ -1,10 +1,16 @@
package ui
import (
"fmt"
"sort"
"strings"
"github.com/charmbracelet/boba"
"github.com/charmbracelet/boba/spinner"
"github.com/charmbracelet/charm"
"github.com/charmbracelet/charm/ui/common"
"github.com/muesli/reflow/indent"
te "github.com/muesli/termenv"
)
// MSG
@ -21,7 +27,7 @@ type stashState int
const (
stashStateInit stashState = iota
stashStateStashLoaded
stashStateLoaded
)
type stashModel struct {
@ -65,8 +71,9 @@ func stashUpdate(msg boba.Msg, m stashModel) (stashModel, boba.Cmd) {
m.err = msg
case gotStashMsg:
sort.Sort(charm.MarkdownsByCreatedAt(msg)) // sort by date
m.documents = msg
m.state = stashStateStashLoaded
m.state = stashStateLoaded
case stashSpinnerTickMsg:
if m.state == stashStateInit {
@ -83,11 +90,52 @@ func stashUpdate(msg boba.Msg, m stashModel) (stashModel, boba.Cmd) {
func stashView(m stashModel) string {
var s string
if (m.state & stashStateStashLoaded) != 0 {
return "Loaded stash."
switch m.state {
case stashStateInit:
s += spinner.View(m.spinner) + " Loading stash..."
case stashStateLoaded:
if len(m.documents) == 0 {
s += stashEmtpyView(m)
break
}
s += stashPopulatedView(m)
}
s += spinner.View(m.spinner) + " Loading stash..."
return s + "\n"
return "\n" + indent.String(s, 2)
}
func stashEmtpyView(m stashModel) string {
return "Nothing stashed yet."
}
func stashPopulatedView(m stashModel) string {
s := "Here's your markdown stash:\n\n"
for _, v := range m.documents {
s += stashListItemView(*v).renderNormal() + "\n\n"
}
s = strings.TrimSpace(s)
return s
}
type stashListItemView charm.Markdown
func (m stashListItemView) renderNormal() string {
line := common.VerticalLine(common.StateNormal) + " "
var s string
s += fmt.Sprintf("%s#%d%s\n", line, m.ID, m.title())
s += fmt.Sprintf("%sStashed: %s", line, m.date())
return s
}
func (m stashListItemView) date() string {
s := m.CreatedAt.Format("02 Jan 2006 15:04:05 MST")
return te.String(s).Foreground(common.Indigo.Color()).String()
}
func (m stashListItemView) title() string {
if m.Note == "" {
return ""
}
return ": " + te.String(m.Note).Foreground(common.Indigo.Color()).String()
}
// CMD

View file

@ -8,6 +8,7 @@ import (
"github.com/charmbracelet/charm"
"github.com/charmbracelet/charm/ui/common"
"github.com/charmbracelet/charm/ui/keygen"
"github.com/muesli/reflow/indent"
)
// NewProgram returns a new Boba program
@ -166,6 +167,9 @@ func view(mdl boba.Model) string {
case stateQuitting:
s += "Thanks for using Glow!"
}
if m.state != stateShowStash {
s = "\n" + indent.String(s, 2)
}
return s + "\n"
}