Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */ 2 : 3 : #include <stdio.h> 4 : 5 : #include "alloc-util.h" 6 : #include "pretty-print.h" 7 : #include "string-util.h" 8 : #include "terminal-util.h" 9 : #include "util.h" 10 : 11 1 : int main(int argc, char *argv[]) { 12 1 : _cleanup_free_ char *urlified = NULL, *q = NULL, *qq = NULL; 13 : char *p, *z; 14 : 15 1 : assert_se(p = strdup("\tFoobar\tbar\twaldo\t")); 16 1 : assert_se(strip_tab_ansi(&p, NULL, NULL)); 17 1 : fprintf(stdout, "<%s>\n", p); 18 1 : assert_se(streq(p, " Foobar bar waldo ")); 19 1 : free(p); 20 : 21 1 : assert_se(p = strdup(ANSI_HIGHLIGHT "Hello" ANSI_NORMAL ANSI_HIGHLIGHT_RED " world!" ANSI_NORMAL)); 22 1 : assert_se(strip_tab_ansi(&p, NULL, NULL)); 23 1 : fprintf(stdout, "<%s>\n", p); 24 1 : assert_se(streq(p, "Hello world!")); 25 1 : free(p); 26 : 27 1 : assert_se(p = strdup("\x1B[\x1B[\t\x1B[" ANSI_HIGHLIGHT "\x1B[" "Hello" ANSI_NORMAL ANSI_HIGHLIGHT_RED " world!" ANSI_NORMAL)); 28 1 : assert_se(strip_tab_ansi(&p, NULL, NULL)); 29 1 : assert_se(streq(p, "\x1B[\x1B[ \x1B[\x1B[Hello world!")); 30 1 : free(p); 31 : 32 1 : assert_se(p = strdup("\x1B[waldo")); 33 1 : assert_se(strip_tab_ansi(&p, NULL, NULL)); 34 1 : assert_se(streq(p, "\x1B[waldo")); 35 1 : free(p); 36 : 37 1 : assert_se(terminal_urlify_path("/etc/fstab", "i am a fabulous link", &urlified) >= 0); 38 1 : assert_se(p = strjoin("something ", urlified, " something-else")); 39 1 : assert_se(q = strdup(p)); 40 1 : printf("<%s>\n", p); 41 1 : assert_se(strip_tab_ansi(&p, NULL, NULL)); 42 1 : printf("<%s>\n", p); 43 1 : assert_se(streq(p, "something i am a fabulous link something-else")); 44 1 : p = mfree(p); 45 : 46 : /* Truncate the formatted string in the middle of an ANSI sequence (in which case we shouldn't touch the 47 : * incomplete sequence) */ 48 1 : z = strstr(q, "fstab"); 49 1 : if (z) { 50 0 : *z = 0; 51 0 : assert_se(qq = strdup(q)); 52 0 : assert_se(strip_tab_ansi(&q, NULL, NULL)); 53 0 : assert_se(streq(q, qq)); 54 : } 55 : 56 1 : return 0; 57 : }