Merge pull request #28146 from keszybz/ansi-seq-skip

Pass ANSI sequences through when ellipsizing output
This commit is contained in:
Lennart Poettering
2023-06-28 19:18:27 +02:00
committed by GitHub
6 changed files with 206 additions and 40 deletions
+1
View File
@@ -93,6 +93,7 @@ simple_tests += files(
'test-fstab-util.c',
'test-glob-util.c',
'test-gpt.c',
'test-gunicode.c',
'test-hash-funcs.c',
'test-hexdecoct.c',
'test-hmac.c',
+41
View File
@@ -4,6 +4,7 @@
#include "alloc-util.h"
#include "constants.h"
#include "escape.h"
#include "string-util.h"
#include "strv.h"
#include "terminal-util.h"
@@ -115,4 +116,44 @@ TEST(ellipsize) {
test_ellipsize_one("shórt");
}
TEST(ellipsize_ansi) {
const char *s = ANSI_HIGHLIGHT_YELLOW_UNDERLINE "yęllow"
ANSI_HIGHLIGHT_GREY_UNDERLINE "grěy"
ANSI_HIGHLIGHT_BLUE_UNDERLINE "blue"
ANSI_NORMAL "nórmął";
size_t len = strlen(s);
for (unsigned percent = 0; percent <= 100; percent += 15)
for (ssize_t x = 21; x >= 0; x--) {
_cleanup_free_ char *t = ellipsize_mem(s, len, x, percent);
printf("%02zd: \"%s\"\n", x, t);
assert_se(utf8_is_valid(t));
if (DEBUG_LOGGING) {
_cleanup_free_ char *e = cescape(t);
printf(" : \"%s\"\n", e);
}
}
}
TEST(ellipsize_ansi_cats) {
_cleanup_free_ char *e, *f, *g, *h;
/* Make sure we don't cut off in the middle of an ANSI escape sequence. */
e = ellipsize("01" ANSI_NORMAL "23", 4, 0);
puts(e);
assert_se(streq(e, "01" ANSI_NORMAL "23"));
f = ellipsize("ab" ANSI_NORMAL "cd", 4, 90);
puts(f);
assert_se(streq(f, "ab" ANSI_NORMAL "cd"));
g = ellipsize("🐱🐱" ANSI_NORMAL "🐱🐱" ANSI_NORMAL, 5, 0);
puts(g);
assert_se(streq(g, "…" ANSI_NORMAL "🐱🐱" ANSI_NORMAL));
h = ellipsize("🐱🐱" ANSI_NORMAL "🐱🐱" ANSI_NORMAL, 5, 90);
puts(h);
assert_se(streq(h, "🐱…" ANSI_NORMAL "🐱" ANSI_NORMAL));
}
DEFINE_TEST_MAIN(LOG_INFO);
+27
View File
@@ -0,0 +1,27 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "gunicode.h"
#include "tests.h"
#include "utf8.h"
TEST(unichar_iswide) {
char32_t c;
int r;
/* FIXME: the cats are wide, but we get this wrong */
for (const char *narrow = "abX_…ąęµ!" "😼😿🙀😸😻"; *narrow; narrow += r) {
r = utf8_encoded_to_unichar(narrow, &c);
bool w = unichar_iswide(c);
assert_se(r > 0);
assert_se(!w);
}
for (const char *wide = "🐱/¥"; *wide; wide += r) {
r = utf8_encoded_to_unichar(wide, &c);
bool w = unichar_iswide(c);
assert_se(r > 0);
assert_se(w);
}
}
DEFINE_TEST_MAIN(LOG_INFO);