Load chosen document into pager

This commit is contained in:
Christian Rocha 2020-05-13 22:08:17 -04:00 committed by Christian Muehlhaeuser
commit 05340539de
2 changed files with 63 additions and 9 deletions

View file

@ -16,10 +16,9 @@ import (
// MSG
type stashErrMsg error
type gotStashMsg []*charm.Markdown
type stashSpinnerTickMsg struct{}
type gotStashMsg []*charm.Markdown
type gotStashedItemMsg *charm.Markdown
// MODEL
@ -28,6 +27,7 @@ type stashState int
const (
stashStateInit stashState = iota
stashStateLoaded
stashStateLoadingItem
)
type stashModel struct {
@ -55,7 +55,7 @@ func stashInit(cc *charm.Client) (stashModel, boba.Cmd) {
}
return m, boba.Batch(
getStash(m),
loadStash(m),
spinner.Tick(s),
)
}
@ -83,18 +83,27 @@ func stashUpdate(msg boba.Msg, m stashModel) (stashModel, boba.Cmd) {
m.index = min(len(m.documents)-1, m.index+1)
return m, nil
case "enter":
m.state = stashStateLoadingItem
return m, boba.Batch(
loadStashedItem(m.cc, m.documents[m.index].ID),
spinner.Tick(m.spinner),
)
}
case stashErrMsg:
m.err = msg
return m, nil
case gotStashMsg:
sort.Sort(charm.MarkdownsByCreatedAt(msg)) // sort by date
m.documents = msg
m.state = stashStateLoaded
return m, nil
case stashSpinnerTickMsg:
if m.state == stashStateInit {
if m.state == stashStateInit || m.state == stashStateLoadingItem {
var cmd boba.Cmd
m.spinner, cmd = spinner.Update(msg, m.spinner)
return m, cmd
@ -111,6 +120,8 @@ func stashView(m stashModel) string {
switch m.state {
case stashStateInit:
s += spinner.View(m.spinner) + " Loading stash..."
case stashStateLoadingItem:
s += spinner.View(m.spinner) + " Loading document..."
case stashStateLoaded:
if len(m.documents) == 0 {
s += stashEmtpyView(m)
@ -175,7 +186,7 @@ func (m stashListItemView) title(state common.State) string {
// CMD
func getStash(m stashModel) boba.Cmd {
func loadStash(m stashModel) boba.Cmd {
return func() boba.Msg {
stash, err := m.cc.GetStash(m.page)
if err != nil {
@ -184,3 +195,13 @@ func getStash(m stashModel) boba.Cmd {
return gotStashMsg(stash)
}
}
func loadStashedItem(cc *charm.Client, id int) boba.Cmd {
return func() boba.Msg {
m, err := cc.GetStashMarkdown(id)
if err != nil {
return stashErrMsg(err)
}
return gotStashedItemMsg(m)
}
}

View file

@ -4,6 +4,7 @@ import (
"errors"
"github.com/charmbracelet/boba"
"github.com/charmbracelet/boba/pager"
"github.com/charmbracelet/boba/spinner"
"github.com/charmbracelet/charm"
"github.com/charmbracelet/charm/ui/common"
@ -32,6 +33,7 @@ const (
stateKeygenRunning
stateKeygenFinished
stateShowStash
stateShowDocument
)
type model struct {
@ -42,6 +44,13 @@ type model struct {
state state
err error
stash stashModel
pager pager.Model
}
func (m *model) unloadDocument() {
m.pager = pager.Model{}
m.state = stateShowStash
m.stash.state = stashStateLoaded
}
// INIT
@ -74,10 +83,16 @@ func update(msg boba.Msg, mdl boba.Model) (boba.Model, boba.Cmd) {
case boba.KeyMsg:
switch msg.String() {
case "q":
fallthrough
case "esc":
fallthrough
if m.state == stateShowDocument {
m.unloadDocument()
return m, nil
}
return m, boba.Quit
case "ctrl+c":
return m, boba.Quit
}
@ -122,6 +137,12 @@ func update(msg boba.Msg, mdl boba.Model) (boba.Model, boba.Cmd) {
m.state = stateShowStash
m.stash, cmd = stashInit(m.cc)
return m, cmd
case gotStashedItemMsg:
m.state = stateShowDocument
m.pager = pager.NewModel()
m.pager.Content(msg.Body)
return m, pager.GetTerminalSize
}
switch m.state {
@ -139,6 +160,16 @@ func update(msg boba.Msg, mdl boba.Model) (boba.Model, boba.Cmd) {
case stateShowStash:
m.stash, cmd = stashUpdate(msg, m.stash)
return m, cmd
case stateShowDocument:
newPagerModel, cmd := pager.Update(msg, boba.Model(m.pager))
newPagerModel_, ok := newPagerModel.(pager.Model)
if !ok {
m.err = errors.New("could not assert boba.Model to pager.Model in main update")
return m, nil
}
m.pager = newPagerModel_
return m, cmd
}
return m, nil
@ -166,11 +197,13 @@ func view(mdl boba.Model) string {
s += spinner.View(m.spinner) + " Re-initializing..."
case stateShowStash:
s += stashView(m.stash)
case stateShowDocument:
s += pager.View(m.pager)
}
if m.state != stateShowStash {
if m.state != stateShowStash && m.state != stateShowDocument {
s = "\n" + indent.String(s, 2)
}
return s + "\n"
return s
}
// COMMANDS