Files
TerminalPolyjuice/nushell/modules/pj_completions.nu
T
gwbeip 804b81a0e5 feat(nushell): gate cache clone behind update flag
- only clone missing nu_scripts cache when using -u/-f

- keep default pj completion as non-cloning check mode

- align completion flag help text with new behavior
2026-05-30 23:19:41 +08:00

200 lines
6.4 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 = 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) {
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 = {
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 *