From 5e30515f147957ff6669bd2e00fd8c4a54fa1e02 Mon Sep 17 00:00:00 2001 From: "Felix C. Stegerman" Date: Thu, 18 Jun 2020 17:20:24 +0200 Subject: [PATCH 1/3] don't change color on empty lines --- main.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/main.cpp b/main.cpp index 14f030d..3a8dc46 100644 --- a/main.cpp +++ b/main.cpp @@ -283,16 +283,21 @@ void abortHandler(int signo) { } void catFile(FILE* fh) { - int c; + int c, prev = 0; while ((c = getc(fh)) >= 0) { putc(c, stdout); if (c == '\n') { - g_currentRow++; - if (g_currentRow == g_colorQueue.size()) { - g_currentRow = 0; + if (prev != '\n') { + g_currentRow++; + if (g_currentRow == g_colorQueue.size()) { + g_currentRow = 0; + } } setColor(g_colorQueue[g_currentRow]); } + if (c != '\r') { + prev = c; + } } } From d07c929baef18549a456bdc8c7aedf1b04b6ea16 Mon Sep 17 00:00:00 2001 From: "Felix C. Stegerman" Date: Fri, 19 Jun 2020 15:14:25 +0200 Subject: [PATCH 2/3] add --change-empty --- README.md | 3 +++ main.cpp | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f67473f..3a89957 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,9 @@ Colorize your terminal output with pride! ## Additional options ``` +-e,--change-empty + Change color on empty lines as well + -f,--force Force color even when stdout is not a tty diff --git a/main.cpp b/main.cpp index 3a8dc46..2c27ae7 100644 --- a/main.cpp +++ b/main.cpp @@ -124,6 +124,7 @@ std::map aliases = { std::vector g_colorQueue; std::vector g_filesToCat; unsigned int g_currentRow = 0; +bool g_changeEmpty = false; #if defined(_WIN32) bool g_useColors = _isatty(_fileno(stdout)); @@ -229,6 +230,8 @@ void parseCommandLine(int argc, char** argv) { } printf("Additional options:\n"); + printf(" -e,--change-empty\n"); + printf(" Change color on empty lines as well\n\n"); printf(" -f,--force\n"); printf(" Force color even when stdout is not a tty\n\n"); printf(" -t,--truecolor\n"); @@ -244,6 +247,9 @@ void parseCommandLine(int argc, char** argv) { printf(" pridecat --trans --bi Alternate between trans and bisexual pride flags.\n"); exit(0); } + else if (strEqual(argv[i], "-e") || strEqual(argv[i], "--change-empty")) { + g_changeEmpty = true; + } else if (strEqual(argv[i], "-f") || strEqual(argv[i], "--force")) { g_useColors = true; } @@ -287,7 +293,7 @@ void catFile(FILE* fh) { while ((c = getc(fh)) >= 0) { putc(c, stdout); if (c == '\n') { - if (prev != '\n') { + if (g_changeEmpty || prev != '\n') { g_currentRow++; if (g_currentRow == g_colorQueue.size()) { g_currentRow = 0; From 30a496bc05c7824ea31999c5d12f596083f1e952 Mon Sep 17 00:00:00 2001 From: "Felix C. Stegerman" Date: Fri, 19 Jun 2020 23:20:41 +0200 Subject: [PATCH 3/3] consider lines with only spaces and tabs empty --- main.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/main.cpp b/main.cpp index 30e4c5f..87c2d25 100644 --- a/main.cpp +++ b/main.cpp @@ -379,23 +379,25 @@ void abortHandler(int signo) { } void catFile(FILE* fh) { - int c, prev = 0; + int c; + bool empty = true; while ((c = getc(fh)) >= 0) { if (c == '\n') { resetColor(); } putc(c, stdout); if (c == '\n') { - if (g_changeEmpty || prev != '\n') { + if (g_changeEmpty || !empty) { g_currentRow++; if (g_currentRow == g_colorQueue.size()) { g_currentRow = 0; } } setColor(g_colorQueue[g_currentRow]); + empty = true; } - if (c != '\r') { - prev = c; + else if (c != ' ' && c != '\t' && c != '\r') { + empty = false; } } }