1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-25 07:50:27 +00:00

Improve pretty printing for table and struct

entry values.
This commit is contained in:
Calvin Rose 2018-12-06 17:26:59 -05:00
parent 7668cd5772
commit f2743aca36
3 changed files with 13 additions and 11 deletions

View File

@ -43,7 +43,7 @@ Janet makes a good system scripting language, or a language to embed in other pr
* Lexical scoping * Lexical scoping
* Imperative programming as well as functional * Imperative programming as well as functional
* REPL * REPL
* 300+ functions and macros in the core library * 300+ functions and macros in the core library
* Interactive environment with detailed stack traces * Interactive environment with detailed stack traces
## Documentation ## Documentation

View File

@ -742,7 +742,7 @@
(if (zero? (length more)) f (if (zero? (length more)) f
(fn [& r] (f ;more ;r)))) (fn [& r] (f ;more ;r))))
(defn every? (defn every?
"Returns true if each value in is truthy, otherwise the first "Returns true if each value in is truthy, otherwise the first
falsey value." falsey value."
[ind] [ind]
@ -889,7 +889,7 @@ value, one key will be ignored."
[xs] [xs]
(flatten-into @[] xs)) (flatten-into @[] xs))
(defn kvs (defn kvs
"Takes a table or struct and returns and array of key value pairs "Takes a table or struct and returns and array of key value pairs
like @[k v k v ...]. Returns a new array." like @[k v k v ...]. Returns a new array."
[dict] [dict]
@ -1250,7 +1250,7 @@ value, one key will be ignored."
(buffer/push-string buf str) (buffer/push-string buf str)
(buffer/push-string buf "\n"))) (buffer/push-string buf "\n")))
(var returnval nil) (var returnval nil)
(run-context *env* chunks (run-context *env* chunks
(fn [sig x f source] (fn [sig x f source]
(if (= sig :dead) (if (= sig :dead)
(:= returnval x) (:= returnval x)
@ -1368,7 +1368,7 @@ value, one key will be ignored."
(:= loading.path false) (:= loading.path false)
newenv))))) newenv)))))
(defn import* (defn import*
[env path & args] [env path & args]
(def targs (table ;args)) (def targs (table ;args))
(def {:as as (def {:as as
@ -1400,7 +1400,7 @@ value, one key will be ignored."
(defn repl (defn repl
"Run a repl. The first parameter is an optional function to call to "Run a repl. The first parameter is an optional function to call to
get a chunk of source code that should return nil for end of file. get a chunk of source code that should return nil for end of file.
The second parameter is a function that is called when a signal is The second parameter is a function that is called when a signal is
caught." caught."
[chunks onsignal &] [chunks onsignal &]
(def newenv (make-env)) (def newenv (make-env))

View File

@ -490,7 +490,7 @@ static void print_newline(struct pretty *S, int just_a_space) {
} }
/* Helper for pretty printing */ /* Helper for pretty printing */
static void janet_pretty_one(struct pretty *S, Janet x) { static void janet_pretty_one(struct pretty *S, Janet x, int is_dict_value) {
/* Add to seen */ /* Add to seen */
switch (janet_type(x)) { switch (janet_type(x)) {
case JANET_NIL: case JANET_NIL:
@ -534,9 +534,10 @@ static void janet_pretty_one(struct pretty *S, Janet x) {
janet_indexed_view(x, &arr, &len); janet_indexed_view(x, &arr, &len);
if (!isarray && len >= 5) if (!isarray && len >= 5)
janet_buffer_push_u8(S->buffer, ' '); janet_buffer_push_u8(S->buffer, ' ');
if (is_dict_value && len >= 5) print_newline(S, 0);
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
if (i) print_newline(S, len < 5); if (i) print_newline(S, len < 5);
janet_pretty_one(S, arr[i]); janet_pretty_one(S, arr[i], 0);
} }
} }
S->indent -= 2; S->indent -= 2;
@ -575,6 +576,7 @@ static void janet_pretty_one(struct pretty *S, Janet x) {
janet_dictionary_view(x, &kvs, &len, &cap); janet_dictionary_view(x, &kvs, &len, &cap);
if (!istable && len >= 4) if (!istable && len >= 4)
janet_buffer_push_u8(S->buffer, ' '); janet_buffer_push_u8(S->buffer, ' ');
if (is_dict_value && len >= 5) print_newline(S, 0);
for (i = 0; i < cap; i++) { for (i = 0; i < cap; i++) {
if (!janet_checktype(kvs[i].key, JANET_NIL)) { if (!janet_checktype(kvs[i].key, JANET_NIL)) {
if (first_kv_pair) { if (first_kv_pair) {
@ -582,9 +584,9 @@ static void janet_pretty_one(struct pretty *S, Janet x) {
} else { } else {
print_newline(S, len < 4); print_newline(S, len < 4);
} }
janet_pretty_one(S, kvs[i].key); janet_pretty_one(S, kvs[i].key, 0);
janet_buffer_push_u8(S->buffer, ' '); janet_buffer_push_u8(S->buffer, ' ');
janet_pretty_one(S, kvs[i].value); janet_pretty_one(S, kvs[i].value, 1);
} }
} }
} }
@ -610,7 +612,7 @@ JanetBuffer *janet_pretty(JanetBuffer *buffer, int depth, Janet x) {
S.depth = depth; S.depth = depth;
S.indent = 0; S.indent = 0;
janet_table_init(&S.seen, 10); janet_table_init(&S.seen, 10);
janet_pretty_one(&S, x); janet_pretty_one(&S, x, 0);
janet_table_deinit(&S.seen); janet_table_deinit(&S.seen);
return S.buffer; return S.buffer;
} }