mirror of
https://github.com/mickael-menu/zk
synced 2024-11-15 12:12:56 +00:00
Show path relative to working directory in fzf
This commit is contained in:
parent
6320a627d2
commit
f70b85ed6f
@ -3,6 +3,7 @@ package fzf
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/mickael-menu/zk/adapter/term"
|
"github.com/mickael-menu/zk/adapter/term"
|
||||||
"github.com/mickael-menu/zk/core/note"
|
"github.com/mickael-menu/zk/core/note"
|
||||||
@ -19,12 +20,21 @@ type NoteFinder struct {
|
|||||||
terminal *term.Terminal
|
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 {
|
type NoteFinderOpts struct {
|
||||||
// Indicates whether fzf is opened for every query, even if empty.
|
// Indicates whether fzf is opened for every query, even if empty.
|
||||||
AlwaysFilter bool
|
AlwaysFilter bool
|
||||||
// When non nil, a "create new note from query" binding will be added to
|
// When non nil, a "create new note from query" binding will be added to
|
||||||
// fzf to create a note in this directory.
|
// fzf to create a note in this directory.
|
||||||
NewNoteDir *zk.Dir
|
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 {
|
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) {
|
func (f *NoteFinder) Find(opts note.FinderOpts) ([]note.Match, error) {
|
||||||
isInteractive, opts := popInteractiveFilter(opts)
|
isInteractive, opts := popInteractiveFilter(opts)
|
||||||
|
selectedMatches := make([]note.Match, 0)
|
||||||
matches, err := f.finder.Find(opts)
|
matches, err := f.finder.Find(opts)
|
||||||
|
relPaths := []string{}
|
||||||
|
|
||||||
if !isInteractive || !f.terminal.IsInteractive() || err != nil || (!f.opts.AlwaysFilter && len(matches) == 0) {
|
if !isInteractive || !f.terminal.IsInteractive() || err != nil || (!f.opts.AlwaysFilter && len(matches) == 0) {
|
||||||
return matches, err
|
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()
|
zkBin, err := os.Executable()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -75,9 +93,9 @@ func (f *NoteFinder) Find(opts note.FinderOpts) ([]note.Match, error) {
|
|||||||
return selectedMatches, err
|
return selectedMatches, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, match := range matches {
|
for i, match := range matches {
|
||||||
fzf.Add([]string{
|
fzf.Add([]string{
|
||||||
match.Path,
|
relPaths[i],
|
||||||
f.terminal.MustStyle(match.Title, style.RuleYellow),
|
f.terminal.MustStyle(match.Title, style.RuleYellow),
|
||||||
f.terminal.MustStyle(stringsutil.JoinLines(match.Body), style.RuleBlack),
|
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 {
|
for _, s := range selection {
|
||||||
path := s[0]
|
path := s[0]
|
||||||
for _, m := range matches {
|
for i, m := range matches {
|
||||||
if m.Path == path {
|
if relPaths[i] == path {
|
||||||
selectedMatches = append(selectedMatches, m)
|
selectedMatches = append(selectedMatches, m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/mickael-menu/zk/adapter/fzf"
|
"github.com/mickael-menu/zk/adapter/fzf"
|
||||||
@ -25,6 +26,11 @@ func (cmd *Edit) Run(container *Container) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
opts, err := NewFinderOpts(zk, cmd.Filtering, cmd.Sorting)
|
opts, err := NewFinderOpts(zk, cmd.Filtering, cmd.Sorting)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "incorrect criteria")
|
return errors.Wrapf(err, "incorrect criteria")
|
||||||
@ -40,6 +46,8 @@ func (cmd *Edit) Run(container *Container) error {
|
|||||||
finder := container.NoteFinder(tx, fzf.NoteFinderOpts{
|
finder := container.NoteFinder(tx, fzf.NoteFinderOpts{
|
||||||
AlwaysFilter: true,
|
AlwaysFilter: true,
|
||||||
NewNoteDir: cmd.newNoteDir(zk),
|
NewNoteDir: cmd.newNoteDir(zk),
|
||||||
|
BasePath: zk.Path,
|
||||||
|
CurrentPath: wd,
|
||||||
})
|
})
|
||||||
notes, err = finder.Find(*opts)
|
notes, err = finder.Find(*opts)
|
||||||
return err
|
return err
|
||||||
|
@ -61,6 +61,8 @@ func (cmd *List) Run(container *Container) error {
|
|||||||
err = db.WithTransaction(func(tx sqlite.Transaction) error {
|
err = db.WithTransaction(func(tx sqlite.Transaction) error {
|
||||||
finder := container.NoteFinder(tx, fzf.NoteFinderOpts{
|
finder := container.NoteFinder(tx, fzf.NoteFinderOpts{
|
||||||
AlwaysFilter: false,
|
AlwaysFilter: false,
|
||||||
|
BasePath: zk.Path,
|
||||||
|
CurrentPath: wd,
|
||||||
})
|
})
|
||||||
notes, err = finder.Find(*opts)
|
notes, err = finder.Find(*opts)
|
||||||
return err
|
return err
|
||||||
|
Loading…
Reference in New Issue
Block a user