Hide index progress in non-interactive shells (#234)

pull/240/head
Mickaël Menu 2 years ago committed by GitHub
parent 68e6b70eae
commit aaf6c42bd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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. * 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. * 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 ## 0.10.1

@ -2,9 +2,13 @@ package cmd
import ( import (
"fmt" "fmt"
"os"
"time"
"github.com/mickael-menu/zk/internal/cli" "github.com/mickael-menu/zk/internal/cli"
"github.com/mickael-menu/zk/internal/core" "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. // 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 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, Force: cmd.Force,
Verbose: cmd.Verbose, 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 { if err != nil {
return err return err
} }

@ -32,7 +32,8 @@ func (cmd *Init) Run(container *cli.Container) error {
return err return err
} }
_, err = notebook.Index(core.NoteIndexOpts{}) index := Index{Quiet: true}
err = index.RunWithNotebook(container, notebook)
if err != nil { if err != nil {
return err return err
} }

@ -2,7 +2,6 @@ package core
import ( import (
"fmt" "fmt"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
@ -11,7 +10,6 @@ import (
"github.com/mickael-menu/zk/internal/util/errors" "github.com/mickael-menu/zk/internal/util/errors"
"github.com/mickael-menu/zk/internal/util/opt" "github.com/mickael-menu/zk/internal/util/opt"
"github.com/mickael-menu/zk/internal/util/paths" "github.com/mickael-menu/zk/internal/util/paths"
"github.com/schollz/progressbar/v3"
) )
// Notebook handles queries and commands performed on an opened notebook. // 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. // Index indexes the content of the notebook to be searchable.
func (n *Notebook) Index(opts NoteIndexOpts) (stats NoteIndexingStats, err error) { func (n *Notebook) Index(opts NoteIndexOpts) (stats NoteIndexingStats, err error) {
// FIXME: Move out of Core return n.IndexWithCallback(opts, func(change paths.DiffChange) {})
bar := progressbar.NewOptions(-1, }
progressbar.OptionSetWriter(os.Stderr),
progressbar.OptionThrottle(100*time.Millisecond),
progressbar.OptionSpinnerType(14),
)
// 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 { err = n.index.Commit(func(index NoteIndex) error {
task := indexTask{ task := indexTask{
path: n.Path, path: n.Path,
@ -79,14 +75,10 @@ func (n *Notebook) Index(opts NoteIndexOpts) (stats NoteIndexingStats, err error
parser: n, parser: n,
logger: n.logger, logger: n.logger,
} }
stats, err = task.execute(func(change paths.DiffChange) { stats, err = task.execute(callback)
bar.Add(1)
bar.Describe(change.String())
})
return err return err
}) })
bar.Clear()
err = errors.Wrap(err, "indexing") err = errors.Wrap(err, "indexing")
return return
} }

@ -103,7 +103,8 @@ func main() {
// command, otherwise it would hide the stats. // command, otherwise it would hide the stats.
if ctx.Command() != "index" { if ctx.Command() != "index" {
if notebook, err := container.CurrentNotebook(); err == nil { 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) ctx.FatalIfErrorf(err)
} }
} }

Loading…
Cancel
Save