perf(nushell): cache static prompt context

- precompute os symbol, user/host, ssh flag, shell label and newline sequence at startup

- reuse cached values in _pj_left_prompt to reduce per-render overhead
This commit is contained in:
2026-05-30 20:32:42 +08:00
parent 9a0ad9384a
commit 4aaf4199bd
+24 -11
View File
@@ -548,9 +548,7 @@ def _pj_lang_segments [] {
$out $out
} }
def _pj_left_prompt [] { def _pj_prompt_static_base [] {
let p = ($env.PJ_PROMPT_PALETTE? | default (_pj_palette_default))
let user = (try { whoami } catch { "" } | into string | str trim) let user = (try { whoami } catch { "" } | into string | str trim)
let effective_user = if ($user != "") { let effective_user = if ($user != "") {
$user $user
@@ -560,16 +558,31 @@ def _pj_left_prompt [] {
let host = (sys host | get hostname? | default "") let host = (sys host | get hostname? | default "")
let in_ssh = (($env.SSH_CONNECTION? | default "") != "" or ($env.SSH_TTY? | default "") != "") let in_ssh = (($env.SSH_CONNECTION? | default "") != "" or ($env.SSH_TTY? | default "") != "")
let prompt_newline = if (($nu.os-info.name | str downcase) == "windows") { "\n\r" } else { "\n" }
let user_color = if ($effective_user == "root") { $p.c_root } else { $p.c_user } {
os_symbol: (_pj_os_symbol)
effective_user: $effective_user
host: $host
in_ssh: $in_ssh
shell_name: "nu"
prompt_newline: $prompt_newline
}
}
let host_part = if $in_ssh { def _pj_left_prompt [] {
$"($p.c_host)@($host)($p.c_reset)($p.c_ssh)  󰬚󰬚󰬏 ($p.c_reset)" let p = ($env.PJ_PROMPT_PALETTE? | default (_pj_palette_default))
let static = ($env.PJ_PROMPT_STATIC? | default (_pj_prompt_static_base))
let user_color = if ($static.effective_user == "root") { $p.c_root } else { $p.c_user }
let host_part = if $static.in_ssh {
$"($p.c_host)@($static.host)($p.c_reset)($p.c_ssh)  󰬚󰬚󰬏 ($p.c_reset)"
} else { } else {
"" ""
} }
let shell_part = $"($p.c_shell) nu ($p.c_reset)" let shell_part = $"($p.c_shell) ($static.shell_name) ($p.c_reset)"
let dir = (_pj_truncate_pwd $env.PWD 4) let dir = (_pj_truncate_pwd $env.PWD 4)
let git_part = (_pj_git_branch_module $p) let git_part = (_pj_git_branch_module $p)
@@ -596,8 +609,8 @@ def _pj_left_prompt [] {
let duration_part = $"($p.c_duration) 󱫑 ($duration_text) ($p.c_reset)" let duration_part = $"($p.c_duration) 󱫑 ($duration_text) ($p.c_reset)"
mut out = "" mut out = ""
$out = $"($out)($p.c_os)(_pj_os_symbol)($p.c_reset)" $out = $"($out)($p.c_os)($static.os_symbol)($p.c_reset)"
$out = $"($out)($user_color)($effective_user)($p.c_reset)" $out = $"($out)($user_color)($static.effective_user)($p.c_reset)"
if ($host_part != "") { $out = $"($out)($host_part)" } if ($host_part != "") { $out = $"($out)($host_part)" }
$out = $"($out)($shell_part)" $out = $"($out)($shell_part)"
$out = $"($out)($p.c_dir) ($dir) ($p.c_reset)" $out = $"($out)($p.c_dir) ($dir) ($p.c_reset)"
@@ -605,8 +618,7 @@ def _pj_left_prompt [] {
for m in $lang_parts { for m in $lang_parts {
$out = $"($out)($m)" $out = $"($out)($m)"
} }
let prompt_newline = if (($nu.os-info.name | str downcase) == "windows") { "\n\r" } else { "\n" } $out = $"($static.prompt_newline)($out)($time_part)($duration_part)($static.prompt_newline)"
$out = $"($prompt_newline)($out)($time_part)($duration_part)($prompt_newline)"
$out $out
} }
@@ -623,6 +635,7 @@ export-env {
load-env { load-env {
PJ_PROMPT_PALETTE: (_pj_palette_default) PJ_PROMPT_PALETTE: (_pj_palette_default)
PJ_OS_KEY: (_pj_detect_os_key) PJ_OS_KEY: (_pj_detect_os_key)
PJ_PROMPT_STATIC: (_pj_prompt_static_base)
PROMPT_MULTILINE_INDICATOR: ":" PROMPT_MULTILINE_INDICATOR: ":"
PROMPT_INDICATOR: {|| PROMPT_INDICATOR: {||
_pj_prompt_indicator _pj_prompt_indicator