ruby-changes:50431
From: nobu <ko1@a...>
Date: Fri, 23 Feb 2018 17:39:09 +0900 (JST)
Subject: [ruby-changes:50431] nobu:r62548 (trunk): eval_error.c: rb_error_write flags
nobu 2018-02-23 17:39:03 +0900 (Fri, 23 Feb 2018) New Revision: 62548 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=62548 Log: eval_error.c: rb_error_write flags * eval_error.c (rb_error_write): add highlight and reverse mode flags. defaulted to rb_stderr_tty_p() if Qnil. Modified files: trunk/error.c trunk/eval_error.c Index: eval_error.c =================================================================== --- eval_error.c (revision 62547) +++ eval_error.c (revision 62548) @@ -79,12 +79,16 @@ error_print(rb_execution_context_t *ec) https://github.com/ruby/ruby/blob/trunk/eval_error.c#L79 rb_ec_error_print(ec, ec->errinfo); } +#define CSI_BEGIN "\033[" +#define CSI_SGR "m" + +static const char underline[] = CSI_BEGIN"1;4"CSI_SGR; +static const char bold[] = CSI_BEGIN"1"CSI_SGR; +static const char reset[] = CSI_BEGIN""CSI_SGR; + static void print_errinfo(const VALUE eclass, const VALUE errat, const VALUE emesg, const VALUE str, int highlight) { - static const char underline[] = "\033[4;1m"; - static const char bold[] = "\033[1m"; - static const char reset[] = "\033[m"; const char *einfo = ""; long elen = 0; VALUE mesg; @@ -199,7 +203,7 @@ print_backtrace(const VALUE eclass, cons https://github.com/ruby/ruby/blob/trunk/eval_error.c#L203 } void -rb_error_write(VALUE errinfo, VALUE errat, VALUE str) +rb_error_write(VALUE errinfo, VALUE errat, VALUE str, VALUE highlight, VALUE reverse) { volatile VALUE eclass = Qundef, emesg = Qundef; @@ -216,13 +220,33 @@ rb_error_write(VALUE errinfo, VALUE erra https://github.com/ruby/ruby/blob/trunk/eval_error.c#L220 emesg = e; } } - if (rb_stderr_tty_p()) { - write_warn(str, "\033[1mTraceback \033[m(most recent call last):\n"); + if (NIL_P(reverse) || NIL_P(highlight)) { + VALUE tty = (VALUE)rb_stderr_tty_p(); + if (NIL_P(reverse)) reverse = tty; + if (NIL_P(highlight)) highlight = tty; + } + if (reverse) { + static const char traceback[] = "Traceback " + "(most recent call last):\n"; + const int bold_part = rb_strlen_lit("Traceback"); + char buff[sizeof(traceback)+sizeof(bold)+sizeof(reset)-2], *p = buff; + const char *msg = traceback; + long len = sizeof(traceback) - 1; + if (highlight) { +#define APPEND(s, l) (memcpy(p, s, l), p += (l)) + APPEND(bold, sizeof(bold)-1); + APPEND(traceback, bold_part); + APPEND(reset, sizeof(reset)-1); + APPEND(traceback + bold_part, sizeof(traceback)-bold_part-1); +#undef APPEND + len = p - (msg = buff); + } + write_warn2(str, msg, len); print_backtrace(eclass, errat, str, TRUE); - print_errinfo(eclass, errat, emesg, str, TRUE); + print_errinfo(eclass, errat, emesg, str, highlight!=0); } else { - print_errinfo(eclass, errat, emesg, str, FALSE); + print_errinfo(eclass, errat, emesg, str, highlight!=0); print_backtrace(eclass, errat, str, FALSE); } } @@ -242,7 +266,7 @@ rb_ec_error_print(rb_execution_context_t https://github.com/ruby/ruby/blob/trunk/eval_error.c#L266 errat = rb_get_backtrace(errinfo); } - rb_error_write(errinfo, errat, Qnil); + rb_error_write(errinfo, errat, Qnil, Qnil, Qnil); EC_POP_TAG(); ec->errinfo = errinfo; Index: error.c =================================================================== --- error.c (revision 62547) +++ error.c (revision 62548) @@ -977,7 +977,7 @@ exc_to_s(VALUE exc) https://github.com/ruby/ruby/blob/trunk/error.c#L977 } /* FIXME: Include eval_error.c */ -void rb_error_write(VALUE errinfo, VALUE errat, VALUE str); +void rb_error_write(VALUE errinfo, VALUE errat, VALUE str, VALUE highlight, VALUE reverse); /* * call-seq: @@ -994,7 +994,7 @@ exc_full_message(VALUE exc) https://github.com/ruby/ruby/blob/trunk/error.c#L994 { VALUE str = rb_str_new2(""); VALUE errat = rb_get_backtrace(exc); - rb_error_write(exc, errat, str); + rb_error_write(exc, errat, str, Qnil, Qnil); return str; } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/