Files
TerminalPolyjuice/nushell/modules/pj_completions.nu
T
gwbeip 9a0ad9384a feat(nushell): add cached nu_scripts completions bootstrap
- add pj completion command to clone/update nu_scripts cache

- load fixed custom-completions for git/ssh/cargo/uv/rustup from cache

- ignore nushell/cache contents in git while keeping placeholder .gitignore

- source pj_completions at startup and align prompt indicators/newline behavior
2026-05-30 20:29:46 +08:00

121 lines
4.1 KiB
Nu

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 = [
($completion_dir | path join 'git' 'git-completions.nu')
($completion_dir | path join 'ssh' 'ssh-completions.nu')
($completion_dir | path join 'cargo' 'cargo-completions.nu')
($completion_dir | path join 'uv' 'uv-completions.nu')
($completion_dir | path join 'rustup' 'rustup-completions.nu')
]
let missing = ($required | where {|f| not ($f | path exists) })
if not ($missing | is-empty) {
print "[pj completion] warning: some required completion files are missing:"
$missing | each {|f| print $" - ($f)" }
}
print "[pj completion] ok"
{
cache_dir: $cache_dir
repo_dir: $repo_dir
completion_dir: $completion_dir
missing_files: $missing
}
}
export-env {
$env.config = {
completions: {
quick: true
partial: true
algorithm: "fuzzy"
case_sensitive: false
external: {
enable: true
max_results: 100
}
}
}
}
const pj_module_dir = (path self | path dirname)
const git_completion = if (($pj_module_dir | path join '..' 'cache' 'nu_scripts' 'custom-completions' 'git' 'git-completions.nu') | path exists) {
($pj_module_dir | path join '..' 'cache' 'nu_scripts' 'custom-completions' 'git' 'git-completions.nu')
} else {
null
}
const ssh_completion = if (($pj_module_dir | path join '..' 'cache' 'nu_scripts' 'custom-completions' 'ssh' 'ssh-completions.nu') | path exists) {
($pj_module_dir | path join '..' 'cache' 'nu_scripts' 'custom-completions' 'ssh' 'ssh-completions.nu')
} else {
null
}
const cargo_completion = if (($pj_module_dir | path join '..' 'cache' 'nu_scripts' 'custom-completions' 'cargo' 'cargo-completions.nu') | path exists) {
($pj_module_dir | path join '..' 'cache' 'nu_scripts' 'custom-completions' 'cargo' 'cargo-completions.nu')
} else {
null
}
const uv_completion = if (($pj_module_dir | path join '..' 'cache' 'nu_scripts' 'custom-completions' 'uv' 'uv-completions.nu') | path exists) {
($pj_module_dir | path join '..' 'cache' 'nu_scripts' 'custom-completions' 'uv' 'uv-completions.nu')
} else {
null
}
const rustup_completion = if (($pj_module_dir | path join '..' 'cache' 'nu_scripts' 'custom-completions' 'rustup' 'rustup-completions.nu') | path exists) {
($pj_module_dir | path join '..' 'cache' 'nu_scripts' 'custom-completions' 'rustup' 'rustup-completions.nu')
} else {
null
}
use $git_completion *
use $ssh_completion *
use $cargo_completion *
use $uv_completion *
use $rustup_completion *