From c5a2bfd924445dfccd943ea34374a5d1266f5cb9 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 13 Jul 2023 15:22:42 -0400 Subject: color: TTY-escape filenames --- src/color.c | 10 +++++++++- src/color.h | 1 + src/printf.c | 4 ++-- 3 files changed, 12 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/color.c b/src/color.c index 00d7920..6593f5b 100644 --- a/src/color.c +++ b/src/color.c @@ -922,9 +922,12 @@ static int print_colored(CFILE *cfile, const struct esc_seq *esc, const char *st if (print_esc(cfile, esc) != 0) { return -1; } - if (dstrxcat(&cfile->buffer, str, len) != 0) { + + // Don't let the string itself interfere with the colors + if (print_wordesc(cfile, str, len, WESC_TTY) != 0) { return -1; } + if (print_reset(cfile) != 0) { return -1; } @@ -1242,6 +1245,11 @@ static int cvbuff(CFILE *cfile, const char *format, va_list args) { return -1; } break; + case 'Q': + if (print_wordesc(cfile, va_arg(args, const char *), SIZE_MAX, WESC_TTY) != 0) { + return -1; + } + break; case 'F': if (print_name(cfile, va_arg(args, const struct BFTW *)) != 0) { diff --git a/src/color.h b/src/color.h index 932d551..0d46c33 100644 --- a/src/color.h +++ b/src/color.h @@ -87,6 +87,7 @@ int cfclose(CFILE *cfile); * %zu: A size_t * %m: strerror(errno) * %pq: A shell-escaped string, like bash's printf %q + * %pQ: A TTY-escaped string. * %pF: A colored file name, from a const struct BFTW * argument * %pP: A colored file path, from a const struct BFTW * argument * %pL: A colored link target, from a const struct BFTW * argument diff --git a/src/printf.c b/src/printf.c index 6520d2d..f0910fa 100644 --- a/src/printf.c +++ b/src/printf.c @@ -298,7 +298,7 @@ static int bfs_printf_h(CFILE *cfile, const struct bfs_printf *directive, const int ret; if (should_color(cfile, directive)) { - ret = cfprintf(cfile, "${di}%s${rs}", buf); + ret = cfprintf(cfile, "${di}%pQ${rs}", buf); } else { ret = dyn_fprintf(cfile->file, directive, buf); } @@ -313,7 +313,7 @@ static int bfs_printf_H(CFILE *cfile, const struct bfs_printf *directive, const if (ftwbuf->depth == 0) { return cfprintf(cfile, "%pP", ftwbuf); } else { - return cfprintf(cfile, "${di}%s${rs}", ftwbuf->root); + return cfprintf(cfile, "${di}%pQ${rs}", ftwbuf->root); } } else { return dyn_fprintf(cfile->file, directive, ftwbuf->root); -- cgit v1.2.3