-
Notifications
You must be signed in to change notification settings - Fork 124
/
list.go
182 lines (149 loc) · 4.71 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package cmd
import (
"fmt"
"io"
"os"
"github.com/mickael-menu/zk/internal/adapter/fzf"
"github.com/mickael-menu/zk/internal/cli"
"github.com/mickael-menu/zk/internal/util/errors"
"github.com/mickael-menu/zk/internal/util/strings"
)
// 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 a custom template or one of the predefined formats: oneline, short, medium, long, 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 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."`
cli.Filtering
}
func (cmd *List) 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.NewNoteFormatter(cmd.noteTemplate())
if err != nil {
return err
}
findOpts, err := cmd.Filtering.NewNoteFindOpts(notebook)
if err != nil {
return errors.Wrapf(err, "incorrect criteria")
}
notes, err := notebook.FindNotes(findOpts)
if err != nil {
return err
}
filter := container.NewNoteFilter(fzf.NoteFilterOpts{
Interactive: cmd.Interactive,
AlwaysFilter: false,
NotebookDir: notebook.Path,
})
notes, err = filter.Apply(notes)
if err != nil {
if err == fzf.ErrCancelled {
return nil
}
return err
}
count := len(notes)
if count > 0 {
err = container.Paginate(cmd.NoPager, func(out io.Writer) error {
if cmd.Header != "" {
fmt.Fprint(out, cmd.Header)
}
for i, note := range notes {
if i > 0 {
fmt.Fprint(out, cmd.Delimiter)
}
ft, err := format(note)
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("note", count))
}
return err
}
func (cmd *List) noteTemplate() string {
format := cmd.Format
if format == "" {
format = "short"
}
templ, ok := defaultNoteFormats[format]
if !ok {
templ = strings.ExpandWhitespaceLiterals(format)
}
return templ
}
var defaultNoteFormats = map[string]string{
"json": `{{json .}}`,
"jsonl": `{{json .}}`,
"path": `{{path}}`,
"link": `{{link}}`,
"oneline": `{{style "title" title}} {{style "path" path}} ({{format-date created "elapsed"}})`,
"short": `{{style "title" title}} {{style "path" path}} ({{format-date created "elapsed"}})
{{list snippets}}`,
"medium": `{{style "title" title}} {{style "path" path}}
Created: {{format-date created "short"}}
{{list snippets}}`,
"long": `{{style "title" title}} {{style "path" path}}
Created: {{format-date created "short"}}
Modified: {{format-date modified "short"}}
{{list snippets}}`,
"full": `{{style "title" title}} {{style "path" path}}
Created: {{format-date created "short"}}
Modified: {{format-date modified "short"}}
Tags: {{join tags ", "}}
{{prepend " " body}}
`,
}