1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-24 17:27:18 +00:00

Add Q and q formatters to buffer/format.

These are similar to P and p, but print values
on a single line for a much more compact version.
This commit is contained in:
Calvin Rose 2019-08-24 22:53:45 -04:00
parent 77870508de
commit 060d11e4c2
5 changed files with 16 additions and 4 deletions

View File

@ -2,6 +2,9 @@
All notable changes to this project will be documented in this file.
## Unreleased
- Add `buffer/format` and `string/format` format flags `Q` and `q` to print colored and
non-colored single-line values.
- Change default repl to print long sequences on one line.
- Add `backmatch` pattern for PEGs.
- jpm detects if not in a Developer Command prompt on windows for a better error message.
- jpm install git submodules in dependencies

View File

@ -1129,7 +1129,7 @@
(defn pp
"Pretty print to stdout."
[x]
(print (buffer/format @"" (dyn :pretty-format "%p") x)))
(print (buffer/format @"" (dyn :pretty-format "%q") x)))
###

View File

@ -310,7 +310,7 @@ struct pretty {
static void print_newline(struct pretty *S, int just_a_space) {
int i;
if (just_a_space) {
if (just_a_space || (S->flags & JANET_PRETTY_ONELINE)) {
janet_buffer_push_u8(S->buffer, ' ');
return;
}
@ -725,12 +725,20 @@ void janet_buffer_format(
janet_description_b(b, argv[arg]);
break;
}
case 'Q':
case 'q':
case 'P':
case 'p': { /* janet pretty , precision = depth */
int depth = atoi(precision);
if (depth < 1)
depth = 4;
janet_pretty_(b, depth, (strfrmt[-1] == 'P') ? JANET_PRETTY_COLOR : 0, argv[arg], startlen);
char c = strfrmt[-1];
int has_color = (c == 'P') || (c == 'Q');
int has_oneline = (c == 'Q') || (c == 'q');
int flags = 0;
flags |= has_color ? JANET_PRETTY_COLOR : 0;
flags |= has_oneline ? JANET_PRETTY_ONELINE : 0;
janet_pretty_(b, depth, flags, argv[arg], startlen);
break;
}
default: {

View File

@ -1262,6 +1262,7 @@ JANET_API int janet_verify(JanetFuncDef *def);
/* Pretty printing */
#define JANET_PRETTY_COLOR 1
#define JANET_PRETTY_ONELINE 2
JANET_API JanetBuffer *janet_pretty(JanetBuffer *buffer, int depth, int flags, Janet x);
/* Misc */

View File

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