libpipecolors/libpipecolors.cpp

159 lines
4.4 KiB
C++
Raw Normal View History

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-07 15:09:36 -07:00
#ifndef PC_REMOVE_INVALID
#define PC_REMOVE_INVALID false
#endif
2015-07-03 00:56:32 -07:00
#include <cstdio>
2015-07-01 07:44:53 -07:00
#include <iostream>
#include <string>
2015-07-03 00:56:32 -07:00
#include <stdarg.h>
2015-07-01 07:44:53 -07:00
#include <map>
2015-07-03 00:56:32 -07:00
#include <boost/regex.hpp>
#include "pipecolors.h"
2015-07-02 12:52:49 -07:00
2015-07-03 00:56:32 -07:00
namespace pipecolors {
2015-07-01 07:44:53 -07:00
std::map<std::string, std::string> colors;
2015-07-03 00:56:32 -07:00
std::map<std::string, std::string> getColors() {
std::map<std::string, std::string> colors;
colors["|00"] = "\x1b[0;30m"; // FG_BLACK;
colors["|01"] = "\x1b[0;34m"; // FG_BLUE;
colors["|02"] = "\x1b[0;32m"; // FG_GREEN;
colors["|03"] = "\x1b[0;36m"; // FG_CYAN;
colors["|04"] = "\x1b[0;31m"; // FG_RED;
colors["|05"] = "\x1b[0;35m"; // FG_MAGENTA;
colors["|06"] = "\x1b[0;33m"; // FG_YELLOW;
colors["|07"] = "\x1b[0;37m"; // FG_GRAY;
colors["|08"] = "\x1b[0;90m"; // FG_GRAY_D;
colors["|09"] = "\x1b[0;94m"; // FG_BLUE_L;
colors["|10"] = "\x1b[0;92m"; // FG_GREEN_L;
colors["|11"] = "\x1b[0;96m"; // FG_CYAN_L;
colors["|12"] = "\x1b[0;91m"; // FG_RED_L;
colors["|13"] = "\x1b[0;95m"; // FG_MAGENTA_L;
colors["|14"] = "\x1b[0;93m"; // FG_YELLOW_L;
colors["|15"] = "\x1b[0;97m"; // FG_WHITE;
colors["|16"] = "\x1b[1;40m"; // BG_DEFAULT;
colors["|17"] = "\x1b[1;44m"; // BG_BLUE;
colors["|18"] = "\x1b[1;42m"; // BG_GREEN;
colors["|19"] = "\x1b[1;46m"; // BG_CYAN;
colors["|20"] = "\x1b[1;41m"; // BG_RED;
colors["|21"] = "\x1b[1;45m"; // BG_MAGENTA;
colors["|22"] = "\x1b[1;43m"; // BG_YELLOW;
colors["|23"] = "\x1b[1;47m"; // BG_WHITE;
colors["|30"] = "\x1b[1m"; // Bold ON
colors["|31"] = "\x1b[0m"; // Bold OFF
colors["|39"] = "\x1b[0;39m"; // FG_DEFAULT
return colors;
}
bool has_colors(void) {
return isatty(fileno(stdout));
}
2015-07-01 07:44:53 -07:00
std::pair<std::string,int> replace_colors( std::string s) {
2015-07-06 15:29:02 -07:00
using namespace boost;
regex re( "(\\|\\d\\d)" );
match_results<std::string::const_iterator> match;
match_flag_type flags = boost::match_default;
std::string::const_iterator start, end;
start = s.begin();
end = s.end();
std::string len(s);
2015-07-14 04:12:59 -07:00
2015-07-06 15:29:02 -07:00
colors = getColors();
while(regex_search(start, end, match, re, flags))
{
2015-07-07 15:09:36 -07:00
if(colors[match[0]].empty() && PC_REMOVE_INVALID == false) goto skip;
2015-07-06 15:33:30 -07:00
len.erase(len.find(match[0]), match[0].length());
2015-07-07 15:09:36 -07:00
if(has_colors() && !colors[match[0]].empty()) {
s.replace(s.find(match[0]), match[0].length(), colors[match[0]]);
} else {
s.erase(s.find(match[0]), match[0].length());
}
2015-07-06 15:29:02 -07:00
2015-07-07 15:09:36 -07:00
skip:;
2015-07-06 15:29:02 -07:00
start = match[0].second;
2015-07-07 15:09:36 -07:00
flags |= boost::match_prev_avail | boost::match_not_bob;
2015-07-06 15:29:02 -07:00
}
2015-07-13 18:03:01 -07:00
2015-07-14 04:12:59 -07:00
return std::make_pair(s, len.length());
2015-07-14 08:21:05 -07:00
2015-07-06 15:29:02 -07:00
}
2015-07-14 08:21:05 -07:00
int pcprintf( const char * format, ...)
2015-07-03 00:56:32 -07:00
{
char * buffer;
va_list args;
int ret;
2015-07-06 15:29:02 -07:00
2015-07-14 08:21:05 -07:00
va_start(args, format);
ret = vasprintf(&buffer, format, args);
2015-07-03 00:56:32 -07:00
va_end(args);
if(ret == -1) {
free(buffer);
exit(EXIT_FAILURE);
}
2015-07-01 07:44:53 -07:00
2015-07-06 15:29:02 -07:00
std::string s(buffer);
2015-07-03 00:56:32 -07:00
free(buffer);
2015-07-02 20:20:18 -07:00
std::pair<std::string, int> result = replace_colors(s);
std::cout << result.first;
2015-07-01 07:44:53 -07:00
return(result.second);
2015-07-01 07:44:53 -07:00
}
2015-07-14 08:21:05 -07:00
int pcsprintf( char * str, const char * format, ... ) {
2015-07-14 04:12:59 -07:00
int ret;
2015-07-14 08:21:05 -07:00
std::cout << sizeof(str);
char** str1 = (char**)str;
2015-07-14 04:12:59 -07:00
va_list args;
2015-07-14 08:21:05 -07:00
va_start(args, format);
ret = vasprintf(str1, format, args);
va_end(args);
2015-07-14 04:12:59 -07:00
if(ret == -1) {
2015-07-14 08:21:05 -07:00
free(str1);
2015-07-14 04:12:59 -07:00
exit(EXIT_FAILURE);
}
2015-07-14 08:21:05 -07:00
std::string s(*str1);
free(*str1);
std::pair<std::string, int> result = replace_colors(s);
const char * s2 = (const char *)result.first.c_str();
strcpy(str, s2);
return result.second;
2015-07-13 18:03:01 -07:00
}
2015-07-03 00:56:32 -07:00
} // namespace