diff --git a/.gitignore b/.gitignore index 2c1ed47..aa96700 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .zed .vscode +nushell/cache/* +!nushell/cache/.gitignore diff --git a/nushell/cache/.gitignore b/nushell/cache/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/nushell/cache/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/nushell/modules/pj_completions.nu b/nushell/modules/pj_completions.nu index cec35c0..b25f429 100644 --- a/nushell/modules/pj_completions.nu +++ b/nushell/modules/pj_completions.nu @@ -1,3 +1,75 @@ +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: { @@ -12,3 +84,37 @@ export-env { } } } + +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 * diff --git a/nushell/modules/pj_prompt.nu b/nushell/modules/pj_prompt.nu index fa56685..f483193 100644 --- a/nushell/modules/pj_prompt.nu +++ b/nushell/modules/pj_prompt.nu @@ -605,7 +605,8 @@ def _pj_left_prompt [] { for m in $lang_parts { $out = $"($out)($m)" } - $out = $"\n($out)($time_part)($duration_part)\n" + let prompt_newline = if (($nu.os-info.name | str downcase) == "windows") { "\n\r" } else { "\n" } + $out = $"($prompt_newline)($out)($time_part)($duration_part)($prompt_newline)" $out } @@ -626,8 +627,12 @@ export-env { PROMPT_INDICATOR: {|| _pj_prompt_indicator } - PROMPT_INDICATOR_VI_INSERT: "" - PROMPT_INDICATOR_VI_NORMAL: "" + PROMPT_INDICATOR_VI_INSERT: {|| + _pj_prompt_indicator + } + PROMPT_INDICATOR_VI_NORMAL: {|| + _pj_prompt_indicator + } PROMPT_COMMAND: {|| _pj_left_prompt } diff --git a/nushell/nu_polyjuice.nu b/nushell/nu_polyjuice.nu index 8831f76..74f3f3d 100644 --- a/nushell/nu_polyjuice.nu +++ b/nushell/nu_polyjuice.nu @@ -6,7 +6,7 @@ $env.NU_POLYJUICE_PATH = $env.FILE_PWD # modules use modules/pj_aliases.nu * -use modules/pj_completions.nu * +source modules/pj_completions.nu use modules/pj_helix.nu * use modules/pj_fzf.nu * use modules/pj_zoxide.nu *