2020-05-13 16:53:58 +00:00
|
|
|
|
package ui
|
|
|
|
|
|
|
|
|
|
import (
|
2020-07-14 20:05:21 +00:00
|
|
|
|
"errors"
|
2020-05-13 23:02:39 +00:00
|
|
|
|
"fmt"
|
2020-07-14 20:05:21 +00:00
|
|
|
|
"io/ioutil"
|
2020-08-18 19:41:57 +00:00
|
|
|
|
"log"
|
2020-05-19 00:45:13 +00:00
|
|
|
|
"math"
|
2020-05-13 23:02:39 +00:00
|
|
|
|
"sort"
|
|
|
|
|
"strings"
|
2020-05-19 00:45:13 +00:00
|
|
|
|
"time"
|
2020-05-13 23:02:39 +00:00
|
|
|
|
|
2020-05-26 17:42:25 +00:00
|
|
|
|
"github.com/charmbracelet/bubbles/paginator"
|
|
|
|
|
"github.com/charmbracelet/bubbles/spinner"
|
|
|
|
|
"github.com/charmbracelet/bubbles/textinput"
|
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2020-05-13 16:53:58 +00:00
|
|
|
|
"github.com/charmbracelet/charm"
|
|
|
|
|
"github.com/charmbracelet/charm/ui/common"
|
2020-05-19 00:45:13 +00:00
|
|
|
|
"github.com/dustin/go-humanize"
|
2020-10-24 20:45:05 +00:00
|
|
|
|
"github.com/lithammer/fuzzysearch/fuzzy"
|
2020-06-08 17:43:55 +00:00
|
|
|
|
runewidth "github.com/mattn/go-runewidth"
|
2020-07-20 15:00:17 +00:00
|
|
|
|
"github.com/muesli/reflow/ansi"
|
2020-05-13 23:02:39 +00:00
|
|
|
|
te "github.com/muesli/termenv"
|
2020-05-13 16:53:58 +00:00
|
|
|
|
)
|
|
|
|
|
|
2020-05-15 00:23:11 +00:00
|
|
|
|
const (
|
2020-07-16 19:09:06 +00:00
|
|
|
|
stashIndent = 1
|
2020-05-22 02:29:46 +00:00
|
|
|
|
stashViewItemHeight = 3
|
2020-06-02 23:44:26 +00:00
|
|
|
|
stashViewTopPadding = 5
|
2020-05-22 02:29:46 +00:00
|
|
|
|
stashViewBottomPadding = 4
|
|
|
|
|
stashViewHorizontalPadding = 6
|
|
|
|
|
setNotePromptText = "Memo: "
|
2020-10-24 20:37:18 +00:00
|
|
|
|
searchNotePromptText = "Search: "
|
2020-05-15 00:23:11 +00:00
|
|
|
|
)
|
|
|
|
|
|
2020-07-28 20:41:44 +00:00
|
|
|
|
var (
|
|
|
|
|
stashHelpItemStyle func(string) string = te.Style{}.Foreground(common.NewColorPair("#5C5C5C", "#9B9B9B").Color()).Styled
|
2020-09-10 17:32:57 +00:00
|
|
|
|
dividerDot string = te.String(" • ").Foreground(common.NewColorPair("#3C3C3C", "#DDDADA").Color()).String()
|
|
|
|
|
offlineHeaderNote string = te.String("(Offline)").Foreground(common.NewColorPair("#3C3C3C", "#DDDADA").Color()).String()
|
2020-07-28 20:41:44 +00:00
|
|
|
|
)
|
|
|
|
|
|
2020-05-13 16:53:58 +00:00
|
|
|
|
// MSG
|
|
|
|
|
|
2020-05-22 19:31:54 +00:00
|
|
|
|
type fetchedMarkdownMsg *markdown
|
2020-05-15 22:34:42 +00:00
|
|
|
|
type deletedStashedItemMsg int
|
2020-05-13 20:00:27 +00:00
|
|
|
|
|
2020-05-13 16:53:58 +00:00
|
|
|
|
// MODEL
|
|
|
|
|
|
2020-10-18 01:46:02 +00:00
|
|
|
|
type DocumentType byte
|
|
|
|
|
|
|
|
|
|
const (
|
2020-10-18 03:13:49 +00:00
|
|
|
|
LocalDocuments DocumentType = 1 << iota
|
|
|
|
|
StashedDocuments
|
|
|
|
|
NewsDocuments
|
2020-07-15 20:41:41 +00:00
|
|
|
|
)
|
|
|
|
|
|
2020-05-21 20:05:13 +00:00
|
|
|
|
type stashState int
|
|
|
|
|
|
2020-05-13 16:53:58 +00:00
|
|
|
|
const (
|
2020-07-15 19:51:51 +00:00
|
|
|
|
stashStateReady stashState = iota
|
2020-05-15 22:34:42 +00:00
|
|
|
|
stashStatePromptDelete
|
2020-05-15 19:08:45 +00:00
|
|
|
|
stashStateLoadingDocument
|
2020-05-22 02:29:46 +00:00
|
|
|
|
stashStateSettingNote
|
2020-07-16 19:54:55 +00:00
|
|
|
|
stashStateShowingError
|
2020-10-24 20:18:19 +00:00
|
|
|
|
stashStateSearchNotes
|
|
|
|
|
stashStateShowFiltered
|
2020-05-13 16:53:58 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type stashModel struct {
|
2020-07-15 20:41:41 +00:00
|
|
|
|
cc *charm.Client
|
2020-10-17 21:39:43 +00:00
|
|
|
|
cfg *Config
|
2020-09-10 17:32:57 +00:00
|
|
|
|
authStatus authStatus
|
2020-07-15 20:41:41 +00:00
|
|
|
|
state stashState
|
2020-07-16 19:19:59 +00:00
|
|
|
|
err error
|
2020-07-15 20:41:41 +00:00
|
|
|
|
markdowns []*markdown
|
|
|
|
|
spinner spinner.Model
|
|
|
|
|
noteInput textinput.Model
|
2020-10-24 20:37:18 +00:00
|
|
|
|
searchInput textinput.Model
|
2020-07-15 20:41:41 +00:00
|
|
|
|
terminalWidth int
|
|
|
|
|
terminalHeight int
|
2020-10-18 03:13:49 +00:00
|
|
|
|
stashFullyLoaded bool // have we loaded all available stashed documents from the server?
|
|
|
|
|
loadingFromNetwork bool // are we currently loading something from the network?
|
|
|
|
|
loaded DocumentType // load status for news, stash and local files loading; we find out exactly with bitmasking
|
2020-05-15 00:23:11 +00:00
|
|
|
|
|
2020-08-25 13:40:53 +00:00
|
|
|
|
// Paths to files being stashed. We treat this like a set, ignoring the
|
|
|
|
|
// value portion with an empty struct.
|
2020-08-21 21:21:48 +00:00
|
|
|
|
filesStashing map[string]struct{}
|
|
|
|
|
|
2020-05-21 19:14:33 +00:00
|
|
|
|
// This is just the index of the current page in view. To get the index
|
|
|
|
|
// of the selected item as it relates to the full set of documents we've
|
|
|
|
|
// fetched use the mardownIndex() method of this struct.
|
|
|
|
|
index int
|
|
|
|
|
|
2020-05-15 00:23:11 +00:00
|
|
|
|
// This handles the local pagination, which is different than the page
|
|
|
|
|
// we're fetching from on the server side
|
|
|
|
|
paginator paginator.Model
|
|
|
|
|
|
2020-05-21 19:14:33 +00:00
|
|
|
|
// Page we're fetching stash items from on the server, which is different
|
|
|
|
|
// from the local pagination. Generally, the server will return more items
|
|
|
|
|
// than we can display at a time so we can paginate locally without having
|
|
|
|
|
// to fetch every time.
|
2020-05-15 00:23:11 +00:00
|
|
|
|
page int
|
2020-08-21 17:39:59 +00:00
|
|
|
|
|
|
|
|
|
showStatusMessage bool
|
|
|
|
|
statusMessage string
|
|
|
|
|
statusMessageTimer *time.Timer
|
2020-05-15 00:23:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 01:46:02 +00:00
|
|
|
|
func (m stashModel) localOnly() bool {
|
2020-10-18 03:13:49 +00:00
|
|
|
|
return m.cfg.DocumentTypes == LocalDocuments
|
2020-10-18 01:46:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m stashModel) stashedOnly() bool {
|
2020-10-18 03:13:49 +00:00
|
|
|
|
return m.cfg.DocumentTypes&LocalDocuments == 0
|
2020-10-18 01:46:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 03:01:20 +00:00
|
|
|
|
func (m stashModel) loadingDone() bool {
|
2020-10-18 03:13:49 +00:00
|
|
|
|
// Do the types loaded match the types we want to have?
|
|
|
|
|
return m.loaded == m.cfg.DocumentTypes
|
2020-10-18 03:01:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-19 17:20:39 +00:00
|
|
|
|
func (m *stashModel) setSize(width, height int) {
|
2020-05-15 00:23:11 +00:00
|
|
|
|
m.terminalWidth = width
|
|
|
|
|
m.terminalHeight = height
|
|
|
|
|
|
|
|
|
|
// Update the paginator
|
2020-05-19 00:10:28 +00:00
|
|
|
|
perPage := max(1, (m.terminalHeight-stashViewTopPadding-stashViewBottomPadding)/stashViewItemHeight)
|
2020-05-15 00:23:11 +00:00
|
|
|
|
m.paginator.PerPage = perPage
|
2020-05-22 22:42:18 +00:00
|
|
|
|
m.paginator.SetTotalPages(len(m.markdowns))
|
2020-05-15 02:48:38 +00:00
|
|
|
|
|
2020-05-22 02:29:46 +00:00
|
|
|
|
m.noteInput.Width = m.terminalWidth - stashViewHorizontalPadding*2 - len(setNotePromptText)
|
2020-10-24 20:37:18 +00:00
|
|
|
|
m.searchInput.Width = m.terminalWidth - stashViewHorizontalPadding*2 - len(searchNotePromptText)
|
2020-05-22 02:29:46 +00:00
|
|
|
|
|
2020-05-15 02:48:38 +00:00
|
|
|
|
// Make sure the page stays in bounds
|
|
|
|
|
if m.paginator.Page >= m.paginator.TotalPages-1 {
|
2020-05-19 00:10:28 +00:00
|
|
|
|
m.paginator.Page = max(0, m.paginator.TotalPages-1)
|
2020-05-15 02:48:38 +00:00
|
|
|
|
}
|
2020-05-13 16:53:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 20:07:43 +00:00
|
|
|
|
// MarkdownIndex returns the index of the currently selected markdown item.
|
2020-05-21 19:14:33 +00:00
|
|
|
|
func (m stashModel) markdownIndex() int {
|
|
|
|
|
return m.paginator.Page*m.paginator.PerPage + m.index
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 20:07:43 +00:00
|
|
|
|
// Return the current selected markdown in the stash.
|
2020-05-22 20:28:15 +00:00
|
|
|
|
func (m stashModel) selectedMarkdown() *markdown {
|
2020-07-15 19:51:51 +00:00
|
|
|
|
i := m.markdownIndex()
|
2020-10-24 20:45:05 +00:00
|
|
|
|
markdowns := m.getNotes()
|
|
|
|
|
|
|
|
|
|
if i < 0 || len(markdowns) == 0 || len(markdowns) <= i {
|
2020-06-02 22:58:15 +00:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2020-10-24 20:45:05 +00:00
|
|
|
|
|
|
|
|
|
return markdowns[i]
|
2020-05-22 20:28:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 20:07:43 +00:00
|
|
|
|
// Adds markdown documents to the model.
|
2020-05-22 20:01:23 +00:00
|
|
|
|
func (m *stashModel) addMarkdowns(mds ...*markdown) {
|
2020-07-15 20:03:05 +00:00
|
|
|
|
if len(mds) > 0 {
|
|
|
|
|
m.markdowns = append(m.markdowns, mds...)
|
|
|
|
|
sort.Sort(markdownsByLocalFirst(m.markdowns))
|
|
|
|
|
m.paginator.SetTotalPages(len(m.markdowns))
|
|
|
|
|
}
|
2020-05-22 20:01:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 20:07:43 +00:00
|
|
|
|
// Find a local markdown by its path and remove it.
|
2020-08-24 18:56:23 +00:00
|
|
|
|
func (m *stashModel) removeLocalMarkdown(localPath string) error {
|
|
|
|
|
i := -1
|
|
|
|
|
|
|
|
|
|
// Look for local markdown
|
|
|
|
|
for j, doc := range m.markdowns {
|
|
|
|
|
if doc.localPath == localPath {
|
|
|
|
|
i = j
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Did we find it?
|
|
|
|
|
if i == -1 {
|
|
|
|
|
err := fmt.Errorf("could't find local markdown %s; not removing from stash", localPath)
|
|
|
|
|
if debug {
|
|
|
|
|
log.Println(err)
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Slice out markdown
|
|
|
|
|
if i >= 0 {
|
|
|
|
|
m.markdowns = append(m.markdowns[:i], m.markdowns[i+1:]...)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 20:07:43 +00:00
|
|
|
|
// Return the number of markdown documents of a given type.
|
2020-07-15 23:56:37 +00:00
|
|
|
|
func (m stashModel) countMarkdowns(t markdownType) (found int) {
|
2020-10-24 20:45:05 +00:00
|
|
|
|
if len(m.getNotes()) == 0 {
|
2020-07-15 23:56:37 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
2020-10-24 20:45:05 +00:00
|
|
|
|
for i := 0; i < len(m.getNotes()); i++ {
|
|
|
|
|
if m.getNotes()[i].markdownType == t {
|
2020-07-15 23:56:37 +00:00
|
|
|
|
found++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-24 20:45:05 +00:00
|
|
|
|
// Returns the stashed markdown notes. When the model state indicates that
|
|
|
|
|
// filtering is desired this also filters and ranks the notes by the search term
|
|
|
|
|
// in the searchinput field.
|
|
|
|
|
func (m *stashModel) getNotes() []*markdown {
|
|
|
|
|
if m.searchInput.Value() == "" {
|
|
|
|
|
return m.markdowns
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if m.state != stashStateSearchNotes &&
|
|
|
|
|
m.state != stashStateShowFiltered &&
|
|
|
|
|
m.state != stashStatePromptDelete &&
|
|
|
|
|
m.state != stashStateSettingNote {
|
|
|
|
|
return m.markdowns
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
targets := []string{}
|
|
|
|
|
|
|
|
|
|
for _, t := range m.markdowns {
|
|
|
|
|
targets = append(targets, t.Note)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ranks := fuzzy.RankFindFold(m.searchInput.Value(), targets)
|
|
|
|
|
sort.Sort(ranks)
|
|
|
|
|
|
|
|
|
|
filtered := []*markdown{}
|
|
|
|
|
for _, r := range ranks {
|
|
|
|
|
filtered = append(filtered, m.markdowns[r.OriginalIndex])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return filtered
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-21 17:39:59 +00:00
|
|
|
|
func (m *stashModel) hideStatusMessage() {
|
|
|
|
|
m.showStatusMessage = false
|
|
|
|
|
if m.statusMessageTimer != nil {
|
|
|
|
|
m.statusMessageTimer.Stop()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-13 16:53:58 +00:00
|
|
|
|
// INIT
|
|
|
|
|
|
2020-10-17 21:39:43 +00:00
|
|
|
|
func newStashModel(cfg *Config, as authStatus) stashModel {
|
2020-08-25 13:40:53 +00:00
|
|
|
|
sp := spinner.NewModel()
|
|
|
|
|
sp.Frames = spinner.Line
|
|
|
|
|
sp.ForegroundColor = common.SpinnerColor.String()
|
|
|
|
|
sp.HideFor = time.Millisecond * 50
|
|
|
|
|
sp.MinimumLifetime = time.Millisecond * 180
|
|
|
|
|
sp.Start()
|
2020-05-13 16:53:58 +00:00
|
|
|
|
|
2020-05-15 00:52:25 +00:00
|
|
|
|
p := paginator.NewModel()
|
|
|
|
|
p.Type = paginator.Dots
|
|
|
|
|
p.InactiveDot = common.Subtle("•")
|
|
|
|
|
|
2020-05-22 02:29:46 +00:00
|
|
|
|
ni := textinput.NewModel()
|
|
|
|
|
ni.Prompt = te.String(setNotePromptText).Foreground(common.YellowGreen.Color()).String()
|
|
|
|
|
ni.CursorColor = common.Fuschia.String()
|
2020-05-22 22:42:18 +00:00
|
|
|
|
ni.CharLimit = noteCharacterLimit
|
2020-05-22 02:29:46 +00:00
|
|
|
|
ni.Focus()
|
|
|
|
|
|
2020-10-24 20:37:18 +00:00
|
|
|
|
si := textinput.NewModel()
|
|
|
|
|
si.Prompt = te.String(searchNotePromptText).Foreground(common.YellowGreen.Color()).String()
|
|
|
|
|
si.CursorColor = common.Fuschia.String()
|
|
|
|
|
si.CharLimit = noteCharacterLimit
|
|
|
|
|
si.Focus()
|
|
|
|
|
|
2020-05-13 16:53:58 +00:00
|
|
|
|
m := stashModel{
|
2020-10-17 21:39:43 +00:00
|
|
|
|
cfg: cfg,
|
2020-09-10 17:32:57 +00:00
|
|
|
|
authStatus: as,
|
2020-08-25 13:40:53 +00:00
|
|
|
|
spinner: sp,
|
2020-07-15 20:41:41 +00:00
|
|
|
|
noteInput: ni,
|
2020-10-24 20:37:18 +00:00
|
|
|
|
searchInput: si,
|
2020-07-15 20:41:41 +00:00
|
|
|
|
page: 1,
|
|
|
|
|
paginator: p,
|
|
|
|
|
loadingFromNetwork: true,
|
2020-08-21 21:21:48 +00:00
|
|
|
|
filesStashing: make(map[string]struct{}),
|
2020-05-13 16:53:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-15 18:18:46 +00:00
|
|
|
|
return m
|
2020-05-13 16:53:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UPDATE
|
|
|
|
|
|
2020-05-26 17:42:25 +00:00
|
|
|
|
func stashUpdate(msg tea.Msg, m stashModel) (stashModel, tea.Cmd) {
|
2020-07-15 23:19:44 +00:00
|
|
|
|
var cmds []tea.Cmd
|
2020-05-15 00:52:25 +00:00
|
|
|
|
|
2020-05-13 16:53:58 +00:00
|
|
|
|
switch msg := msg.(type) {
|
2020-07-16 19:19:59 +00:00
|
|
|
|
case errMsg:
|
|
|
|
|
m.err = msg
|
|
|
|
|
|
2020-07-17 16:25:40 +00:00
|
|
|
|
case stashLoadErrMsg:
|
|
|
|
|
m.err = msg.err
|
2020-10-18 03:13:49 +00:00
|
|
|
|
m.loaded |= StashedDocuments // still done, albeit unsuccessfully
|
2020-07-17 16:25:40 +00:00
|
|
|
|
m.stashFullyLoaded = true
|
|
|
|
|
m.loadingFromNetwork = false
|
|
|
|
|
|
|
|
|
|
case newsLoadErrMsg:
|
|
|
|
|
m.err = msg.err
|
2020-10-18 03:13:49 +00:00
|
|
|
|
m.loaded |= NewsDocuments // still done, albeit unsuccessfully
|
2020-07-17 16:25:40 +00:00
|
|
|
|
|
2020-07-15 20:41:41 +00:00
|
|
|
|
// We're finished searching for local files
|
|
|
|
|
case localFileSearchFinished:
|
2020-10-18 03:13:49 +00:00
|
|
|
|
m.loaded |= LocalDocuments
|
2020-07-15 20:41:41 +00:00
|
|
|
|
|
2020-05-21 20:05:13 +00:00
|
|
|
|
// Stash results have come in from the server
|
2020-05-13 16:53:58 +00:00
|
|
|
|
case gotStashMsg:
|
2020-07-15 20:41:41 +00:00
|
|
|
|
// This doesn't mean the whole stash listing is loaded, but some we've
|
|
|
|
|
// finished checking for the stash, at least, so mark the stash as
|
|
|
|
|
// loaded here.
|
2020-10-18 03:13:49 +00:00
|
|
|
|
m.loaded |= StashedDocuments
|
2020-07-15 20:41:41 +00:00
|
|
|
|
|
|
|
|
|
m.loadingFromNetwork = false
|
2020-05-15 23:16:22 +00:00
|
|
|
|
|
|
|
|
|
if len(msg) == 0 {
|
2020-06-02 21:15:26 +00:00
|
|
|
|
// If the server comes back with nothing then we've got everything
|
2020-07-15 20:41:41 +00:00
|
|
|
|
m.stashFullyLoaded = true
|
2020-06-02 21:15:26 +00:00
|
|
|
|
} else {
|
2020-07-14 21:32:50 +00:00
|
|
|
|
docs := wrapMarkdowns(stashedMarkdown, msg)
|
2020-06-02 21:15:26 +00:00
|
|
|
|
m.addMarkdowns(docs...)
|
2020-05-15 23:16:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-14 23:18:38 +00:00
|
|
|
|
// News has come in from the server
|
2020-05-21 19:14:33 +00:00
|
|
|
|
case gotNewsMsg:
|
2020-10-18 03:13:49 +00:00
|
|
|
|
m.loaded |= NewsDocuments
|
2020-05-21 20:05:13 +00:00
|
|
|
|
if len(msg) > 0 {
|
|
|
|
|
docs := wrapMarkdowns(newsMarkdown, msg)
|
2020-05-22 20:01:23 +00:00
|
|
|
|
m.addMarkdowns(docs...)
|
2020-05-21 19:14:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-22 19:11:48 +00:00
|
|
|
|
case spinner.TickMsg:
|
2020-10-18 03:01:20 +00:00
|
|
|
|
condition := !m.loadingDone() ||
|
2020-08-21 21:30:33 +00:00
|
|
|
|
m.loadingFromNetwork ||
|
|
|
|
|
m.state == stashStateLoadingDocument ||
|
2020-08-25 13:40:53 +00:00
|
|
|
|
len(m.filesStashing) > 0 ||
|
|
|
|
|
m.spinner.Visible()
|
2020-08-21 21:30:33 +00:00
|
|
|
|
|
|
|
|
|
if condition {
|
2020-07-15 23:19:44 +00:00
|
|
|
|
newSpinnerModel, cmd := spinner.Update(msg, m.spinner)
|
|
|
|
|
m.spinner = newSpinnerModel
|
2020-05-15 22:39:23 +00:00
|
|
|
|
cmds = append(cmds, cmd)
|
2020-05-13 16:53:58 +00:00
|
|
|
|
}
|
2020-05-20 19:18:59 +00:00
|
|
|
|
|
2020-07-15 19:51:51 +00:00
|
|
|
|
// A note was set on a document. This may have happened in the pager so
|
2020-05-22 02:29:46 +00:00
|
|
|
|
// we'll find the corresponding document here and update accordingly.
|
2020-05-20 19:18:59 +00:00
|
|
|
|
case noteSavedMsg:
|
2020-05-22 22:42:18 +00:00
|
|
|
|
for i := range m.markdowns {
|
|
|
|
|
if m.markdowns[i].ID == msg.ID {
|
|
|
|
|
m.markdowns[i].Note = msg.Note
|
2020-05-20 19:18:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-21 17:39:59 +00:00
|
|
|
|
|
|
|
|
|
// Something was stashed. Add it to the stash listing.
|
|
|
|
|
case stashSuccessMsg:
|
|
|
|
|
md := markdown(msg)
|
2020-08-21 21:21:48 +00:00
|
|
|
|
delete(m.filesStashing, md.localPath) // remove from the things-we're-stashing list
|
2020-08-24 18:56:23 +00:00
|
|
|
|
|
|
|
|
|
_ = m.removeLocalMarkdown(md.localPath)
|
2020-08-21 17:39:59 +00:00
|
|
|
|
m.addMarkdowns(&md)
|
|
|
|
|
|
|
|
|
|
m.showStatusMessage = true
|
|
|
|
|
m.statusMessage = "Stashed!"
|
|
|
|
|
if m.statusMessageTimer != nil {
|
|
|
|
|
m.statusMessageTimer.Stop()
|
|
|
|
|
}
|
2020-08-21 20:41:06 +00:00
|
|
|
|
m.statusMessageTimer = time.NewTimer(statusMessageTimeout)
|
2020-08-21 17:39:59 +00:00
|
|
|
|
cmds = append(cmds, waitForStatusMessageTimeout(stashContext, m.statusMessageTimer))
|
|
|
|
|
|
|
|
|
|
case statusMessageTimeoutMsg:
|
|
|
|
|
if applicationContext(msg) == stashContext {
|
|
|
|
|
m.hideStatusMessage()
|
|
|
|
|
}
|
2020-05-13 16:53:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 02:29:46 +00:00
|
|
|
|
switch m.state {
|
2020-10-24 20:18:19 +00:00
|
|
|
|
case stashStateReady, stashStateShowFiltered:
|
2020-10-24 20:45:05 +00:00
|
|
|
|
pages := len(m.getNotes())
|
|
|
|
|
|
2020-08-21 17:39:59 +00:00
|
|
|
|
switch msg := msg.(type) {
|
2020-07-16 19:37:48 +00:00
|
|
|
|
// Handle keys
|
2020-08-21 17:39:59 +00:00
|
|
|
|
case tea.KeyMsg:
|
2020-05-22 13:38:34 +00:00
|
|
|
|
switch msg.String() {
|
2020-07-20 17:33:49 +00:00
|
|
|
|
case "k", "up":
|
2020-05-22 13:38:34 +00:00
|
|
|
|
m.index--
|
|
|
|
|
if m.index < 0 && m.paginator.Page == 0 {
|
|
|
|
|
// Stop
|
|
|
|
|
m.index = 0
|
|
|
|
|
} else if m.index < 0 {
|
|
|
|
|
// Go to previous page
|
|
|
|
|
m.paginator.PrevPage()
|
2020-05-22 22:42:18 +00:00
|
|
|
|
m.index = m.paginator.ItemsOnPage(len(m.markdowns)) - 1
|
2020-05-22 13:38:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 17:33:49 +00:00
|
|
|
|
case "j", "down":
|
2020-05-22 22:42:18 +00:00
|
|
|
|
itemsOnPage := m.paginator.ItemsOnPage(len(m.markdowns))
|
2020-05-22 13:38:34 +00:00
|
|
|
|
m.index++
|
|
|
|
|
if m.index >= itemsOnPage && m.paginator.OnLastPage() {
|
|
|
|
|
// Stop
|
|
|
|
|
m.index = itemsOnPage - 1
|
|
|
|
|
} else if m.index >= itemsOnPage {
|
|
|
|
|
// Go to next page
|
|
|
|
|
m.index = 0
|
|
|
|
|
m.paginator.NextPage()
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-25 23:32:19 +00:00
|
|
|
|
// Go to the very start
|
|
|
|
|
case "home", "g":
|
|
|
|
|
m.paginator.Page = 0
|
|
|
|
|
m.index = 0
|
|
|
|
|
|
|
|
|
|
// Go to the very end
|
|
|
|
|
case "end", "G":
|
|
|
|
|
m.paginator.Page = m.paginator.TotalPages - 1
|
2020-10-24 20:45:05 +00:00
|
|
|
|
m.index = m.paginator.ItemsOnPage(pages) - 1
|
2020-08-25 23:32:19 +00:00
|
|
|
|
|
2020-05-22 13:38:34 +00:00
|
|
|
|
// Open document
|
|
|
|
|
case "enter":
|
2020-08-21 17:39:59 +00:00
|
|
|
|
m.hideStatusMessage()
|
|
|
|
|
|
2020-10-24 20:45:05 +00:00
|
|
|
|
if pages == 0 {
|
2020-06-03 16:46:53 +00:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 13:38:34 +00:00
|
|
|
|
// Load the document from the server. We'll handle the message
|
|
|
|
|
// that comes back in the main update function.
|
|
|
|
|
m.state = stashStateLoadingDocument
|
2020-05-22 20:28:15 +00:00
|
|
|
|
md := m.selectedMarkdown()
|
2020-05-22 19:31:54 +00:00
|
|
|
|
|
2020-07-15 23:56:37 +00:00
|
|
|
|
if md.markdownType == localMarkdown {
|
2020-07-15 23:19:44 +00:00
|
|
|
|
cmds = append(cmds, loadLocalMarkdown(md))
|
2020-07-14 20:05:21 +00:00
|
|
|
|
} else {
|
2020-07-15 23:19:44 +00:00
|
|
|
|
cmds = append(cmds, loadRemoteMarkdown(m.cc, md.ID, md.markdownType))
|
2020-07-14 20:05:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-15 23:19:44 +00:00
|
|
|
|
cmds = append(cmds, spinner.Tick(m.spinner))
|
2020-05-22 13:38:34 +00:00
|
|
|
|
|
2020-10-24 20:18:19 +00:00
|
|
|
|
// Search through your notes
|
|
|
|
|
case "/":
|
|
|
|
|
m.hideStatusMessage()
|
|
|
|
|
|
|
|
|
|
if pages == 0 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m.state = stashStateSearchNotes
|
2020-10-24 20:37:18 +00:00
|
|
|
|
m.searchInput.CursorEnd()
|
|
|
|
|
m.searchInput.Focus()
|
|
|
|
|
return m, textinput.Blink(m.searchInput)
|
|
|
|
|
|
2020-05-22 13:38:34 +00:00
|
|
|
|
// Set note
|
2020-06-03 18:24:40 +00:00
|
|
|
|
case "m":
|
2020-08-21 17:39:59 +00:00
|
|
|
|
m.hideStatusMessage()
|
|
|
|
|
|
2020-05-22 20:28:15 +00:00
|
|
|
|
md := m.selectedMarkdown()
|
2020-08-24 21:00:27 +00:00
|
|
|
|
isUserMarkdown := md.markdownType == stashedMarkdown || md.markdownType == convertedMarkdown
|
2020-05-22 20:28:15 +00:00
|
|
|
|
isSettingNote := m.state == stashStateSettingNote
|
|
|
|
|
isPromptingDelete := m.state == stashStatePromptDelete
|
|
|
|
|
|
|
|
|
|
if isUserMarkdown && !isSettingNote && !isPromptingDelete {
|
2020-05-22 13:38:34 +00:00
|
|
|
|
m.state = stashStateSettingNote
|
2020-05-22 20:28:15 +00:00
|
|
|
|
m.noteInput.SetValue(md.Note)
|
2020-05-22 13:38:34 +00:00
|
|
|
|
m.noteInput.CursorEnd()
|
|
|
|
|
return m, textinput.Blink(m.noteInput)
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-21 17:39:59 +00:00
|
|
|
|
// Stash
|
|
|
|
|
case "s":
|
2020-09-10 17:32:57 +00:00
|
|
|
|
if m.authStatus != authOK || m.selectedMarkdown() == nil {
|
2020-08-21 21:21:48 +00:00
|
|
|
|
break
|
2020-08-21 17:39:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 17:32:57 +00:00
|
|
|
|
md := m.selectedMarkdown()
|
|
|
|
|
|
|
|
|
|
// is the file in the process of being stashed?
|
|
|
|
|
_, isBeingStashed := m.filesStashing[md.localPath]
|
|
|
|
|
|
|
|
|
|
isLocalMarkdown := md.markdownType == localMarkdown
|
|
|
|
|
markdownPathMissing := md.localPath == ""
|
2020-08-21 21:21:48 +00:00
|
|
|
|
|
2020-09-10 17:32:57 +00:00
|
|
|
|
if isBeingStashed || !isLocalMarkdown || markdownPathMissing {
|
|
|
|
|
if debug && isBeingStashed {
|
|
|
|
|
log.Printf("refusing to stash markdown; we're already stashing %s", md.localPath)
|
|
|
|
|
} else if debug && isLocalMarkdown && markdownPathMissing {
|
2020-08-25 13:40:53 +00:00
|
|
|
|
log.Printf("refusing to stash markdown; local path is empty: %#v", md)
|
2020-08-24 18:56:23 +00:00
|
|
|
|
}
|
2020-08-21 21:21:48 +00:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-25 13:40:53 +00:00
|
|
|
|
// Checks passed; perform the stash
|
2020-08-21 21:21:48 +00:00
|
|
|
|
m.filesStashing[md.localPath] = struct{}{}
|
2020-08-25 13:40:53 +00:00
|
|
|
|
cmds = append(cmds, stashDocument(m.cc, *md))
|
|
|
|
|
|
2020-10-18 03:01:20 +00:00
|
|
|
|
if m.loadingDone() && !m.spinner.Visible() {
|
2020-08-25 13:40:53 +00:00
|
|
|
|
m.spinner.Start()
|
|
|
|
|
cmds = append(cmds, spinner.Tick(m.spinner))
|
|
|
|
|
}
|
2020-08-21 21:21:48 +00:00
|
|
|
|
|
2020-05-22 13:38:34 +00:00
|
|
|
|
// Prompt for deletion
|
|
|
|
|
case "x":
|
2020-08-21 17:39:59 +00:00
|
|
|
|
m.hideStatusMessage()
|
|
|
|
|
|
2020-08-24 18:56:23 +00:00
|
|
|
|
t := m.selectedMarkdown().markdownType
|
|
|
|
|
isUserMarkdown := t == stashedMarkdown || t == convertedMarkdown
|
2020-05-22 13:38:34 +00:00
|
|
|
|
isValidState := m.state != stashStateSettingNote
|
2020-05-22 20:28:15 +00:00
|
|
|
|
|
2020-05-22 13:38:34 +00:00
|
|
|
|
if isUserMarkdown && isValidState {
|
|
|
|
|
m.state = stashStatePromptDelete
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-13 19:37:53 +00:00
|
|
|
|
// Show errors
|
2020-07-16 20:51:44 +00:00
|
|
|
|
case "!":
|
2020-07-16 19:54:55 +00:00
|
|
|
|
if m.err != nil && m.state == stashStateReady {
|
|
|
|
|
m.state = stashStateShowingError
|
2020-07-16 19:37:48 +00:00
|
|
|
|
return m, nil
|
|
|
|
|
}
|
2020-05-22 13:38:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-15 23:16:22 +00:00
|
|
|
|
|
2020-07-20 17:33:49 +00:00
|
|
|
|
// Update paginator. Pagination key handling is done here, but it could
|
|
|
|
|
// also be moved up to this level, in which case we'd use model methods
|
|
|
|
|
// like model.PageUp().
|
2020-07-15 23:19:44 +00:00
|
|
|
|
newPaginatorModel, cmd := paginator.Update(msg, m.paginator)
|
|
|
|
|
m.paginator = newPaginatorModel
|
2020-05-15 00:52:25 +00:00
|
|
|
|
cmds = append(cmds, cmd)
|
2020-05-15 21:31:43 +00:00
|
|
|
|
|
2020-07-21 18:55:00 +00:00
|
|
|
|
// Extra paginator keystrokes
|
|
|
|
|
if key, ok := msg.(tea.KeyMsg); ok {
|
|
|
|
|
if key.Type == tea.KeyRune {
|
|
|
|
|
switch key.Rune {
|
2020-07-28 20:46:23 +00:00
|
|
|
|
case 'b', 'u':
|
2020-07-21 18:55:00 +00:00
|
|
|
|
m.paginator.PrevPage()
|
2020-07-28 20:46:23 +00:00
|
|
|
|
case 'f', 'd':
|
2020-07-21 18:55:00 +00:00
|
|
|
|
m.paginator.NextPage()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-15 21:31:43 +00:00
|
|
|
|
// Keep the index in bounds when paginating
|
2020-10-24 20:45:05 +00:00
|
|
|
|
itemsOnPage := m.paginator.ItemsOnPage(len(m.getNotes()))
|
2020-05-15 21:31:43 +00:00
|
|
|
|
if m.index > itemsOnPage-1 {
|
2020-07-15 20:03:05 +00:00
|
|
|
|
m.index = max(0, itemsOnPage-1)
|
2020-05-15 21:31:43 +00:00
|
|
|
|
}
|
2020-05-15 23:16:22 +00:00
|
|
|
|
|
|
|
|
|
// If we're on the last page and we haven't loaded everything, get
|
|
|
|
|
// more stuff.
|
2020-07-15 20:41:41 +00:00
|
|
|
|
if m.paginator.OnLastPage() && !m.loadingFromNetwork && !m.stashFullyLoaded {
|
2020-05-15 23:16:22 +00:00
|
|
|
|
m.page++
|
2020-07-15 20:41:41 +00:00
|
|
|
|
m.loadingFromNetwork = true
|
2020-05-15 23:16:22 +00:00
|
|
|
|
cmds = append(cmds, loadStash(m))
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 13:38:34 +00:00
|
|
|
|
case stashStatePromptDelete:
|
2020-05-26 17:42:25 +00:00
|
|
|
|
if msg, ok := msg.(tea.KeyMsg); ok {
|
2020-05-22 13:38:34 +00:00
|
|
|
|
switch msg.String() {
|
|
|
|
|
// Confirm deletion
|
|
|
|
|
case "y":
|
|
|
|
|
if m.state != stashStatePromptDelete {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i := m.markdownIndex()
|
2020-05-22 22:42:18 +00:00
|
|
|
|
id := m.markdowns[i].ID
|
2020-05-22 13:38:34 +00:00
|
|
|
|
|
2020-08-24 19:59:42 +00:00
|
|
|
|
if m.markdowns[i].markdownType == convertedMarkdown {
|
|
|
|
|
// If document was stashed during this session, convert it
|
|
|
|
|
// back to a local file.
|
|
|
|
|
m.markdowns[i].markdownType = localMarkdown
|
2020-08-24 21:34:15 +00:00
|
|
|
|
m.markdowns[i].Note = m.markdowns[i].displayPath
|
2020-08-24 19:59:42 +00:00
|
|
|
|
} else {
|
|
|
|
|
// Delete optimistically and remove the stashed item
|
|
|
|
|
// before we've received a success response.
|
|
|
|
|
m.markdowns = append(m.markdowns[:i], m.markdowns[i+1:]...)
|
|
|
|
|
}
|
2020-05-22 13:38:34 +00:00
|
|
|
|
|
|
|
|
|
// Update pagination
|
2020-05-22 22:42:18 +00:00
|
|
|
|
m.paginator.SetTotalPages(len(m.markdowns))
|
2020-05-22 13:38:34 +00:00
|
|
|
|
m.paginator.Page = min(m.paginator.Page, m.paginator.TotalPages-1)
|
|
|
|
|
|
|
|
|
|
// Set state and delete
|
|
|
|
|
m.state = stashStateReady
|
|
|
|
|
return m, deleteStashedItem(m.cc, id)
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
m.state = stashStateReady
|
2020-10-24 20:37:18 +00:00
|
|
|
|
if m.searchInput.Value() != "" {
|
|
|
|
|
m.state = stashStateShowFiltered
|
|
|
|
|
}
|
2020-05-22 13:38:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-24 20:18:19 +00:00
|
|
|
|
case stashStateSearchNotes:
|
|
|
|
|
if msg, ok := msg.(tea.KeyMsg); ok {
|
|
|
|
|
switch msg.String() {
|
|
|
|
|
case "esc":
|
|
|
|
|
// Cancel search
|
|
|
|
|
m.state = stashStateReady
|
2020-10-24 20:37:18 +00:00
|
|
|
|
m.searchInput.Reset()
|
|
|
|
|
case "enter", "tab":
|
|
|
|
|
m.hideStatusMessage()
|
|
|
|
|
|
|
|
|
|
if len(m.markdowns) == 0 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m.searchInput.Blur()
|
|
|
|
|
|
|
|
|
|
m.state = stashStateShowFiltered
|
|
|
|
|
if m.searchInput.Value() == "" {
|
|
|
|
|
m.searchInput.Reset()
|
|
|
|
|
m.state = stashStateReady
|
|
|
|
|
}
|
2020-10-24 20:18:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-24 20:37:18 +00:00
|
|
|
|
|
|
|
|
|
// Update the text input component
|
|
|
|
|
newSearchInputModel, cmd := textinput.Update(msg, m.searchInput)
|
|
|
|
|
m.searchInput = newSearchInputModel
|
|
|
|
|
cmds = append(cmds, cmd)
|
2020-05-22 02:29:46 +00:00
|
|
|
|
case stashStateSettingNote:
|
2020-05-26 17:42:25 +00:00
|
|
|
|
if msg, ok := msg.(tea.KeyMsg); ok {
|
2020-05-22 02:29:46 +00:00
|
|
|
|
switch msg.String() {
|
|
|
|
|
case "esc":
|
|
|
|
|
// Cancel note
|
|
|
|
|
m.state = stashStateReady
|
2020-10-24 20:37:18 +00:00
|
|
|
|
if m.searchInput.Value() != "" {
|
|
|
|
|
m.state = stashStateShowFiltered
|
|
|
|
|
}
|
2020-05-22 02:29:46 +00:00
|
|
|
|
m.noteInput.Reset()
|
|
|
|
|
case "enter":
|
|
|
|
|
// Set new note
|
2020-05-22 22:42:18 +00:00
|
|
|
|
md := m.selectedMarkdown()
|
2020-05-22 02:29:46 +00:00
|
|
|
|
newNote := m.noteInput.Value()
|
2020-07-15 23:19:44 +00:00
|
|
|
|
cmd := saveDocumentNote(m.cc, md.ID, newNote)
|
2020-05-22 22:42:18 +00:00
|
|
|
|
md.Note = newNote
|
2020-05-22 02:29:46 +00:00
|
|
|
|
m.noteInput.Reset()
|
|
|
|
|
m.state = stashStateReady
|
2020-10-24 20:37:18 +00:00
|
|
|
|
if m.searchInput.Value() != "" {
|
|
|
|
|
m.state = stashStateShowFiltered
|
|
|
|
|
}
|
2020-05-22 02:29:46 +00:00
|
|
|
|
return m, cmd
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update the text input component used to set notes
|
2020-07-15 23:19:44 +00:00
|
|
|
|
newNoteInputModel, cmd := textinput.Update(msg, m.noteInput)
|
|
|
|
|
m.noteInput = newNoteInputModel
|
2020-05-22 02:29:46 +00:00
|
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
|
|
2020-07-16 19:54:55 +00:00
|
|
|
|
case stashStateShowingError:
|
|
|
|
|
// Any key exists the error view
|
|
|
|
|
if _, ok := msg.(tea.KeyMsg); ok {
|
|
|
|
|
m.state = stashStateReady
|
|
|
|
|
}
|
2020-05-15 00:52:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-15 22:34:42 +00:00
|
|
|
|
// If an item is being confirmed for delete, any key (other than the key
|
|
|
|
|
// used for confirmation above) cancels the deletion
|
2020-05-26 17:42:25 +00:00
|
|
|
|
return m, tea.Batch(cmds...)
|
2020-05-13 16:53:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VIEW
|
|
|
|
|
|
|
|
|
|
func stashView(m stashModel) string {
|
|
|
|
|
var s string
|
2020-05-13 23:02:39 +00:00
|
|
|
|
switch m.state {
|
2020-07-16 19:54:55 +00:00
|
|
|
|
case stashStateShowingError:
|
|
|
|
|
return errorView(m.err, false)
|
2020-05-15 19:08:45 +00:00
|
|
|
|
case stashStateLoadingDocument:
|
2020-05-26 16:36:14 +00:00
|
|
|
|
s += " " + spinner.View(m.spinner) + " Loading document..."
|
2020-10-24 20:18:19 +00:00
|
|
|
|
case stashStateReady, stashStateSettingNote, stashStatePromptDelete, stashStateSearchNotes, stashStateShowFiltered:
|
2020-06-02 22:58:15 +00:00
|
|
|
|
|
2020-10-24 20:37:18 +00:00
|
|
|
|
loadingIndicator := " "
|
2020-10-18 03:01:20 +00:00
|
|
|
|
if !m.localOnly() && (!m.loadingDone() || m.loadingFromNetwork || m.spinner.Visible()) {
|
2020-07-15 20:41:41 +00:00
|
|
|
|
loadingIndicator = spinner.View(m.spinner)
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-21 19:14:33 +00:00
|
|
|
|
// We need to fill any empty height with newlines so the footer reaches
|
|
|
|
|
// the bottom.
|
2020-06-02 23:44:26 +00:00
|
|
|
|
numBlankLines := max(0, (m.terminalHeight-stashViewTopPadding-stashViewBottomPadding)%stashViewItemHeight)
|
2020-05-15 00:23:11 +00:00
|
|
|
|
blankLines := ""
|
|
|
|
|
if numBlankLines > 0 {
|
|
|
|
|
blankLines = strings.Repeat("\n", numBlankLines)
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-13 19:37:53 +00:00
|
|
|
|
var header string
|
2020-08-21 17:39:59 +00:00
|
|
|
|
if m.showStatusMessage {
|
|
|
|
|
header = greenFg(m.statusMessage)
|
|
|
|
|
} else {
|
|
|
|
|
switch m.state {
|
|
|
|
|
case stashStatePromptDelete:
|
2020-08-24 19:49:26 +00:00
|
|
|
|
header = redFg("Delete this item from your stash? ") + faintRedFg("(y/N)")
|
2020-08-21 17:39:59 +00:00
|
|
|
|
case stashStateSettingNote:
|
|
|
|
|
header = yellowFg("Set the memo for this item?")
|
|
|
|
|
}
|
2020-05-15 22:34:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 19:09:06 +00:00
|
|
|
|
// Only draw the normal header if we're not using the header area for
|
2020-08-25 13:40:53 +00:00
|
|
|
|
// something else (like a prompt or status message)
|
2020-07-16 19:09:06 +00:00
|
|
|
|
if header == "" {
|
|
|
|
|
header = stashHeaderView(m)
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-24 20:37:18 +00:00
|
|
|
|
logoOrSearch := glowLogoView(" Glow ")
|
|
|
|
|
// we replace the logo with the search field in
|
|
|
|
|
if m.state == stashStateSearchNotes || m.state == stashStateShowFiltered {
|
|
|
|
|
logoOrSearch = textinput.View(m.searchInput)
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-15 23:16:22 +00:00
|
|
|
|
var pagination string
|
2020-05-15 22:44:22 +00:00
|
|
|
|
if m.paginator.TotalPages > 1 {
|
2020-05-15 23:16:22 +00:00
|
|
|
|
pagination = paginator.View(m.paginator)
|
|
|
|
|
|
2020-07-20 15:00:17 +00:00
|
|
|
|
// If the dot pagination is wider than the width of the window
|
|
|
|
|
// switch to the arabic paginator.
|
|
|
|
|
if ansi.PrintableRuneWidth(pagination) > m.terminalWidth-stashViewHorizontalPadding {
|
|
|
|
|
m.paginator.Type = paginator.Arabic
|
|
|
|
|
pagination = common.Subtle(paginator.View(m.paginator))
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-13 16:32:48 +00:00
|
|
|
|
// We could also look at m.stashFullyLoaded and add an indicator
|
|
|
|
|
// showing that we don't actually know how many more pages there
|
|
|
|
|
// are.
|
2020-05-15 22:44:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-15 00:52:25 +00:00
|
|
|
|
s += fmt.Sprintf(
|
2020-10-24 20:37:18 +00:00
|
|
|
|
"%s %s\n\n %s\n\n%s\n\n%s %s\n\n %s",
|
2020-07-15 20:41:41 +00:00
|
|
|
|
loadingIndicator,
|
2020-10-24 20:37:18 +00:00
|
|
|
|
logoOrSearch,
|
2020-05-15 22:34:42 +00:00
|
|
|
|
header,
|
2020-06-02 23:44:26 +00:00
|
|
|
|
stashPopulatedView(m),
|
2020-05-15 01:12:13 +00:00
|
|
|
|
blankLines,
|
2020-05-15 23:16:22 +00:00
|
|
|
|
pagination,
|
2020-05-21 19:14:33 +00:00
|
|
|
|
stashHelpView(m),
|
2020-05-15 00:52:25 +00:00
|
|
|
|
)
|
2020-05-13 23:02:39 +00:00
|
|
|
|
}
|
2020-07-16 19:09:06 +00:00
|
|
|
|
return "\n" + indent(s, stashIndent)
|
2020-05-13 23:02:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-19 17:20:39 +00:00
|
|
|
|
func glowLogoView(text string) string {
|
|
|
|
|
return te.String(text).
|
|
|
|
|
Bold().
|
|
|
|
|
Foreground(glowLogoTextColor).
|
|
|
|
|
Background(common.Fuschia.Color()).
|
|
|
|
|
String()
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 19:09:06 +00:00
|
|
|
|
func stashHeaderView(m stashModel) string {
|
2020-10-18 03:01:20 +00:00
|
|
|
|
loading := !m.loadingDone()
|
2020-07-16 19:09:06 +00:00
|
|
|
|
noMarkdowns := len(m.markdowns) == 0
|
|
|
|
|
|
2020-10-18 01:46:02 +00:00
|
|
|
|
if m.authStatus == authFailed && m.stashedOnly() {
|
2020-09-10 17:32:57 +00:00
|
|
|
|
return common.Subtle("Can’t load stash. Are you offline?")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var maybeOffline string
|
|
|
|
|
if m.authStatus == authFailed {
|
|
|
|
|
maybeOffline = " " + offlineHeaderNote
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 19:09:06 +00:00
|
|
|
|
// Still loading. We haven't found files, stashed items, or news yet.
|
|
|
|
|
if loading && noMarkdowns {
|
2020-10-18 01:46:02 +00:00
|
|
|
|
if m.stashedOnly() {
|
2020-09-07 23:32:19 +00:00
|
|
|
|
return common.Subtle("Loading your stash...")
|
|
|
|
|
} else {
|
2020-09-10 17:32:57 +00:00
|
|
|
|
return common.Subtle("Looking for stuff...") + maybeOffline
|
2020-09-07 23:32:19 +00:00
|
|
|
|
}
|
2020-07-16 19:09:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
localItems := m.countMarkdowns(localMarkdown)
|
2020-08-25 13:46:52 +00:00
|
|
|
|
stashedItems := m.countMarkdowns(stashedMarkdown) + m.countMarkdowns(convertedMarkdown)
|
2020-07-16 19:09:06 +00:00
|
|
|
|
|
|
|
|
|
// Loading's finished and all we have is news.
|
2020-07-28 20:21:36 +00:00
|
|
|
|
if !loading && localItems == 0 && stashedItems == 0 {
|
2020-10-18 01:46:02 +00:00
|
|
|
|
if m.stashedOnly() {
|
2020-09-10 17:32:57 +00:00
|
|
|
|
return common.Subtle("No stashed markdown files found.") + maybeOffline
|
2020-09-07 23:14:59 +00:00
|
|
|
|
} else {
|
2020-09-10 17:32:57 +00:00
|
|
|
|
return common.Subtle("No local or stashed markdown files found.") + maybeOffline
|
2020-09-07 23:14:59 +00:00
|
|
|
|
}
|
2020-07-16 19:09:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// There are local and/or stashed files, so display counts.
|
|
|
|
|
var s string
|
|
|
|
|
if localItems > 0 {
|
2020-09-10 17:32:57 +00:00
|
|
|
|
s += common.Subtle(fmt.Sprintf("%d Local", localItems))
|
2020-07-16 19:09:06 +00:00
|
|
|
|
}
|
|
|
|
|
if stashedItems > 0 {
|
|
|
|
|
var divider string
|
|
|
|
|
if localItems > 0 {
|
2020-09-10 17:32:57 +00:00
|
|
|
|
divider = dividerDot
|
2020-07-16 19:09:06 +00:00
|
|
|
|
}
|
2020-09-10 17:32:57 +00:00
|
|
|
|
si := common.Subtle(fmt.Sprintf("%d Stashed", stashedItems))
|
|
|
|
|
s += fmt.Sprintf("%s%s", divider, si)
|
2020-07-16 19:09:06 +00:00
|
|
|
|
}
|
2020-09-10 17:32:57 +00:00
|
|
|
|
return common.Subtle(s) + maybeOffline
|
2020-05-13 23:02:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func stashPopulatedView(m stashModel) string {
|
2020-05-26 16:36:14 +00:00
|
|
|
|
var b strings.Builder
|
2020-05-15 00:52:25 +00:00
|
|
|
|
|
2020-10-24 20:45:05 +00:00
|
|
|
|
markdowns := m.getNotes()
|
|
|
|
|
if len(markdowns) > 0 {
|
|
|
|
|
start, end := m.paginator.GetSliceBounds(len(markdowns))
|
|
|
|
|
docs := markdowns[start:end]
|
2020-05-15 00:23:11 +00:00
|
|
|
|
|
2020-06-02 23:44:26 +00:00
|
|
|
|
for i, md := range docs {
|
|
|
|
|
stashItemView(&b, m, i, md)
|
|
|
|
|
if i != len(docs)-1 {
|
|
|
|
|
fmt.Fprintf(&b, "\n\n")
|
|
|
|
|
}
|
2020-05-26 16:36:14 +00:00
|
|
|
|
}
|
2020-05-13 23:02:39 +00:00
|
|
|
|
}
|
2020-05-15 00:52:25 +00:00
|
|
|
|
|
|
|
|
|
// If there aren't enough items to fill up this page (always the last page)
|
2020-06-02 23:44:26 +00:00
|
|
|
|
// then we need to add some newlines to fill up the space where stash items
|
|
|
|
|
// would have been.
|
2020-10-24 20:45:05 +00:00
|
|
|
|
itemsOnPage := m.paginator.ItemsOnPage(len(markdowns))
|
2020-05-15 00:52:25 +00:00
|
|
|
|
if itemsOnPage < m.paginator.PerPage {
|
2020-05-18 18:37:50 +00:00
|
|
|
|
n := (m.paginator.PerPage - itemsOnPage) * stashViewItemHeight
|
2020-10-24 20:45:05 +00:00
|
|
|
|
if len(markdowns) == 0 {
|
2020-06-02 23:44:26 +00:00
|
|
|
|
n -= stashViewItemHeight - 1
|
|
|
|
|
}
|
2020-05-26 16:36:14 +00:00
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
|
fmt.Fprint(&b, "\n")
|
|
|
|
|
}
|
2020-05-15 00:52:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-26 16:36:14 +00:00
|
|
|
|
return b.String()
|
2020-05-15 00:23:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-21 19:14:33 +00:00
|
|
|
|
func stashHelpView(m stashModel) string {
|
|
|
|
|
var (
|
2020-07-14 20:16:26 +00:00
|
|
|
|
h []string
|
2020-07-15 19:51:51 +00:00
|
|
|
|
isStashed bool
|
2020-08-21 17:39:59 +00:00
|
|
|
|
isLocal bool
|
2020-05-21 19:14:33 +00:00
|
|
|
|
)
|
|
|
|
|
|
2020-07-15 19:51:51 +00:00
|
|
|
|
if len(m.markdowns) > 0 {
|
|
|
|
|
md := m.selectedMarkdown()
|
|
|
|
|
isStashed = md != nil && md.markdownType == stashedMarkdown
|
2020-08-21 17:39:59 +00:00
|
|
|
|
isLocal = md != nil && md.markdownType == localMarkdown
|
2020-07-15 19:51:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 02:29:46 +00:00
|
|
|
|
if m.state == stashStateSettingNote {
|
|
|
|
|
h = append(h, "enter: confirm", "esc: cancel")
|
|
|
|
|
} else if m.state == stashStatePromptDelete {
|
2020-05-15 22:34:42 +00:00
|
|
|
|
h = append(h, "y: delete", "n: cancel")
|
2020-10-24 20:18:19 +00:00
|
|
|
|
} else if m.state == stashStateSearchNotes {
|
|
|
|
|
h = append(h, "enter: confirm", "esc: cancel", "ctrl+j/ctrl+k, ↑/↓: choose")
|
2020-05-15 22:34:42 +00:00
|
|
|
|
} else {
|
2020-05-22 22:42:18 +00:00
|
|
|
|
if len(m.markdowns) > 0 {
|
2020-06-03 16:46:53 +00:00
|
|
|
|
h = append(h, "enter: open")
|
2020-07-16 19:38:48 +00:00
|
|
|
|
}
|
2020-10-24 20:18:19 +00:00
|
|
|
|
if m.state == stashStateShowFiltered {
|
|
|
|
|
h = append(h, "esc: clear search")
|
|
|
|
|
}
|
2020-07-16 19:38:48 +00:00
|
|
|
|
if len(m.markdowns) > 1 {
|
2020-05-15 22:34:42 +00:00
|
|
|
|
h = append(h, "j/k, ↑/↓: choose")
|
|
|
|
|
}
|
|
|
|
|
if m.paginator.TotalPages > 1 {
|
|
|
|
|
h = append(h, "h/l, ←/→: page")
|
|
|
|
|
}
|
2020-08-21 17:39:59 +00:00
|
|
|
|
if isStashed {
|
2020-10-24 20:18:19 +00:00
|
|
|
|
h = append(h, "x: delete", "m: set memo")
|
2020-09-10 17:32:57 +00:00
|
|
|
|
} else if isLocal && m.authStatus == authOK {
|
2020-08-21 17:39:59 +00:00
|
|
|
|
h = append(h, "s: stash")
|
2020-05-21 19:14:33 +00:00
|
|
|
|
}
|
2020-07-16 19:37:48 +00:00
|
|
|
|
if m.err != nil {
|
2020-08-21 17:39:59 +00:00
|
|
|
|
h = append(h, "!: errors")
|
2020-07-16 19:37:48 +00:00
|
|
|
|
}
|
2020-10-24 20:18:19 +00:00
|
|
|
|
h = append(h, "/: search")
|
2020-08-21 17:39:59 +00:00
|
|
|
|
h = append(h, "q: quit")
|
2020-05-15 00:23:11 +00:00
|
|
|
|
}
|
2020-07-28 20:41:44 +00:00
|
|
|
|
return stashHelpViewBuilder(m.terminalWidth, h...)
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-13 19:37:53 +00:00
|
|
|
|
// builds the help view from various sections pieces, truncating it if the view
|
|
|
|
|
// would otherwise wrap to two lines.
|
2020-07-28 20:41:44 +00:00
|
|
|
|
func stashHelpViewBuilder(windowWidth int, sections ...string) string {
|
2020-08-21 18:22:20 +00:00
|
|
|
|
if len(sections) == 0 {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-28 20:41:44 +00:00
|
|
|
|
const truncationWidth = 1 // width of "…"
|
2020-08-21 18:22:20 +00:00
|
|
|
|
|
2020-07-28 20:41:44 +00:00
|
|
|
|
var (
|
|
|
|
|
s string
|
|
|
|
|
next string
|
|
|
|
|
maxWidth = windowWidth - stashViewHorizontalPadding - truncationWidth
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
for i := 0; i < len(sections); i++ {
|
2020-08-21 18:04:46 +00:00
|
|
|
|
// If we need this more often we'll formalize something rather than
|
|
|
|
|
// use an if clause/switch here.
|
|
|
|
|
switch sections[i] {
|
|
|
|
|
case "s: stash":
|
|
|
|
|
next = greenFg(sections[i])
|
|
|
|
|
default:
|
|
|
|
|
next = stashHelpItemStyle(sections[i])
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-28 20:41:44 +00:00
|
|
|
|
if i < len(sections)-1 {
|
2020-09-10 17:32:57 +00:00
|
|
|
|
next += dividerDot
|
2020-07-28 20:41:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-21 18:22:20 +00:00
|
|
|
|
// Only this (and the following) help text items if we have the
|
|
|
|
|
// horizontal space
|
2020-07-28 20:41:44 +00:00
|
|
|
|
if ansi.PrintableRuneWidth(s)+ansi.PrintableRuneWidth(next) >= maxWidth {
|
|
|
|
|
s += common.Subtle("…")
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s += next
|
|
|
|
|
}
|
|
|
|
|
return s
|
2020-05-13 23:02:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 20:48:10 +00:00
|
|
|
|
// COMMANDS
|
2020-05-13 16:53:58 +00:00
|
|
|
|
|
2020-07-14 20:05:21 +00:00
|
|
|
|
func loadRemoteMarkdown(cc *charm.Client, id int, t markdownType) tea.Cmd {
|
2020-05-26 17:42:25 +00:00
|
|
|
|
return func() tea.Msg {
|
2020-05-22 19:31:54 +00:00
|
|
|
|
var (
|
|
|
|
|
md *charm.Markdown
|
|
|
|
|
err error
|
|
|
|
|
)
|
2020-07-14 20:05:21 +00:00
|
|
|
|
|
2020-08-24 18:56:23 +00:00
|
|
|
|
if t == stashedMarkdown || t == convertedMarkdown {
|
2020-05-22 19:31:54 +00:00
|
|
|
|
md, err = cc.GetStashMarkdown(id)
|
|
|
|
|
} else {
|
|
|
|
|
md, err = cc.GetNewsMarkdown(id)
|
|
|
|
|
}
|
2020-07-14 20:05:21 +00:00
|
|
|
|
|
2020-05-21 19:14:33 +00:00
|
|
|
|
if err != nil {
|
2020-08-18 19:41:57 +00:00
|
|
|
|
if debug {
|
|
|
|
|
log.Println("error loading remote markdown:", err)
|
|
|
|
|
}
|
2020-08-21 20:13:38 +00:00
|
|
|
|
return errMsg{err}
|
2020-05-21 19:14:33 +00:00
|
|
|
|
}
|
2020-07-14 20:05:21 +00:00
|
|
|
|
|
2020-05-22 19:31:54 +00:00
|
|
|
|
return fetchedMarkdownMsg(&markdown{
|
2020-05-22 20:28:15 +00:00
|
|
|
|
markdownType: t,
|
2020-08-21 00:21:52 +00:00
|
|
|
|
Markdown: *md,
|
2020-05-21 19:14:33 +00:00
|
|
|
|
})
|
2020-05-14 02:08:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-15 22:34:42 +00:00
|
|
|
|
|
2020-07-14 20:05:21 +00:00
|
|
|
|
func loadLocalMarkdown(md *markdown) tea.Cmd {
|
|
|
|
|
return func() tea.Msg {
|
2020-07-15 23:56:37 +00:00
|
|
|
|
if md.markdownType != localMarkdown {
|
2020-08-21 20:13:38 +00:00
|
|
|
|
return errMsg{errors.New("could not load local file: not a local file")}
|
2020-07-14 20:05:21 +00:00
|
|
|
|
}
|
|
|
|
|
if md.localPath == "" {
|
2020-08-21 20:13:38 +00:00
|
|
|
|
return errMsg{errors.New("could not load file: missing path")}
|
2020-07-14 20:05:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(md.localPath)
|
|
|
|
|
if err != nil {
|
2020-08-18 19:41:57 +00:00
|
|
|
|
if debug {
|
|
|
|
|
log.Println("error reading local markdown:", err)
|
|
|
|
|
}
|
2020-08-21 20:13:38 +00:00
|
|
|
|
return errMsg{err}
|
2020-07-14 20:05:21 +00:00
|
|
|
|
}
|
|
|
|
|
md.Body = string(data)
|
|
|
|
|
return fetchedMarkdownMsg(md)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-26 17:42:25 +00:00
|
|
|
|
func deleteStashedItem(cc *charm.Client, id int) tea.Cmd {
|
|
|
|
|
return func() tea.Msg {
|
2020-05-15 22:34:42 +00:00
|
|
|
|
err := cc.DeleteMarkdown(id)
|
|
|
|
|
if err != nil {
|
2020-09-09 23:43:03 +00:00
|
|
|
|
if debug {
|
2020-08-24 19:59:28 +00:00
|
|
|
|
log.Println("could not delete stashed item:", err)
|
|
|
|
|
}
|
2020-08-21 20:13:38 +00:00
|
|
|
|
return errMsg{err}
|
2020-05-15 22:34:42 +00:00
|
|
|
|
}
|
|
|
|
|
return deletedStashedItemMsg(id)
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-18 20:08:49 +00:00
|
|
|
|
|
|
|
|
|
// ETC
|
|
|
|
|
|
2020-05-22 02:29:46 +00:00
|
|
|
|
// wrapMarkdowns wraps a *charm.Markdown with a *markdown in order to add some
|
|
|
|
|
// extra metadata.
|
2020-05-21 19:14:33 +00:00
|
|
|
|
func wrapMarkdowns(t markdownType, md []*charm.Markdown) (m []*markdown) {
|
|
|
|
|
for _, v := range md {
|
|
|
|
|
m = append(m, &markdown{
|
|
|
|
|
markdownType: t,
|
2020-08-21 00:21:52 +00:00
|
|
|
|
Markdown: *v,
|
2020-05-21 19:14:33 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return m
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-18 20:08:49 +00:00
|
|
|
|
func truncate(str string, num int) string {
|
2020-06-08 17:43:55 +00:00
|
|
|
|
return runewidth.Truncate(str, num, "…")
|
2020-05-18 20:08:49 +00:00
|
|
|
|
}
|
2020-05-19 00:45:13 +00:00
|
|
|
|
|
|
|
|
|
var magnitudes = []humanize.RelTimeMagnitude{
|
|
|
|
|
{D: time.Second, Format: "now", DivBy: time.Second},
|
|
|
|
|
{D: 2 * time.Second, Format: "1 second %s", DivBy: 1},
|
|
|
|
|
{D: time.Minute, Format: "%d seconds %s", DivBy: time.Second},
|
|
|
|
|
{D: 2 * time.Minute, Format: "1 minute %s", DivBy: 1},
|
|
|
|
|
{D: time.Hour, Format: "%d minutes %s", DivBy: time.Minute},
|
|
|
|
|
{D: 2 * time.Hour, Format: "1 hour %s", DivBy: 1},
|
|
|
|
|
{D: humanize.Day, Format: "%d hours %s", DivBy: time.Hour},
|
|
|
|
|
{D: 2 * humanize.Day, Format: "1 day %s", DivBy: 1},
|
|
|
|
|
{D: humanize.Week, Format: "%d days %s", DivBy: humanize.Day},
|
|
|
|
|
{D: 2 * humanize.Week, Format: "1 week %s", DivBy: 1},
|
|
|
|
|
{D: humanize.Month, Format: "%d weeks %s", DivBy: humanize.Week},
|
|
|
|
|
{D: 2 * humanize.Month, Format: "1 month %s", DivBy: 1},
|
|
|
|
|
{D: humanize.Year, Format: "%d months %s", DivBy: humanize.Month},
|
|
|
|
|
{D: 18 * humanize.Month, Format: "1 year %s", DivBy: 1},
|
|
|
|
|
{D: 2 * humanize.Year, Format: "2 years %s", DivBy: 1},
|
|
|
|
|
{D: humanize.LongTime, Format: "%d years %s", DivBy: humanize.Year},
|
|
|
|
|
{D: math.MaxInt64, Format: "a long while %s", DivBy: 1},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func relativeTime(then time.Time) string {
|
|
|
|
|
now := time.Now()
|
2020-08-21 18:22:41 +00:00
|
|
|
|
ago := now.Sub(then)
|
|
|
|
|
if ago < time.Minute {
|
|
|
|
|
return "just now"
|
|
|
|
|
} else if ago < humanize.Week {
|
2020-05-19 00:45:13 +00:00
|
|
|
|
return humanize.CustomRelTime(then, now, "ago", "from now", magnitudes)
|
|
|
|
|
}
|
|
|
|
|
return then.Format("02 Jan 2006 15:04 MST")
|
|
|
|
|
}
|