From d941957b92bc605dc654fdff8ffd26c4513dc8a7 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Mon, 20 Jul 2020 19:22:12 -0400 Subject: [PATCH] Also exit the pager with left arrow, h and delete/backspace --- ui/pager.go | 6 ++---- ui/ui.go | 30 +++++++++++++++++++----------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/ui/pager.go b/ui/pager.go index da11521..5bab71a 100644 --- a/ui/pager.go +++ b/ui/pager.go @@ -179,9 +179,7 @@ func pagerUpdate(msg tea.Msg, m pagerModel) (pagerModel, tea.Cmd) { } default: switch msg.String() { - case "q": - fallthrough - case "esc": + case "q", "esc": if m.state != pagerStateBrowse { m.state = pagerStateBrowse return m, nil @@ -194,7 +192,7 @@ func pagerUpdate(msg tea.Msg, m pagerModel) (pagerModel, tea.Cmd) { m.state = pagerStateSetNote if m.textInput.Value() == "" { - // Pre-populate with existing value + // Pre-populate note with existing value m.textInput.SetValue(m.currentDocument.Note) m.textInput.CursorEnd() } diff --git a/ui/ui.go b/ui/ui.go index b71b277..3a46fce 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -104,11 +104,22 @@ type model struct { localFileFinder chan gitcha.SearchResult } -func (m *model) unloadDocument() { +// unloadDocument unloads a document from the pager. Note that while this +// method alters the model we also need to send along any commands returned. +func (m *model) unloadDocument() []tea.Cmd { m.state = stateShowStash m.stash.state = stashStateReady m.pager.unload() m.pager.showHelp = false + + var batch []tea.Cmd + if m.pager.viewport.HighPerformanceRendering { + batch = append(batch, tea.ClearScrollArea) + } + if !m.stash.loaded.done() || m.stash.loadingFromNetwork { + batch = append(batch, spinner.Tick(m.stash.spinner)) + } + return batch } // INIT @@ -161,9 +172,7 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.KeyMsg: switch msg.String() { - case "q": - fallthrough - case "esc": + case "q", "esc": var cmd tea.Cmd // Send these keys through to stash @@ -181,13 +190,7 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) { var batch []tea.Cmd if m.pager.state == pagerStateBrowse { // If the user is just browing a document, exit the pager. - m.unloadDocument() - if m.pager.viewport.HighPerformanceRendering { - batch = append(batch, tea.ClearScrollArea) - } - if !m.stash.loaded.done() || m.stash.loadingFromNetwork { - batch = append(batch, spinner.Tick(m.stash.spinner)) - } + batch = m.unloadDocument() } else { // Otherwise send these key messages through to pager for // processing @@ -200,6 +203,11 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) { return m, tea.Quit + case "left", "h", "delete": + if m.state == stateShowDocument && m.pager.state == pagerStateBrowse { + cmds = append(cmds, m.unloadDocument()...) + } + // Ctrl+C always quits no matter where in the application you are. case "ctrl+c": return m, tea.Quit