From aaf6c42bd84f0319175d63785bccfe6360a71fe7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Menu?= Date: Wed, 6 Jul 2022 08:58:35 +0200 Subject: [PATCH] Hide index progress in non-interactive shells (#234) --- CHANGELOG.md | 4 ++++ internal/cli/cmd/index.go | 33 ++++++++++++++++++++++++++++++++- internal/cli/cmd/init.go | 3 ++- internal/core/notebook.go | 18 +++++------------- main.go | 3 ++- 5 files changed, 45 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5dfd8a1..d4ed6a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,10 @@ All notable changes to this project will be documented in this file. * For example, `[[Planet]]` can match a note with filename `i4w0 Planet.md` but not `i4w0.md` with a Markdown title `Planet` anymore. * This "smart" fallback resolution based on note titles was too fragile and not supported by the `zk` CLI. +### Fixed + +* [#233](https://github.com/mickael-menu/zk/issues/233) Hide index progress in non-interactive shells. + ## 0.10.1 diff --git a/internal/cli/cmd/index.go b/internal/cli/cmd/index.go index 42dd3f9..8121fd6 100644 --- a/internal/cli/cmd/index.go +++ b/internal/cli/cmd/index.go @@ -2,9 +2,13 @@ package cmd import ( "fmt" + "os" + "time" "github.com/mickael-menu/zk/internal/cli" "github.com/mickael-menu/zk/internal/core" + "github.com/mickael-menu/zk/internal/util/paths" + "github.com/schollz/progressbar/v3" ) // Index indexes the content of all the notes in the notebook. @@ -24,10 +28,37 @@ func (cmd *Index) Run(container *cli.Container) error { return err } - stats, err := notebook.Index(core.NoteIndexOpts{ + return cmd.RunWithNotebook(container, notebook) +} + +func (cmd *Index) RunWithNotebook(container *cli.Container, notebook *core.Notebook) error { + showProgress := container.Terminal.IsInteractive() + + var bar *progressbar.ProgressBar + if showProgress { + bar = progressbar.NewOptions(-1, + progressbar.OptionSetWriter(os.Stderr), + progressbar.OptionThrottle(100*time.Millisecond), + progressbar.OptionSpinnerType(14), + ) + } + + opts := core.NoteIndexOpts{ Force: cmd.Force, Verbose: cmd.Verbose, + } + + stats, err := notebook.IndexWithCallback(opts, func(change paths.DiffChange) { + if showProgress { + bar.Add(1) + bar.Describe(change.String()) + } }) + + if showProgress { + bar.Clear() + } + if err != nil { return err } diff --git a/internal/cli/cmd/init.go b/internal/cli/cmd/init.go index dbc9e01..2f57fe4 100644 --- a/internal/cli/cmd/init.go +++ b/internal/cli/cmd/init.go @@ -32,7 +32,8 @@ func (cmd *Init) Run(container *cli.Container) error { return err } - _, err = notebook.Index(core.NoteIndexOpts{}) + index := Index{Quiet: true} + err = index.RunWithNotebook(container, notebook) if err != nil { return err } diff --git a/internal/core/notebook.go b/internal/core/notebook.go index 45f6a4f..9d58803 100644 --- a/internal/core/notebook.go +++ b/internal/core/notebook.go @@ -2,7 +2,6 @@ package core import ( "fmt" - "os" "path/filepath" "strings" "time" @@ -11,7 +10,6 @@ import ( "github.com/mickael-menu/zk/internal/util/errors" "github.com/mickael-menu/zk/internal/util/opt" "github.com/mickael-menu/zk/internal/util/paths" - "github.com/schollz/progressbar/v3" ) // Notebook handles queries and commands performed on an opened notebook. @@ -62,13 +60,11 @@ type NotebookFactory func(path string, config Config) (*Notebook, error) // Index indexes the content of the notebook to be searchable. func (n *Notebook) Index(opts NoteIndexOpts) (stats NoteIndexingStats, err error) { - // FIXME: Move out of Core - bar := progressbar.NewOptions(-1, - progressbar.OptionSetWriter(os.Stderr), - progressbar.OptionThrottle(100*time.Millisecond), - progressbar.OptionSpinnerType(14), - ) + return n.IndexWithCallback(opts, func(change paths.DiffChange) {}) +} +// Index indexes the content of the notebook to be searchable. +func (n *Notebook) IndexWithCallback(opts NoteIndexOpts, callback func(change paths.DiffChange)) (stats NoteIndexingStats, err error) { err = n.index.Commit(func(index NoteIndex) error { task := indexTask{ path: n.Path, @@ -79,14 +75,10 @@ func (n *Notebook) Index(opts NoteIndexOpts) (stats NoteIndexingStats, err error parser: n, logger: n.logger, } - stats, err = task.execute(func(change paths.DiffChange) { - bar.Add(1) - bar.Describe(change.String()) - }) + stats, err = task.execute(callback) return err }) - bar.Clear() err = errors.Wrap(err, "indexing") return } diff --git a/main.go b/main.go index 1eb0254..f58af44 100644 --- a/main.go +++ b/main.go @@ -103,7 +103,8 @@ func main() { // command, otherwise it would hide the stats. if ctx.Command() != "index" { if notebook, err := container.CurrentNotebook(); err == nil { - _, err = notebook.Index(core.NoteIndexOpts{}) + index := cmd.Index{Quiet: true} + err = index.RunWithNotebook(container, notebook) ctx.FatalIfErrorf(err) } }