Skip to content

Instantly share code, notes, and snippets.

@jvoytech
Created January 22, 2019 22:13
Show Gist options
  • Save jvoytech/970bd6bffba6eb459c60afd674b3f1cb to your computer and use it in GitHub Desktop.
Save jvoytech/970bd6bffba6eb459c60afd674b3f1cb to your computer and use it in GitHub Desktop.
/*
How to print color text in terminal (Linux terminals, Putty, etc.)
"\e[31mI am red\e[0m" <- prints text in red
*/
#include <stdio.h>
#include <math.h>
const char* const ANSI_RED = "\033[31m";
const char* const ANSI_GREEN = "\033[32m";
const char* const ANSI_YELLOW = "\033[33m";
const char* const ANSI_RESET = "\033[0m";
int main() {
int test_freqs[] = { 0, 1000, 4499, 4500, 4999, 5000, 5001, 5500, 5501, 7000, -1};
const int freq = 5000;
const int margin = 500;
printf("Setup:\n\texpected frequency = %s%d%s\n\tmargin = %s%d%s\n\n",
ANSI_GREEN, freq, ANSI_RESET,
ANSI_GREEN, margin, ANSI_RESET);
int iter = 0;
const char* color = 0;
while (test_freqs[iter] != -1) {
if (test_freqs[iter] == freq)
color = ANSI_YELLOW;
else if (abs(test_freqs[iter] - freq) > margin)
color = ANSI_RED;
else
color = ANSI_GREEN;
printf("frequency = %s%d%s\n", color, test_freqs[iter++], ANSI_RESET);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment