Improve help output and add --delimiter0 in list command

pull/6/head
Mickaël Menu 3 years ago
parent e61a2c9dba
commit 17c30a66e5
No known key found for this signature in database
GPG Key ID: 53D73664CD359895

@ -11,31 +11,31 @@ import (
// Filtering holds filtering options to select notes.
type Filtering struct {
Path []string `arg optional placeholder:"<glob>"`
Match string `short:"m" placeholder:"<query>" help:"Terms to search for in the notes."`
Limit int `short:"n" placeholder:"<count>" help:"Limit the number of results."`
Created string ` placeholder:"<date>" help:"Only the notes created on the given date."`
CreatedBefore string ` placeholder:"<date>" help:"Only the notes created before the given date."`
CreatedAfter string ` placeholder:"<date>" help:"Only the notes created after the given date."`
Modified string ` placeholder:"<date>" help:"Only the notes modified on the given date."`
ModifiedBefore string ` placeholder:"<date>" help:"Only the notes modified before the given date."`
ModifiedAfter string ` placeholder:"<date>" help:"Only the notes modified after the given date."`
Related []string ` help:"Only the notes which might be related to the given notes." xor:"link"`
LinkedBy []string `short:"l" placeholder:"<path>" help:"Only the notes linked by the given notes." xor:"link"`
LinkingTo []string `short:"L" placeholder:"<path>" help:"Only the notes linking to the given notes." xor:"link"`
NotLinkedBy []string ` placeholder:"<path>" help:"Only the notes not linked by the given notes." xor:"link"`
NotLinkingTo []string ` placeholder:"<path>" help:"Only the notes not linking to the given notes." xor:"link"`
MaxDistance int ` help:"Maximum distance between two linked notes."`
Orphan bool ` help:"Only the notes which don't have any other note linking to them."`
Exclude []string `short:"x" placeholder:"<glob>" help:"Excludes notes matching the given file path pattern from the list."`
Recursive bool `short:"r" help:"Follow links recursively."`
Interactive bool `short:"i" help:"Further filter the list of notes interactively."`
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."`
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."`
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`
NotLinkedBy []string `group:filter placeholder:PATH help:"Find notes which are not linked by the given ones." xor:link`
NotLinkingTo []string `group:filter placeholder:PATH help:"Find notes which are not linking to the given notes." xor:link`
Related []string `group:filter placeholder:PATH help:"Find notes which might be related to the given ones." xor:link`
MaxDistance int `group:filter placeholder:COUNT help:"Maximum distance between two linked notes."`
Recursive bool `group:filter short:r help:"Follow links recursively."`
Created string `group:filter placeholder:DATE help:"Find notes created on the given date."`
CreatedBefore string `group:filter placeholder:DATE help:"Find notes created before the given date."`
CreatedAfter string `group:filter placeholder:DATE help:"Find notes created after the given date."`
Modified string `group:filter placeholder:DATE help:"Find notes modified on the given date."`
ModifiedBefore string `group:filter placeholder:DATE help:"Find notes modified before the given date."`
ModifiedAfter string `group:filter placeholder:DATE help:"Find notes modified after the given date."`
}
// Sorting holds sorting options to order notes.
type Sorting struct {
Sort []string `short:"s" placeholder:"<term>" help:"Sort the notes by the given criterion."`
Sort []string `group:sort short:s placeholder:TERM help:"Sort the listed notes by the given criterion."`
}
// NewFinderOpts creates an instance of note.FinderOpts from a set of user flags.

@ -8,23 +8,28 @@ import (
"github.com/mickael-menu/zk/adapter/fzf"
"github.com/mickael-menu/zk/adapter/sqlite"
"github.com/mickael-menu/zk/core/note"
"github.com/mickael-menu/zk/util/errors"
"github.com/mickael-menu/zk/util/opt"
"github.com/mickael-menu/zk/util/strings"
)
// List displays notes matching a set of criteria.
type List struct {
Format string `help:"Pretty prints the list using the given format." short:"f" placeholder:"<template>"`
NoPager bool `help:"Do not pipe zk output into a pager." short:"P"`
Quiet bool `help:"Don't show anything besides the notes themselves." short:"q"`
Delimiter string `default:"
" help:"Delimiter separating each result." short:"d"`
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`.\""
Filtering
Sorting
}
func (cmd *List) Run(container *Container) error {
if cmd.Delimiter0 {
cmd.Delimiter = "\x00"
}
zk, err := container.OpenZk()
if err != nil {
return err
@ -32,7 +37,7 @@ func (cmd *List) Run(container *Container) error {
opts, err := NewFinderOpts(zk, cmd.Filtering, cmd.Sorting)
if err != nil {
return errors.Wrapf(err, "incorrect criteria")
return err
}
db, err := container.Database(zk.DBPath())
@ -82,6 +87,9 @@ func (cmd *List) Run(container *Container) error {
}
fmt.Fprintf(out, ft)
}
if cmd.Delimiter0 {
fmt.Fprintf(out, "\x00")
}
return nil
})

@ -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."`
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."`
}
func (cmd *New) ConfigOverrides() zk.ConfigOverrides {

@ -5,6 +5,7 @@ import (
"os"
"strings"
"github.com/kballard/go-shellquote"
"github.com/mickael-menu/zk/core/zk"
"github.com/mickael-menu/zk/util/errors"
executil "github.com/mickael-menu/zk/util/exec"
@ -19,7 +20,7 @@ func Edit(zk *zk.Zk, paths ...string) error {
return fmt.Errorf("no editor set in config")
}
cmd := executil.CommandFromString(editor.String(), paths...)
cmd := executil.CommandFromString(editor.String() + " " + shellquote.Join(paths...))
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

@ -150,7 +150,7 @@ func SorterFromString(str string) (Sorter, error) {
case "word-count", "wc":
sorter = Sorter{Field: SortWordCount, Ascending: true}
default:
return sorter, fmt.Errorf("%s: unknown sorting term", str)
return sorter, fmt.Errorf("%s: unknown sorting term\ntry created, modified, path, title, random or word-count", str)
}
switch orderSymbol {

@ -4,7 +4,7 @@ go 1.15
require (
github.com/AlecAivazis/survey/v2 v2.2.7
github.com/alecthomas/kong v0.2.12
github.com/alecthomas/kong v0.2.16-0.20210209082517-405b2f4fd9a4
github.com/aymerick/raymond v2.0.2+incompatible
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
github.com/fatih/color v1.10.0

@ -5,6 +5,8 @@ github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8/go.mod h1:oX5x61
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
github.com/alecthomas/kong v0.2.12 h1:X3kkCOXGUNzLmiu+nQtoxWqj4U2a39MpSJR3QdQXOwI=
github.com/alecthomas/kong v0.2.12/go.mod h1:kQOmtJgV+Lb4aj+I2LEn40cbtawdWJ9Y8QLq+lElKxE=
github.com/alecthomas/kong v0.2.16-0.20210209082517-405b2f4fd9a4 h1:TeW3HEkctVgQL2uQiHERcwxZkN9U4WQ+6pKEgOfhCt0=
github.com/alecthomas/kong v0.2.16-0.20210209082517-405b2f4fd9a4/go.mod h1:kQOmtJgV+Lb4aj+I2LEn40cbtawdWJ9Y8QLq+lElKxE=
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/aymerick/raymond v2.0.2+incompatible h1:VEp3GpgdAnv9B2GFyTvqgcKvY+mfKMjPOA3SbKLtnU0=
github.com/aymerick/raymond v2.0.2+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=

@ -7,6 +7,7 @@ import (
"github.com/alecthomas/kong"
"github.com/mickael-menu/zk/cmd"
"github.com/mickael-menu/zk/core/style"
executil "github.com/mickael-menu/zk/util/exec"
)
@ -14,11 +15,13 @@ var Version = "dev"
var Build = "dev"
var cli struct {
Index cmd.Index `cmd help:"Index the notes in the given directory to be searchable."`
Init cmd.Init `cmd help:"Create a slip box in the given directory."`
List cmd.List `cmd help:"List notes matching given criteria."`
Edit cmd.Edit `cmd help:"Edit notes matching given criteria."`
New cmd.New `cmd help:"Create a new note in the given slip box directory."`
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."`
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."`
ShowHelp ShowHelp `cmd default:"1" hidden:true`
@ -64,16 +67,25 @@ func main() {
}
func options(container *cmd.Container) []kong.Option {
term := container.Terminal
return []kong.Option{
kong.Bind(container),
kong.Name("zk"),
kong.UsageOnError(),
kong.ConfigureHelp(kong.HelpOptions{
Compact: true,
}),
kong.HelpOptions{
Compact: true,
FlagsLast: true,
},
kong.Vars{
"version": Version,
},
kong.Groups(map[string]string{
"filter": "Filtering",
"sort": "Sorting",
"format": "Formatting",
"notes": term.MustStyle("NOTES", style.RuleYellow, style.RuleBold) + "\n" + term.MustStyle("Edit or browse your notes", style.RuleBold),
"zk": term.MustStyle("SLIP BOX", style.RuleYellow, style.RuleBold) + "\n" + term.MustStyle("A slip box is a directory containing your notes", style.RuleBold),
}),
}
}

Loading…
Cancel
Save