Add the tag list command

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

@ -0,0 +1,143 @@
package cmd
import (
"fmt"
"io"
"os"
"github.com/mickael-menu/zk/internal/cli"
"github.com/mickael-menu/zk/internal/core"
"github.com/mickael-menu/zk/internal/util/errors"
"github.com/mickael-menu/zk/internal/util/strings"
)
// Tag manages the note tags in the notebook.
type Tag struct {
List TagList `cmd group:"cmd" default:"withargs" help:"List all the note tags."`
}
// TagList lists all the note tags.
type TagList struct {
Format string `group:format short:f placeholder:TEMPLATE help:"Pretty print the list using a custom template or one of the predefined formats: name, full, json, jsonl."`
Header string `group:format help:"Arbitrary text printed at the start of the list."`
Footer string `group:format default:\n help:"Arbitrary text printed at the end of the list."`
Delimiter string "group:format short:d default:\n help:\"Print tags delimited by the given separator.\""
Delimiter0 bool "group:format short:0 name:delimiter0 help:\"Print tags 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 tags found."`
Sort []string `group:sort short:s placeholder:TERM help:"Order the tags by the given criterion."`
}
func (cmd *TagList) Run(container *cli.Container) error {
cmd.Header = strings.ExpandWhitespaceLiterals(cmd.Header)
cmd.Footer = strings.ExpandWhitespaceLiterals(cmd.Footer)
cmd.Delimiter = strings.ExpandWhitespaceLiterals(cmd.Delimiter)
if cmd.Delimiter0 {
if cmd.Delimiter != "\n" {
return errors.New("--delimiter and --delimiter0 can't be used together")
}
if cmd.Header != "" {
return errors.New("--footer and --delimiter0 can't be used together")
}
if cmd.Footer != "\n" {
return errors.New("--footer and --delimiter0 can't be used together")
}
cmd.Delimiter = "\x00"
cmd.Footer = "\x00"
}
if cmd.Format == "json" || cmd.Format == "jsonl" {
if cmd.Header != "" {
return errors.New("--header can't be used with JSON format")
}
if cmd.Footer != "\n" {
return errors.New("--footer can't be used with JSON format")
}
if cmd.Delimiter != "\n" {
return errors.New("--delimiter can't be used with JSON format")
}
switch cmd.Format {
case "json":
cmd.Delimiter = ","
cmd.Header = "["
cmd.Footer = "]\n"
case "jsonl":
// > The last character in the file may be a line separator, and it
// > will be treated the same as if there was no line separator
// > present.
// > https://jsonlines.org/
cmd.Footer = "\n"
}
}
notebook, err := container.CurrentNotebook()
if err != nil {
return err
}
format, err := notebook.NewCollectionFormatter(cmd.tagTemplate())
if err != nil {
return err
}
tags, err := notebook.FindCollections(core.CollectionKindTag)
if err != nil {
return err
}
count := len(tags)
if count > 0 {
err = container.Paginate(cmd.NoPager, func(out io.Writer) error {
if cmd.Header != "" {
fmt.Fprint(out, cmd.Header)
}
for i, tag := range tags {
if i > 0 {
fmt.Fprint(out, cmd.Delimiter)
}
ft, err := format(tag)
if err != nil {
return err
}
fmt.Fprint(out, ft)
}
if cmd.Footer != "" {
fmt.Fprint(out, cmd.Footer)
}
return nil
})
}
if err == nil && !cmd.Quiet {
fmt.Fprintf(os.Stderr, "\nFound %d %s\n", count, strings.Pluralize("tag", count))
}
return err
}
func (cmd *TagList) tagTemplate() string {
format := cmd.Format
if format == "" {
format = "full"
}
templ, ok := defaultTagFormats[format]
if !ok {
templ = strings.ExpandWhitespaceLiterals(format)
}
return templ
}
var defaultTagFormats = map[string]string{
"json": `{{json .}}`,
"jsonl": `{{json .}}`,
"name": `{{name}}`,
"full": `{{name}} ({{note-count}})`,
}

@ -0,0 +1,44 @@
package core
import (
"encoding/json"
)
// CollectionFormatter formats collections to be printed on the screen.
type CollectionFormatter func(collection Collection) (string, error)
func newCollectionFormatter(template Template) (CollectionFormatter, error) {
return func(collection Collection) (string, error) {
return template.Render(collectionFormatRenderContext{
ID: collection.ID,
Kind: collection.Kind,
Name: collection.Name,
NoteCount: collection.NoteCount,
})
}, nil
}
// collectionFormatRenderContext holds the variables available to the
// collection formatting templates.
type collectionFormatRenderContext struct {
// Unique ID of this collection in the Notebook.
ID CollectionID `json:"id"`
// Kind of this note collection, such as a tag.
Kind CollectionKind `json:"kind"`
// Name of this collection.
Name string `json:"name"`
// Number of notes associated with this collection.
NoteCount int `json:"noteCount" handlebars:"note-count"`
}
func (c collectionFormatRenderContext) Equal(other collectionFormatRenderContext) bool {
json1, err := json.Marshal(c)
if err != nil {
return false
}
json2, err := json.Marshal(other)
if err != nil {
return false
}
return string(json1) == string(json2)
}

@ -352,6 +352,20 @@ func (n *Notebook) NewNoteFormatter(templateString string) (NoteFormatter, error
return newNoteFormatter(n.Path, template, linkFormatter, n.osEnv(), n.fs)
}
// NewCollectionFormatter returns a CollectionFormatter used to format notes with the given template.
func (n *Notebook) NewCollectionFormatter(templateString string) (CollectionFormatter, error) {
templates, err := n.templateLoaderFactory(n.Config.Note.Lang)
if err != nil {
return nil, err
}
template, err := templates.LoadTemplate(templateString)
if err != nil {
return nil, err
}
return newCollectionFormatter(template)
}
// NewLinkFormatter returns a LinkFormatter used to generate internal links between notes.
func (n *Notebook) NewLinkFormatter() (LinkFormatter, error) {
templates, err := n.templateLoaderFactory(n.Config.Note.Lang)

@ -25,6 +25,7 @@ var root struct {
New cmd.New `cmd group:"notes" help:"Create a new note in the given notebook 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."`
Tag cmd.Tag `cmd group:"notes" help:"Manage the note tags."`
NotebookDir string `type:path placeholder:PATH help:"Turn off notebook auto-discovery and set manually the notebook where commands are run."`
WorkingDir string `short:W type:path placeholder:PATH help:"Run as if zk was started in <PATH> instead of the current working directory."`
@ -111,6 +112,7 @@ func options(container *cli.Container) []kong.Option {
"version": "zk " + strings.TrimPrefix(Version, "v"),
},
kong.Groups(map[string]string{
"cmd": "Commands:",
"filter": "Filtering",
"sort": "Sorting",
"format": "Formatting",

Loading…
Cancel
Save