Use string shorten for git

This checked the locale, but did so in a way that's fundamentally
broken:

1. $LANG isn't the only variable ($LC_ALL and $LC_CTYPE)
2. Even if $LANG is set that doesn't mean it's actually working

We could add a `status is-multibyte` here to figure out if we have a
multibyte locale?

But instead, since this is dealing with adding an ellipsis, let's just
add it to `string ellipsize`.

One slight difference is that shortening the branch now counts the ellipsis width.

I.e. assuming the branch is "long-branch-name"

```fish
set -g __fish_git_prompt_shorten_branch_len 8
```

might now print "long-br…" instead of "long-bra…". This is nicer because we can now give the actual maximum width.

The alternative is to add a "--exclusive" option to "string ellipsize" that doesn't count the ellipsis width. So `string ellipsize --char "..." --max 8" long-branch-name` might result in "long-bra...", which is 11 wide.
This commit is contained in:
Fabian Boehm 2022-08-16 22:02:52 +02:00
parent 41c22d5e60
commit d5db260375
2 changed files with 8 additions and 18 deletions

View File

@ -523,14 +523,6 @@ function __fish_git_complete_rev_files
printf "$rev:%s\n" (__fish_git_rev_files $rev $path)
end
# This is the same logic used on $LANG in fish_job_summary. Print a pretty ellipse if we can,
# returns the width.
function __fish_git_ellipsis
string match -iqr 'utf.?8' -- $LANG && set -f str \u2026 || set -f str ..
printf %s $str
return (string length -V $str)
end
# Determines whether we can/should complete with __fish_git_rev_files
function __fish_git_needs_rev_files
# git (as of 2.20) accepts the rev:path syntax for a number of subcommands,
@ -615,7 +607,7 @@ function __fish_git_config_keys
# With -z, key and value are separated by space, not "="
__fish_git config -lz | while read -lz key value
# Print only first line of value(with an ellipsis) if multiline
printf '%s\t%s\n' $key (string replace \n (__fish_git_ellipsis)\n -- $value)[1]
printf '%s\t%s\n' $key (string shorten -N -- $value)
end
# Print all recognized config keys; duplicates are not shown twice by fish
printf '%s\n' (__fish_git help --config)[1..-2] # Last line is a footer; ignore it
@ -739,10 +731,7 @@ function __fish_git_aliases
__fish_git config -z --get-regexp '^alias\.' 2>/dev/null | while read -lz key value
begin
set -l name (string replace -r '^.*\.' '' -- $key)
# Only use the first line of the value as the description.
# Also shorten it to 35/36 chars depending on how wide the ellipsis is.
set -f ellipsis (__fish_git_ellipsis)
set -l val (printf '%s\n' $value | string replace -r "^(.{0,$(math 37 - $status)}).+" '$1'$ellipsis)[1]
set -l val (string shorten --no-newline -m 36 -- $value)
printf "%s\t%s\n" $name "alias: $val"
end
end

View File

@ -324,10 +324,10 @@ function fish_git_prompt --description "Prompt function for Git"
end
set b (string replace refs/heads/ '' -- $b)
set -q __fish_git_prompt_shorten_branch_char_suffix
or set -l __fish_git_prompt_shorten_branch_char_suffix "…"
if string match -qr '^\d+$' "$__fish_git_prompt_shorten_branch_len"; and test (string length "$b") -gt $__fish_git_prompt_shorten_branch_len
set b (string sub -l "$__fish_git_prompt_shorten_branch_len" "$b")"$__fish_git_prompt_shorten_branch_char_suffix"
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"
set b (string shorten -m "$__fish_git_prompt_shorten_branch_len" $char -- "$b")
end
if test -n "$b"
set b "$branch_color$b$branch_done"
@ -486,8 +486,9 @@ function __fish_git_prompt_operation_branch_bare --description "fish_git_prompt
if test $status -ne 0
# Shorten the sha ourselves to 8 characters - this should be good for most repositories,
# and even for large ones it should be good for most commits
# No need for an ellipsis.
if set -q sha
set branch (string match -r '^.{8}' -- $sha)
set branch (string shorten -m8 -c "" -- $sha)
else
set branch unknown
end