Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't change color on empty lines #11

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

-e,--change-empty
Change color on empty lines as well

-f,--force
Force color even when stdout is not a tty

Expand Down
19 changes: 16 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ std::vector<color_t> g_colorQueue;
std::vector<std::string> g_filesToCat;
unsigned int g_currentRow = 0;
colorAdjust g_colorAdjustment = colorAdjust::none;
bool g_changeEmpty = false;

#if defined(_WIN32)
bool g_useColors = _isatty(_fileno(stdout));
Expand Down Expand Up @@ -306,6 +307,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(" -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");
Expand All @@ -325,6 +328,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;
}
Expand Down Expand Up @@ -374,17 +380,24 @@ void abortHandler(int signo) {

void catFile(FILE* fh) {
int c;
bool empty = true;
while ((c = getc(fh)) >= 0) {
if (c == '\n') {
resetColor();
}
putc(c, stdout);
if (c == '\n') {
g_currentRow++;
if (g_currentRow == g_colorQueue.size()) {
g_currentRow = 0;
if (g_changeEmpty || !empty) {
g_currentRow++;
if (g_currentRow == g_colorQueue.size()) {
g_currentRow = 0;
}
}
setColor(g_colorQueue[g_currentRow]);
empty = true;
}
else if (c != ' ' && c != '\t' && c != '\r') {
empty = false;
}
}
}
Expand Down