Skip to content

Commit 44f1071

Browse files
committed
Fix bug with calculating max line length & truncating
1 parent a02d2f8 commit 44f1071

2 files changed

Lines changed: 22 additions & 18 deletions

File tree

hdi

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -916,15 +916,10 @@ _truncate_visible() {
916916
return
917917
fi
918918

919-
# Strip ANSI to get visible length (single parameter expansion, no subshell)
920-
local plain="${str//$'\x1b'\[[0-9;]*[a-zA-Z]/}"
921-
if (( ${#plain} <= max )); then
922-
_TRUNC="$str"
923-
return
924-
fi
925-
# Truncation needed: keep first (max-1) visible chars with their interleaved
926-
# escapes, append …, then append any escapes after the cut so that RESET
927-
# (or other terminating codes) still take effect.
919+
# Single walk: collect prefix (up to max-1 visible chars with their
920+
# interleaved escapes), trailing escapes after the cut, and total visible
921+
# count. A glob-based ANSI strip would be simpler, but bash `*` inside
922+
# `[0-9;]*` is greedy and swallows content across escape boundaries.
928923
local prefix="" trailing="" keep=$((max - 1))
929924
local visible=0 i=0 in_esc=0 ch
930925
local len=${#str}
@@ -941,10 +936,17 @@ _truncate_visible() {
941936
elif (( visible < keep )); then
942937
prefix+="$ch"
943938
((visible++))
939+
else
940+
((visible++))
944941
fi
945942
((i++))
946943
done
947944

945+
if (( visible <= max )); then
946+
_TRUNC="$str"
947+
return
948+
fi
949+
948950
_TRUNC="${prefix}${trailing}"
949951
}
950952

src/picker.sh

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,10 @@ _truncate_visible() {
5353
return
5454
fi
5555

56-
# Strip ANSI to get visible length (single parameter expansion, no subshell)
57-
local plain="${str//$'\x1b'\[[0-9;]*[a-zA-Z]/}"
58-
if (( ${#plain} <= max )); then
59-
_TRUNC="$str"
60-
return
61-
fi
62-
# Truncation needed: keep first (max-1) visible chars with their interleaved
63-
# escapes, append …, then append any escapes after the cut so that RESET
64-
# (or other terminating codes) still take effect.
56+
# Single walk: collect prefix (up to max-1 visible chars with their
57+
# interleaved escapes), trailing escapes after the cut, and total visible
58+
# count. A glob-based ANSI strip would be simpler, but bash `*` inside
59+
# `[0-9;]*` is greedy and swallows content across escape boundaries.
6560
local prefix="" trailing="" keep=$((max - 1))
6661
local visible=0 i=0 in_esc=0 ch
6762
local len=${#str}
@@ -78,10 +73,17 @@ _truncate_visible() {
7873
elif (( visible < keep )); then
7974
prefix+="$ch"
8075
((visible++))
76+
else
77+
((visible++))
8178
fi
8279
((i++))
8380
done
8481

82+
if (( visible <= max )); then
83+
_TRUNC="$str"
84+
return
85+
fi
86+
8587
_TRUNC="${prefix}${trailing}"
8688
}
8789

0 commit comments

Comments
 (0)