Skip to content

Instantly share code, notes, and snippets.

@Komosa
Created September 18, 2018 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Komosa/60475885e44ccab04c852aec226bc215 to your computer and use it in GitHub Desktop.
Save Komosa/60475885e44ccab04c852aec226bc215 to your computer and use it in GitHub Desktop.
pass thru, but color some words and cut some lines
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
#define EACH(it, cont) for (auto &it: cont)
#define TermReset "\e[0m"
#define TermBlack "\e[0;30m"
#define TermRed "\e[0;31m"
#define TermGreen "\e[0;32m"
#define TermYellow "\e[0;33m"
#define TermBlue "\e[0;34m"
#define TermPurple "\e[0;35m"
#define TermBrown "\e[0;36m"
#define TermLightGray "\e[0;37m"
#define TermDarkGray "\e[0;90m"
#define TermPink "\e[0;91m"
#define TermLightGreen "\e[0;92m"
#define TermTan "\e[0;93m"
#define TermLightBlue "\e[0;94m"
#define TermCyan "\e[0;95m"
#define TermOrange "\e[0;96m"
#define TermWhite "\e[0;97m"
const char *letterToColor[256];
void init_colors() {
letterToColor['r'] = TermRed;
letterToColor['R'] = TermPink;
letterToColor['g'] = TermGreen;
letterToColor['G'] = TermLightGreen;
letterToColor['y'] = TermYellow;
letterToColor['Y'] = TermTan;
letterToColor['t'] = TermTan;
letterToColor['b'] = TermBlue;
letterToColor['B'] = TermLightBlue;
letterToColor['p'] = TermPurple;
letterToColor['m'] = TermPurple;
letterToColor[ 0 ] = TermCyan;
letterToColor['P'] = TermCyan;
letterToColor['c'] = TermCyan;
letterToColor['a'] = TermOrange;
letterToColor['o'] = TermOrange;
letterToColor['O'] = TermBrown;
letterToColor['A'] = TermBrown;
letterToColor['s'] = TermLightGray;
letterToColor['S'] = TermDarkGray;
}
bool starts_with(const string &s, const string &small) {
return s.size() >= small.size()
&& s.compare(0, small.size(), small) == 0;
}
int main(int argc, char **argv) {
init_colors();
vector<pair<string, string>> toColorize, toCut;
string colorTo = TermCyan;
for (int i = 1; i < argc; ++i) {
string arg = argv[i];
if (starts_with(arg, "-c")) {
colorTo = letterToColor[argv[i][2]];
} else if (starts_with(arg, "-x")) {
if (i + 2 >= argc) {
cerr << "too small number of args for -x\n";
return -1;
}
toCut.push_back(make_pair(argv[i+1], argv[i+2]));
i += 2;
} else {
toColorize.push_back(make_pair(arg, colorTo));
}
}
string line, cutUntil;
while (getline(cin, line)) {
if (line.empty()) {
cout << '\n';
continue;
}
if (!cutUntil.empty()) {
if (line.find(cutUntil) == string::npos) {
continue;
}
cout << "--- cut end ---\n";
cutUntil = "";
}
char colorAtBeg[32] = TermReset;
sscanf(line.c_str(), "%[\e[0;0-90-9m]", colorAtBeg);
string endBy = colorAtBeg;
EACH (k, toColorize) {
string replaceBy = k.second + k.first + endBy;
for (size_t i = 0; (i = line.find(k.first, i)) != string::npos; i += replaceBy.size()) {
line.replace(i, k.first.size(), replaceBy);
}
}
cout << line << '\n';
EACH (k, toCut) {
if (line.find(k.first) != string::npos) {
cutUntil = k.second;
cout << "--- cut, until '" << cutUntil << "' ---\n";
break;
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment