zk/cmd/list.go

103 lines
2.4 KiB
Go
Raw Normal View History

2021-01-03 20:19:21 +00:00
package cmd
import (
2021-01-09 16:14:54 +00:00
"fmt"
"io"
2021-01-23 12:29:14 +00:00
"os"
2021-01-09 16:14:54 +00:00
"github.com/mickael-menu/zk/adapter/fzf"
2021-01-03 20:19:21 +00:00
"github.com/mickael-menu/zk/adapter/sqlite"
"github.com/mickael-menu/zk/core/note"
2021-01-09 16:14:54 +00:00
"github.com/mickael-menu/zk/util/opt"
"github.com/mickael-menu/zk/util/strings"
2021-01-03 20:19:21 +00:00
)
// List displays notes matching a set of criteria.
type List struct {
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
2021-01-03 20:19:21 +00:00
}
func (cmd *List) Run(container *Container) error {
if cmd.Delimiter0 {
cmd.Delimiter = "\x00"
}
2021-01-24 11:10:13 +00:00
zk, err := container.OpenZk()
2021-01-03 20:19:21 +00:00
if err != nil {
return err
}
2021-01-23 14:39:47 +00:00
opts, err := NewFinderOpts(zk, cmd.Filtering, cmd.Sorting)
2021-01-12 19:54:08 +00:00
if err != nil {
return err
2021-01-12 19:54:08 +00:00
}
2021-01-03 20:19:21 +00:00
db, err := container.Database(zk.DBPath())
if err != nil {
return err
}
2021-01-23 12:29:14 +00:00
wd, err := os.Getwd()
if err != nil {
return err
}
2021-01-23 12:29:14 +00:00
templates := container.TemplateLoader(zk.Config.Lang)
styler := container.Terminal
2021-01-23 12:29:14 +00:00
format := opt.NewNotEmptyString(cmd.Format)
formatter, err := note.NewFormatter(zk.Path, wd, format, templates, styler)
if err != nil {
return err
}
2021-01-03 20:19:21 +00:00
2021-01-23 12:29:14 +00:00
var notes []note.Match
err = db.WithTransaction(func(tx sqlite.Transaction) error {
finder := container.NoteFinder(tx, fzf.NoteFinderOpts{
AlwaysFilter: false,
})
notes, err = finder.Find(*opts)
2021-01-23 12:29:14 +00:00
return err
})
if err != nil {
if err == note.ErrCanceled {
return nil
}
2021-01-23 12:29:14 +00:00
return err
}
2021-01-23 12:29:14 +00:00
count := len(notes)
if count > 0 {
err = container.Paginate(cmd.NoPager, zk.Config, func(out io.Writer) error {
2021-01-27 19:20:37 +00:00
for i, note := range notes {
if i > 0 {
fmt.Fprintf(out, cmd.Delimiter)
}
2021-01-23 12:29:14 +00:00
ft, err := formatter.Format(note)
if err != nil {
return err
}
2021-01-27 19:20:37 +00:00
fmt.Fprintf(out, ft)
2021-01-23 12:29:14 +00:00
}
if cmd.Delimiter0 {
fmt.Fprintf(out, "\x00")
}
2021-01-23 12:29:14 +00:00
return nil
})
2021-01-23 12:29:14 +00:00
}
2021-01-25 20:44:44 +00:00
if err == nil && !cmd.Quiet {
2021-02-07 18:20:07 +00:00
fmt.Fprintf(os.Stderr, "\n\nFound %d %s\n", count, strings.Pluralize("note", count))
2021-01-23 12:29:14 +00:00
}
2021-01-12 19:54:08 +00:00
2021-01-23 12:29:14 +00:00
return err
2021-01-12 19:54:08 +00:00
}