Proof of concept for filtering notes interactively with fzf

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

@ -0,0 +1,70 @@
package note
import (
"fmt"
"io"
"os"
"os/exec"
"github.com/mickael-menu/zk/adapter/tty"
"github.com/mickael-menu/zk/core/style"
"github.com/mickael-menu/zk/util/strings"
)
func WithMatchFilter(callback func(func(Match) error)) ([]string, error) {
styler := tty.NewStyler()
return withFilter(func(w io.Writer) {
callback(func(m Match) error {
fmt.Fprintf(w, "%v\x01 %v %v\n",
m.Path,
styler.MustStyle(m.Title, style.Rule("yellow")),
styler.MustStyle(strings.JoinLines(m.Body), style.Rule("black")),
)
return nil
})
})
}
func withFilter(callback func(w io.Writer)) ([]string, error) {
zkBin, err := os.Executable()
if err != nil {
return []string{}, err
}
cmd := exec.Command(
"fzf",
"--delimiter", "\x01",
"--tiebreak", "begin",
"--ansi",
"--exact",
"--height", "100%",
// FIXME: Use it to create a new note? Like notational velocity
// "--print-query",
// Make sure the path and titles are always visible
"--no-hscroll",
"--tabstop", "4",
// Don't highlight search terms
"--color", "hl:-1,hl+:-1",
// "--preview", `bat -p --theme Nord --color always {1}`,
"--preview", zkBin+" list -f {{raw-content}} {1}",
"--preview-window", "noborder:wrap",
)
cmd.Stderr = os.Stderr
w, err := cmd.StdinPipe()
if err != nil {
return []string{}, err
}
go func() {
callback(w)
w.Close()
}()
output, err := cmd.Output()
if err != nil {
return []string{}, err
}
return strings.SplitLines(string(output)), nil
}

@ -130,8 +130,8 @@ type matchRenderContext struct {
Title string
Lead string
Body string
RawContent string
WordCount int
RawContent string `handlebars:"raw-content"`
WordCount int `handlebars:"word-count"`
Snippet string
Created time.Time
Modified time.Time

Loading…
Cancel
Save