1
0
mirror of https://github.com/janet-lang/janet synced 2025-07-04 02:52:59 +00:00

Add some color to stacktraces in repl.

This commit is contained in:
Calvin Rose 2019-08-12 19:20:01 -05:00
parent ec62e871dd
commit 54170d92db
3 changed files with 8 additions and 2 deletions

View File

@ -100,6 +100,9 @@ void janet_stacktrace(JanetFiber *fiber, Janet err) {
JanetFiber **fibers = NULL; JanetFiber **fibers = NULL;
int wrote_error = 0; int wrote_error = 0;
int print_color = janet_truthy(janet_dyn("err-color"));
if (print_color) fprintf(out, "\x1b[31m");
while (fiber) { while (fiber) {
janet_v_push(fibers, fiber); janet_v_push(fibers, fiber);
fiber = fiber->child; fiber = fiber->child;
@ -157,6 +160,8 @@ void janet_stacktrace(JanetFiber *fiber, Janet err) {
} }
} }
if (print_color) fprintf(out, "\x1b[0m");
janet_v_free(fibers); janet_v_free(fibers);
} }

View File

@ -179,7 +179,7 @@ static void vm_do_trace(JanetFunction *func) {
static Janet call_nonfn(JanetFiber *fiber, Janet callee) { static Janet call_nonfn(JanetFiber *fiber, Janet callee) {
int32_t argn = fiber->stacktop - fiber->stackstart; int32_t argn = fiber->stacktop - fiber->stackstart;
Janet ds, key; Janet ds, key;
if (argn != 1) janet_panicf("%v called with arity %d, expected 1", callee, argn); if (argn != 1) janet_panicf("%v called with %d arguments, possibly expected 1", callee, argn);
if (janet_checktypes(callee, JANET_TFLAG_INDEXED | JANET_TFLAG_DICTIONARY | if (janet_checktypes(callee, JANET_TFLAG_INDEXED | JANET_TFLAG_DICTIONARY |
JANET_TFLAG_STRING | JANET_TFLAG_BUFFER | JANET_TFLAG_ABSTRACT)) { JANET_TFLAG_STRING | JANET_TFLAG_BUFFER | JANET_TFLAG_ABSTRACT)) {
ds = callee; ds = callee;
@ -195,7 +195,7 @@ static Janet call_nonfn(JanetFiber *fiber, Janet callee) {
/* Get a callable from a keyword method name and check ensure that it is valid. */ /* Get a callable from a keyword method name and check ensure that it is valid. */
static Janet resolve_method(Janet name, JanetFiber *fiber) { static Janet resolve_method(Janet name, JanetFiber *fiber) {
int32_t argc = fiber->stacktop - fiber->stackstart; int32_t argc = fiber->stacktop - fiber->stackstart;
if (argc < 1) janet_panicf("method call takes at least 1 argument, got %d", argc); if (argc < 1) janet_panicf("method call (%v) takes at least 1 argument, got 0", name);
Janet callee = janet_get(fiber->data[fiber->stackstart], name); Janet callee = janet_get(fiber->data[fiber->stackstart], name);
if (janet_checktype(callee, JANET_NIL)) if (janet_checktype(callee, JANET_NIL))
janet_panicf("unknown method %v invoked on %v", name, fiber->data[fiber->stackstart]); janet_panicf("unknown method %v invoked on %v", name, fiber->data[fiber->stackstart]);

View File

@ -92,4 +92,5 @@
(getter (prompter p) buf)) (getter (prompter p) buf))
(def onsig (if *quiet* (fn [x &] x) nil)) (def onsig (if *quiet* (fn [x &] x) nil))
(setdyn :pretty-format (if *colorize* "%.20P" "%.20p")) (setdyn :pretty-format (if *colorize* "%.20P" "%.20p"))
(setdyn :err-color (if *colorize* true))
(repl getchunk onsig))) (repl getchunk onsig)))