Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt at colors on linux #821

Merged
merged 1 commit into from
Jun 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/host/premake.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/host/premake.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
47 changes: 37 additions & 10 deletions src/host/term_textColor.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

#include "premake.h"

#if PLATFORM_POSIX
static int s_currentColor = -1;
#endif

int term_doGetTextColor()
{
#if PLATFORM_WINDOWS
Expand All @@ -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)
Expand All @@ -27,25 +31,48 @@ 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
}


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;
}