Run glow stash to browse stashed files only

This commit is contained in:
Christian Rocha 2020-09-07 19:14:59 -04:00 committed by Christian Muehlhaeuser
parent 6e7cea7e52
commit eb6fa55ec3
4 changed files with 72 additions and 46 deletions

66
main.go
View file

@ -171,36 +171,7 @@ func executeArg(cmd *cobra.Command, arg string, w io.Writer) error {
// Only run TUI if there are no arguments (excluding flags)
if arg == "" {
// Read environment to get debugging stuff
var cfg ui.Config
if err := babyenv.Parse(&cfg); err != nil {
return fmt.Errorf("error parsing config: %v", err)
}
// Log to file, if set
if cfg.Logfile != "" {
f, err := tea.LogToFile(cfg.Logfile, "glow")
if err != nil {
return err
}
defer f.Close()
}
cfg.ShowAllFiles = showAllFiles
// Run Bubble Tea program
p := ui.NewProgram(style, cfg)
p.EnterAltScreen()
if err := p.Start(); err != nil {
return err
}
p.ExitAltScreen()
p.DisableMouseCellMotion()
// Exit message
fmt.Printf("\n Thanks for using Glow!\n\n")
return nil
return runTUI(false)
}
// create an io.Reader from the markdown source in cli-args
@ -274,6 +245,41 @@ func executeArg(cmd *cobra.Command, arg string, w io.Writer) error {
return nil
}
func runTUI(stashedOnly bool) error {
// Read environment to get debugging stuff
var cfg ui.Config
if err := babyenv.Parse(&cfg); err != nil {
return fmt.Errorf("error parsing config: %v", err)
}
// Log to file, if set
if cfg.Logfile != "" {
f, err := tea.LogToFile(cfg.Logfile, "glow")
if err != nil {
return err
}
defer f.Close()
}
cfg.ShowAllFiles = showAllFiles
if stashedOnly {
cfg.StashedOnly = true
}
// Run Bubble Tea program
p := ui.NewProgram(style, cfg)
p.EnterAltScreen()
if err := p.Start(); err != nil {
return err
}
p.ExitAltScreen()
p.DisableMouseCellMotion()
// Exit message
fmt.Printf("\n Thanks for using Glow!\n\n")
return nil
}
func main() {
if err := rootCmd.Execute(); err != nil {
os.Exit(-1)

View file

@ -18,13 +18,17 @@ var (
memo string
stashCmd = &cobra.Command{
Use: "stash SOURCE",
Use: "stash [SOURCE]",
Hidden: false,
Short: "Stash a markdown",
Long: formatBlock(fmt.Sprintf("\nSave a mardkdown file to your %s.", common.Keyword("stash"))),
Example: formatBlock("glow stash README.md\nglow stash -m \"secret notes\" path/to/notes.md"),
Args: cobra.ExactArgs(1),
Long: formatBlock(fmt.Sprintf("\nDo %s stuff. Run with no arguments to browse your stash or pass a path to a markdown file to stash it.", common.Keyword("stash"))),
Example: formatBlock("glow stash\nglow stash README.md\nglow stash -m \"secret notes\" path/to/notes.md"),
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return runTUI(true)
}
filePath := args[0]
if memo == "" {

View file

@ -107,8 +107,12 @@ const (
loadedLocalFiles
)
func (s loadedState) done() bool {
return s&loadedStash != 0 && s&loadedNews != 0 && s&loadedLocalFiles != 0
func (s loadedState) done(stashedOnly bool) bool {
state := s&loadedStash != 0 && s&loadedNews != 0
if stashedOnly {
return state
}
return state && s&loadedLocalFiles != 0
}
type stashState int
@ -327,7 +331,7 @@ func stashUpdate(msg tea.Msg, m stashModel) (stashModel, tea.Cmd) {
}
case spinner.TickMsg:
condition := !m.loaded.done() ||
condition := !m.loaded.done(stashedOnly) ||
m.loadingFromNetwork ||
m.state == stashStateLoadingDocument ||
len(m.filesStashing) > 0 ||
@ -466,7 +470,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.spinner.Visible() {
if m.loaded.done(stashedOnly) && !m.spinner.Visible() {
m.spinner.Start()
cmds = append(cmds, spinner.Tick(m.spinner))
}
@ -610,7 +614,7 @@ func stashView(m stashModel) string {
case stashStateReady, stashStateSettingNote, stashStatePromptDelete:
loadingIndicator := ""
if !m.loaded.done() || m.loadingFromNetwork || m.spinner.Visible() {
if !m.loaded.done(stashedOnly) || m.loadingFromNetwork || m.spinner.Visible() {
loadingIndicator = spinner.View(m.spinner)
}
@ -679,7 +683,7 @@ func glowLogoView(text string) string {
}
func stashHeaderView(m stashModel) string {
loading := !m.loaded.done()
loading := !m.loaded.done(stashedOnly)
noMarkdowns := len(m.markdowns) == 0
// Still loading. We haven't found files, stashed items, or news yet.
@ -692,7 +696,11 @@ func stashHeaderView(m stashModel) string {
// Loading's finished and all we have is news.
if !loading && localItems == 0 && stashedItems == 0 {
return common.Subtle("No local or stashed markdown files found.")
if stashedOnly {
return common.Subtle("No stashed markdown files found.")
} else {
return common.Subtle("No local or stashed markdown files found.")
}
}
// There are local and/or stashed files, so display counts.

View file

@ -28,13 +28,15 @@ 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 configuration specified to the TUI.
// Config contains TUI-specific configuration.
type Config struct {
ShowAllFiles bool
Gopath string `env:"GOPATH"`
HomeDir string `env:"HOME"`
StashedOnly bool
// For debugging the UI
Logfile string `env:"GLOW_UI_LOGFILE"`
@ -143,7 +145,7 @@ func (m *model) unloadDocument() []tea.Cmd {
if m.pager.viewport.HighPerformanceRendering {
batch = append(batch, tea.ClearScrollArea)
}
if !m.stash.loaded.done() || m.stash.loadingFromNetwork {
if !m.stash.loaded.done(stashedOnly) || m.stash.loadingFromNetwork {
batch = append(batch, spinner.Tick(m.stash.spinner))
}
return batch
@ -162,6 +164,8 @@ func initialize(cfg Config, style string) func() (tea.Model, tea.Cmd) {
}
}
stashedOnly = cfg.StashedOnly
m := model{
cfg: cfg,
stash: newStashModel(),
@ -170,11 +174,15 @@ func initialize(cfg Config, style string) func() (tea.Model, tea.Cmd) {
keygenState: keygenUnstarted,
}
return m, tea.Batch(
findLocalFiles(m),
cmds := []tea.Cmd{
newCharmClient,
spinner.Tick(m.stash.spinner),
)
}
if !cfg.StashedOnly {
cmds = append(cmds, findLocalFiles(m))
}
return m, tea.Batch(cmds...)
}
}