camdencheek / fre

Command line frecency tracking
MIT License
122 stars 8 forks source link

fre for files : how? #25

Open jefftemplon opened 8 months ago

jefftemplon commented 8 months ago

Hi,

The intro in the docs says "for tracking your most-used directories and files" - everything else in the docs refers to directories. How to use it for files?

What I'd like to achieve is to replace fasd, so when I type less ,2022, I get a completion list of all files and directories I've recently accessed that have 2022 somewhere in the name or path, frecency sorted.

camdencheek commented 8 months ago

Hey @jefftemplon! fre just provides an API to store access events -- it does not mandate where those events come from. Directories are fairly straightforward because we can add shell hooks that log an access event with fre --add. Files are a little trickier because "opening a file" can mean a bunch of different things:

So, to get it to work with files, you'll need to define which events count as an access, then add a hook to call fre --add <file>. You might decide that you really only care about files you open with vim, so you add an autocommand that calls fre --add on the file. I don't personally use fre for files, so I don't have any complete examples

jefftemplon commented 8 months ago

Hi,

Thanks for this - it gives me enough information to get started.

JT

On 16 Jan 2024, at 15:53, Camden Cheek @.***> wrote:

Hey @jefftemplon https://github.com/jefftemplon! fre just provides an API to store access events -- it does not mandate where those events come from. Directories are fairly straightforward because we can add shell hooks that log an access event with fre --add. Files are a little trickier because "opening a file" can mean a bunch of different things:

Open in vim Open in emacs Inspect with cat Rename the file echo "test" >> file ...plus basically any other thing that interacts with a file So, to get it to work with files, you'll need to define which events count as an access, then add a hook to call fre --add . You might decide that you really only care about files you open with vim, so you add an autocommand that calls fre --add on the file. I don't personally use fre for files, so I don't have any complete examples

— Reply to this email directly, view it on GitHub https://github.com/camdencheek/fre/issues/25#issuecomment-1893907223, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAI5BONF6WJT5TLXZZZQZBTYO2H6TAVCNFSM6AAAAABB45MXUWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOJTHEYDOMRSGM. You are receiving this because you were mentioned.

jefftemplon commented 8 months ago

I've got something working! A related question: can fre be built statically, so that a binary I produce can run on other machines that don't have any rust libraries installed?

What works:

preexec () {
#   echo $1 $2
   local tmpcmd="$1"
   local cand_fname="${tmpcmd##* }"
#   echo "cand_fname $cand_fname"
   [ -f "$cand_fname" ] && fre --add "$cand_fname"
}

export FZF_CTRL_T_COMMAND='command cat <(fre --sorted) <(fd -t f -t d) <(fd -t d . ~)'
export FZF_CTRL_T_OPTS='--tiebreak=index'
jefftemplon commented 8 months ago

Hi, managed to deal with the fre binary. The only thing I have left is that the expansion for cand_fname doesn't work when the filename has embedded spaces. That's a shell thing, not a fre thing.

jefftemplon commented 8 months ago

I have something working - do you want me to contribute something to the docs about it?

camdencheek commented 8 months ago

Hey! Glad to hear you got it working. Adding something to the docs would be great -- thank you!

jefftemplon commented 7 months ago

This note documents how to use fre together with fzf to find files instead of directories.

The use case

I often access files located in /var or other locations outside of my own home-directory hierarchy. I used to use fasd for this, however this project is unsupported. Given the setup described below, files I access are passed to fre and are then available in the fzf listing when I do e.g. ctrl-t

See attached screenshot Screenshot 2024-02-09 at 15 14 00

Here I had typed less(note the trailing space) and then ctrl-t - that list of files is the result. I can scroll via arrow keys, or type more characters to limit the search results (see the fzf docs for this).

Here is an example zsh (via oh-my-zsh) configuration that enables this:

plugins=(zsh-autosuggestions fzf)

fre_preexec () {
   local tmpcmd="$3"
   local cand_fname=${(Q)${(z)tmpcmd}[-1]}
   [ -f "$cand_fname" ] && fre --add "$cand_fname"
}
add-zsh-hook preexec fre_preexec

export FZF_DEFAULT_COMMAND='fd --type f --strip-cwd-prefix'
export FZF_CTRL_T_COMMAND='command cat <(fre --sorted) <(fd -t f -t d) <(fd -t d . ~)'
export FZF_CTRL_T_OPTS='--tiebreak=index'

fre_preexec examines each executed command and checks whether the last argument of that command is a valid file name, if so it's added to the fre database. The fzf environment variables shown assume that fd is installed.

jefftemplon commented 7 months ago

Is this enough for you to put something under examples?

camdencheek commented 7 months ago

Yes! This is great -- I'll get this integrated. Thanks!

jefftemplon commented 7 months ago

I saw the other discussion about paths, and now I have fre --add "$(realpath $cand_fname)"