From f70b85ed6fd11351c27fce710f31c229a1a585aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mickae=CC=88l=20Menu?= Date: Wed, 10 Feb 2021 21:30:03 +0100 Subject: [PATCH] Show path relative to working directory in fzf --- adapter/fzf/finder.go | 28 +++++++++++++++++++++++----- cmd/edit.go | 8 ++++++++ cmd/list.go | 2 ++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/adapter/fzf/finder.go b/adapter/fzf/finder.go index 25e35c1..19a2048 100644 --- a/adapter/fzf/finder.go +++ b/adapter/fzf/finder.go @@ -3,6 +3,7 @@ package fzf import ( "fmt" "os" + "path/filepath" "github.com/mickael-menu/zk/adapter/term" "github.com/mickael-menu/zk/core/note" @@ -19,12 +20,21 @@ type NoteFinder struct { terminal *term.Terminal } +// NoteFinderOpts holds the configuration for the fzf notes finder. +// +// The absolute path to the slip box (BasePath) and the working directory +// (CurrentPath) are used to make the path of each note relative to the working +// directory. type NoteFinderOpts struct { // Indicates whether fzf is opened for every query, even if empty. AlwaysFilter bool // When non nil, a "create new note from query" binding will be added to // fzf to create a note in this directory. NewNoteDir *zk.Dir + // Absolute path to the slip box. + BasePath string + // Path to the working directory. + CurrentPath string } func NewNoteFinder(opts NoteFinderOpts, finder note.Finder, terminal *term.Terminal) *NoteFinder { @@ -37,13 +47,21 @@ func NewNoteFinder(opts NoteFinderOpts, finder note.Finder, terminal *term.Termi func (f *NoteFinder) Find(opts note.FinderOpts) ([]note.Match, error) { isInteractive, opts := popInteractiveFilter(opts) + selectedMatches := make([]note.Match, 0) matches, err := f.finder.Find(opts) + relPaths := []string{} if !isInteractive || !f.terminal.IsInteractive() || err != nil || (!f.opts.AlwaysFilter && len(matches) == 0) { return matches, err } - selectedMatches := make([]note.Match, 0) + for _, match := range matches { + path, err := filepath.Rel(f.opts.CurrentPath, filepath.Join(f.opts.BasePath, match.Path)) + if err != nil { + return selectedMatches, err + } + relPaths = append(relPaths, path) + } zkBin, err := os.Executable() if err != nil { @@ -75,9 +93,9 @@ func (f *NoteFinder) Find(opts note.FinderOpts) ([]note.Match, error) { return selectedMatches, err } - for _, match := range matches { + for i, match := range matches { fzf.Add([]string{ - match.Path, + relPaths[i], f.terminal.MustStyle(match.Title, style.RuleYellow), f.terminal.MustStyle(stringsutil.JoinLines(match.Body), style.RuleBlack), }) @@ -90,8 +108,8 @@ func (f *NoteFinder) Find(opts note.FinderOpts) ([]note.Match, error) { for _, s := range selection { path := s[0] - for _, m := range matches { - if m.Path == path { + for i, m := range matches { + if relPaths[i] == path { selectedMatches = append(selectedMatches, m) } } diff --git a/cmd/edit.go b/cmd/edit.go index c29c207..26ea9e5 100644 --- a/cmd/edit.go +++ b/cmd/edit.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "os" "path/filepath" "github.com/mickael-menu/zk/adapter/fzf" @@ -25,6 +26,11 @@ func (cmd *Edit) Run(container *Container) error { return err } + wd, err := os.Getwd() + if err != nil { + return err + } + opts, err := NewFinderOpts(zk, cmd.Filtering, cmd.Sorting) if err != nil { return errors.Wrapf(err, "incorrect criteria") @@ -40,6 +46,8 @@ func (cmd *Edit) Run(container *Container) error { finder := container.NoteFinder(tx, fzf.NoteFinderOpts{ AlwaysFilter: true, NewNoteDir: cmd.newNoteDir(zk), + BasePath: zk.Path, + CurrentPath: wd, }) notes, err = finder.Find(*opts) return err diff --git a/cmd/list.go b/cmd/list.go index b07938f..0aff63c 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -61,6 +61,8 @@ func (cmd *List) Run(container *Container) error { err = db.WithTransaction(func(tx sqlite.Transaction) error { finder := container.NoteFinder(tx, fzf.NoteFinderOpts{ AlwaysFilter: false, + BasePath: zk.Path, + CurrentPath: wd, }) notes, err = finder.Find(*opts) return err