From fcf109aaff277fe8e26e9afb29609498c1a9cc64 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Sat, 17 Oct 2020 21:46:02 -0400 Subject: [PATCH] Add flag for local files only (aka disable network) in TUI --- main.go | 9 ++++++++- ui/stash.go | 30 +++++++++++++++++++++++------- ui/ui.go | 31 +++++++++++++++++++++---------- 3 files changed, 52 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index 2c26497..7f43103 100644 --- a/main.go +++ b/main.go @@ -31,6 +31,7 @@ var ( style string width uint showAllFiles bool + localOnly bool rootCmd = &cobra.Command{ Use: "glow SOURCE", @@ -262,7 +263,12 @@ func runTUI(stashedOnly bool) error { } cfg.ShowAllFiles = showAllFiles - cfg.StashedOnly = stashedOnly + + if stashedOnly { + cfg.DocumentTypes = ui.StashedDocument | ui.NewsDocument + } else if localOnly { + cfg.DocumentTypes = ui.LocalDocument + } // Run Bubble Tea program p := ui.NewProgram(style, cfg) @@ -299,6 +305,7 @@ func init() { rootCmd.Flags().StringVarP(&style, "style", "s", "auto", "style name or JSON path") rootCmd.Flags().UintVarP(&width, "width", "w", 0, "word-wrap at width") rootCmd.Flags().BoolVarP(&showAllFiles, "all", "a", false, "show system files and directories (TUI-mode only)") + rootCmd.Flags().BoolVarP(&localOnly, "local", "l", false, "show local files only; no network (TUI-mode only)") // Stash stashCmd.PersistentFlags().StringVarP(&memo, "memo", "m", "", "memo/note for stashing") diff --git a/ui/stash.go b/ui/stash.go index 5f81012..a4520a0 100644 --- a/ui/stash.go +++ b/ui/stash.go @@ -44,6 +44,14 @@ type deletedStashedItemMsg int // MODEL +type DocumentType byte + +const ( + LocalDocument DocumentType = 1 << iota + StashedDocument + NewsDocument +) + type loadedState byte const ( @@ -109,6 +117,14 @@ type stashModel struct { statusMessageTimer *time.Timer } +func (m stashModel) localOnly() bool { + return m.cfg.DocumentTypes == LocalDocument +} + +func (m stashModel) stashedOnly() bool { + return m.cfg.DocumentTypes&LocalDocument == 0 +} + func (m *stashModel) setSize(width, height int) { m.terminalWidth = width m.terminalHeight = height @@ -280,7 +296,7 @@ func stashUpdate(msg tea.Msg, m stashModel) (stashModel, tea.Cmd) { } case spinner.TickMsg: - condition := !m.loaded.done(m.cfg.StashedOnly) || + condition := !m.loaded.done(m.stashedOnly()) || m.loadingFromNetwork || m.state == stashStateLoadingDocument || len(m.filesStashing) > 0 || @@ -426,7 +442,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(m.cfg.StashedOnly) && !m.spinner.Visible() { + if m.loaded.done(m.stashedOnly()) && !m.spinner.Visible() { m.spinner.Start() cmds = append(cmds, spinner.Tick(m.spinner)) } @@ -570,7 +586,7 @@ func stashView(m stashModel) string { case stashStateReady, stashStateSettingNote, stashStatePromptDelete: loadingIndicator := "" - if !m.loaded.done(m.cfg.StashedOnly) || m.loadingFromNetwork || m.spinner.Visible() { + if !m.localOnly() && (!m.loaded.done(m.stashedOnly()) || m.loadingFromNetwork || m.spinner.Visible()) { loadingIndicator = spinner.View(m.spinner) } @@ -639,10 +655,10 @@ func glowLogoView(text string) string { } func stashHeaderView(m stashModel) string { - loading := !m.loaded.done(m.cfg.StashedOnly) + loading := !m.loaded.done(m.stashedOnly()) noMarkdowns := len(m.markdowns) == 0 - if m.authStatus == authFailed && m.cfg.StashedOnly { + if m.authStatus == authFailed && m.stashedOnly() { return common.Subtle("Can’t load stash. Are you offline?") } @@ -653,7 +669,7 @@ func stashHeaderView(m stashModel) string { // Still loading. We haven't found files, stashed items, or news yet. if loading && noMarkdowns { - if m.cfg.StashedOnly { + if m.stashedOnly() { return common.Subtle("Loading your stash...") } else { return common.Subtle("Looking for stuff...") + maybeOffline @@ -665,7 +681,7 @@ func stashHeaderView(m stashModel) string { // Loading's finished and all we have is news. if !loading && localItems == 0 && stashedItems == 0 { - if m.cfg.StashedOnly { + if m.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 92edba1..75cd87d 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -35,7 +35,9 @@ type Config struct { ShowAllFiles bool Gopath string `env:"GOPATH"` HomeDir string `env:"HOME"` - StashedOnly bool + + // Which document types shall we show? We work though this with bitmasking. + DocumentTypes DocumentType // For debugging the UI Logfile string `env:"GLOW_LOGFILE"` @@ -163,7 +165,10 @@ func (m *model) unloadDocument() []tea.Cmd { if m.pager.viewport.HighPerformanceRendering { batch = append(batch, tea.ClearScrollArea) } - if !m.stash.loaded.done(m.cfg.StashedOnly) || m.stash.loadingFromNetwork { + + stashedOnly := m.cfg.DocumentTypes&LocalDocument == 0 + + if !m.stash.loaded.done(stashedOnly) || m.stash.loadingFromNetwork { batch = append(batch, spinner.Tick(m.stash.spinner)) } return batch @@ -197,11 +202,20 @@ func initialize(cfg Config, style string) func() (tea.Model, tea.Cmd) { m.pager = newPagerModel(m.authStatus, style) m.stash = newStashModel(&cfg, m.authStatus) - cmds := []tea.Cmd{ - newCharmClient, - spinner.Tick(m.stash.spinner), + if cfg.DocumentTypes == 0 { + cfg.DocumentTypes = LocalDocument | StashedDocument | NewsDocument } - if !cfg.StashedOnly { + + var cmds []tea.Cmd + + if cfg.DocumentTypes&StashedDocument != 0 || cfg.DocumentTypes&NewsDocument != 0 { + cmds = append(cmds, + newCharmClient, + spinner.Tick(m.stash.spinner), + ) + } + + if cfg.DocumentTypes&LocalDocument != 0 { cmds = append(cmds, findLocalFiles(m)) } @@ -297,21 +311,18 @@ 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: newMd := localFileToMarkdown(m.cwd, gitcha.SearchResult(msg)) m.stash.addMarkdowns(newMd) cmds = append(cmds, findNextLocalFile(m)) case sshAuthErrMsg: - // If we haven't run the keygen yet, do that - if m.keygenState != keygenFinished { + if m.keygenState != keygenFinished { // if we haven't run the keygen yet, do that m.keygenState = keygenRunning cmds = append(cmds, generateSSHKeys) } else {