| 1 |
# ____ ____ |
| 2 |
# / __/___ / __/ |
| 3 |
# / /_/_ / / /_ |
| 4 |
# / __/ / /_/ __/ |
| 5 |
# /_/ /___/_/ completion-examples.nu |
| 6 |
# |
| 7 |
# Example custom completers for fzf's Nushell integration. |
| 8 |
# |
| 9 |
# To use these, add the desired entries to $env.FZF_COMPLETERS in your |
| 10 |
# config.nu. Each closure receives two arguments: |
| 11 |
# - prefix: the text before the trigger (e.g. "vim" in "vim **<TAB>") |
| 12 |
# - spans: the full command as a list of words (e.g. ["pacman", "-S", "vim**"]) |
| 13 |
# |
| 14 |
# A closure can return either: |
| 15 |
# - a list of candidate strings (fzf will use default options), or |
| 16 |
# - a record with the following optional fields: |
| 17 |
# candidates: list<string> # candidates to feed to fzf |
| 18 |
# opts: list<string> # custom fzf options (default: ["-m"]) |
| 19 |
# post: closure (|sel| ...) # post-processing of the selected item |
| 20 |
# |
| 21 |
# Simple example: |
| 22 |
# $env.FZF_COMPLETERS = { |
| 23 |
# git: {|prefix, spans| ["branch-main", "branch-dev", "branch-feature"]} |
| 24 |
# } |
| 25 |
|
| 26 |
# --- pacman / paru --- |
| 27 |
# Completes package names for pacman and paru. |
| 28 |
# Uses the spans to distinguish between subcommands: |
| 29 |
# -S (sync), -F (files): list available packages from repos |
| 30 |
# -Q (query), -R (remove): list installed packages |
| 31 |
# Returns a record with custom fzf options for package preview. |
| 32 |
|
| 33 |
$env.FZF_COMPLETERS = {} |
| 34 |
|
| 35 |
$env.FZF_COMPLETERS.pacman = {|prefix, spans| |
| 36 |
let sub = $spans | skip 1 | first |
| 37 |
let candidates = (if ($sub =~ "-[SF]") { |
| 38 |
^pacman -Slq | lines |
| 39 |
} else if ($sub =~ "-[QR]") { |
| 40 |
^pacman -Qq | lines |
| 41 |
} else { |
| 42 |
[] |
| 43 |
}) |
| 44 |
{ |
| 45 |
candidates: $candidates |
| 46 |
opts: ["-m", "--preview", "pacman -Si {}", "--prompt", "Package > "] |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
$env.FZF_COMPLETERS.paru = {|prefix, spans| |
| 51 |
let sub = $spans | skip 1 | first |
| 52 |
let candidates = (if ($sub =~ "-[SF]") { |
| 53 |
^pacman -Slq | lines |
| 54 |
} else if ($sub =~ "-[QR]") { |
| 55 |
^pacman -Qq | lines |
| 56 |
} else { |
| 57 |
[] |
| 58 |
}) |
| 59 |
{ |
| 60 |
candidates: $candidates |
| 61 |
opts: ["-m", "--preview", "pacman -Si {}", "--prompt", "Package > "] |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
# --- pass (password-store) --- |
| 66 |
# Completes entry names from ~/.password-store. |
| 67 |
# Returns a simple list (no custom fzf options needed). |
| 68 |
|
| 69 |
$env.FZF_COMPLETERS.pass = {|prefix, spans| |
| 70 |
try { |
| 71 |
ls ~/.password-store/**/*.gpg |
| 72 |
| get name |
| 73 |
| each {$in | str replace -r '^.*?\.password-store/(.*).gpg' '${1}'} |
| 74 |
} catch { |
| 75 |
[] |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
# --- Example with post-processing hook --- |
| 80 |
# The "post" closure transforms the selected line after fzf returns. |
| 81 |
# This is useful when the displayed line contains more information than |
| 82 |
# what you want inserted on the command line (e.g. extracting a PID from |
| 83 |
# a full "ps" output line). |
| 84 |
# |
| 85 |
# $env.FZF_COMPLETERS.mycommand = {|prefix, spans| |
| 86 |
# { |
| 87 |
# candidates: (^some-command | lines) |
| 88 |
# opts: ["+m", "--header-lines=1"] |
| 89 |
# post: {|selection| $selection | split row ' ' | get 0} |
| 90 |
# } |
| 91 |
# } |