From 3aed0e88d5817b49ea2e25ac4c478532b77dec43 Mon Sep 17 00:00:00 2001 From: Khairul Azhar Kasmiran Date: Tue, 28 May 2019 00:21:17 +0800 Subject: [PATCH] Default to scr.color=COLOR_MODE_16 for Alacritty on Windows ##cons --- libr/cons/cons.c | 6 +++--- libr/cons/dietline.c | 4 ++-- libr/cons/grep.c | 2 +- libr/cons/hud.c | 2 +- libr/cons/pal.c | 4 ++-- libr/cons/rgb.c | 2 +- libr/core/cconfig.c | 6 +++--- libr/core/visual.c | 8 ++++---- libr/core/vmenus.c | 2 +- libr/include/r_cons.h | 2 +- libr/main/radare2.c | 12 ++++++++++++ 11 files changed, 31 insertions(+), 19 deletions(-) diff --git a/libr/cons/cons.c b/libr/cons/cons.c index 2de5c618f9..332e72e3bb 100644 --- a/libr/cons/cons.c +++ b/libr/cons/cons.c @@ -115,10 +115,10 @@ static void cons_context_init(RConsContext *context, R_NULLABLE RConsContext *pa context->log_callback = NULL; if (parent) { - context->color = parent->color; + context->color_mode = parent->color_mode; r_cons_pal_copy (context, parent); } else { - context->color = COLOR_MODE_DISABLED; + context->color_mode = COLOR_MODE_DISABLED; r_cons_pal_init (context); } } @@ -165,7 +165,7 @@ static inline void r_cons_write(const char *obuf, int olen) { R_API RColor r_cons_color_random(ut8 alpha) { RColor rcolor = {0}; - if (I.context->color > COLOR_MODE_16) { + if (I.context->color_mode > COLOR_MODE_16) { rcolor.r = r_num_rand (0xff); rcolor.g = r_num_rand (0xff); rcolor.b = r_num_rand (0xff); diff --git a/libr/cons/dietline.c b/libr/cons/dietline.c index 7d21aeea80..c2e398f4f9 100644 --- a/libr/cons/dietline.c +++ b/libr/cons/dietline.c @@ -458,8 +458,8 @@ static void selection_widget_draw() { } sel_widget->w = R_MIN (sel_widget->w, R_SELWIDGET_MAXW); - char *background_color = cons->context->color ? cons->context->pal.widget_bg : Color_INVERT_RESET; - char *selected_color = cons->context->color ? cons->context->pal.widget_sel : Color_INVERT; + char *background_color = cons->context->color_mode ? cons->context->pal.widget_bg : Color_INVERT_RESET; + char *selected_color = cons->context->color_mode ? cons->context->pal.widget_sel : Color_INVERT; bool scrollbar = sel_widget->options_len > R_SELWIDGET_MAXH; int scrollbar_y = 0, scrollbar_l = 0; if (scrollbar) { diff --git a/libr/cons/grep.c b/libr/cons/grep.c index c8d28c81e2..16cf9fa257 100644 --- a/libr/cons/grep.c +++ b/libr/cons/grep.c @@ -479,7 +479,7 @@ R_API void r_cons_grepbuf() { Color_RESET, NULL }; - char *out = r_print_json_indent (buf, I (context->color), " ", palette); + char *out = r_print_json_indent (buf, I (context->color_mode), " ", palette); if (!out) { return; } diff --git a/libr/cons/hud.c b/libr/cons/hud.c index dd93594dc9..c2c18bb188 100644 --- a/libr/cons/hud.c +++ b/libr/cons/hud.c @@ -128,7 +128,7 @@ static RList *hud_filter(RList *list, char *user_input, int top_entry_n, int *cu r_list_append (res, r_str_newf (" %c %s", first_line? '-': ' ', p)); } else { // otherwise we need to emphasize the matching part - if (I (context->color)) { + if (I (context->color_mode)) { int last_color_change = 0; int last_mask = 0; char *str = r_str_newf (" %c ", first_line? '-': ' '); diff --git a/libr/cons/pal.c b/libr/cons/pal.c index 12197aa70c..fe3808351f 100644 --- a/libr/cons/pal.c +++ b/libr/cons/pal.c @@ -462,7 +462,7 @@ R_API void r_cons_pal_show() { colors[i].bgcode, colors[i].name); } - switch (r_cons_singleton ()->context->color) { + switch (r_cons_singleton ()->context->color_mode) { case COLOR_MODE_256: // 256 color palette r_cons_pal_show_gs (); r_cons_pal_show_256 (); @@ -595,7 +595,7 @@ static void cons_pal_update_event(RConsContext *ctx) { if (*color) { R_FREE (*color); } - *color = r_cons_rgb_str_mode (ctx->color, NULL, 0, rcolor); + *color = r_cons_rgb_str_mode (ctx->color_mode, NULL, 0, rcolor); const char *rgb = sdb_fmt ("rgb:%02x%02x%02x", rcolor->r, rcolor->g, rcolor->b); sdb_set (db, rgb, "1", 0); } diff --git a/libr/cons/rgb.c b/libr/cons/rgb.c index 8bfd57b9ab..053ce8a9e0 100644 --- a/libr/cons/rgb.c +++ b/libr/cons/rgb.c @@ -272,7 +272,7 @@ R_API char *r_cons_rgb_str_mode(RConsColorMode mode, char *outstr, size_t sz, RC /* Return the computed color string for the specified color */ R_API char *r_cons_rgb_str(char *outstr, size_t sz, RColor *rcolor) { - return r_cons_rgb_str_mode (r_cons_singleton ()->context->color, outstr, sz, rcolor); + return r_cons_rgb_str_mode (r_cons_singleton ()->context->color_mode, outstr, sz, rcolor); } R_API char *r_cons_rgb_tostring(ut8 r, ut8 g, ut8 b) { diff --git a/libr/core/cconfig.c b/libr/core/cconfig.c index 138636bb80..0824896e84 100644 --- a/libr/core/cconfig.c +++ b/libr/core/cconfig.c @@ -1136,7 +1136,7 @@ static bool cb_color(void *user, void *data) { } else if (!strcmp (node->value, "false")) { node->i_value = 0; } - r_cons_singleton ()->context->color = (node->i_value > COLOR_MODE_16M) + r_cons_singleton ()->context->color_mode = (node->i_value > COLOR_MODE_16M) ? COLOR_MODE_16M: node->i_value; r_cons_pal_update_event (); r_print_set_flags (core->print, core->print->flags); @@ -1145,9 +1145,9 @@ static bool cb_color(void *user, void *data) { static bool cb_color_getter(void *user, RConfigNode *node) { (void)user; - node->i_value = r_cons_singleton ()->context->color; + node->i_value = r_cons_singleton ()->context->color_mode; char buf[128]; - r_config_node_value_format_i (buf, sizeof (buf), r_cons_singleton ()->context->color, node); + r_config_node_value_format_i (buf, sizeof (buf), r_cons_singleton ()->context->color_mode, node); if (!node->value || strcmp (node->value, buf) != 0) { free (node->value); node->value = strdup (buf); diff --git a/libr/core/visual.c b/libr/core/visual.c index a78100c1e7..140da856c6 100644 --- a/libr/core/visual.c +++ b/libr/core/visual.c @@ -414,7 +414,7 @@ R_API int r_core_visual_hud(RCore *core) { char *homehud = r_str_home (R2_HOME_HUD); char *res = NULL; char *p = 0; - r_cons_singleton ()->context->color = use_color; + r_cons_singleton ()->context->color_mode = use_color; r_core_visual_showcursor (core, true); if (c && *c && r_file_exists (c)) { @@ -469,9 +469,9 @@ R_API void r_core_visual_jump(RCore *core, ut8 ch) { R_API void r_core_visual_append_help(RStrBuf *p, const char *title, const char **help) { int i, max_length = 0, padding = 0; RConsContext *cons_ctx = r_cons_singleton ()->context; - const char *pal_args_color = cons_ctx->color ? cons_ctx->pal.args : "", - *pal_help_color = cons_ctx->color ? cons_ctx->pal.help : "", - *pal_reset = cons_ctx->color ? cons_ctx->pal.reset : ""; + const char *pal_args_color = cons_ctx->color_mode ? cons_ctx->pal.args : "", + *pal_help_color = cons_ctx->color_mode ? cons_ctx->pal.help : "", + *pal_reset = cons_ctx->color_mode ? cons_ctx->pal.reset : ""; for (i = 0; help[i]; i += 2) { max_length = R_MAX (max_length, strlen (help[i])); } diff --git a/libr/core/vmenus.c b/libr/core/vmenus.c index 834dfd46b5..5c2737658f 100644 --- a/libr/core/vmenus.c +++ b/libr/core/vmenus.c @@ -3920,7 +3920,7 @@ R_API void r_core_visual_colors(RCore *core) { char *color = calloc (1, 64), cstr[32]; char preview_cmd[128] = "pd $r"; int ch, opt = 0, oopt = -1; - bool truecolor = r_cons_singleton ()->context->color == COLOR_MODE_16M; + bool truecolor = r_cons_singleton ()->context->color_mode == COLOR_MODE_16M; char *rgb_xxx_fmt = truecolor ? "rgb:%2.2x%2.2x%2.2x ":"rgb:%x%x%x "; const char *k; RColor rcolor; diff --git a/libr/include/r_cons.h b/libr/include/r_cons.h index 991de8c4e0..13df0aea8d 100644 --- a/libr/include/r_cons.h +++ b/libr/include/r_cons.h @@ -419,7 +419,7 @@ typedef struct r_cons_context_t { bool is_interactive; bool pageable; - RConsColorMode color; + int color_mode; RConsPalette cpal; RConsPrintablePalette pal; } RConsContext; diff --git a/libr/main/radare2.c b/libr/main/radare2.c index a153595b35..9a2dcb6882 100644 --- a/libr/main/radare2.c +++ b/libr/main/radare2.c @@ -399,6 +399,18 @@ static bool mustSaveHistory(RConfig *c) { // Try to set the correct scr.color for the current terminal. static void set_color_default(void) { +#ifdef __WINDOWS__ + char *alacritty = r_sys_getenv ("ALACRITTY_LOG"); + if (alacritty) { + // Despite the setting of env vars to the contrary, Alacritty on + // Windows may not actually support >16 colors out-of-the-box + // (https://github.com/jwilm/alacritty/issues/1662). + // TODO: Windows 10 version check. + r_config_set_i (r.config, "scr.color", COLOR_MODE_16); + free (alacritty); + return; + } +#endif char *tmp = r_sys_getenv ("COLORTERM"); if (tmp) { if ((r_str_endswith (tmp, "truecolor") || r_str_endswith (tmp, "24bit"))) {