mirror of
https://github.com/mickael-menu/zk
synced 2024-11-07 15:20:21 +00:00
Don't launch fzf or the pager when --no-input is set
This commit is contained in:
parent
17c30a66e5
commit
aa87c570c2
@ -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),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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.
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
|
@ -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 {
|
||||
|
9
main.go
9
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.
|
||||
|
Loading…
Reference in New Issue
Block a user