From e73190a2177b85bddb0c128c7aaf0f09a52443fb Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Sat, 17 Oct 2020 17:39:43 -0400 Subject: [PATCH] Move "stashed only" flag from the package-level into the model --- main.go | 4 +--- ui/stash.go | 18 ++++++++++-------- ui/ui.go | 11 ++++------- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index 59888d8..2c26497 100644 --- a/main.go +++ b/main.go @@ -262,9 +262,7 @@ func runTUI(stashedOnly bool) error { } cfg.ShowAllFiles = showAllFiles - if stashedOnly { - cfg.StashedOnly = true - } + cfg.StashedOnly = stashedOnly // Run Bubble Tea program p := ui.NewProgram(style, cfg) diff --git a/ui/stash.go b/ui/stash.go index b95b921..5f81012 100644 --- a/ui/stash.go +++ b/ui/stash.go @@ -72,6 +72,7 @@ const ( type stashModel struct { cc *charm.Client + cfg *Config authStatus authStatus state stashState err error @@ -198,7 +199,7 @@ func (m *stashModel) hideStatusMessage() { // INIT -func newStashModel(as authStatus) stashModel { +func newStashModel(cfg *Config, as authStatus) stashModel { sp := spinner.NewModel() sp.Frames = spinner.Line sp.ForegroundColor = common.SpinnerColor.String() @@ -217,6 +218,7 @@ func newStashModel(as authStatus) stashModel { ni.Focus() m := stashModel{ + cfg: cfg, authStatus: as, spinner: sp, noteInput: ni, @@ -278,7 +280,7 @@ func stashUpdate(msg tea.Msg, m stashModel) (stashModel, tea.Cmd) { } case spinner.TickMsg: - condition := !m.loaded.done(stashedOnly) || + condition := !m.loaded.done(m.cfg.StashedOnly) || m.loadingFromNetwork || m.state == stashStateLoadingDocument || len(m.filesStashing) > 0 || @@ -424,7 +426,7 @@ func stashUpdate(msg tea.Msg, m stashModel) (stashModel, tea.Cmd) { m.filesStashing[md.localPath] = struct{}{} cmds = append(cmds, stashDocument(m.cc, *md)) - if m.loaded.done(stashedOnly) && !m.spinner.Visible() { + if m.loaded.done(m.cfg.StashedOnly) && !m.spinner.Visible() { m.spinner.Start() cmds = append(cmds, spinner.Tick(m.spinner)) } @@ -568,7 +570,7 @@ func stashView(m stashModel) string { case stashStateReady, stashStateSettingNote, stashStatePromptDelete: loadingIndicator := "" - if !m.loaded.done(stashedOnly) || m.loadingFromNetwork || m.spinner.Visible() { + if !m.loaded.done(m.cfg.StashedOnly) || m.loadingFromNetwork || m.spinner.Visible() { loadingIndicator = spinner.View(m.spinner) } @@ -637,10 +639,10 @@ func glowLogoView(text string) string { } func stashHeaderView(m stashModel) string { - loading := !m.loaded.done(stashedOnly) + loading := !m.loaded.done(m.cfg.StashedOnly) noMarkdowns := len(m.markdowns) == 0 - if m.authStatus == authFailed && stashedOnly { + if m.authStatus == authFailed && m.cfg.StashedOnly { return common.Subtle("Can’t load stash. Are you offline?") } @@ -651,7 +653,7 @@ func stashHeaderView(m stashModel) string { // Still loading. We haven't found files, stashed items, or news yet. if loading && noMarkdowns { - if stashedOnly { + if m.cfg.StashedOnly { return common.Subtle("Loading your stash...") } else { return common.Subtle("Looking for stuff...") + maybeOffline @@ -663,7 +665,7 @@ func stashHeaderView(m stashModel) string { // Loading's finished and all we have is news. if !loading && localItems == 0 && stashedItems == 0 { - if stashedOnly { + if m.cfg.StashedOnly { return common.Subtle("No stashed markdown files found.") + maybeOffline } else { return common.Subtle("No local or stashed markdown files found.") + maybeOffline diff --git a/ui/ui.go b/ui/ui.go index 8255f32..92edba1 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -28,7 +28,6 @@ var ( config Config glowLogoTextColor = common.Color("#ECFD65") debug = false // true if we're logging to a file, in which case we'll log more stuff - stashedOnly = false ) // Config contains TUI-specific configuration. @@ -135,7 +134,7 @@ const ( ) type model struct { - cfg Config + cfg *Config cc *charm.Client authStatus authStatus keygenState keygenState @@ -164,7 +163,7 @@ func (m *model) unloadDocument() []tea.Cmd { if m.pager.viewport.HighPerformanceRendering { batch = append(batch, tea.ClearScrollArea) } - if !m.stash.loaded.done(stashedOnly) || m.stash.loadingFromNetwork { + if !m.stash.loaded.done(m.cfg.StashedOnly) || m.stash.loadingFromNetwork { batch = append(batch, spinner.Tick(m.stash.spinner)) } return batch @@ -189,16 +188,14 @@ func initialize(cfg Config, style string) func() (tea.Model, tea.Cmd) { } } - stashedOnly = cfg.StashedOnly - m := model{ - cfg: cfg, + cfg: &cfg, state: stateShowStash, authStatus: authConnecting, keygenState: keygenUnstarted, } m.pager = newPagerModel(m.authStatus, style) - m.stash = newStashModel(m.authStatus) + m.stash = newStashModel(&cfg, m.authStatus) cmds := []tea.Cmd{ newCharmClient,