Skip to content
Myr

completion-examples.nu

This file

Path

shell/completion-examples.nu

91 lines · 2.9 KiB

Last changed by

shell: nushell integration scripts (#4630)Simon Désaulniersbefore adoption290b18d

History of this file

Take it

1#     ____      ____2#    / __/___  / __/3#   / /_/_  / / /_4#  / __/ / /_/ __/5# /_/   /___/_/ completion-examples.nu6#7# Example custom completers for fzf's Nushell integration.8#9# To use these, add the desired entries to $env.FZF_COMPLETERS in your10# 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), or16#   - a record with the following optional fields:17#       candidates: list<string>          # candidates to feed to fzf18#       opts:       list<string>          # custom fzf options (default: ["-m"])19#       post:       closure (|sel| ...)   # post-processing of the selected item20#21# Simple example:22#   $env.FZF_COMPLETERS = {23#       git: {|prefix, spans| ["branch-main", "branch-dev", "branch-feature"]}24#   }2526# --- 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 repos30#   -Q (query), -R (remove): list installed packages31# Returns a record with custom fzf options for package preview.3233$env.FZF_COMPLETERS = {}3435$env.FZF_COMPLETERS.pacman = {|prefix, spans|36    let sub = $spans | skip 1 | first37    let candidates = (if ($sub =~ "-[SF]") {38        ^pacman -Slq | lines39    } else if ($sub =~ "-[QR]") {40        ^pacman -Qq | lines41    } else {42        []43    })44    {45        candidates: $candidates46        opts: ["-m", "--preview", "pacman -Si {}", "--prompt", "Package > "]47    }48}4950$env.FZF_COMPLETERS.paru = {|prefix, spans|51    let sub = $spans | skip 1 | first52    let candidates = (if ($sub =~ "-[SF]") {53        ^pacman -Slq | lines54    } else if ($sub =~ "-[QR]") {55        ^pacman -Qq | lines56    } else {57        []58    })59    {60        candidates: $candidates61        opts: ["-m", "--preview", "pacman -Si {}", "--prompt", "Package > "]62    }63}6465# --- pass (password-store) ---66# Completes entry names from ~/.password-store.67# Returns a simple list (no custom fzf options needed).6869$env.FZF_COMPLETERS.pass = {|prefix, spans|70    try {71        ls ~/.password-store/**/*.gpg72        | get name73        | each {$in | str replace -r '^.*?\.password-store/(.*).gpg' '${1}'}74    } catch {75        []76    }77}7879# --- 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 than82# what you want inserted on the command line (e.g. extracting a PID from83# 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# }