From f30463e6d6d95e8d0d258458a1f1bda50ad911b0 Mon Sep 17 00:00:00 2001 From: Tom van Dijck Date: Mon, 19 Jun 2017 03:00:59 -0700 Subject: [PATCH] Attempt at colors on linux --- src/host/premake.c | 8 +++---- src/host/premake.h | 2 +- src/host/term_textColor.c | 47 ++++++++++++++++++++++++++++++--------- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/host/premake.c b/src/host/premake.c index d223c3cc7e..c6877f737d 100644 --- a/src/host/premake.c +++ b/src/host/premake.c @@ -223,9 +223,9 @@ int premake_execute(lua_State* L, int argc, const char** argv, const char* scrip /* Find and run the main Premake bootstrapping script */ if (run_premake_main(L, script) != OKAY) { int color = term_doGetTextColor(); - term_dosetTextColor(getErrorColor(L)); + term_doSetTextColor(getErrorColor(L)); printf(ERROR_MESSAGE, lua_tostring(L, -1)); - term_dosetTextColor(color); + term_doSetTextColor(color); return !OKAY; } @@ -242,9 +242,9 @@ int premake_execute(lua_State* L, int argc, const char** argv, const char* scrip lua_getglobal(L, "_premake_main"); if (lua_pcall(L, 0, 1, iErrFunc) != OKAY) { int color = term_doGetTextColor(); - term_dosetTextColor(getErrorColor(L)); + term_doSetTextColor(getErrorColor(L)); printf(ERROR_MESSAGE, lua_tostring(L, -1)); - term_dosetTextColor(color); + term_doSetTextColor(color); return !OKAY; } else { diff --git a/src/host/premake.h b/src/host/premake.h index 2a1854fcf4..dc90458626 100644 --- a/src/host/premake.h +++ b/src/host/premake.h @@ -88,7 +88,7 @@ int do_pathsearch(lua_State* L, const char* filename, const char* path); void do_translate(char* value, const char sep); int term_doGetTextColor(); -void term_dosetTextColor(int color); +void term_doSetTextColor(int color); /* Built-in functions */ int criteria_compile(lua_State* L); diff --git a/src/host/term_textColor.c b/src/host/term_textColor.c index fd867fccca..f31935c71e 100644 --- a/src/host/term_textColor.c +++ b/src/host/term_textColor.c @@ -6,6 +6,10 @@ #include "premake.h" +#if PLATFORM_POSIX +static int s_currentColor = -1; +#endif + int term_doGetTextColor() { #if PLATFORM_WINDOWS @@ -14,11 +18,11 @@ int term_doGetTextColor() return -1; return (int)info.wAttributes; #else - return -1; + return s_currentColor; #endif } -void term_dosetTextColor(int color) +void term_doSetTextColor(int color) { #if PLATFORM_WINDOWS if (color >= 0) @@ -27,7 +31,34 @@ void term_dosetTextColor(int color) SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), (WORD)color); } #else - (void)(color); /* warning: unused parameter */ + s_currentColor = color; + + const char* colorTable[] = + { + "\x1B[0;30m", // term.black = 0 + "\x1B[0;34m", // term.blue = 1 + "\x1B[0;32m", // term.green = 2 + "\x1B[0;36m", // term.cyan = 3 + "\x1B[0;31m", // term.red = 4 + "\x1B[0;35m", // term.purple = 5 + "\x1B[0;33m", // term.brown = 6 + "\x1B[0;37m", // term.lightGray = 7 + "\x1B[1;30m", // term.gray = 8 + "\x1B[1;34m", // term.lightBlue = 9 + "\x1B[1;32m", // term.lightGreen = 10 + "\x1B[1;36m", // term.lightCyan = 11 + "\x1B[1;31m", // term.lightRed = 12 + "\x1B[1;35m", // term.magenta = 13 + "\x1B[1;33m", // term.yellow = 14 + "\x1B[1;37m", // term.white = 15 + }; + if (color >= 0 && color < 16) + { + puts(colorTable[color]); + } else + { + puts("\x1B[0m"); + } #endif } @@ -35,17 +66,13 @@ void term_dosetTextColor(int color) int term_getTextColor(lua_State* L) { int color = term_doGetTextColor(); - if (color >= 0) - { - lua_pushinteger(L, color); - return 1; - } - return 0; + lua_pushinteger(L, color); + return 1; } int term_setTextColor(lua_State* L) { - term_dosetTextColor((int)luaL_optinteger(L, 1, -1)); + term_doSetTextColor((int)luaL_optinteger(L, 1, -1)); return 0; }