Files
TerminalPolyjuice/nushell/modules/pj_completions.nu
T
gwbeip deb4e45c4e fix(nushell): improve robustness and clarify abstraction boundaries
- pj_completions: merge into $env.config instead of replacing it wholesale
- pj_completions: use parse-time const for cache dir instead of pwd fallback
- pj_fzf / pj_historyhintcomplete: null-safe keybindings access via ? and default
- pj_helix: replace $env.OS (Windows-only) with $nu.os-info.name; extract shared launcher
- pj_prompt: fix off-by-one in git OID substring (0..7 -> 0..<7)
- pj_prompt: use $nu.os-info.name for OS detection instead of sys host hostname
- pj_prompt: cache git availability in static base, skip per-render which call
- pj_prompt: bind LAST_EXIT_CODE once in right-prompt closure
- pj_brew / pj_uv: add --ignore-errors to hide-env calls
- pj_aliases: convert quote/dadjoke to defs with try/catch error handling
- pj_deepseek: guard curl availability before invoking
- pj_zoxide: add ? to $nu.os-info.name access
- pj_starship: fix null-to-nuon producing "null" string; broaden detection
- nu_polyjuice: load pj_starship module
2026-07-31 00:31:04 +08:00

196 lines
6.3 KiB
Nu

const pj_module_dir = (path self | path dirname)
const pj_cache_dir = ($pj_module_dir | path join '..' 'cache')
const pj_completion_base_dir = ($pj_cache_dir | path join 'nu_scripts' 'custom-completions')
const pj_completion_commands = [git ssh cargo uv rustup make pytest rg tar vscode winget zig]
const pj_completion_noop_module = ($pj_cache_dir | path join 'noop.nu')
const pj_completion_registry_module = ($pj_cache_dir | path join 'pj_completions_registry.nu')
const pj_completion_bootstrap_module = if ($pj_completion_registry_module | path exists) {
$pj_completion_registry_module
} else {
$pj_completion_noop_module
}
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)
}
}
}
def _pj_completion_registry_lines [] {
let header = [
"# Auto-generated by `pj completion sync`."
"# Edit pj_completion_commands in pj_completions.nu, then re-run sync."
"const pj_registry_dir = (path self | path dirname)"
"const pj_completion_base_dir = ($pj_registry_dir | path join 'nu_scripts' 'custom-completions')"
"const pj_completion_noop_module = ($pj_registry_dir | path join 'noop.nu')"
""
]
let lines = ($pj_completion_commands | each {|name|
[
(
"export use (if ((($pj_completion_base_dir | path join '"
+ $name
+ "' '"
+ $name
+ "-completions.nu') | path exists)) { ($pj_completion_base_dir | path join '"
+ $name
+ "' '"
+ $name
+ "-completions.nu') } else { $pj_completion_noop_module }) *"
)
""
]
} | flatten)
($header | append $lines)
}
def _pj_completion_help [] {
print "pj completion commands:"
print " pj completion Check required completion files from existing cache."
print " pj completion -u Update cache, or clone nu_scripts when cache is missing."
print " pj completion -f Force re-clone cached nu_scripts repository."
print " pj completion sync Regenerate cache registry and restart Nushell."
print " pj completion -s Same as `pj completion sync`."
print " pj completion -h Show this help message."
}
export def "pj completion sync" [
--help(-h)
] {
if $help {
_pj_completion_help
return
}
mkdir $pj_cache_dir
let content = ((_pj_completion_registry_lines) | str join "\n")
$content | save -f $pj_completion_registry_module
print $"[pj completion] synced: ($pj_completion_registry_module)"
print "[pj completion] restarting Nushell..."
exec nu
}
export def "pj completion" [
--update(-u) # Update cached repo, or clone when cache is missing.
--force(-f) # Force re-clone cached nu_scripts repository.
--sync(-s) # Regenerate static registry and restart Nushell.
--help(-h) # Show help.
] {
if $help {
_pj_completion_help
return
}
if $sync {
pj completion sync
return
}
let repo_url = "https://github.com/nushell/nu_scripts.git"
let cache_dir = $pj_cache_dir
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) {
if ($update or $force) {
print "[pj completion] nu_scripts cache not found, cloning..."
^git clone $repo_url $repo_dir
} else {
print "[pj completion] nu_scripts cache not found. run `pj completion -u` to clone it."
return {
cache_dir: $cache_dir
repo_dir: $repo_dir
completion_dir: $completion_dir
commands: $pj_completion_commands
missing_files: []
}
}
} 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 = ($env.config? | default {} | merge {
completions: {
quick: true
partial: true
algorithm: "fuzzy"
case_sensitive: false
external: {
enable: true
max_results: 100
}
}
})
if not ($pj_completion_registry_module | path exists) {
mkdir $pj_cache_dir
let content = ((_pj_completion_registry_lines) | str join "\n")
$content | save -f $pj_completion_registry_module
if (($env.PJ_COMPLETION_BOOTSTRAPPED? | default "0") != "1") {
load-env { PJ_COMPLETION_BOOTSTRAPPED: "1" }
print "[pj completion] generated cache registry, restarting Nushell..."
exec nu
}
}
}
# Re-export completion externs from generated static registry.
export use $pj_completion_bootstrap_module *