From c58f5c34bfeb09af8cfd8f592de7d56a24f0c61f Mon Sep 17 00:00:00 2001 From: Christian Goeschel Ndjomouo Date: Wed, 24 Sep 2025 10:44:57 -0400 Subject: [PATCH] lib/strutils: add helper function for --annotation option This function helps evaluates the --annotation option argument and determines whether annotations are enabled or not. It is identical to the hyperlinkwanted() routine, and although that forms some form of redundancy, it is intentionally kept seperate for potential future changes. Signed-off-by: Christian Goeschel Ndjomouo --- include/strutils.h | 1 + lib/strutils.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/include/strutils.h b/include/strutils.h index e4518cdcf..d034f6b95 100644 --- a/include/strutils.h +++ b/include/strutils.h @@ -62,6 +62,7 @@ extern void strtotimespec_or_err(const char *str, struct timespec *ts, extern time_t strtotime_or_err(const char *str, const char *errmesg); extern bool hyperlinkwanted(const char *mode); +extern bool annotationwanted(const char *mode); extern int isdigit_strend(const char *str, const char **end); #define isdigit_string(_s) isdigit_strend(_s, NULL) diff --git a/lib/strutils.c b/lib/strutils.c index 1ecc0c398..e4dabf212 100644 --- a/lib/strutils.c +++ b/lib/strutils.c @@ -551,6 +551,20 @@ bool hyperlinkwanted(const char *mode) errx(EXIT_FAILURE, _("invalid argument of --hyperlink: %s"), mode); } +bool annotationwanted(const char *mode) +{ + if (mode && strcmp(mode, "never") == 0) + return false; + + if (mode && strcmp(mode, "always") == 0) + return true; + + if (!mode || strcmp(mode, "auto") == 0) + return isatty(STDOUT_FILENO) ? true : false; + + errx(EXIT_FAILURE, _("invalid argument of --annotation: %s"), mode); +} + /* * Converts stat->st_mode to ls(1)-like mode string. The size of "str" must * be 11 bytes. -- 2.47.3