Fix string truncation

This commit is contained in:
Mattia Basaglia 2015-05-24 17:27:12 +02:00
parent c7977cd03f
commit 3b12c83e16

View file

@ -163,7 +163,6 @@ function help()
echo echo
} }
function list_ponies() function list_ponies()
{ {
ponydir=$(get_data_file "rendered/ansi/") ponydir=$(get_data_file "rendered/ansi/")
@ -190,6 +189,26 @@ function get_data_file()
fi fi
} }
# Returns the number of displayed characters, ignoring color codes
function strlen_color()
{
echo -n "$1" | sed -r 's/\x1b\[[0-9;]+m//g' | wc -c
}
# Truncates a string
function truncate_color()
{
local length="$(strlen_color "$1")"
local target="$2"
if [ "$length" -gt "$target" ]
then
let cut="$target-$length"
echo -n "$1" | head -c $cut
return
fi
echo "$1"
}
# Read global config # Read global config
globalconfig=$(get_data_file systempony.conf) globalconfig=$(get_data_file systempony.conf)
if [ -r "$globalconfig" ] if [ -r "$globalconfig" ]
@ -322,17 +341,12 @@ then
do do
if [ $info_index -lt ${#INFO[@]} -a $current_line -ge $start_line ] if [ $info_index -lt ${#INFO[@]} -a $current_line -ge $start_line ]
then then
let msgsize="$COLUMNS - $info_x - $info_key_max_length - 3" let msgsize="$COLUMNS - $info_x - $info_key_max_length - 4"
current_line_length=$(echo "$line" | sed -r 's/\x1b\[[0-9;]+m//g' | wc -c) line="$(truncate_color "$line" $info_x)"
if [ $current_line_length -gt $info_x ] printf "%s \x1b[31;1m%-${info_key_max_length}s\x1b[0m: %s %s\n" \
then "$line$backspaces" \
let linediff="$current_line_length - $info_x"
line="$(echo "$line" | head -c -$linediff)";
fi
printf "%s \x1b[31;1m%-${info_key_max_length}s\x1b[0m: %s\n" \
"$line" \
"${INFO[$info_index]}" \ "${INFO[$info_index]}" \
"$(echo -n "${info_values[$info_index]}" | head -c $msgsize)" "$(truncate_color "${info_values[$info_index]}" $msgsize)"
let info_index++ let info_index++
else else
echo -n "$line" echo -n "$line"