From f6bd41ada767e9fd4d93fcd390eb69e55777ba0c Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sun, 26 Apr 2020 13:17:28 -0500 Subject: [PATCH] Add %M, %m, %N, and %n formatters. These will not truncate long values. --- CHANGELOG.md | 3 +++ src/core/pp.c | 29 ++++++++++++++++++++--------- src/include/janet.h | 1 + 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72efa55b..76c6040c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,10 @@ All notable changes to this project will be documented in this file. ## Unreleased - ??? +- Add `%M`, `%m`, `%N`, and `%n` formatters to formatting functions. These are the + same as `%Q`, `%q`, `%P`, and `%p`, but will not truncate long values. - Add beta `net/` module to core for socket based networking. +- Add the `dedent` function. - Add the `parse` function to parse strings of source code more conveniently. - Add `jpm rule-tree` subcommand. - Add `--offline` flag to jpm to force use of the cache. diff --git a/src/core/pp.c b/src/core/pp.c index 35ed9b12..7dbdc31d 100644 --- a/src/core/pp.c +++ b/src/core/pp.c @@ -527,7 +527,7 @@ static void janet_pretty_one(struct pretty *S, Janet x, int is_dict_value) { if (!isarray && !(S->flags & JANET_PRETTY_ONELINE) && len >= JANET_PRETTY_IND_ONELINE) janet_buffer_push_u8(S->buffer, ' '); if (is_dict_value && len >= JANET_PRETTY_IND_ONELINE) print_newline(S, 0); - if (len > JANET_PRETTY_ARRAY_LIMIT) { + if (len > JANET_PRETTY_ARRAY_LIMIT && !(S->flags && JANET_PRETTY_NOTRUNC)) { for (i = 0; i < 3; i++) { if (i) print_newline(S, 0); janet_pretty_one(S, arr[i], 0); @@ -591,7 +591,7 @@ static void janet_pretty_one(struct pretty *S, Janet x, int is_dict_value) { if (is_dict_value && len >= JANET_PRETTY_DICT_ONELINE) print_newline(S, 0); for (i = 0; i < cap; i++) { if (!janet_checktype(kvs[i].key, JANET_NIL)) { - if (counter == JANET_PRETTY_DICT_LIMIT) { + if (counter == JANET_PRETTY_DICT_LIMIT && !(S->flags && JANET_PRETTY_NOTRUNC)) { print_newline(S, 0); janet_buffer_push_cstring(S->buffer, "..."); break; @@ -802,6 +802,10 @@ void janet_formatbv(JanetBuffer *b, const char *format, va_list args) { pushtypes(b, types); break; } + case 'M': + case 'm': + case 'N': + case 'n': case 'Q': case 'q': case 'P': @@ -809,11 +813,13 @@ void janet_formatbv(JanetBuffer *b, const char *format, va_list args) { int depth = atoi(precision); if (depth < 1) depth = 4; char d = c[-1]; - int has_color = (d == 'P') || (d == 'Q'); - int has_oneline = (d == 'Q') || (d == 'q'); + int has_color = (d == 'P') || (d == 'Q') || (d == 'M') || (d == 'N'); + int has_oneline = (d == 'Q') || (d == 'q') || (d == 'N') || (d == 'n'); + int has_notrunc = (d == 'M') || (d == 'm') || (d == 'N') || (d == 'n'); int flags = 0; flags |= has_color ? JANET_PRETTY_COLOR : 0; flags |= has_oneline ? JANET_PRETTY_ONELINE : 0; + flags |= has_notrunc ? JANET_PRETTY_NOTRUNC : 0; janet_pretty_(b, depth, flags, va_arg(args, Janet), startlen); break; } @@ -946,19 +952,24 @@ void janet_buffer_format( janet_description_b(b, argv[arg]); break; } + case 'M': + case 'm': + case 'N': + case 'n': case 'Q': case 'q': case 'P': case 'p': { /* janet pretty , precision = depth */ int depth = atoi(precision); - if (depth < 1) - depth = 4; - char c = strfrmt[-1]; - int has_color = (c == 'P') || (c == 'Q'); - int has_oneline = (c == 'Q') || (c == 'q'); + if (depth < 1) depth = 4; + char d = strfrmt[-1]; + int has_color = (d == 'P') || (d == 'Q') || (d == 'M') || (d == 'N'); + int has_oneline = (d == 'Q') || (d == 'q') || (d == 'N') || (d == 'n'); + int has_notrunc = (d == 'M') || (d == 'm') || (d == 'N') || (d == 'n'); int flags = 0; flags |= has_color ? JANET_PRETTY_COLOR : 0; flags |= has_oneline ? JANET_PRETTY_ONELINE : 0; + flags |= has_notrunc ? JANET_PRETTY_NOTRUNC : 0; janet_pretty_(b, depth, flags, argv[arg], startlen); break; } diff --git a/src/include/janet.h b/src/include/janet.h index 31bd0a73..8a432f3b 100644 --- a/src/include/janet.h +++ b/src/include/janet.h @@ -1362,6 +1362,7 @@ JANET_API int janet_verify(JanetFuncDef *def); /* Pretty printing */ #define JANET_PRETTY_COLOR 1 #define JANET_PRETTY_ONELINE 2 +#define JANET_PRETTY_NOTRUNC 4 JANET_API JanetBuffer *janet_pretty(JanetBuffer *buffer, int depth, int flags, Janet x); /* Misc */