1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-08-01 06:37:46 +00:00

update oh-my.nu to respect use_ansi_coloring config setting (#1003)

A quick update to allow the oh-my.nu prompt to respect the
$env.config.use_ansi_coloring by way of a new command called `config
use-colors`. This way the prompt will be rendered correctly as far as
ansi escapes.

This file was also formatted with `topiary` which makes up for the rest
of the changes.

This will only work on nushell version 0.101.1 and above.
This commit is contained in:
Darren Schroeder 2024-12-27 07:10:35 -06:00 committed by GitHub
parent 4fa48b3e60
commit a83a40dff0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -11,7 +11,6 @@
# inspired by https://github.com/IlanCosman/tide
# inspired by https://github.com/JanDeDobbeleer/oh-my-posh
# Abbreviate home path
def home_abbrev [os_name] {
let is_home_in_path = ($env.PWD | str starts-with $nu.home-path)
@ -36,9 +35,9 @@ def home_abbrev [os_name] {
def path_abbrev_if_needed [apath term_width] {
# probably shouldn't do coloring here but since we're coloring
# only certain parts, it's kind of tricky to do it in another place
let T = (ansi { fg: "#BCBCBC" bg: "#3465A4"}) # truncated
let P = (ansi { fg: "#E4E4E4" bg: "#3465A4"}) # path
let PB = (ansi { fg: "#E4E4E4" bg: "#3465A4" attr: b}) # path bold
let T = (ansi { fg: "#BCBCBC" bg: "#3465A4" }) # truncated
let P = (ansi { fg: "#E4E4E4" bg: "#3465A4" }) # path
let PB = (ansi { fg: "#E4E4E4" bg: "#3465A4" attr: b }) # path bold
let R = (ansi reset)
let is_home_in_path = ($env.PWD | str starts-with $nu.home-path)
@ -49,9 +48,11 @@ def path_abbrev_if_needed [apath term_width] {
let splits_len = ($splits | length)
# get all the tokens except the last
let tokens = (1..<($splits_len - 1) | each {|x|
let tokens = (
1..<($splits_len - 1) | each {|x|
$"($T)((($splits) | get $x | split chars) | get 0)($R)"
})
}
)
# need an insert command
let tokens = ($tokens | prepend $"($T)~")
@ -74,21 +75,25 @@ def path_abbrev_if_needed [apath term_width] {
} else if ($splits.0 | is-empty) {
let top_part = ($splits | skip | first ($splits_len - 2))
let end_part = ($splits | last)
let tokens = ($top_part | each {|x|
let tokens = (
$top_part | each {|x|
$"($T)/(($x | split chars).0)($R)"
})
}
)
let tokens = ($tokens | append $"/($PB)($end_part)($R)")
$tokens | str join $"($T)"
} else {
let top_part = ($splits | first ($splits_len - 1))
let end_part = ($splits | last)
let tokens = ($top_part | each {|x|
let tokens = (
$top_part | each {|x|
if $x == '~' {
$"($T)(($x | split chars).0)($R)"
} else {
$"/($T)(($x | split chars).0)($R)"
}
})
}
)
let tokens = ($tokens | append $"/($PB)($end_part)($R)")
$tokens | str join $"($T)"
}
@ -141,19 +146,19 @@ def get_behind_count [gs] {
def get_icons_list [] {
{
AHEAD_ICON: (char branch_ahead), # "↑" 2191
BEHIND_ICON: (char branch_behind), # "↓" 2193
AHEAD_ICON: (char branch_ahead) # "↑" 2191
BEHIND_ICON: (char branch_behind) # "↓" 2193
NO_CHANGE_ICON: (char branch_identical) # ≣ 2263
HAS_CHANGE_ICON: "*",
INDEX_CHANGE_ICON: "♦",
WT_CHANGE_ICON: "✚",
CONFLICTED_CHANGE_ICON: "✖",
HAS_CHANGE_ICON: "*"
INDEX_CHANGE_ICON: "♦"
WT_CHANGE_ICON: "✚"
CONFLICTED_CHANGE_ICON: "✖"
UNTRACKED_CHANGE_ICON: (char branch_untracked) # ≢ 2262
INSERT_SYMBOL_ICON: "",
INSERT_SYMBOL_ICON: ""
HAMBURGER_ICON: (char hamburger) # "≡" 2261
GITHUB_ICON: "", # f408
GITHUB_ICON: "" # f408
BRANCH_ICON: (char nf_branch) # "" e0a0
REBASE_ICON: "", # e728
REBASE_ICON: "" # e728
TAG_ICON: "" # f412
}
}
@ -265,19 +270,19 @@ def get_repo_status [gs os] {
let repo_status = (
$"(
if ($ahead_cnt > 0) { $'($A_COLOR)($AHEAD_ICON)($ahead_cnt)($R)' }
if ($ahead_cnt > 0) {$'($A_COLOR)($AHEAD_ICON)($ahead_cnt)($R)'}
)(
if ($behind_cnt > 0) { $'($B_COLOR)($BEHIND_ICON)($behind_cnt)($R)' }
if ($behind_cnt > 0) {$'($B_COLOR)($BEHIND_ICON)($behind_cnt)($R)'}
)(
if ($index_change_cnt > 0) { $'($I_COLOR)($INDEX_CHANGE_ICON)($index_change_cnt)($R)' }
if ($index_change_cnt > 0) {$'($I_COLOR)($INDEX_CHANGE_ICON)($index_change_cnt)($R)'}
)(
if ($conflicted_cnt > 0) { $'($C_COLOR)($CONFLICTED_CHANGE_ICON)($conflicted_cnt)($R)' }
if ($conflicted_cnt > 0) {$'($C_COLOR)($CONFLICTED_CHANGE_ICON)($conflicted_cnt)($R)'}
)(
if ($wt_change_cnt > 0) { $'($W_COLOR)($WT_CHANGE_ICON)($wt_change_cnt)($R)' }
if ($wt_change_cnt > 0) {$'($W_COLOR)($WT_CHANGE_ICON)($wt_change_cnt)($R)'}
)(
if ($untracked_cnt > 0) { $'($U_COLOR)($UNTRACKED_CHANGE_ICON)($untracked_cnt)($R)' }
if ($untracked_cnt > 0) {$'($U_COLOR)($UNTRACKED_CHANGE_ICON)($untracked_cnt)($R)'}
)(
if $has_no_changes { $'($N_COLOR)($NO_CHANGE_ICON)($R)' } else { $'($H_COLOR)($HAS_CHANGE_ICON)($R)' }
if $has_no_changes {$'($N_COLOR)($NO_CHANGE_ICON)($R)'} else {$'($H_COLOR)($HAS_CHANGE_ICON)($R)'}
)"
)
@ -309,23 +314,26 @@ def git_left_prompt [gs os] {
let repo_status = (get_repo_status $gs $os)
# build segments and then put together the segments for the prompt
let os_segment = ([
(ansi { fg: "#080808" bg: "#CED7CF"}) # os bg color
let os_segment = (
[
(ansi { fg: "#080808" bg: "#CED7CF" }) # os bg color
(char space) # space
(get_os_icon $os) # os icon
(char space) # space
(ansi { fg: "#CED7CF" bg: "#3465A4"}) # color transition
(ansi { fg: "#CED7CF" bg: "#3465A4" }) # color transition
(char -u e0b0) # 
(char space) # space
] | str join)
] | str join
)
let is_home_in_path = ($env.PWD | str starts-with $nu.home-path)
let path_segment = (if (($is_home_in_path) and ($branch_name == "")) {
let path_segment = (
if (($is_home_in_path) and ($branch_name == "")) {
[
(char -u f015) #  home icon
(char space) # space
$display_path # ~/src/forks/nushell
(ansi { fg: "#CED7CF" bg: "#3465A4"}) # color just to color the next space
(ansi { fg: "#CED7CF" bg: "#3465A4" }) # color just to color the next space
(char space) # space
] | str join
} else {
@ -333,52 +341,59 @@ def git_left_prompt [gs os] {
(char -u f07c) #  folder icon
(char space) # space
$display_path # ~/src/forks/nushell
(ansi { fg: "#CED7CF" bg: "#3465A4"}) # color just to color the next space
(ansi { fg: "#CED7CF" bg: "#3465A4" }) # color just to color the next space
(char space) # space
] | str join
})
}
)
let git_segment = (if ($branch_name != "") {
let git_segment = (
if ($branch_name != "") {
[
(ansi { fg: "#3465A4" bg: "#4E9A06"}) # color
(ansi { fg: "#3465A4" bg: "#4E9A06" }) # color
(char -u e0b0) # 
(char space) # space
(ansi { fg: $TERM_BG bg: "#4E9A06"}) # color
(ansi { fg: $TERM_BG bg: "#4E9A06" }) # color
# (char -u f1d3) # 
(char -u e0a0) # 
(char space) # space
($branch_name) # main
(char space) # space
(ansi { fg: "#4E9A06" bg: $GIT_BG}) # color
(ansi { fg: "#4E9A06" bg: $GIT_BG }) # color
(char -u e0b0) # 
(char space) # space
($R) # reset color
$repo_status # repo status
] | str join
})
}
)
let git_right = false
let indicator_segment = (if ($branch_name == "" or $git_right) {
let indicator_segment = (
if ($branch_name == "" or $git_right) {
[
(ansi { fg: "#3465A4" bg: $TERM_BG}) # color
(ansi { fg: "#3465A4" bg: $TERM_BG }) # color
(char -u e0b0) # 
($R) # reset color
] | str join
} else {
[
(ansi { fg: $GIT_BG bg: $TERM_BG}) # color
(ansi { fg: $GIT_BG bg: $TERM_BG }) # color
(char -u e0b0) # 
($R) # reset color
] | str join
})
}
)
# assemble all segments for final prompt printing
[
$os_segment
$path_segment
(if ($git_right == false) {
(
if ($git_right == false) {
$git_segment
})
}
)
$indicator_segment
] | str join
}
@ -403,38 +418,43 @@ def git_right_prompt [gs os] {
let TERM_FG_DEFAULT = "\e[39m"
let TERM_BG_DEFAULT = "\e[49m"
let datetime_segment = ([
(ansi { fg: $TIME_BG bg: $TERM_FG})
let datetime_segment = (
[
(ansi { fg: $TIME_BG bg: $TERM_FG })
(char -u e0b2) # 
(ansi { fg: $TERM_FG bg: $TIME_BG})
(ansi { fg: $TERM_FG bg: $TIME_BG })
(char space)
(date now | format date '%m/%d/%Y %I:%M:%S%.3f')
(char space)
($R)
] | str join)
] | str join
)
let time_segment = ([
(ansi { fg: $TIME_BG bg: $TERM_FG})
let time_segment = (
[
(ansi { fg: $TIME_BG bg: $TERM_FG })
(char -u e0b2) # 
(ansi { fg: $TERM_FG bg: $TIME_BG})
(ansi { fg: $TERM_FG bg: $TIME_BG })
(char space)
(date now | format date '%I:%M:%S %p')
(char space)
($R)
] | str join)
] | str join
)
let git_segment = (if ($branch_name != "") {
let git_segment = (
if ($branch_name != "") {
[
(ansi { fg: $GIT_BG bg: $TERM_BG}) # color
(ansi { fg: $GIT_BG bg: $TERM_BG }) # color
(char -u e0b2) # 
(ansi { fg: $TERM_FG bg: $GIT_BG}) # color
(ansi { fg: $TERM_FG bg: $GIT_BG }) # color
(char space) # space
$repo_status # repo status
(ansi { fg: $TERM_FG bg: $GIT_BG}) # color
(ansi { fg: $TERM_FG bg: $GIT_BG }) # color
(char space)
(ansi { fg: "#4E9A06" bg: $GIT_BG }) # color
(char -u e0b2) # 
(ansi { fg: $TERM_BG bg: "#4E9A06"}) # color
(ansi { fg: $TERM_BG bg: "#4E9A06" }) # color
(char space) # space
# (char -u f1d3) # 
# (char -u e0a0) # 
@ -444,12 +464,13 @@ def git_right_prompt [gs os] {
(char space) # space
($R) # reset color
] | str join
})
}
)
let execution_time_segment = (
[
# (ansi { fg: "#606060" bg: "#191323"})
(ansi { fg: "#606060"})
(ansi { fg: "#606060" })
$TERM_BG_DEFAULT
(char -u e0b3)
(char space)
@ -461,11 +482,13 @@ def git_right_prompt [gs os] {
let status_segment = (
[
(if $env.LAST_EXIT_CODE != 0 {
(
if $env.LAST_EXIT_CODE != 0 {
(ansi { fg: "#CC0000" })
} else {
(ansi { fg: "#606060" })
})
}
)
(char -u e0b3)
(char space)
$env.LAST_EXIT_CODE
@ -478,9 +501,11 @@ def git_right_prompt [gs os] {
# 2. time only - working
[
(if $env.LAST_EXIT_CODE != 0 {
(
if $env.LAST_EXIT_CODE != 0 {
$status_segment
})
}
)
$execution_time_segment
$time_segment
] | str join
@ -507,6 +532,7 @@ export def git_prompt [] {
let os = $nu.os-info
let left_prompt = (git_left_prompt $gs $os)
let right_prompt = (git_right_prompt $gs $os)
let use_ansi = (config use-colors)
# set the title of the window/tab
# Wezterm accepts:
@ -533,8 +559,8 @@ export def git_prompt [] {
# return in record literal syntax to be used kind of like a tuple
# so we don't have to run this script more than once per prompt
{
left_prompt: $left_prompt
right_prompt: $right_prompt
left_prompt: (if $use_ansi { $left_prompt } else { $left_prompt | ansi strip })
right_prompt: (if $use_ansi { $right_prompt } else { $right_prompt | ansi strip })
}
#
# in the config.nu you would do something like