Clean up "strip control characters" changes

Simplify these and move the sanitization closer to the point of output.

Stop worrying about control characters beyond what PCRE2 reports in
"[[:cntrl:]]" for clarity.
This commit is contained in:
Peter Ammon
2026-05-25 16:31:26 -07:00
parent cbc3cd21f7
commit 17fdd2f153
5 changed files with 15 additions and 25 deletions

View File

@@ -48,10 +48,7 @@ function fish_fossil_prompt --description 'Write out the fossil prompt'
echo -n ' ('
set_color magenta
# Strip control characters to avoid injecting terminal escape sequences into the prompt.
# Raw C1 bytes are stored in fish's private-use encoding at U+F680-U+F69F.
set branch (string replace -ra '[\x00-\x1f\x7f-\x9f\x{f680}-\x{f69f}]' '' -- $branch)
echo -n "$branch"
echo -n "$branch" | string replace -ra '[[:cntrl:]]' ''
set_color --reset
echo -n '|'
#set -l repo_status (fossil changes --differ 2>/dev/null | string match -rv '\w:|^\s' | string split " " -f1 | sort -u)

View File

@@ -369,6 +369,9 @@ function fish_git_prompt --description "Prompt function for Git"
end
set b (string replace refs/heads/ '' -- $b)
# Strip control characters before display.
set b (string replace -ra '[[:cntrl:]]' '' -- $b)
set r (string replace -ra '[[:cntrl:]]' '' -- $r)
if string match -qr '^\d+$' "$__fish_git_prompt_shorten_branch_len"
set -q __fish_git_prompt_shorten_branch_char_suffix
and set -l char -c "$__fish_git_prompt_shorten_branch_char_suffix"
@@ -495,8 +498,6 @@ function __fish_git_prompt_operation_branch_bare --description "fish_git_prompt
set -l step
set -l total
# Strip control characters from these dot-files before showing them in the prompt.
# (git itself does not validate the contents of rebase-merge/rebase-apply state files).
if test -d $git_dir/rebase-merge
set branch (cat $git_dir/rebase-merge/head-name 2>/dev/null)
set step (cat $git_dir/rebase-merge/msgnum 2>/dev/null)
@@ -529,6 +530,10 @@ function __fish_git_prompt_operation_branch_bare --description "fish_git_prompt
end
end
if test -n "$step" -a -n "$total"
set operation "$operation $step/$total"
end
if test -z "$branch"
if not set branch (command git symbolic-ref HEAD 2>/dev/null)
set detached yes
@@ -565,16 +570,6 @@ function __fish_git_prompt_operation_branch_bare --description "fish_git_prompt
end
end
for var_name in branch step total
if set -q $var_name[1]
set $var_name (string replace -ra '[\x00-\x1f\x7f-\x9f\x{f680}-\x{f69f}]' '' -- $$var_name)
end
end
if test -n "$step" -a -n "$total"
set operation "$operation $step/$total"
end
echo $operation
echo $branch
echo $detached

View File

@@ -30,17 +30,15 @@ function fish_hg_prompt --description 'Write out the hg prompt'
set -l root (fish_print_hg_root)
or return 1
# Read branch and bookmark.
# Strip control characters before showing branch/bookmark names in the prompt.
# Raw C1 bytes are stored in fish's private-use encoding at U+F680-U+F69F.
# (they live under .hg/ and are not validated by hg itself in this code path).
# Read branch and bookmark
set -l branch (cat $root/branch 2>/dev/null; or echo default)
set branch (string replace -ra '[\x00-\x1f\x7f-\x9f\x{f680}-\x{f69f}]' '' -- $branch)
if set -l bookmark (cat $root/bookmarks.current 2>/dev/null)
set bookmark (string replace -ra '[\x00-\x1f\x7f-\x9f\x{f680}-\x{f69f}]' '' -- $bookmark)
set branch "$branch|$bookmark"
end
# Strip control characters for display.
set branch (string replace -ra '[[:cntrl:]]' '' -- $branch)
if not set -q fish_prompt_hg_show_informative_status
set_color --reset
echo -n " ($branch)"

View File

@@ -3,7 +3,7 @@
function fish_prompt
set_color $fish_color_cwd
echo -n (prompt_pwd | path basename)
echo -n (path basename $PWD)
set_color --reset
echo -n ' ) '
end

View File

@@ -15,7 +15,7 @@ set -gx PATH $tmp/bin $PATH
mkdir -p $tmp/hgrepo/.hg
touch $tmp/hgrepo/.hg/dirstate
printf 'main\e]0;OHNO\a\x7f\u0080\x80' >$tmp/hgrepo/.hg/branch
printf 'main\e]0;OHNO\a\x7f' >$tmp/hgrepo/.hg/branch
cd $tmp/hgrepo
fish_hg_prompt
@@ -27,7 +27,7 @@ mkdir $tmp/gitrepo
cd $tmp/gitrepo
git init -q
mkdir .git/rebase-merge
printf 'refs/heads/main\e]0;OHNO\a\x7f\u0080\x80' >.git/rebase-merge/head-name
printf 'refs/heads/main\e]0;OHNO\a\x7f' >.git/rebase-merge/head-name
: >.git/rebase-merge/msgnum
: >.git/rebase-merge/end