Skip to content

Commit 81da07b

Browse files
committed
respect user NO_COLOR preference in localscore
1 parent f4d2640 commit 81da07b

File tree

3 files changed

+57
-20
lines changed

3 files changed

+57
-20
lines changed

localscore/localscore.cpp

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "cmd.h"
3535
#include "benchmark.h"
3636
#include "printer.h"
37+
#include "utils.h"
3738

3839
#include "localscore.h"
3940

@@ -338,20 +339,41 @@ static LocalScoreResultsSummary getResultsSummary(Json data) {
338339

339340
static void displayResults(LocalScoreResultsSummary results_summary, bool plaintext) {
340341

341-
printf("\n\033[1;35m");
342+
printf("\n%s", utils::color_str("\033[1;35m"));
342343
if (!plaintext) {
343344
ascii_display::print_logo();
344345
printf("\n");
345346
ascii_display::printLargeNumber((int)results_summary.performance_score);
346347
} else {
347348
printf("LocalScore: \t\t %d", (int)results_summary.performance_score);
348349
}
349-
printf("\033[0m\n");
350-
351-
printf("\033[32mToken Generation: \t \033[1;32m%.2f\033[0m \033[3;32mtok/s\033[0m\n", results_summary.avg_gen_tps);
352-
printf("\033[36mPrompt Processing: \t \033[1;36m%.2f\033[0m \033[3;36mtok/s\033[0m\n", results_summary.avg_prompt_tps);
353-
printf("\033[33mTime to First Token:\t \033[1;33m%.2f\033[0m \033[3;33mms\033[0m\n", results_summary.avg_ttft_ms);
354-
printf("\033[0m");
350+
printf("%s\n", utils::color_str("\033[0m"));
351+
352+
printf("%sToken Generation: \t %s%.2f%s %stok/s%s\n",
353+
utils::color_str("\033[32m"),
354+
utils::color_str("\033[1;32m"),
355+
results_summary.avg_gen_tps,
356+
utils::color_str("\033[0m"),
357+
utils::color_str("\033[3;32m"),
358+
utils::color_str("\033[0m"));
359+
360+
printf("%sPrompt Processing: \t %s%.2f%s %stok/s%s\n",
361+
utils::color_str("\033[36m"),
362+
utils::color_str("\033[1;36m"),
363+
results_summary.avg_prompt_tps,
364+
utils::color_str("\033[0m"),
365+
utils::color_str("\033[3;36m"),
366+
utils::color_str("\033[0m"));
367+
368+
printf("%sTime to First Token:\t %s%.2f%s %sms%s\n",
369+
utils::color_str("\033[33m"),
370+
utils::color_str("\033[1;33m"),
371+
results_summary.avg_ttft_ms,
372+
utils::color_str("\033[0m"),
373+
utils::color_str("\033[3;33m"),
374+
utils::color_str("\033[0m"));
375+
376+
printf("%s", utils::color_str("\033[0m"));
355377
}
356378

357379
struct SystemData {

localscore/system.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,12 @@ void get_runtime_info(RuntimeInfo* info) {
158158
strncpy(info->llamafile_version, LLAMAFILE_VERSION_STRING, MAX_STRING_LENGTH - 1);
159159
strncpy(info->llama_commit, LLAMA_COMMIT, MAX_STRING_LENGTH - 1);
160160

161-
fprintf(stderr, "\033[0;35m\n"); // Sets purple color
162-
utils::print_centered(stderr, 70, '=', "\033[1mLocalScore Runtime Information\033[0;35m");
161+
fprintf(stderr, "%s\n", utils::color_str("\033[0;35m")); // Sets purple color
162+
utils::print_centered(stderr, 70, '=', "%sLocalScore Runtime Information%s", utils::color_str("\033[1m"), utils::color_str("\033[0;35m"));
163163
fprintf(stderr, "\n");
164-
fprintf(stderr, "%-20s \033[1m%s\033[22m\n", "llamafile version:", info->llamafile_version);
164+
fprintf(stderr, "%-20s %s%s%s\n", "llamafile version:", utils::color_str("\033[1m"), info->llamafile_version, utils::color_str("\033[22m"));
165165
fprintf(stderr, "%-20s %s\n", "llama.cpp commit:", info->llama_commit);
166-
fprintf(stderr, "\n======================================================================\n\n\033[0m");
166+
fprintf(stderr, "\n======================================================================\n\n%s", utils::color_str("\033[0m"));
167167
}
168168

169169
double get_mem_gb() {
@@ -194,7 +194,7 @@ void get_sys_info(SystemInfo* info) {
194194

195195
info->ram_gb = get_mem_gb();
196196

197-
utils::print_centered(stderr, 70, '=', "\033[1mSystem Information\033[0m");
197+
utils::print_centered(stderr, 70, '=', "%sSystem Information%s", utils::color_str("\033[1m"), utils::color_str("\033[0m"));
198198
fprintf(stderr, "\n");
199199
fprintf(stderr, "%-20s %s\n", "Kernel Type:", info->kernel_type);
200200
fprintf(stderr, "%-20s %s\n", "Kernel Release:", info->kernel_release);
@@ -232,11 +232,11 @@ void get_accelerator_info(AcceleratorInfo* info, cmd_params * params) {
232232
}
233233

234234
if (i == params->main_gpu) {
235-
fprintf(stderr, "\033[0;32m"); // Sets green color
236-
utils::print_centered(stderr, 70, '=', "\033[1mActive GPU (GPU %d) Information\033[0;32m", i);
235+
fprintf(stderr, "%s", utils::color_str("\033[0;32m")); // Sets green color
236+
utils::print_centered(stderr, 70, '=', "%sActive GPU (GPU %d) Information%s", utils::color_str("\033[1m"), i, utils::color_str("\033[0;32m"));
237237
fprintf(stderr, "\n");
238238
} else {
239-
fprintf(stderr, "\033[0;90m"); // Sets gray color
239+
fprintf(stderr, "%s", utils::color_str("\033[0;90m")); // Sets gray color
240240
utils::print_centered(stderr, 70, '=', "GPU %d Information", i);
241241
fprintf(stderr, "\n");
242242
}
@@ -245,7 +245,7 @@ void get_accelerator_info(AcceleratorInfo* info, cmd_params * params) {
245245
fprintf(stderr, "%-26s %.1f GiB\n", "VRAM:", rounded_memory_gb);
246246
fprintf(stderr, "%-26s %d\n", "Streaming Multiprocessors:", props.multiProcessorCount);
247247
fprintf(stderr, "%-26s %.1f\n", "CUDA Capability:", atof(props.compute));
248-
fprintf(stderr, "\n======================================================================\n\n\033[0m");
248+
fprintf(stderr, "\n======================================================================\n\n%s", utils::color_str("\033[0m"));
249249
}
250250
}
251251

@@ -276,14 +276,14 @@ void get_accelerator_info(AcceleratorInfo* info, cmd_params * params) {
276276
strncpy(info->manufacturer, "Apple", MAX_STRING_LENGTH - 1);
277277

278278

279-
fprintf(stderr, "\033[0;32m===== GPU information =====\n\n");
279+
fprintf(stderr, "%s===== GPU information =====\n\n", utils::color_str("\033[0;32m"));
280280
fprintf(stderr, "%-26s %s\n", "GPU Name:", props.name);
281281
fprintf(stderr, "%-26s %.1f GiB\n", "VRAM:", info->total_memory_gb);
282282
fprintf(stderr, "%-26s %d\n", "Core Count:", props.core_count);
283283
fprintf(stderr, "%-26s %d\n", "Metal Version:", props.metal_version);
284284
fprintf(stderr, "%-26s %d\n", "GPU Family:", props.gpu_family);
285285
fprintf(stderr, "%-26s %d\n", "Common GPU Family:", props.gpu_family_common);
286-
fprintf(stderr, "\n======================================================================\n\n\033[0m");
286+
fprintf(stderr, "\n======================================================================\n\n%s", utils::color_str("\033[0m"));
287287
}
288288
} else {
289289
#ifdef __x86_64__
@@ -306,7 +306,7 @@ void list_available_accelerators() {
306306
fprintf(stderr, "Apple Metal\n");
307307
} else if (llamafile_has_cuda()) {
308308
int count = ggml_backend_cuda_get_device_count();
309-
fprintf(stderr, "\n\033[0;32m==================== Available GPUs ====================\n\n");
309+
fprintf(stderr, "\n%s==================== Available GPUs ====================\n\n", utils::color_str("\033[0;32m"));
310310
for (int i = 0; i < count; i++) {
311311
struct ggml_cuda_device_properties props;
312312
ggml_backend_cuda_get_device_properties(i, &props);
@@ -318,7 +318,7 @@ void list_available_accelerators() {
318318
} else {
319319
fprintf(stderr, "No Accelerator support available\n");
320320
}
321-
fprintf(stderr, "\n======================================================================\n\033[0m");
321+
fprintf(stderr, "\n======================================================================\n%s", utils::color_str("\033[0m"));
322322
}
323323

324324
void get_model_info(ModelInfo *info, llama_model *model) {

localscore/utils.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <memory>
1212
#include <array>
1313
#include <cstdio>
14+
#include <cstring>
1415
#include <stdexcept>
1516
#include <string.h>
1617
#include <stdarg.h>
@@ -185,6 +186,20 @@ namespace utils {
185186
return round(value * multiplier) / multiplier;
186187
}
187188

189+
inline bool should_use_color() {
190+
const char* no_color = getenv("NO_COLOR");
191+
if (no_color != NULL && no_color[0] != '\0') {
192+
return false;
193+
}
194+
return true;
195+
}
196+
197+
inline const char* color_str(const char* color_code) {
198+
if (should_use_color()) {
199+
return color_code;
200+
}
201+
return "";
202+
}
188203

189204
} // namespace utils
190205

0 commit comments

Comments
 (0)