diff --git a/plugins/pdf/README.md b/plugins/pdf/README.md new file mode 100644 index 000000000..0d8921456 --- /dev/null +++ b/plugins/pdf/README.md @@ -0,0 +1,49 @@ +# PDF Plugin for Oh My Zsh + +This plugin provides a convenient way to manage and open PDF files from your terminal using fuzzy finding. + +## Features + +- Automatically detects available PDF readers (okular, evince, xpdf, firefox, and some others) +- Creates and maintains a list of PDF files in your home directory +- Uses `fzf` for fuzzy finding and selecting PDF files +- Fallback to `find` command if `fd` is not available + + +## Requirements + +- Zsh with Oh My Zsh framework +- Any suitable PDF reader (e.g., okular, evince, xpdf, firefox, zathura, mupdf, qpdfview, atril) +- `fzf` for fuzzy finding (recommended) +- `fd` command (optional, improves file search performance) + + + +1. Activate the plugin in `~/.zshrc`: + + ```zsh + plugins=(... pdf) + ``` + +2. Restart your shell or run: + + ```zsh + source ~/.zshrc + ``` + +## Usage +Example usage: + +```zsh +# Open fzf to select a PDF +pdf + +# Open a specific PDF file +pdf ~/Documents/example.pdf +``` +## Configuration + +The plugin will automatically create a directory `~/.config/pdfiledoc/` and a file `pdfs.txt` to store the list of PDF files. This list is updated when: + +- The `pdfs.txt` file doesn't exist +- No PDF is selected from the existing list diff --git a/plugins/pdf/pdf.plugin.zsh b/plugins/pdf/pdf.plugin.zsh new file mode 100644 index 000000000..ad4a88f15 --- /dev/null +++ b/plugins/pdf/pdf.plugin.zsh @@ -0,0 +1,48 @@ +#set -xe +# pdf.plugin.zsh + +PDF_READER="" + +_check_pdf_reader() { + for reader in okular evince xpdf firefox zathura atril mupdf xreader deepin-reader gv papers; do + if command -v $reader >/dev/null 2>&1; then + PDF_READER=$reader + return + fi + done + echo "No suitable PDF reader found. Please install okular, evince, xpdf, or firefox." + return 1 +} + +_pdfdirectory() { + if [[ ! -f ~/.config/pdfiledoc/pdfs.txt ]]; then + mkdir -p ~/.config/pdfiledoc + if command -v fd &> /dev/null; then + fd -e pdf . "$HOME" > ~/.config/pdfiledoc/pdfs.txt + else + find "$HOME" -type f -name '*.pdf' > ~/.config/pdfiledoc/pdfs.txt + fi + fi +} + +pdf() { + _check_pdf_reader || return 1 + _pdfdirectory + + local var1=$(cat ~/.config/pdfiledoc/pdfs.txt | fzf -i) + echo "$var1" + + if [[ -n $var1 ]]; then + $PDF_READER "$var1" & + elif ! command -v fd &> /dev/null; then + local newinput=$(find "$HOME" -type f -name '*.pdf') + echo "$newinput" > ~/.config/pdfiledoc/pdfs.txt + else + local newinput=$(fd -e pdf . "$HOME") + echo "$newinput" > ~/.config/pdfiledoc/pdfs.txt + fi + + if [[ $# -gt 0 && -f $1 ]]; then + $PDF_READER "$1" & + fi +}