From 9a03447d6b7a8db1c4d7439e658d54d79a73b744 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Sun, 14 Mar 2021 20:13:29 +0100 Subject: [PATCH] :art: Add support for colors --- ast | 2 +- include/arg.h | 9 ++++++++- src/arg.c | 21 ++++++++++++++++++--- src/gwion.c | 7 +++++++ util | 2 +- 5 files changed, 35 insertions(+), 6 deletions(-) diff --git a/ast b/ast index c5ee17c5..22b03ebd 160000 --- a/ast +++ b/ast @@ -1 +1 @@ -Subproject commit c5ee17c59538709a7be39201461e7e1ba12acd37 +Subproject commit 22b03ebd504913226eea6f48d01d0ab23c19695f diff --git a/include/arg.h b/include/arg.h index 6076e76c..9a109d58 100644 --- a/include/arg.h +++ b/include/arg.h @@ -1,6 +1,12 @@ #ifndef __ARG #define __ARG +enum COLOR { + COLOR_NEVER, + COLOR_AUTO, + COLOR_ALWAYS, +} __attribute__((packed)); + typedef struct Arg_ { struct CArg arg; struct Map_ mod; @@ -9,7 +15,8 @@ typedef struct Arg_ { struct Vector_ config; struct SoundInfo_ *si; m_bool loop; - m_bool quit; + m_bool quit : 1; + enum COLOR color; } Arg; ANN void arg_release(Arg*); diff --git a/src/arg.c b/src/arg.c index 4161e1d6..5a808ae8 100644 --- a/src/arg.c +++ b/src/arg.c @@ -13,7 +13,7 @@ enum { CONFIG, PLUGIN, MODULE, - LOOP, PASS, STDIN, + LOOP, PASS, STDIN, COLOR, // sound options DRIVER, SRATE, NINPUT, NOUTPUT, // pp options @@ -56,6 +56,7 @@ ANN static void arg_init(Arg* arg) { vector_init(&arg->lib); vector_init(&arg->config); vector_add(&arg->lib, (vtype)plug_dir()); + arg->color = COLOR_AUTO; } ANN void arg_release(Arg* arg) { @@ -109,7 +110,7 @@ ANN2(1) static void module_arg(const Map map, const char *str) { static void setup_options(cmdapp_t* app, cmdopt_t* opt) { cmdapp_set(app, - 'c', "config", + 'C', "config", CMDOPT_TAKESARG, NULL, "parse a config file", &opt[CONFIG] ); @@ -138,6 +139,11 @@ static void setup_options(cmdapp_t* app, cmdopt_t* opt) { 0, NULL, "read from stdin", &opt[STDIN] ); + cmdapp_set(app, + 'c', "color", + CMDOPT_TAKESARG, NULL, + "set colored output (never/auto/always, defaults to auto)", &opt[STDIN] + ); // sound options cmdapp_set(app, 'd', "driver", @@ -249,7 +255,7 @@ static void myproc(void *data, cmdopt_t* option, const char* arg) { case 'm': module_arg(&_arg->mod, option->value); break; - case 'c': + case 'C': config_parse(arg_int, option->value); break; case 'l': @@ -261,6 +267,15 @@ static void myproc(void *data, cmdopt_t* option, const char* arg) { case '\0': vector_add(&_arg->add, (vtype)ARG_STDIN); break; + case 'c': + if(!strcmp(option->value, "never")) + _arg->color = COLOR_NEVER; + else if(!strcmp(option->value, "auto")) + _arg->color = COLOR_AUTO; + else if(!strcmp(option->value, "always")) + _arg->color = COLOR_ALWAYS; + // ignore error silently + break; // sound options case 's': _arg->si->sr = (uint32_t)ARG2INT(option->value); diff --git a/src/gwion.c b/src/gwion.c index a360c819..f7b43923 100644 --- a/src/gwion.c +++ b/src/gwion.c @@ -2,6 +2,7 @@ #include #include #endif +#include #include "gwion_util.h" #include "gwion_ast.h" #include "gwion_env.h" @@ -92,6 +93,12 @@ ANN m_bool gwion_ini(const Gwion gwion, Arg* arg) { pass_default(gwion); arg->si = gwion->vm->bbq->si = new_soundinfo(gwion->mp); CHECK_BB(arg_parse(gwion, arg)) + if(arg->color == COLOR_NEVER) + tcol_override_color_checks(0); + else if(arg->color == COLOR_AUTO) + tcol_override_color_checks(isatty(1)); + else if(arg->color == COLOR_ALWAYS) + tcol_override_color_checks(1); return !arg->quit ? gwion_ok(gwion, arg) : GW_ERROR; } diff --git a/util b/util index 8ada759f..c532f5ba 160000 --- a/util +++ b/util @@ -1 +1 @@ -Subproject commit 8ada759fba134948e5747a9472edf27efa89e71c +Subproject commit c532f5ba7fbf0014be7749d0d78018a83267270a -- 2.43.0