Skip to content

Commit

Permalink
don't change color on blank lines
Browse files Browse the repository at this point in the history
optionally do, with -c --change-blank
consider lines with only spaces and tabs empty

Co-authored-by: Felix C. Stegerman <[email protected]>
  • Loading branch information
landfillbaby and obfusk committed Jun 22, 2020
1 parent 92396b1 commit 99c50d5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 13 additions & 6 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <cctype>
#include <cstdio>
#include <cstdint>
#include <cstdlib>
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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");
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 99c50d5

Please sign in to comment.