aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--colorize.15
-rw-r--r--colorize.c35
-rw-r--r--doc/colorize.html3
-rwxr-xr-xtest.pl9
4 files changed, 36 insertions, 16 deletions
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
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)
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
</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 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');