Skip to content

Commit 3b7a093

Browse files
committed
Name colors
It's more descriptive, use #define's so we can use compile-time concatenations. Signed-off-by: Eric Curtin <[email protected]>
1 parent 3d804de commit 3b7a093

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

common/common.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
#include <vector>
99
#include <sstream>
1010

11+
#define COL_DEFAULT "\033[0m"
12+
#define COL_BOLD "\033[1m"
13+
#define COL_RED "\033[31m"
14+
#define COL_GREEN "\033[32m"
15+
#define COL_YELLOW "\033[33m"
16+
#define COL_BLUE "\033[34m"
17+
#define COL_MAGENTA "\033[35m"
18+
#define COL_CYAN "\033[36m"
19+
#define COL_WHITE "\033[37m"
20+
1121
#ifdef _WIN32
1222
#define DIRECTORY_SEPARATOR '\\'
1323
#else

common/log.cpp

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,14 @@
88
#include <thread>
99
#include <vector>
1010

11+
#include "common.h"
12+
1113
int common_log_verbosity_thold = LOG_DEFAULT_LLAMA;
1214

1315
void common_log_set_verbosity_thold(int verbosity) {
1416
common_log_verbosity_thold = verbosity;
1517
}
1618

17-
#define LOG_COL_DEFAULT "\033[0m"
18-
#define LOG_COL_BOLD "\033[1m"
19-
#define LOG_COL_RED "\033[31m"
20-
#define LOG_COL_GREEN "\033[32m"
21-
#define LOG_COL_YELLOW "\033[33m"
22-
#define LOG_COL_BLUE "\033[34m"
23-
#define LOG_COL_MAGENTA "\033[35m"
24-
#define LOG_COL_CYAN "\033[36m"
25-
#define LOG_COL_WHITE "\033[37m"
26-
2719
static int64_t t_us() {
2820
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
2921
}
@@ -193,9 +185,9 @@ struct common_log {
193185
std::stringstream ss;
194186
for (int i = 0; fmt[i] != 0; i++) {
195187
if (fmt[i] == '%') {
196-
ss << LOG_COL_BOLD;
188+
ss << COL_BOLD;
197189
while (fmt[i] != ' ' && fmt[i] != ')' && fmt[i] != ']' && fmt[i] != 0) ss << fmt[i++];
198-
ss << LOG_COL_DEFAULT;
190+
ss << COL_DEFAULT;
199191
if (fmt[i] == 0) break;
200192
}
201193
ss << fmt[i];
@@ -320,15 +312,15 @@ struct common_log {
320312
pause();
321313

322314
if (colors) {
323-
g_col[COMMON_LOG_COL_DEFAULT] = LOG_COL_DEFAULT;
324-
g_col[COMMON_LOG_COL_BOLD] = LOG_COL_BOLD;
325-
g_col[COMMON_LOG_COL_RED] = LOG_COL_RED;
326-
g_col[COMMON_LOG_COL_GREEN] = LOG_COL_GREEN;
327-
g_col[COMMON_LOG_COL_YELLOW] = LOG_COL_YELLOW;
328-
g_col[COMMON_LOG_COL_BLUE] = LOG_COL_BLUE;
329-
g_col[COMMON_LOG_COL_MAGENTA] = LOG_COL_MAGENTA;
330-
g_col[COMMON_LOG_COL_CYAN] = LOG_COL_CYAN;
331-
g_col[COMMON_LOG_COL_WHITE] = LOG_COL_WHITE;
315+
g_col[COMMON_LOG_COL_DEFAULT] = COL_DEFAULT;
316+
g_col[COMMON_LOG_COL_BOLD] = COL_BOLD;
317+
g_col[COMMON_LOG_COL_RED] = COL_RED;
318+
g_col[COMMON_LOG_COL_GREEN] = COL_GREEN;
319+
g_col[COMMON_LOG_COL_YELLOW] = COL_YELLOW;
320+
g_col[COMMON_LOG_COL_BLUE] = COL_BLUE;
321+
g_col[COMMON_LOG_COL_MAGENTA] = COL_MAGENTA;
322+
g_col[COMMON_LOG_COL_CYAN] = COL_CYAN;
323+
g_col[COMMON_LOG_COL_WHITE] = COL_WHITE;
332324
} else {
333325
for (size_t i = 0; i < g_col.size(); i++) {
334326
g_col[i] = "";

examples/run/run.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(_WIN32)
3434
[[noreturn]] static void sigint_handler(int) {
35-
printf("\n\033[0m");
35+
printf("\n" COL_DEFAULT);
3636
exit(0); // not ideal, but it's the only way to guarantee exit in all cases
3737
}
3838
#endif
@@ -847,7 +847,7 @@ static int check_context_size(const llama_context_ptr & ctx, const llama_batch &
847847
const int n_ctx = llama_n_ctx(ctx.get());
848848
const int n_ctx_used = llama_get_kv_cache_used_cells(ctx.get());
849849
if (n_ctx_used + batch.n_tokens > n_ctx) {
850-
printf("\033[0m\n");
850+
printf(COL_DEFAULT "\n");
851851
printe("context size exceeded\n");
852852
return 1;
853853
}
@@ -910,7 +910,7 @@ static int generate(LlamaData & llama_data, const std::string & prompt, std::str
910910
batch = llama_batch_get_one(&new_token_id, 1);
911911
}
912912

913-
printf("\033[0m");
913+
printf(COL_DEFAULT);
914914
return 0;
915915
}
916916

@@ -919,7 +919,7 @@ static int read_user_input(std::string & user_input) {
919919
#ifdef WIN32
920920
printf(
921921
"\r%*s"
922-
"\r\033[0m%s",
922+
"\r" COL_DEFAULT "%s",
923923
get_terminal_width(), " ", prompt_prefix);
924924

925925
std::getline(std::cin, user_input);
@@ -956,7 +956,7 @@ static int generate_response(LlamaData & llama_data, const std::string & prompt,
956956
const bool stdout_a_terminal) {
957957
// Set response color
958958
if (stdout_a_terminal) {
959-
printf("\033[33m");
959+
printf(COL_YELLOW);
960960
}
961961

962962
if (generate(llama_data, prompt, response)) {
@@ -965,7 +965,7 @@ static int generate_response(LlamaData & llama_data, const std::string & prompt,
965965
}
966966

967967
// End response with color reset and newline
968-
printf("\n%s", stdout_a_terminal ? "\033[0m" : "");
968+
printf("\n%s", stdout_a_terminal ? COL_DEFAULT : "");
969969
return 0;
970970
}
971971

0 commit comments

Comments
 (0)