diff --git a/README.md b/README.md index 444b8e5..4c38fd4 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,9 @@ Colorize your terminal output with pride! -b,--background Change the background color instead of the text color +-c,--change-blank + Change color on blank lines as well + -f,--force Force color even when stdout is not a tty diff --git a/main.cpp b/main.cpp index 944a9f6..d89ada1 100644 --- a/main.cpp +++ b/main.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -155,6 +156,8 @@ bool g_useColors = isatty(STDOUT_FILENO); bool g_trueColor = isTrueColorTerminal(); #endif bool g_setBackgroundColor = false; +bool g_changeBlank = false; +bool g_blankLine = true; bool strEqual(char const* a, char const* b) { @@ -306,6 +309,8 @@ void parseCommandLine(int argc, char** argv) { printf("Additional options:\n"); printf(" -b,--background\n"); printf(" Change the background color instead of the text color\n\n"); + printf(" -c,--change-blank\n"); + printf(" Change color on blank 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"); @@ -325,6 +330,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], "-c") || strEqual(argv[i], "--change-blank")) { + g_changeBlank = true; + } else if (strEqual(argv[i], "-f") || strEqual(argv[i], "--force")) { g_useColors = true; } @@ -377,15 +385,16 @@ void catFile(FILE* fh) { while ((c = getc(fh)) >= 0) { if (c == '\n') { resetColor(); + g_blankLine = true; } - putc(c, stdout); - if (c == '\n') { - g_currentRow++; + else if (g_blankLine && (g_changeBlank || !isspace(c))) { + setColor(g_colorQueue[g_currentRow++]); if (g_currentRow == g_colorQueue.size()) { g_currentRow = 0; } - setColor(g_colorQueue[g_currentRow]); + g_blankLine = false; } + putc(c, stdout); } } @@ -426,8 +435,6 @@ int main(int argc, char** argv) { pushFlag(allFlags.at("lgbt")); } - setColor(g_colorQueue[0]); - if (g_filesToCat.empty()) { catFile(stdin); } else {