Skip to content

ipstone/fast-p

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fast-p

Quickly find and open a pdf among a collection of thousands of unsorted pdfs through fzf (fuzzy finder)

Installation on Unix or Linux based systems

  1. Requirements. Make sure the following requirements are satisfied:

    • install pdftotext. This comes with the texlive distribution on linux, On ubuntu, sudo apt-get install poppler-utils .
    • install fzf: https://github.com/junegunn/fzf
    • install GNU grep, ag (silver searcher).
  2. Install binary. Do either one of the two steps below:

    • Compile from source with go and go get. With a working golang installation, do go get github.com/bellecp/fast-p It will fetch the code and its dependencies, compile and create an executable fast-p in the /bin folder of your go installation, typically ~/go/bin. Make sure the command fast-p can be found (for instance, add ~/go/bin to your $PATH.)
    • Or: Use the precompiled binary for your architecture. Download the binary that corresponds to your architecture at https://github.com/bellecp/fast-p/releases and make sure that the command fast-p can be found. For instance, put the binary file fast-p in ~/custom/bin and add export PATH=~/custom/bin:$PATH to your .bashrc.
  3. Tweak your .bashrc. Add the following code to your .bashrc

p () {
    local open
    if [ "$(uname)" = "Darwin" ]; then
        open=open       # on OSX, "open" opens a pdf in preview
    else
        open=xdg-open   # this will open pdf file withthe default PDF viewer on KDE, xfce, LXDE and perhaps on other desktops.
    fi
    
    interactive_find() {
    # bash func to return the found pdf file name/path
        ag -U -g ".pdf$" \
        | fast-p \
        | fzf --read0 --reverse -e -d $'\t'  \
            --preview-window down:80% --preview '
                v=$(echo {q} | tr " " "|"); 
                echo -e {1}"\n"{2} | grep -E "^|$v" -i --color=always;
            ' \
        | gcut -z -f 1 -d $'\t' | tr -d '\n' | xargs -I F echo "F"
    }
    
    last_found_pdf=$(interactive_find) # store the last found pdf in the variable
    echo $last_found_pdf
    open "$last_found_pdf" # Double quote to deal with filepath with spaces"
}

  • You may replace ag -U -g ".pdf$" with another command that returns a list of pdf files.
  • You may replace open=... by your favorite PDF viewer, for instance open=evince or open=okular.

Installation on OSX with homebrew

  1. Install homebrew and run
brew install bellecp/fast-p/fast-pdf-finder

The above brew formula is experimental. Please report any issues/suggestions/feedback at bellecp#11

  1. Tweak your .bashrc. Add the following code to your .bashrc
p () {
    local open
    open=open   # on OSX, "open" opens a pdf in preview
    ag -U -g ".pdf$" \
    | fast-p \
    | fzf --read0 --reverse -e -d $'\t'  \
        --preview-window down:80% --preview '
            v=$(echo {q} | gtr " " "|"); 
            echo -e {1}"\n"{2} | ggrep -E "^|$v" -i --color=always;
        ' \
    | gcut -z -f 1 -d $'\t' | gtr -d '\n' | gxargs -r --null $open > /dev/null 2> /dev/null
}

  • You may replace ag -U -g ".pdf$" with another command that returns a list of pdf files.
  • You may replace open=... by your favorite PDF viewer, for instance open=evince or open=okular.

Remark: On OSX, we use the command line tools gcut, gxargs, ggrep, gtr which are the GNU versions of the tools cut, xargs, grep, tr. This way, we avoid the specifics of the versions of these tools pre-installed on OSX, and the same .bashrc code can be used for both OSX and GNU Linux systems.

Usage

Use the command p to browse among the PDF files in the current directory and its subdirectories.

The first run of the command will take some time to cache the text extracted from each pdf. Further runs of the command will be much faster since the text extraction will only apply to new pdfs.

How to clear the cache?

To clear the cache (which contains text extracted from PDF), you may safely remove the file ~/.cache/fast-p-pdftotext-output/fast-p_cached_pdftotext_output.db, or
~/.cache/fast-p_cached_pdftotext_output.db in older version.

See it in action

illustration of the p command

Is the historical bash code still available?

Yes, see https://github.com/bellecp/fast-p/blob/master/p but using the go binary as explained above is recommended for speed and interoperability.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%