diff --git a/.gitignore b/.gitignore index a9f14b7..0e014a5 100644 --- a/.gitignore +++ b/.gitignore @@ -2,9 +2,6 @@ sites/*/*settings*.php # Ignore paths that contain generated content. -files/ -sites/*/files -sites/*/private # Ignore default text files .deps @@ -32,6 +29,8 @@ libpipecolors.so .libs/ libpipecolors.la libpipecolors.lo - +libpipecolors.pc +test/ +timestamp # Ignore backup files -*~ \ No newline at end of file +*~ diff --git a/Makefile.am b/Makefile.am index bb957ac..f4d9278 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,8 +1,3 @@ -man-hook: - gzip $(mandir)/man7/libpipecolors.7 - gzip $(mandir)/man3/pcprintf.3 - - pkgconfig_DATA = libpipecolors.pc AM_CXXFLAGS = -std=c++11 ACLOCAL_AMFLAGS = -I m4 -I config @@ -13,7 +8,7 @@ libtool: $(LIBTOOL_DEPS) lib_LTLIBRARIES = libpipecolors.la libpipecolors_la_SOURCES = libpipecolors.cpp -#libpipecolors_la_LIBADD = -ltermcap +libpipecolors_la_LIBADD = -lboost_regex libpipecolors_la_CFLAGS = -fPIC -DPIC -pthread libpipecolors_la_LDFLAGS = -module \ -release ${PIPECOLORS_VERSION} @@ -35,3 +30,4 @@ MAINTAINERCLEANFILES = \ -rf config \ -rf m4 \ configure \ + Makefile.in diff --git a/boost-pipecolors.cpp b/boost-pipecolors.cpp deleted file mode 100644 index 37a5a63..0000000 --- a/boost-pipecolors.cpp +++ /dev/null @@ -1,130 +0,0 @@ -/* - * libpipecolors: linux color code library - * - * Authors: - * Eric Wheeler - * - * 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 - */ - -#include -#include -#include -#include -#include -#include -#include -#include "pipecolors.h" - -namespace pipecolors { - -#define C_DEFAULT 0 -#define C_BOLD 1 -#define C_ITALIC 3 -#define C_UNDERLINE 4 -#define C_INVERT 7 - -#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" - -#define C_BG_NONE "" -#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" - - std::map getColors() { - std::map 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 - colors["|30"] = C_B0; //Bold OFF - colors["|31"] = C_B1; // Bold ON - return colors; -} - -bool has_colors(void) { - return isatty(fileno(stdout)); -} - -void pcprintf( const char * fmt, ... ) { - - char buffer[256]; - std::map colors; - - va_list args; - va_start(args, fmt); - vasprintf(buffer, fmt, args); - std::string text(buffer), s(buffer); - va_end(args); - std::size_t index; - - std::smatch matches; - 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; - -} -} diff --git a/configure.ac b/configure.ac index 7e336c2..8b402e3 100644 --- a/configure.ac +++ b/configure.ac @@ -41,7 +41,6 @@ AC_PROG_LIBTOOL AC_PROG_LN_S LT_INIT - AC_LANG_CPLUSPLUS AC_PREFIX_DEFAULT([/usr]) #AC_CHECK_LIB([pipecolors], [cprintf]) @@ -66,11 +65,12 @@ if test "${gcc_minor_vers}" -lt 9; then else AC_MSG_RESULT([${gcc_full_vers} ... acceptable]) fi -AC_HEADER_STDC -AC_CHECK_FUNCS([vprintf vsprintf vasprintf printf]) +#AC_HEADER_STDC + # Checks for libraries. -AC_CHECK_HEADERS([stdarg.h regex.h iostream map string unistd.h stdlib.h],[],[AC_MSG_ERROR[no]]) +AC_CHECK_HEADERS([stdarg.h stdlib.h unistd.h string iostream map boost/regex.hpp],[],[AC_MSG_ERROR([We couldn't find some of the headers.])]) +AC_CHECK_FUNCS([vprintf vsprintf vasprintf printf]) AC_CHECK_TYPE([size_t]) AC_CHECK_TYPES([std::string], [], [], [[ @@ -84,5 +84,5 @@ AC_ARG_ENABLE([the-force], AM_CONDITIONAL([ENABLE_THE_FORCE], [test "x$enable_the_force" = "xyes"]) AC_CONFIG_FILES([Makefile libpipecolors.pc:libpipecolors.pc.in],[],[APIVERSION=PIPECOLORS_VERSION]) -AC_CONFIG_COMMANDS([default],[[]],[[]]) +AC_CONFIG_COMMANDS([timestamp], [date >timestamp]) AC_OUTPUT diff --git a/examples/test.cpp b/examples/test.cpp deleted file mode 100644 index b2049c8..0000000 --- a/examples/test.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include -#include -#include - - -using namespace pipecolors; - -int main(void) { - - const char* str = "|0101|0202|0303|0404|0505|0606|0707|0808|0909|1010|1111|1212|1313|1414|1515|07"; - const char* name = "|01P|02i|03p|04e|05c|06ol|07o|08r|09s |10l|11i|12b|13r|14a|15r|14y |130|12.|111|07"; - - pcprintf("\n%s\n\n", name); - pcprintf("%s\n", str); - -} diff --git a/libpipecolors.cpp b/libpipecolors.cpp index bd284fc..0b749d6 100644 --- a/libpipecolors.cpp +++ b/libpipecolors.cpp @@ -18,119 +18,95 @@ * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ - +#include #include -#include -#include -#include #include +#include #include -#include -//#include "libpipecolors.h" +#include +#include "pipecolors.h" + +using namespace boost; namespace pipecolors { -#define C_DEFAULT 0 -#define C_BOLD 1 -#define C_ITALIC 3 -#define C_UNDERLINE 4 -#define C_INVERT 7 - -#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" - -#define C_BG_NONE "" -#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" - - std::map getColors() { std::map 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 - colors["|30"] = C_B0; //Bold OFF - colors["|31"] = C_B1; // Bold ON - return colors; -} -bool has_colors(void) { - return isatty(fileno(stdout)); -} - -void pcprintf( const char * fmt, ... ) { - - char *buffer; - std::map colors; - std::size_t index; - std::smatch matches; - - va_list args; - va_start(args, fmt); - - if(int size = vasprintf(&buffer, fmt, args) == -1) { - free(buffer); - exit(EXIT_FAILURE); + std::map getColors() { + std::map 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)); } - std::string text(buffer), s(buffer); - va_end(args); - free(buffer); - - 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]); + int pcprintf( const char * fmt, ...) + { + char * buffer; + va_list args; + int ret; + colors = getColors(); + va_start(args, fmt); + ret = vasprintf(&buffer, fmt, args); + va_end(args); + if(ret == -1) { + free(buffer); + exit(EXIT_FAILURE); } - text = matches.suffix().str(); + + + std::string s(buffer), result(buffer); + free(buffer); + regex re( "(\\|\\d\\d)" ); + std::size_t index; + std::string::const_iterator start, end; + start = s.begin(); + end = s.end(); + match_results match; + match_flag_type flags = boost::match_default; + while(regex_search(start, end, match, re, flags)) + { + while ((index = s.find(match[0])) != std::string::npos) + { + s.replace(index, match[0].length(), colors[match[0]]); + } + start = match[0].second; + // update flags: + flags |= boost::match_prev_avail; + flags |= boost::match_not_bob; + } + + std::cout << s; + + return(ret); + } - std::cout << s; - -} -} +} // namespace diff --git a/libpipecolors.pc.in b/libpipecolors.pc.in new file mode 100644 index 0000000..c141cf6 --- /dev/null +++ b/libpipecolors.pc.in @@ -0,0 +1,10 @@ +prefix=@prefix@ +exec_prefix=@exec_prefix@ +libdir=@libdir@ +includedir=@includedir@ + +Name: libpipecolors +Description: Uses old renegade pipe color codes to print colors to the terminal +Version: @PACKAGE_VERSION@ +Libs: -L${libdir} -lpipecolors +Cflags: -I${includedir}/pipecolors diff --git a/pipecolors.h b/pipecolors.h index 780b232..14b1f95 100644 --- a/pipecolors.h +++ b/pipecolors.h @@ -10,8 +10,8 @@ namespace pipecolors { extern "C" { #endif -void pcprintf(const char* fmt, ...); -void pcsprintf(char **strp, const char *fmt, va_list ap); +int pcprintf( const char * format, ... ); +//int pcsprintf(char **strp, const char *fmt, va_list ap); #ifdef __cplusplus }