Files
TerminalPolyjuice/nushell/modules/pj_completions.nu
T
gwbeip 10d133444d feat(nushell): re-export completions for use-based startup
- switch completion module imports to export use so externs propagate

- extend managed completion command set and required file checks

- rename prompt static cache env var to PJ_LEFT_PROMPT_STATIC_PART

- load completion module via use in nu_polyjuice
2026-05-30 21:38:36 +08:00

112 lines
4.0 KiB
Nu

const pj_module_dir = (path self | path dirname)
const pj_completion_base_dir = ($pj_module_dir | path join '..' 'cache' 'nu_scripts' 'custom-completions')
const pj_completion_commands = [git ssh cargo uv rustup make pytest rg tar fzf vscode winget zig]
def _pj_completion_script_path [name: string] {
($pj_completion_base_dir | path join $name $"($name)-completions.nu")
}
def _pj_required_completion_files [] {
$pj_completion_commands | each {|name|
{
command: $name
file: (_pj_completion_script_path $name)
}
}
}
export def "pj completion" [
--update(-u) # Pull latest changes when repo already exists.
--force(-f) # Re-clone even when repo already exists.
] {
let repo_url = "https://github.com/nushell/nu_scripts.git"
let cache_dir = if ('NU_POLYJUICE_PATH' in $env) {
($env.NU_POLYJUICE_PATH | path join 'cache')
} else {
(pwd | path join 'nushell' 'cache')
}
let repo_dir = ($cache_dir | path join 'nu_scripts')
let completion_dir = ($repo_dir | path join 'custom-completions')
if (which git | is-empty) {
error make {
msg: "`git` command is required for pj completion"
help: "Install Git first, then run `pj completion` again."
}
}
mkdir $cache_dir
if $force {
if ($repo_dir | path exists) {
rm -rf $repo_dir
}
}
if not ($repo_dir | path exists) {
print "[pj completion] nu_scripts cache not found, cloning..."
^git clone $repo_url $repo_dir
} else {
if not (($repo_dir | path join '.git') | path exists) {
print "[pj completion] existing cache is not a git repo, re-cloning..."
rm -rf $repo_dir
^git clone $repo_url $repo_dir
} else if $update {
print "[pj completion] cache exists, updating..."
^git -C $repo_dir pull --ff-only
}
}
if not ($completion_dir | path exists) {
error make {
msg: "nu_scripts cloned, but custom-completions directory is missing"
help: $"Check repository content under ($repo_dir)."
}
}
let required = (_pj_required_completion_files)
let missing = ($required | where {|it| not ($it.file | path exists) })
if not ($missing | is-empty) {
print "[pj completion] warning: some required completion files are missing:"
$missing | each {|it| print $" - ($it.command): ($it.file)" }
}
print "[pj completion] ok"
{
cache_dir: $cache_dir
repo_dir: $repo_dir
completion_dir: $completion_dir
commands: $pj_completion_commands
missing_files: $missing
}
}
export-env {
$env.config = {
completions: {
quick: true
partial: true
algorithm: "fuzzy"
case_sensitive: false
external: {
enable: true
max_results: 100
}
}
}
}
# Re-export imported extern signatures so they are visible when this module is `use`d.
export use ($pj_completion_base_dir | path join 'git' 'git-completions.nu') *
export use ($pj_completion_base_dir | path join 'ssh' 'ssh-completions.nu') *
export use ($pj_completion_base_dir | path join 'cargo' 'cargo-completions.nu') *
export use ($pj_completion_base_dir | path join 'uv' 'uv-completions.nu') *
export use ($pj_completion_base_dir | path join 'rustup' 'rustup-completions.nu') *
export use ($pj_completion_base_dir | path join 'make' 'make-completions.nu') *
export use ($pj_completion_base_dir | path join 'pytest' 'pytest-completions.nu') *
export use ($pj_completion_base_dir | path join 'rg' 'rg-completions.nu') *
export use ($pj_completion_base_dir | path join 'tar' 'tar-completions.nu') *
export use ($pj_completion_base_dir | path join 'vscode' 'vscode-completions.nu') *
export use ($pj_completion_base_dir | path join 'winget' 'winget-completions.nu') *
export use ($pj_completion_base_dir | path join 'zig' 'zig-completions.nu') *