]> git.refcnt.org Git - colorize.git/commitdiff
Merge branch 'opt_omit_color_empty'
authorSteven Schubiger <stsc@refcnt.org>
Fri, 2 Feb 2018 12:41:35 +0000 (13:41 +0100)
committerSteven Schubiger <stsc@refcnt.org>
Fri, 2 Feb 2018 12:41:35 +0000 (13:41 +0100)
colorize.1
colorize.c
doc/colorize.html
test.pl

index 66d16782da09a2c8e63193f1259a80c80063c2d1..20e968b47056571c5620ee0128fef0aa396d488f 100644 (file)
@@ -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
index 012a376bcc760d7f83133d142e3f0e10fc7fc74a..ba5926a4f0503841afe6777d2d75455a64b4b615 100644 (file)
@@ -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)
index 5100c2f0a54c16487badc8121b5d8bc5dea9a190..c32c7622c40b2b91442a4141939c9a572ed1e1b2 100644 (file)
@@ -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
 </pre>
@@ -101,7 +102,7 @@ permitted by applicable law.
 </pre>
 <pre>
 <span style="color:yellow;font-weight:bold;">[sts@apollo ~/colorize]$</span> ./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: &quot;-ansi -pedantic &quot;
 Linker flags: &quot;&quot;
 Preprocessor flags: &quot;&quot;
diff --git a/test.pl b/test.pl
index 3ddec2d824ac5affbc9493894e07ea321066cb11..85e12b5d1f345bd64c6c43e6e938d672f37b75da 100755 (executable)
--- 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');