diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2023-06-16 13:44:21 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2023-06-16 13:44:21 -0400 |
commit | 2eeee3e0d0d3aeca5e6487cc2431c1f65e35a6d8 (patch) | |
tree | c481462d4e4ffb4a3c43a50b6f5bec7777b4a7a9 | |
parent | d3f4340a460a023656151f220f5923ec8ccf9894 (diff) | |
download | bfs-2eeee3e0d0d3aeca5e6487cc2431c1f65e35a6d8.tar.xz |
color: New %pq formatter for shell-escaped strings
-rw-r--r-- | src/color.c | 18 | ||||
-rw-r--r-- | src/color.h | 1 |
2 files changed, 19 insertions, 0 deletions
diff --git a/src/color.c b/src/color.c index 87575c9..1edd8b5 100644 --- a/src/color.c +++ b/src/color.c @@ -858,6 +858,18 @@ static int print_link_target(CFILE *cfile, const struct BFTW *ftwbuf) { return ret; } +/** Print an shell-escaped string. */ +static int print_wordesc(CFILE *cfile, const char *str) { + char *esc = wordesc(str); + if (!esc) { + return -1; + } + + int ret = dstrcat(&cfile->buffer, esc); + free(esc); + return ret; +} + /** Format some colored output to the buffer. */ BFS_FORMATTER(2, 3) static int cbuff(CFILE *cfile, const char *format, ...); @@ -994,6 +1006,12 @@ static int cvbuff(CFILE *cfile, const char *format, va_list args) { case 'p': switch (*++i) { + case 'q': + if (print_wordesc(cfile, va_arg(args, const char *)) != 0) { + return -1; + } + break; + case 'F': if (print_name(cfile, va_arg(args, const struct BFTW *)) != 0) { return -1; diff --git a/src/color.h b/src/color.h index 3db2b07..aca6a34 100644 --- a/src/color.h +++ b/src/color.h @@ -84,6 +84,7 @@ int cfclose(CFILE *cfile); * %s: A string * %zu: A size_t * %m: strerror(errno) + * %pq: A shell-escaped string, like bash's printf %q * %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 |