2015-07-01 08:12:04 -07:00
|
|
|
/*
|
|
|
|
* libpipecolors: linux color code library
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Eric Wheeler <eric@ericwheeler.net>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2015-07-02 12:52:49 -07:00
|
|
|
|
2015-07-01 07:44:53 -07:00
|
|
|
#include <iostream>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
2015-07-01 15:17:12 -07:00
|
|
|
#include <unistd.h>
|
2015-07-01 07:44:53 -07:00
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include <regex>
|
2015-07-02 20:20:18 -07:00
|
|
|
//#include "libpipecolors.h"
|
2015-07-01 07:44:53 -07:00
|
|
|
|
2015-07-02 12:52:49 -07:00
|
|
|
namespace pipecolors {
|
|
|
|
|
2015-07-01 07:44:53 -07:00
|
|
|
#define C_DEFAULT 0
|
|
|
|
#define C_BOLD 1
|
|
|
|
#define C_ITALIC 3
|
|
|
|
#define C_UNDERLINE 4
|
|
|
|
#define C_INVERT 7
|
|
|
|
|
2015-07-02 12:52:49 -07:00
|
|
|
#define C_B0 "\x1b[1m"
|
|
|
|
#define C_B1 "\x1b[0m"
|
|
|
|
|
|
|
|
#define C_FG_BLACK "\x1b[0;30m"
|
|
|
|
#define C_FG_RED "\x1b[0;31m"
|
|
|
|
#define C_FG_GREEN "\x1b[0;32m"
|
|
|
|
#define C_FG_YELLOW "\x1b[0;33m"
|
|
|
|
#define C_FG_BLUE "\x1b[0;34m"
|
|
|
|
#define C_FG_MAGENTA "\x1b[0;35m"
|
|
|
|
#define C_FG_CYAN "\x1b[0;36m"
|
|
|
|
#define C_FG_GRAY "\x1b[0;37m"
|
|
|
|
#define C_FG_DEFAULT "\x1b[0;39m"
|
|
|
|
|
|
|
|
|
|
|
|
#define C_FG_GRAY_D "\x1b[0;90m"
|
|
|
|
#define C_FG_RED_L "\x1b[0;91m"
|
|
|
|
#define C_FG_GREEN_L "\x1b[0;92m"
|
|
|
|
#define C_FG_YELLOW_L "\x1b[0;93m"
|
|
|
|
#define C_FG_BLUE_L "\x1b[0;94m"
|
|
|
|
#define C_FG_MAGENTA_L "\x1b[0;95m"
|
|
|
|
#define C_FG_CYAN_L "\x1b[0;96m"
|
|
|
|
#define C_FG_WHITE "\x1b[0;97m"
|
2015-07-01 07:44:53 -07:00
|
|
|
|
|
|
|
#define C_BG_NONE ""
|
2015-07-02 12:52:49 -07:00
|
|
|
#define C_BG_RED "\x1b[1;41m"
|
|
|
|
#define C_BG_GREEN "\x1b[1;42m"
|
|
|
|
#define C_BG_BLUE "\x1b[1;44m"
|
|
|
|
#define C_BG_DEFAULT "\x1b[1;49m"
|
2015-07-01 07:44:53 -07:00
|
|
|
|
|
|
|
std::map<std::string, std::string> getColors() {
|
|
|
|
std::map<std::string, std::string> colors;
|
|
|
|
colors["|00"] = C_FG_BLACK;
|
|
|
|
colors["|01"] = C_FG_BLUE;
|
|
|
|
colors["|02"] = C_FG_GREEN;
|
|
|
|
colors["|03"] = C_FG_CYAN;
|
|
|
|
colors["|04"] = C_FG_RED;
|
|
|
|
colors["|05"] = C_FG_MAGENTA;
|
|
|
|
colors["|06"] = C_FG_YELLOW;
|
|
|
|
colors["|07"] = C_FG_GRAY;
|
|
|
|
colors["|08"] = C_FG_GRAY_D;
|
|
|
|
colors["|09"] = C_FG_BLUE_L;
|
|
|
|
colors["|10"] = C_FG_GREEN_L;
|
|
|
|
colors["|11"] = C_FG_CYAN_L;
|
|
|
|
colors["|12"] = C_FG_RED_L;
|
|
|
|
colors["|13"] = C_FG_MAGENTA_L;
|
|
|
|
colors["|14"] = C_FG_YELLOW_L;
|
|
|
|
colors["|15"] = C_FG_WHITE;
|
|
|
|
colors["|16"] = C_BG_DEFAULT; // C_BG_BLACK
|
|
|
|
colors["|17"] = C_BG_BLUE;
|
|
|
|
colors["|18"] = C_BG_GREEN;
|
|
|
|
colors["|19"] = C_BG_BLUE; // C_BG_CYAN
|
|
|
|
colors["|20"] = C_BG_RED;
|
|
|
|
colors["|21"] = C_BG_RED; // C_BG_MAGENTA
|
|
|
|
colors["|22"] = C_FG_WHITE; //C_BG_BROWN
|
|
|
|
colors["|23"] = C_BG_DEFAULT; // C_BG_WHITE
|
2015-07-02 12:52:49 -07:00
|
|
|
colors["|30"] = C_B0; //Bold OFF
|
|
|
|
colors["|31"] = C_B1; // Bold ON
|
2015-07-01 07:44:53 -07:00
|
|
|
return colors;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool has_colors(void) {
|
2015-07-02 12:52:49 -07:00
|
|
|
return isatty(fileno(stdout));
|
2015-07-01 07:44:53 -07:00
|
|
|
}
|
|
|
|
|
2015-07-02 12:52:49 -07:00
|
|
|
void pcprintf( const char * fmt, ... ) {
|
2015-07-01 07:44:53 -07:00
|
|
|
|
2015-07-02 20:20:18 -07:00
|
|
|
char *buffer;
|
2015-07-01 07:44:53 -07:00
|
|
|
std::map<std::string, std::string> colors;
|
2015-07-02 20:20:18 -07:00
|
|
|
std::size_t index;
|
|
|
|
std::smatch matches;
|
2015-07-01 07:44:53 -07:00
|
|
|
|
|
|
|
va_list args;
|
2015-07-02 12:52:49 -07:00
|
|
|
va_start(args, fmt);
|
2015-07-02 20:20:18 -07:00
|
|
|
|
|
|
|
if(int size = vasprintf(&buffer, fmt, args) == -1) {
|
|
|
|
free(buffer);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-07-01 07:44:53 -07:00
|
|
|
std::string text(buffer), s(buffer);
|
|
|
|
va_end(args);
|
2015-07-02 20:20:18 -07:00
|
|
|
free(buffer);
|
2015-07-01 07:44:53 -07:00
|
|
|
|
|
|
|
std::regex reg ("(\\|\\d\\d)", std::regex_constants::ECMAScript | std::regex_constants::nosubs);
|
|
|
|
|
|
|
|
colors = getColors();
|
|
|
|
while (std::regex_search (text,matches,reg)) {
|
|
|
|
for (auto x:matches) {
|
|
|
|
while ((index = s.find(x)) != std::string::npos)
|
|
|
|
s.replace(index, x.length(), colors[x]);
|
|
|
|
}
|
|
|
|
text = matches.suffix().str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << s;
|
|
|
|
|
|
|
|
}
|
2015-07-02 12:52:49 -07:00
|
|
|
}
|