]> git.refcnt.org Git - colorize.git/commitdiff
Don't increment rainbow color with partial line master
authorSteven Schubiger <stsc@refcnt.org>
Sat, 3 Aug 2024 21:00:20 +0000 (23:00 +0200)
committerSteven Schubiger <stsc@refcnt.org>
Sat, 3 Aug 2024 21:00:20 +0000 (23:00 +0200)
colorize.c
test.pl

index 3da2b6cb7fd055b823b1cb13f8d3067ec614fcb4..eb8f7c084c257789ec64e5453fb00d66e942cf56 100644 (file)
@@ -74,6 +74,7 @@
 
 #define LF 0x01
 #define CR 0x02
+#define PARTIAL 0x04
 
 #define COUNT_OF(obj, type) (sizeof (obj) / sizeof (type))
 
@@ -1376,13 +1377,15 @@ read_print_stream (const char *attr, const struct color **colors, const char *fi
         if (feof (stream))
           {
             if (*line != '\0')
-              print_line (attr, colors, line, 0, true);
+              print_line (attr, colors, line, PARTIAL, true);
           }
         else if (*line != '\0')
           {
             char *p;
             if ((clean || clean_all) && (p = strrchr (line, '\033')))
               merge_print_line (line, p, stream);
+            else if (rainbow_fg || rainbow_bg)
+              print_line (attr, colors, line, PARTIAL, true);
             else
               print_line (attr, colors, line, 0, true);
           }
@@ -1625,7 +1628,8 @@ print_line (const char *attr, const struct color **colors, const char *const lin
 
             colors[color_iter] = (struct color *)&tables[color_iter].entries[index];
 
-            rainbow_index = index + 1;
+            if (!(flags & PARTIAL))
+              rainbow_index = index + 1;
           }
 
         /* Foreground color code is guaranteed to be set when background color code is present.  */
diff --git a/test.pl b/test.pl
index 8294e8fb02b9118b3a512b447e985e2369312781..375df035c38eeb8e3a8bf2cd7101eba08867ea13 100755 (executable)
--- a/test.pl
+++ b/test.pl
@@ -13,7 +13,7 @@ use Getopt::Long qw(:config no_auto_abbrev no_ignore_case);
 use Test::Harness qw(runtests);
 use Test::More;
 
-my $tests = 36;
+my $tests = 38;
 
 my $valgrind_cmd = '';
 {
@@ -135,6 +135,23 @@ SKIP: {
 
     is_deeply([split /\n/, qx(cat $infile2 | $valgrind_cmd$program none/none)], [split /\n/, $repeated], "read ${\length $repeated} bytes (BUF_SIZE=$BUF_SIZE{normal})");
 
+    SKIP: {
+        my $program_buf = tmpnam();
+        skip 'compiling failed (short buffer)', 2 unless system("$compiler -DTEST -DBUF_SIZE=$BUF_SIZE{short} -o $program_buf $source") == 0;
+
+        my $short_text = 'foo bar baz' x 2;
+
+        is(qx(printf %s "$short_text" | $valgrind_cmd$program_buf blue --rainbow-fg),
+          "\e[34mfoo bar ba\e[0m\e[34mzfoo bar b\e[0m\e[34maz\e[0m",
+          "partial line (BUF_SIZE=$BUF_SIZE{short}, rainbow-fg)");
+
+        is(qx(printf %s "$short_text" | $valgrind_cmd$program_buf blue/black --rainbow-bg),
+          "\e[40m\e[34mfoo bar ba\e[0m\e[40m\e[34mzfoo bar b\e[0m\e[40m\e[34maz\e[0m",
+          "partial line (BUF_SIZE=$BUF_SIZE{short}, rainbow-bg)");
+
+        unlink $program_buf;
+    }
+
     {
         my $colored_text = qx(printf '%s\n' "foo bar baz" | $valgrind_cmd$program red);
         my $sequences = 0;