Skip to content

Commit

Permalink
Add flag for local files only (aka disable network) in TUI
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed Oct 19, 2020
1 parent e73190a commit fcf109a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
style string
width uint
showAllFiles bool
localOnly bool

rootCmd = &cobra.Command{
Use: "glow SOURCE",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
30 changes: 23 additions & 7 deletions ui/stash.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ type deletedStashedItemMsg int

// MODEL

type DocumentType byte

const (
LocalDocument DocumentType = 1 << iota
StashedDocument
NewsDocument
)

type loadedState byte

const (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ||
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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?")
}

Expand All @@ -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
Expand All @@ -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
Expand Down
31 changes: 21 additions & 10 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit fcf109a

Please sign in to comment.