Skip to content

Commit

Permalink
misc/codepoint_width: assume tab width to be 8
Browse files Browse the repository at this point in the history
It has been hardcoded to the same value in stats.lua so keep the current
behaviour. Can be made configurable if requested in the future.
  • Loading branch information
kasper93 committed Oct 20, 2024
1 parent bcf169a commit 60ed7cb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 7 additions & 1 deletion misc/codepoint_width.c
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,12 @@ int term_disp_width(bstr str, int max_width, const unsigned char **cut_pos)
goto next;
}

if (cp == '\t') {
// Assume tab width is 8
current_width = 8;
goto next;
}

if (cp < 0x20)
goto next;

Expand Down Expand Up @@ -736,8 +742,8 @@ int term_disp_width(bstr str, int max_width, const unsigned char **cut_pos)
str = cluster_end;
}

next:
current_width = MPMIN(current_width, 2);
next:
if (width + current_width > max_width) {
assert(prev_pos < str.start + str.len);
*cut_pos = prev_pos;
Expand Down
4 changes: 2 additions & 2 deletions test/codepoint_width.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int main(void) {
assert_int_equal(W("\U0001F1F5\U0001F1F1"), 2);

assert_int_equal(W("\n"), 0); // Newline (should not take up any visual space)
assert_int_equal(W("\t"), 0); // Tab (no visual width itself)
assert_int_equal(W("\t"), 8); // Tab (assumed to be 8 spaces wide)
assert_int_equal(W("\0"), 0); // Null character (non-visible)

assert_int_equal(W("A\u3042"), 3); // ASCII 'A' + full-width Japanese Hiragana 'あ' (U+3042)
Expand All @@ -56,7 +56,7 @@ int main(void) {
assert_int_equal(W("\U0001F469\u200D\u2764\uFE0F\u200D\U0001F469A\u3042"), 5);

assert_int_equal(W("A\nB"), 2); // ASCII characters with newline (newline should not affect width)
assert_int_equal(W("ABC\tDEF"), 6); // Tab inside a string (no visual width for '\t')
assert_int_equal(W("ABC\tDEF"), 14); // Tab inside a string

// ASCII characters with color
assert_int_equal(W("\033[31mABC\033[0m\033[32mDEF\033[0m"), 6);
Expand Down

0 comments on commit 60ed7cb

Please sign in to comment.