refactor nushell prompt helpers and reduce call depth
This commit is contained in:
@@ -142,34 +142,6 @@ def _pj_palette_default [] {
|
||||
}
|
||||
}
|
||||
|
||||
def _pj_palette [] {
|
||||
($env.PJ_PROMPT_PALETTE? | default (_pj_palette_default))
|
||||
}
|
||||
|
||||
def _pj_detect_user [] {
|
||||
let name = (try { whoami } catch { "" } | into string | str trim)
|
||||
if ($name != "") {
|
||||
return $name
|
||||
}
|
||||
|
||||
($env.USER? | default ($env.USERNAME? | default "user"))
|
||||
}
|
||||
|
||||
def _pj_runtime_default [] {
|
||||
let user = (_pj_detect_user)
|
||||
let host = (sys host | get hostname? | default "")
|
||||
{
|
||||
user: $user
|
||||
is_root: ($user == "root")
|
||||
host: $host
|
||||
in_ssh: (($env.SSH_CONNECTION? | default "") != "" or ($env.SSH_TTY? | default "") != "")
|
||||
}
|
||||
}
|
||||
|
||||
def _pj_runtime [] {
|
||||
(_pj_runtime_default)
|
||||
}
|
||||
|
||||
def _pj_first_version [text: string] {
|
||||
let parsed = ($text | parse -r '(?<ver>\d+(?:\.\d+)+)')
|
||||
if (($parsed | length) > 0) {
|
||||
@@ -576,34 +548,28 @@ def _pj_lang_segments [] {
|
||||
$out
|
||||
}
|
||||
|
||||
def _pj_shell_module [p: record] {
|
||||
$"($p.c_shell) nu ($p.c_reset)"
|
||||
}
|
||||
|
||||
def _pj_time_module [p: record] {
|
||||
let now = (date now | format date "%R")
|
||||
$"($p.c_time) ($now) ($p.c_reset)"
|
||||
}
|
||||
|
||||
def _pj_duration_module [p: record] {
|
||||
let ms = (_pj_cmd_duration_ms)
|
||||
let duration_text = (_pj_format_duration $ms)
|
||||
$"($p.c_duration) ($duration_text) ($p.c_reset)"
|
||||
}
|
||||
|
||||
def _pj_left_prompt [] {
|
||||
let p = (_pj_palette)
|
||||
let runtime = (_pj_runtime)
|
||||
let p = ($env.PJ_PROMPT_PALETTE? | default (_pj_palette_default))
|
||||
|
||||
let user_color = if ($runtime.is_root | default false) { $p.c_root } else { $p.c_user }
|
||||
let user = (try { whoami } catch { "" } | into string | str trim)
|
||||
let effective_user = if ($user != "") {
|
||||
$user
|
||||
} else {
|
||||
($env.USER? | default ($env.USERNAME? | default "user"))
|
||||
}
|
||||
|
||||
let host_part = if ($runtime.in_ssh | default false) {
|
||||
$"($p.c_host)@($runtime.host)($p.c_reset)($p.c_ssh) ($p.c_reset)"
|
||||
let host = (sys host | get hostname? | default "")
|
||||
let in_ssh = (($env.SSH_CONNECTION? | default "") != "" or ($env.SSH_TTY? | default "") != "")
|
||||
|
||||
let user_color = if ($effective_user == "root") { $p.c_root } else { $p.c_user }
|
||||
|
||||
let host_part = if $in_ssh {
|
||||
$"($p.c_host)@($host)($p.c_reset)($p.c_ssh) ($p.c_reset)"
|
||||
} else {
|
||||
""
|
||||
}
|
||||
|
||||
let shell_part = (_pj_shell_module $p)
|
||||
let shell_part = $"($p.c_shell) nu ($p.c_reset)"
|
||||
let dir = (_pj_truncate_pwd $env.PWD 4)
|
||||
let git_part = (_pj_git_branch_module $p)
|
||||
|
||||
@@ -624,12 +590,14 @@ def _pj_left_prompt [] {
|
||||
}
|
||||
})
|
||||
|
||||
let time_part = (_pj_time_module $p)
|
||||
let duration_part = (_pj_duration_module $p)
|
||||
let now = (date now | format date "%R")
|
||||
let time_part = $"($p.c_time) ($now) ($p.c_reset)"
|
||||
let duration_text = (_pj_format_duration (_pj_cmd_duration_ms))
|
||||
let duration_part = $"($p.c_duration) ($duration_text) ($p.c_reset)"
|
||||
|
||||
mut out = ""
|
||||
$out = $"($out)($p.c_os)(_pj_os_symbol)($p.c_reset)"
|
||||
$out = $"($out)($user_color)($runtime.user)($p.c_reset)"
|
||||
$out = $"($out)($user_color)($effective_user)($p.c_reset)"
|
||||
if ($host_part != "") { $out = $"($out)($host_part)" }
|
||||
$out = $"($out)($shell_part)"
|
||||
$out = $"($out)($p.c_dir) ($dir) ($p.c_reset)"
|
||||
@@ -642,7 +610,7 @@ def _pj_left_prompt [] {
|
||||
}
|
||||
|
||||
def _pj_prompt_indicator [] {
|
||||
let p = (_pj_palette)
|
||||
let p = ($env.PJ_PROMPT_PALETTE? | default (_pj_palette_default))
|
||||
if (($env.LAST_EXIT_CODE? | default 0) == 0) {
|
||||
$"($p.c_indicator_ok)❯($p.c_reset) "
|
||||
} else {
|
||||
@@ -650,15 +618,6 @@ def _pj_prompt_indicator [] {
|
||||
}
|
||||
}
|
||||
|
||||
def _pj_right_prompt [] {
|
||||
let p = (_pj_palette)
|
||||
if (($env.LAST_EXIT_CODE? | default 0) != 0) {
|
||||
$"($p.c_right_error)Exit code: ($env.LAST_EXIT_CODE)($p.c_reset)"
|
||||
} else {
|
||||
$""
|
||||
}
|
||||
}
|
||||
|
||||
export-env {
|
||||
load-env {
|
||||
PJ_PROMPT_PALETTE: (_pj_palette_default)
|
||||
@@ -673,7 +632,12 @@ export-env {
|
||||
_pj_left_prompt
|
||||
}
|
||||
PROMPT_COMMAND_RIGHT: {||
|
||||
_pj_right_prompt
|
||||
let p = ($env.PJ_PROMPT_PALETTE? | default (_pj_palette_default))
|
||||
if (($env.LAST_EXIT_CODE? | default 0) != 0) {
|
||||
$"($p.c_right_error)Exit code: ($env.LAST_EXIT_CODE)($p.c_reset)"
|
||||
} else {
|
||||
""
|
||||
}
|
||||
}
|
||||
config: ($env.config? | default {} | merge {
|
||||
render_right_prompt_on_last_line: true
|
||||
|
||||
Reference in New Issue
Block a user