diff options
author | Christian Rocha <christian@rocha.is> | 2020-07-15 14:18:46 -0400 |
---|---|---|
committer | Christian Muehlhaeuser <muesli@gmail.com> | 2020-10-05 13:50:04 +0200 |
commit | d4f192487c02d40759ec7c0b0a53aad447da7244 (patch) | |
tree | 572f486a0f8cff9862a64551738507724092e82b /ui | |
parent | 1200b8672989fd93bd4aff9cbd9a0212dbe7f729 (diff) |
Start looking for local files the moment Glow starts
Diffstat (limited to 'ui')
-rw-r--r-- | ui/stash.go | 82 | ||||
-rw-r--r-- | ui/ui.go | 86 |
2 files changed, 83 insertions, 85 deletions
diff --git a/ui/stash.go b/ui/stash.go index 849f52c..a93f00e 100644 --- a/ui/stash.go +++ b/ui/stash.go @@ -18,7 +18,6 @@ import ( "github.com/charmbracelet/charm/ui/common" "github.com/dustin/go-humanize" runewidth "github.com/mattn/go-runewidth" - "github.com/muesli/gitcha" te "github.com/muesli/termenv" ) @@ -36,12 +35,6 @@ type gotStashMsg []*charm.Markdown type gotNewsMsg []*charm.Markdown type fetchedMarkdownMsg *markdown type deletedStashedItemMsg int -type initLocalFileSearchMsg struct { - cwd string - ch chan string -} -type foundLocalFileMsg string -type localFileSearchFinished struct{} // MODEL @@ -117,7 +110,6 @@ type stashModel struct { loaded stashLoadedState // what's loaded? we find out with bitmasking loading bool // are we currently loading something? fullyLoaded bool // Have we loaded everything from the server? - cwd string // working directory where glow is running hasStash bool // do we have stashed files to show? hasLocalFiles bool // do we have local files to show? hasNews bool // do we have news to show? @@ -136,10 +128,6 @@ type stashModel struct { // than we can display at a time so we can paginate locally without having // to fetch every time. page int - - // github.com/muesli/gitcha channel that receives paths to local markdown - // files. - localFileFinder chan string } func (m *stashModel) setSize(width, height int) { @@ -181,7 +169,7 @@ func (m *stashModel) addMarkdowns(mds ...*markdown) { // INIT -func stashInit(cc *charm.Client) (stashModel, tea.Cmd) { +func newStashModel() stashModel { s := spinner.NewModel() s.Frames = spinner.Dot s.ForegroundColor = common.SpinnerColor @@ -197,19 +185,13 @@ func stashInit(cc *charm.Client) (stashModel, tea.Cmd) { ni.Focus() m := stashModel{ - cc: cc, spinner: s, noteInput: ni, page: 1, paginator: p, } - return m, tea.Batch( - loadLocalFiles, - loadStash(m), - loadNews(m), - spinner.Tick(s), - ) + return m } // UPDATE @@ -222,21 +204,6 @@ func stashUpdate(msg tea.Msg, m stashModel) (stashModel, tea.Cmd) { switch msg := msg.(type) { - // We've started looking for local files - case initLocalFileSearchMsg: - m.localFileFinder = msg.ch - m.cwd = msg.cwd - cmds = append(cmds, findNextLocalFile(m)) - - // We found a local file - case foundLocalFileMsg: - pathStr, err := localFileToMarkdown(m.cwd, string(msg)) - if err == nil { - m.hasLocalFiles = true - m.addMarkdowns(pathStr) - } - cmds = append(cmds, findNextLocalFile(m)) - // We're finished searching for local files case localFileSearchFinished: m.loaded |= loadedLocalFiles @@ -588,51 +555,6 @@ func stashHelpView(m stashModel) string { // CMD -func loadLocalFiles() tea.Msg { - cwd, err := os.Getwd() - if err != nil { - return errMsg(err) - } - - ch := gitcha.FindFileFromList(cwd, []string{"*.md"}) - return initLocalFileSearchMsg{ - ch: ch, - cwd: cwd, - } -} - -func findNextLocalFile(m stashModel) tea.Cmd { - return func() tea.Msg { - pathStr, ok := <-m.localFileFinder - if ok { - // Okay now find the next one - return foundLocalFileMsg(pathStr) - } - // We're done - return localFileSearchFinished{} - } -} - -func loadStash(m stashModel) tea.Cmd { - return func() tea.Msg { - stash, err := m.cc.GetStash(m.page) - if err != nil { - return errMsg(err) - } - return gotStashMsg(stash) - } -} - -func loadNews(m stashModel) tea.Cmd { - return func() tea.Msg { - news, err := m.cc.GetNews(1) // just fetch the first page - if err != nil { - return errMsg(err) - } - return gotNewsMsg(news) - } -} - func loadRemoteMarkdown(cc *charm.Client, id int, t markdownType) tea.Cmd { return func() tea.Msg { var ( @@ -4,6 +4,7 @@ import ( "errors" "fmt" "log" + "os" "strings" "github.com/charmbracelet/bubbles/spinner" @@ -11,6 +12,7 @@ import ( "github.com/charmbracelet/charm" "github.com/charmbracelet/charm/ui/common" "github.com/charmbracelet/charm/ui/keygen" + "github.com/muesli/gitcha" te "github.com/muesli/termenv" ) @@ -47,6 +49,12 @@ func NewProgram(style string, cfg UIConfig) *tea.Program { type errMsg error type newCharmClientMsg *charm.Client type sshAuthErrMsg struct{} +type initLocalFileSearchMsg struct { + cwd string + ch chan string +} +type foundLocalFileMsg string +type localFileSearchFinished struct{} // MODEL @@ -83,6 +91,11 @@ type model struct { pager pagerModel terminalWidth int terminalHeight int + cwd string // directory from which we're running Glow + + // Channel that receives paths to local markdown files + // (via the github.com/muesli/gitcha package) + localFileFinder chan string } func (m *model) unloadDocument() { @@ -111,9 +124,11 @@ func initialize(style string) func() (tea.Model, tea.Cmd) { return model{ spinner: s, + stash: newStashModel(), pager: newPagerModel(style), state: stateInitCharmClient, }, tea.Batch( + findLocalFiles, newCharmClient, spinner.Tick(s), ) @@ -194,6 +209,7 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) { m.err = msg return m, nil + // Window size is received when starting up and on every resize case tea.WindowSizeMsg: m.terminalWidth = msg.Width m.terminalHeight = msg.Height @@ -203,6 +219,21 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) { // TODO: load more stash pages if we've resized, are on the last page, // and haven't loaded more pages yet. + // We've started looking for local files + case initLocalFileSearchMsg: + m.localFileFinder = msg.ch + m.cwd = msg.cwd + cmds = append(cmds, findNextLocalFile(m)) + + // We found a local file + case foundLocalFileMsg: + pathStr, err := localFileToMarkdown(m.cwd, string(msg)) + if err == nil { + m.stash.hasLocalFiles = true + m.stash.addMarkdowns(pathStr) + } + cmds = append(cmds, findNextLocalFile(m)) + case sshAuthErrMsg: // If we haven't run the keygen yet, do that if m.state != stateKeygenFinished { @@ -223,6 +254,7 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) { cmds = append(cmds, cmd) case keygen.DoneMsg: + // The keygen's done, so let's try initializing the charm client again m.state = stateKeygenFinished cmds = append(cmds, newCharmClient) @@ -236,10 +268,9 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) { case newCharmClientMsg: m.cc = msg m.state = stateShowStash - m.stash, cmd = stashInit(msg) - m.stash.setSize(m.terminalWidth, m.terminalHeight) + m.stash.cc = msg m.pager.cc = msg - cmds = append(cmds, cmd) + cmds = append(cmds, loadStash(m.stash), loadNews(m.stash)) case fetchedMarkdownMsg: m.pager.currentDocument = msg @@ -321,6 +352,31 @@ func errorView(err error) string { // COMMANDS +func findLocalFiles() tea.Msg { + cwd, err := os.Getwd() + if err != nil { + return errMsg(err) + } + + ch := gitcha.FindFileFromList(cwd, []string{"*.md"}) + return initLocalFileSearchMsg{ + ch: ch, + cwd: cwd, + } +} + +func findNextLocalFile(m model) tea.Cmd { + return func() tea.Msg { + pathStr, ok := <-m.localFileFinder + if ok { + // Okay now find the next one + return foundLocalFileMsg(pathStr) + } + // We're done + return localFileSearchFinished{} + } +} + func newCharmClient() tea.Msg { cfg, err := charm.ConfigFromEnv() if err != nil { @@ -337,6 +393,28 @@ func newCharmClient() tea.Msg { return newCharmClientMsg(cc) } +func loadStash(m stashModel) tea.Cmd { + return func() tea.Msg { + stash, err := m.cc.GetStash(m.page) + if err != nil { + return errMsg(err) + } + return gotStashMsg(stash) + } +} + +func loadNews(m stashModel) tea.Cmd { + return func() tea.Msg { + news, err := m.cc.GetNews(1) // just fetch the first page + if err != nil { + return errMsg(err) + } + return gotNewsMsg(news) + } +} + +// ETC + func indent(s string, n int) string { if n <= 0 || s == "" { return s @@ -350,8 +428,6 @@ func indent(s string, n int) string { return b.String() } -// ETC - func min(a, b int) int { if a < b { return a |