From aa87c570c255e508923440e3442a96e6135030ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mickae=CC=88l=20Menu?= Date: Wed, 10 Feb 2021 20:53:25 +0100 Subject: [PATCH] Don't launch fzf or the pager when --no-input is set --- adapter/fzf/finder.go | 21 +++++++++++---------- adapter/term/prompt.go | 12 ++++++++---- cmd/container.go | 2 +- cmd/edit.go | 8 ++++++-- cmd/finder_opts.go | 6 +++--- cmd/index.go | 4 ++++ cmd/list.go | 5 ++--- cmd/new.go | 4 ++-- main.go | 9 +++++---- 9 files changed, 42 insertions(+), 29 deletions(-) diff --git a/adapter/fzf/finder.go b/adapter/fzf/finder.go index b717bad..25e35c1 100644 --- a/adapter/fzf/finder.go +++ b/adapter/fzf/finder.go @@ -4,6 +4,7 @@ import ( "fmt" "os" + "github.com/mickael-menu/zk/adapter/term" "github.com/mickael-menu/zk/core/note" "github.com/mickael-menu/zk/core/style" "github.com/mickael-menu/zk/core/zk" @@ -13,9 +14,9 @@ import ( // NoteFinder wraps a note.Finder and filters its result interactively using fzf. type NoteFinder struct { - opts NoteFinderOpts - finder note.Finder - styler style.Styler + opts NoteFinderOpts + finder note.Finder + terminal *term.Terminal } type NoteFinderOpts struct { @@ -26,11 +27,11 @@ type NoteFinderOpts struct { NewNoteDir *zk.Dir } -func NewNoteFinder(opts NoteFinderOpts, finder note.Finder, styler style.Styler) *NoteFinder { +func NewNoteFinder(opts NoteFinderOpts, finder note.Finder, terminal *term.Terminal) *NoteFinder { return &NoteFinder{ - opts: opts, - finder: finder, - styler: styler, + opts: opts, + finder: finder, + terminal: terminal, } } @@ -38,7 +39,7 @@ func (f *NoteFinder) Find(opts note.FinderOpts) ([]note.Match, error) { isInteractive, opts := popInteractiveFilter(opts) matches, err := f.finder.Find(opts) - if !isInteractive || err != nil || (!f.opts.AlwaysFilter && len(matches) == 0) { + if !isInteractive || !f.terminal.IsInteractive() || err != nil || (!f.opts.AlwaysFilter && len(matches) == 0) { return matches, err } @@ -77,8 +78,8 @@ func (f *NoteFinder) Find(opts note.FinderOpts) ([]note.Match, error) { for _, match := range matches { fzf.Add([]string{ match.Path, - f.styler.MustStyle(match.Title, style.RuleYellow), - f.styler.MustStyle(stringsutil.JoinLines(match.Body), style.RuleBlack), + f.terminal.MustStyle(match.Title, style.RuleYellow), + f.terminal.MustStyle(stringsutil.JoinLines(match.Body), style.RuleBlack), }) } diff --git a/adapter/term/prompt.go b/adapter/term/prompt.go index 12e5368..c115cee 100644 --- a/adapter/term/prompt.go +++ b/adapter/term/prompt.go @@ -5,12 +5,16 @@ import ( ) // Confirm is a shortcut to prompt a yes/no question to the user. -func (t *Terminal) Confirm(msg string) bool { - confirmed := false +func (t *Terminal) Confirm(msg string, defaultAnswer bool) (confirmed, skipped bool) { + if !t.IsInteractive() { + return defaultAnswer, true + } + + confirmed = false prompt := &survey.Confirm{ Message: msg, - Default: true, + Default: defaultAnswer, } survey.AskOne(prompt, &confirmed) - return confirmed + return confirmed, false } diff --git a/cmd/container.go b/cmd/container.go index c753d0b..993e005 100644 --- a/cmd/container.go +++ b/cmd/container.go @@ -88,7 +88,7 @@ func (c *Container) Paginate(noPager bool, config zk.Config, run func(out io.Wri } func (c *Container) pager(noPager bool, config zk.Config) (*pager.Pager, error) { - if noPager { + if noPager || !c.Terminal.IsInteractive() { return pager.PassthroughPager, nil } else { return pager.New(config.Pager, c.Logger) diff --git a/cmd/edit.go b/cmd/edit.go index 1409894..c29c207 100644 --- a/cmd/edit.go +++ b/cmd/edit.go @@ -13,9 +13,10 @@ import ( // Edit opens notes matching a set of criteria with the user editor. type Edit struct { + Force bool `short:f help:"Do not confirm before editing many notes at the same time."` + Filtering Sorting - Force bool `help:"Don't confirm before editing many notes at the same time." short:"f"` } func (cmd *Edit) Run(container *Container) error { @@ -54,7 +55,10 @@ func (cmd *Edit) Run(container *Container) error { if count > 0 { if !cmd.Force && count > 5 { - if !container.Terminal.Confirm(fmt.Sprintf("Are you sure you want to open %v notes in the editor?", count)) { + confirmed, skipped := container.Terminal.Confirm(fmt.Sprintf("Are you sure you want to open %v notes in the editor?", count), false) + if skipped { + return fmt.Errorf("too many notes to be opened in the editor, aborting…") + } else if !confirmed { return nil } } diff --git a/cmd/finder_opts.go b/cmd/finder_opts.go index a8b7566..6ced751 100644 --- a/cmd/finder_opts.go +++ b/cmd/finder_opts.go @@ -14,9 +14,9 @@ type Filtering struct { Path []string `group:filter arg optional placeholder:PATH help:"Find notes matching the given path, including its descendants."` Interactive bool `group:filter short:i help:"Select notes interactively with fzf."` - Limit int `group:filter short:n placeholder:COUNT help:"Limit the number of notes listed."` + Limit int `group:filter short:n placeholder:COUNT help:"Limit the number of notes found."` Match string `group:filter short:m placeholder:QUERY help:"Terms to search for in the notes."` - Exclude []string `group:filter short:x placeholder:PATH help:"Do not list notes matching the given path, including its descendants."` + Exclude []string `group:filter short:x placeholder:PATH help:"Ignore notes matching the given path, including its descendants."` Orphan bool `group:filter help:"Find notes which are not linked by any other note." xor:link` LinkedBy []string `group:filter short:l placeholder:PATH help:"Find notes which are linked by the given ones." xor:link` LinkingTo []string `group:filter short:L placeholder:PATH help:"Find notes which are linking to the given ones." xor:link` @@ -35,7 +35,7 @@ type Filtering struct { // Sorting holds sorting options to order notes. type Sorting struct { - Sort []string `group:sort short:s placeholder:TERM help:"Sort the listed notes by the given criterion."` + Sort []string `group:sort short:s placeholder:TERM help:"Order the notes by the given criterion."` } // NewFinderOpts creates an instance of note.FinderOpts from a set of user flags. diff --git a/cmd/index.go b/cmd/index.go index dd37c6d..fde24d6 100644 --- a/cmd/index.go +++ b/cmd/index.go @@ -17,6 +17,10 @@ type Index struct { Quiet bool `short:"q" help:"Do not print statistics nor progress."` } +func (cmd *Index) Help() string { + return "You usually don't need to run `zk index` manually, as notes are indexed automatically before each zk invocation." +} + func (cmd *Index) Run(container *Container) error { zk, err := container.OpenZk() if err != nil { diff --git a/cmd/list.go b/cmd/list.go index f4cffe9..b07938f 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -14,12 +14,11 @@ import ( // List displays notes matching a set of criteria. type List struct { - NoPager bool `short:P help:"Do not pipe output into a pager."` - Quiet bool `short:q help:"Do not print the total number of notes found."` - Format string `group:format short:f placeholder:TEMPLATE help:"Pretty print the list using the given format."` Delimiter string "group:format short:d default:\n help:\"Print notes delimited by the given separator.\"" Delimiter0 bool "group:format short:0 name:delimiter0 help:\"Print notes delimited by ASCII NUL characters. This is useful when used in conjunction with `xargs -0`.\"" + NoPager bool `group:format short:P help:"Do not pipe output into a pager."` + Quiet bool `group:format short:q help:"Do not print the total number of notes found."` Filtering Sorting diff --git a/cmd/new.go b/cmd/new.go index c9172e0..5b680e3 100644 --- a/cmd/new.go +++ b/cmd/new.go @@ -13,10 +13,10 @@ import ( type New struct { Directory string `arg optional type:"path" default:"." help:"Directory in which to create the note."` - PrintPath bool `short:p help:"Prints the path of the created note to stdin instead of editing it."` Title string `short:t placeholder:TITLE help:"Title of the new note."` - Template string `type:path placeholder:PATH help:"Custom template to use to render the note."` Extra map[string]string ` help:"Extra variables passed to the templates."` + Template string `type:path placeholder:PATH help:"Custom template used to render the note."` + PrintPath bool `short:p help:"Print the path of the created note instead of editing it."` } func (cmd *New) ConfigOverrides() zk.ConfigOverrides { diff --git a/main.go b/main.go index 3a2877b..00df93a 100644 --- a/main.go +++ b/main.go @@ -15,16 +15,17 @@ var Version = "dev" var Build = "dev" var cli struct { - Init cmd.Init `cmd group:"zk" help:"Create a slip box in the given directory."` - Index cmd.Index `cmd group:"zk" help:"Index the notes in the given directory to be searchable."` + Init cmd.Init `cmd group:"zk" help:"Create a new slip box in the given directory."` + Index cmd.Index `cmd group:"zk" help:"Index the notes to be searchable."` New cmd.New `cmd group:"notes" help:"Create a new note in the given slip box directory."` List cmd.List `cmd group:"notes" help:"List notes matching the given criteria."` Edit cmd.Edit `cmd group:"notes" help:"Edit notes matching the given criteria."` - NoInput NoInput `help:"Never prompt or ask for confirmation."` - Version kong.VersionFlag `help:"Print zk version."` + NoInput NoInput `help:"Never prompt or ask for confirmation."` + ShowHelp ShowHelp `cmd default:"1" hidden:true` + Version kong.VersionFlag `help:"Print zk version." hidden:true` } // NoInput is a flag preventing any user prompt when enabled.