mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
mruby-bin-mirb: add colored output for results and errors
Result values are shown in cyan, errors in bold red. The arrow " => " uses gray for subtle appearance. Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -45,26 +45,22 @@
|
||||
#endif
|
||||
|
||||
static void
|
||||
p(mrb_state *mrb, mrb_value obj)
|
||||
p(mrb_state *mrb, mrb_value obj, mirb_highlighter *hl)
|
||||
{
|
||||
mrb_value val = mrb_funcall_argv(mrb, obj, MRB_SYM(inspect), 0, NULL);
|
||||
if (!mrb->exc) {
|
||||
fputs(" => ", stdout);
|
||||
}
|
||||
else {
|
||||
if (mrb->exc) {
|
||||
val = mrb_exc_get_output(mrb, mrb->exc);
|
||||
}
|
||||
if (!mrb_string_p(val)) {
|
||||
val = mrb_obj_as_string(mrb, obj);
|
||||
}
|
||||
char* msg = mrb_locale_from_utf8(RSTRING_PTR(val), (int)RSTRING_LEN(val));
|
||||
fwrite(msg, strlen(msg), 1, stdout);
|
||||
mirb_highlight_print_result(hl, msg);
|
||||
mrb_locale_free(msg);
|
||||
putc('\n', stdout);
|
||||
}
|
||||
|
||||
static void
|
||||
p_error(mrb_state *mrb, struct RObject* exc, mrb_ccontext *cxt)
|
||||
p_error(mrb_state *mrb, struct RObject* exc, mrb_ccontext *cxt, mirb_highlighter *hl)
|
||||
{
|
||||
mrb_value val = mrb_exc_get_output(mrb, exc);
|
||||
if (!mrb_string_p(val)) {
|
||||
@@ -104,9 +100,8 @@ p_error(mrb_state *mrb, struct RObject* exc, mrb_ccontext *cxt)
|
||||
}
|
||||
|
||||
char* msg = mrb_locale_from_utf8(RSTRING_PTR(val), (int)RSTRING_LEN(val));
|
||||
fwrite(msg, strlen(msg), 1, stdout);
|
||||
mirb_highlight_print_error(hl, msg);
|
||||
mrb_locale_free(msg);
|
||||
putc('\n', stdout);
|
||||
}
|
||||
|
||||
/* Guess if the user might want to enter more
|
||||
@@ -762,7 +757,7 @@ main(int argc, char **argv)
|
||||
/* did an exception occur? */
|
||||
if (mrb->exc) {
|
||||
MRB_EXC_CHECK_EXIT(mrb, mrb->exc);
|
||||
p_error(mrb, mrb->exc, cxt);
|
||||
p_error(mrb, mrb->exc, cxt, &editor.highlight);
|
||||
mrb->exc = 0;
|
||||
}
|
||||
else {
|
||||
@@ -770,7 +765,7 @@ main(int argc, char **argv)
|
||||
if (!mrb_respond_to(mrb, result, MRB_SYM(inspect))){
|
||||
result = mrb_any_to_s(mrb, result);
|
||||
}
|
||||
p(mrb, result);
|
||||
p(mrb, result, &editor.highlight);
|
||||
#ifndef MRB_NO_MIRB_UNDERSCORE
|
||||
*(mrb->c->ci->stack + 1) = result;
|
||||
#endif
|
||||
|
||||
@@ -23,6 +23,9 @@
|
||||
#define DARK_IVAR "\033[34m" /* blue */
|
||||
#define DARK_GVAR "\033[1;34m" /* bold blue */
|
||||
#define DARK_REGEXP "\033[31m" /* red */
|
||||
#define DARK_RESULT "\033[36m" /* cyan (same as number) */
|
||||
#define DARK_ERROR "\033[1;31m" /* bold red */
|
||||
#define DARK_ARROW "\033[90m" /* gray */
|
||||
|
||||
/* Light theme colors (dark colors on light background) */
|
||||
#define LIGHT_KEYWORD "\033[35m" /* magenta */
|
||||
@@ -34,6 +37,9 @@
|
||||
#define LIGHT_IVAR "\033[34m" /* blue */
|
||||
#define LIGHT_GVAR "\033[34m" /* blue */
|
||||
#define LIGHT_REGEXP "\033[31m" /* red */
|
||||
#define LIGHT_RESULT "\033[36m" /* cyan */
|
||||
#define LIGHT_ERROR "\033[31m" /* red */
|
||||
#define LIGHT_ARROW "\033[90m" /* gray */
|
||||
|
||||
#define COLOR_RESET "\033[0m"
|
||||
|
||||
@@ -456,3 +462,50 @@ mirb_highlight_print_line(mirb_highlighter *hl, const char *line)
|
||||
putchar(*p++);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Print result value with highlighting
|
||||
*/
|
||||
void
|
||||
mirb_highlight_print_result(mirb_highlighter *hl, const char *result)
|
||||
{
|
||||
if (!hl->enabled) {
|
||||
fputs(" => ", stdout);
|
||||
fputs(result, stdout);
|
||||
putchar('\n');
|
||||
return;
|
||||
}
|
||||
|
||||
if (hl->theme == MIRB_THEME_DARK) {
|
||||
fputs(DARK_ARROW " => " COLOR_RESET, stdout);
|
||||
fputs(DARK_RESULT, stdout);
|
||||
}
|
||||
else {
|
||||
fputs(LIGHT_ARROW " => " COLOR_RESET, stdout);
|
||||
fputs(LIGHT_RESULT, stdout);
|
||||
}
|
||||
fputs(result, stdout);
|
||||
fputs(COLOR_RESET "\n", stdout);
|
||||
}
|
||||
|
||||
/*
|
||||
* Print error message with highlighting
|
||||
*/
|
||||
void
|
||||
mirb_highlight_print_error(mirb_highlighter *hl, const char *error)
|
||||
{
|
||||
if (!hl->enabled) {
|
||||
fputs(error, stdout);
|
||||
putchar('\n');
|
||||
return;
|
||||
}
|
||||
|
||||
if (hl->theme == MIRB_THEME_DARK) {
|
||||
fputs(DARK_ERROR, stdout);
|
||||
}
|
||||
else {
|
||||
fputs(LIGHT_ERROR, stdout);
|
||||
}
|
||||
fputs(error, stdout);
|
||||
fputs(COLOR_RESET "\n", stdout);
|
||||
}
|
||||
|
||||
@@ -81,4 +81,15 @@ void mirb_highlight_print_line(mirb_highlighter *hl, const char *line);
|
||||
*/
|
||||
void mirb_highlight_reset(mirb_highlighter *hl);
|
||||
|
||||
/*
|
||||
* Print result value with highlighting
|
||||
* Prints " => " prefix and the result string with appropriate colors
|
||||
*/
|
||||
void mirb_highlight_print_result(mirb_highlighter *hl, const char *result);
|
||||
|
||||
/*
|
||||
* Print error message with highlighting
|
||||
*/
|
||||
void mirb_highlight_print_error(mirb_highlighter *hl, const char *error);
|
||||
|
||||
#endif /* MIRB_HIGHLIGHT_H */
|
||||
|
||||
Reference in New Issue
Block a user