Le 18/02/2016 01:52, Stéphane Blondon a écrit : > This is a diff of crontab.c providing a color change for comment lines > for the `crontab -l` output. You can see a demo with the provided > screenshot attached to this message too.
With the previous patch, the colorized output creates a problem if: - a redirection (like `crontab -l > /tmp/crontab`) - or a pipe (like `crontab -l | cat`) are used because the escape characters are displayed. This new patch fixes that by checking what the output is. The provided patch is a diff against the original source code. The diff between the two provided diffs is only: < +++ crontab.c 2016-02-18 01:18:31.655518325 +0100 --- > +++ crontab.c 2016-02-29 20:54:07.801740970 +0100 38c38 < + if(ch == '#'){ --- > + if(ch == '#' && isatty(STDOUT)){ So this diff provides the same feature than the previous one and fixes a bug included in the previous one. -- Stéphane
--- crontab.c.orig 2016-02-18 01:12:36.253608497 +0100 +++ crontab.c 2016-02-29 20:54:07.801740970 +0100 @@ -33,6 +33,7 @@ #include <fcntl.h> #include <libgen.h> #include <signal.h> +#include <stdio.h> #include <sys/file.h> #include <sys/stat.h> #ifdef USE_UTIMES @@ -48,6 +49,10 @@ #define NHEADER_LINES 3 +#define COMMENT_COLOR "\x1B[34m" +#define RESET_COLOR "\033[0m" + + enum opt_t { opt_unknown, opt_list, opt_delete, opt_edit, opt_replace }; #if DEBUGGING @@ -302,6 +307,7 @@ char n[MAX_FNAME]; FILE *f; int ch; + int new_line = 1; #ifdef DEBIAN int x; char *ctnh; @@ -345,8 +351,17 @@ } } #endif - while (EOF != (ch = get_char(f))) - putchar(ch); + while (EOF != (ch = get_char(f))){ + if(new_line){ + if(ch == '#' && isatty(STDOUT)){ + printf(COMMENT_COLOR); + }else{ + printf(RESET_COLOR); + } + } + putchar(ch); + new_line = ch == '\n'; + } fclose(f); }
signature.asc
Description: OpenPGP digital signature