commit 4cc27050f876ba0e062c2d90042241d42072b042
parent 0c98c2767105ece2b087e0b002be27ca23893778
Author: Chris Bracken <chris@bracken.jp>
Date: Fri, 20 Feb 2026 21:38:02 +0900
format: simplify and add tests for print_gopher_link_padded
Diffstat:
2 files changed, 34 insertions(+), 9 deletions(-)
diff --git a/src/format.c b/src/format.c
@@ -241,15 +241,12 @@ void print_gopher_link_padded(FILE* out,
int w = wcwidth(wc);
size_t char_width = (w < 0) ? 0 : w;
- // Print ellipsis if one character from max width but more remains.
- if (display_width == width - 1 && ptr + bytes < end) {
- fprintf(out, "%s", kUtf8Ellipsis);
- display_width++;
- break;
- }
-
- // Stop if adding this character exceeds the max width.
- if (display_width + char_width > width) {
+ if (display_width + char_width > width ||
+ (ptr + bytes < end && display_width + char_width == width)) {
+ if (display_width < width) {
+ fprintf(out, "%s", kUtf8Ellipsis);
+ display_width++;
+ }
break;
}
diff --git a/src/format_tests.c b/src/format_tests.c
@@ -568,3 +568,31 @@ UTEST(print_gopher_link_padded, ComplexEmoji) {
free(buf);
}
+
+UTEST(print_gopher_link_padded, MultiColumnTruncation) {
+ {
+ char* buf = NULL;
+ size_t size = 0;
+ FILE* out = open_memstream(&buf, &size);
+ ASSERT_NE(NULL, out);
+
+ /* "あいう" (6 columns). Width 4. Should be "あ…" + 1 space. */
+ print_gopher_link_padded(out, "あいう", 4, ' ');
+ fclose(out);
+ EXPECT_STREQ("あ… ", buf);
+ free(buf);
+ }
+
+ {
+ char* buf = NULL;
+ size_t size = 0;
+ FILE* out = open_memstream(&buf, &size);
+ ASSERT_NE(NULL, out);
+
+ /* "あいう" (6 columns). Width 5. Should be "あい…" (5 columns). */
+ print_gopher_link_padded(out, "あいう", 5, ' ');
+ fclose(out);
+ EXPECT_STREQ("あい…", buf);
+ free(buf);
+ }
+}