Add WISH_PATH_POWERLINE_MAX_PERC to limit path length.

WISH_PATH_POWERLINE_MAX_PERC tries to ensure that the path stays below
the specified percentage of the remaining characters. The path is
shortened by replacing each dir with its initial until it becomes short
enough. The last part is never shortened.
This commit is contained in:
Ceda EI 2019-12-29 20:26:47 +05:30
parent 30ebb302b8
commit fe18fd52db
1 changed files with 35 additions and 0 deletions

View File

@ -10,10 +10,45 @@ function wish_path_powerline_set_colors() {
WISH_PATH_POWERLINE_FG=${WISH_PATH_POWERLINE_FG:-$WISH_DEFAULT_FG}
WISH_PATH_POWERLINE_BG=${WISH_PATH_POWERLINE_BG:-$WISH_DEFAULT_BG}
WISH_PATH_POWERLINE_NO_WRITE_SUFFIX=${WISH_PATH_POWERLINE_NO_WRITE_SUFFIX:-}
WISH_PATH_POWERLINE_MAX_PERC=${WISH_PATH_POWERLINE_MAX_PERC:-0}
}
function wish_path_lensum() {
sum=0
for i in $*; do
sum=$(($sum + ${#i}))
done
echo $sum
}
function wish_path_powerline_shrink() {
local IFS='/'
local path=( $1 )
local max=$2
for ((i=0; i <$((${#path[@]} - 1)); i++)); do
path[$i]=${path[$i]:0:1}
if [[ $(wish_path_lensum ${path[@]}) -lt $max ]]; then
local short_path=""
for i in ${path[@]}; do
short_path+="$i/"
done
echo "${short_path%/}"
return
fi
done
local short_path=""
for i in ${path[@]}; do
short_path+="$i/"
done
echo "${short_path%/}"
}
function wish_path_powerline_main() {
local path="${PWD/#$HOME/\~}"
if [[ $WISH_PATH_POWERLINE_MAX_PERC -ne 0 && ${#path} -ge $WISH_PATH_POWERLINE_MAX_LEN ]] ; then
local max=$(($WISH_PATH_POWERLINE_MAX_PERC * $(wish_remaining_chars) / 100))
local path=$(wish_path_powerline_shrink "$path" "$max")
fi
local path="${path//\//  }"
if [[ -w $PWD ]]; then
local path=" $path "