From bcd256396593896a9e2c287afead4bbfbd65a437 Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Sun, 28 Jan 2018 22:52:04 +0100 Subject: First draft of --omit-color-empty --- colorize.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/colorize.c b/colorize.c index 012a376..ba5926a 100644 --- a/colorize.c +++ b/colorize.c @@ -205,18 +205,20 @@ enum { OPT_CLEAN, OPT_CLEAN_ALL, OPT_EXCLUDE_RANDOM, + OPT_OMIT_COLOR_EMPTY, OPT_HELP, OPT_VERSION }; static int opt_type; static const struct option long_opts[] = { - { "attr", required_argument, &opt_type, OPT_ATTR }, - { "clean", no_argument, &opt_type, OPT_CLEAN }, - { "clean-all", no_argument, &opt_type, OPT_CLEAN_ALL }, - { "exclude-random", required_argument, &opt_type, OPT_EXCLUDE_RANDOM }, - { "help", no_argument, &opt_type, OPT_HELP }, - { "version", no_argument, &opt_type, OPT_VERSION }, - { NULL, 0, NULL, 0 }, + { "attr", required_argument, &opt_type, OPT_ATTR }, + { "clean", no_argument, &opt_type, OPT_CLEAN }, + { "clean-all", no_argument, &opt_type, OPT_CLEAN_ALL }, + { "exclude-random", required_argument, &opt_type, OPT_EXCLUDE_RANDOM }, + { "omit-color-empty", no_argument, &opt_type, OPT_OMIT_COLOR_EMPTY }, + { "help", no_argument, &opt_type, OPT_HELP }, + { "version", no_argument, &opt_type, OPT_VERSION }, + { NULL, 0, NULL, 0 }, }; enum attr_type { @@ -242,6 +244,7 @@ static void **vars_list; static bool clean; static bool clean_all; +static bool omit_color_empty; static char attr[MAX_ATTRIBUTE_CHARS + 1]; static char *exclude; @@ -267,7 +270,7 @@ static bool get_next_char (char *, const char **, FILE *, bool *); static void save_char (char, char **, size_t *, size_t *); static void find_color_entries (struct color_name **, const struct color **); static void find_color_entry (const struct color_name *, unsigned int, const struct color **); -static void print_line (const char *, const struct color **, const char * const, unsigned int); +static void print_line (const char *, const struct color **, const char * const, unsigned int, bool); static void print_clean (const char *); static bool is_esc (const char *); static const char *get_end_of_esc (const char *); @@ -413,6 +416,9 @@ process_opts (int argc, char **argv) vfprintf_fail (formats[FMT_GENERIC], "--exclude-random switch must be provided a plain color"); break; } + case OPT_OMIT_COLOR_EMPTY: + omit_color_empty = true; + break; case OPT_HELP: PRINT_HELP_EXIT (); case OPT_VERSION: @@ -888,6 +894,7 @@ read_print_stream (const char *attr, const struct color **colors, const char *fi line = buf; while ((eol = strpbrk (line, "\n\r"))) { + const bool has_text = (eol > line); const char *p; flags &= ~(CR|LF); if (*eol == '\r') @@ -902,13 +909,14 @@ read_print_stream (const char *attr, const struct color **colors, const char *fi vfprintf_fail (formats[FMT_FILE], file, "unrecognized line ending"); p = eol + SKIP_LINE_ENDINGS (flags); *eol = '\0'; - print_line (attr, colors, line, flags); + print_line (attr, colors, line, flags, + omit_color_empty ? has_text : true); line = p; } if (feof (stream)) { if (*line != '\0') - print_line (attr, colors, line, 0); + print_line (attr, colors, line, 0, true); } else if (*line != '\0') { @@ -916,7 +924,7 @@ read_print_stream (const char *attr, const struct color **colors, const char *fi if ((clean || clean_all) && (p = strrchr (line, '\033'))) merge_print_line (line, p, stream); else - print_line (attr, colors, line, 0); + print_line (attr, colors, line, 0, true); } } } @@ -1123,12 +1131,13 @@ find_color_entry (const struct color_name *color_name, unsigned int index, const } static void -print_line (const char *attr, const struct color **colors, const char *const line, unsigned int flags) +print_line (const char *attr, const struct color **colors, const char *const line, unsigned int flags, bool emit_colors) { /* --clean[-all] */ if (clean || clean_all) print_clean (line); - else + /* skip for --omit-color-empty? */ + else if (emit_colors) { /* Foreground color code is guaranteed to be set when background color code is present. */ if (colors[BACKGROUND] && colors[BACKGROUND]->code) -- cgit v1.2.3 From b03f1cd686c25a4b76c523ccc70c29398afddc47 Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Wed, 31 Jan 2018 12:36:20 +0100 Subject: Test --omit-color-empty --- test.pl | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test.pl b/test.pl index 3ddec2d..85e12b5 100755 --- a/test.pl +++ b/test.pl @@ -12,7 +12,7 @@ use Getopt::Long qw(:config no_auto_abbrev no_ignore_case); use Test::Harness qw(runtests); use Test::More; -my $tests = 29; +my $tests = 30; my $valgrind_cmd = ''; { @@ -143,6 +143,13 @@ SKIP: { is(system(qq(printf '%s\n' "hello world" | $valgrind_cmd$program random --exclude-random=black >/dev/null)), 0, 'switch exclude-random'); + { + my $infile = $write_to_tmpfile->("foo\n\nbar"); + is_deeply([split /\n/, qx($valgrind_cmd$program yellow --omit-color-empty $infile)], + [split /\n/, "\e[33mfoo\e[0m\n\n\e[33mbar\e[0m"], + 'switch omit-color-empty'); + } + SKIP: { skip 'valgrind not found', 1 unless system('which valgrind >/dev/null 2>&1') == 0; like(qx(valgrind $program none/none $infile1 2>&1 >/dev/null), qr/no leaks are possible/, 'valgrind memleaks'); -- cgit v1.2.3 From add8a17f880fc4f00aa204fd3ee1f7a47f61a15a Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Wed, 31 Jan 2018 20:31:28 +0100 Subject: Document --omit-color-empty --- colorize.1 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/colorize.1 b/colorize.1 index 66d1678..20e968b 100644 --- a/colorize.1 +++ b/colorize.1 @@ -1,4 +1,4 @@ -.TH COLORIZE 1 "2017-07-16" "colorize v0.63" "User Commands" +.TH COLORIZE 1 "2018-01-31" "colorize v0.63" "User Commands" .SH NAME colorize \- colorize text on terminal with ANSI escape sequences .SH SYNOPSIS @@ -43,6 +43,9 @@ clean text from all valid color escape sequences .BR \-\-exclude\-random=\fICOLOR\fR text color to be excluded when selecting a random foreground color .TP +.BR \-\-omit\-color\-empty +omit printing color escape sequences for empty lines +.TP .BR \-h ", " \-\-help show help screen and exit .TP -- cgit v1.2.3 From a9d54ff20482e80ea17deb5a431f1bbae3a91637 Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Thu, 1 Feb 2018 17:38:30 +0100 Subject: Update plain documentation --- doc/colorize.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/colorize.html b/doc/colorize.html index 5100c2f..c32c762 100644 --- a/doc/colorize.html +++ b/doc/colorize.html @@ -32,6 +32,7 @@ Usage: ./colorize (foreground) OR (foreground)/(background) OR --clean[-all] [-| --clean --clean-all --exclude-random=COLOR + --omit-color-empty -h, --help -V, --version @@ -101,7 +102,7 @@ permitted by applicable law.
 [sts@apollo ~/colorize]$ ./colorize --version
-colorize v0.62-10-g5be3a18 (compiled at Oct 14 2017, 13:07:07)
+colorize v0.63-5-gadd8a17 (compiled at Feb  1 2018, 17:05:52)
 Compiler flags: "-ansi -pedantic "
 Linker flags: ""
 Preprocessor flags: ""
-- 
cgit v1.2.3