Skip to content

Commit

Permalink
FEAT: optionally truncate output of probe function
Browse files Browse the repository at this point in the history
  • Loading branch information
Oldes committed Oct 8, 2021
1 parent 2ee6c0a commit e108cfc
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/boot/sysobj.reb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ options: object [ ; Options supplied to REBOL during startup

binary-base: 16 ; Default base for FORMed binary values (64, 16, 2)
decimal-digits: 15 ; Max number of decimal digits to print.
probe-limit: 16000 ; Max probed output size, 0 means no limit
module-paths: [%./]
default-suffix: %.reb ; Used by IMPORT if no suffix is provided
file-types: []
Expand Down
6 changes: 3 additions & 3 deletions src/core/n-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ static REBSER *Read_All_File(char *fname)
{
REBVAL *val = D_ARG(1);
REB_MOLD mo = {0};
REBINT len = -1; // no limit
REBINT len = NO_LIMIT;

if (D_REF(3)) SET_FLAG(mo.opts, MOPT_MOLD_ALL);
if (D_REF(4)) SET_FLAG(mo.opts, MOPT_INDENT);
if (D_REF(5)) {
if (VAL_INT64(D_ARG(6)) > (i64)MAX_I32)
len = MAX_I32;
else if (VAL_INT64(D_ARG(6)) < 0)
len = 0;
else if (VAL_INT64(D_ARG(6)) <= 0)
len = NO_LIMIT;
else
len = VAL_INT32(D_ARG(6));
}
Expand Down
16 changes: 11 additions & 5 deletions src/mezz/base-debug.reb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ REBOL [
probe: func [
{Debug print a molded value and returns that same value.}
value [any-type!]
/local len
][
print mold :value
len: system/options/probe-limit
print either 0 < len [
ellipsize (mold/part :value len + 1) len
][
mold :value
]
:value
]

Expand All @@ -33,7 +39,7 @@ probe: func [
word? :name
path? :name
][
prin ajoin ["^[[1;32;49m" mold :name "^[[0m: ^[[32m"]
prin ajoin ["^[[1;32m" mold :name "^[[0m: ^[[32m"]
prin either value? :name [mold/all get/any :name] ["#[unset!]"]
print "^[[0m"
]
Expand All @@ -43,15 +49,15 @@ probe: func [
word? :word
path? :word
][
prin ajoin ["^[[1;32;49m" mold :word "^[[0m: ^[[32m"]
prin ajoin ["^[[1;32m" mold :word "^[[0m: ^[[32m"]
prin either value? :word [mold/all get/any :word]["#[unset!]"]
print "^[[0m"
][
print ajoin ["^[[1;32;49m" mold/all word "^[[0m"]
print ajoin ["^[[1;32m" mold/all word "^[[0m"]
]
]
]
true [print ajoin ["^[[1;32;49m" mold/all :name "^[[0m"]]
true [print ajoin ["^[[1;32m" mold/all :name "^[[0m"]]
]
exit
]
Expand Down
24 changes: 24 additions & 0 deletions src/mezz/base-series.reb
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,27 @@ reform: func [
][
form reduce :value
]

ellipsize: func [
"Truncate and add ellipsis if str is longer than len"
str [string!] "(modified)"
len [integer!] "Max length"
/one-line "Escape line breaks"
/local chars
][
if one-line [
chars: #[bitset! [not bits #{0024}]]
parse str [
any [
some chars
| change #"^/" "^^/"
| change #"^M" "^^M"
]
]
]
if len < length? str [
append clear skip str (len - 3) "..."
]

str
]
12 changes: 2 additions & 10 deletions src/mezz/mezz-help.reb
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,6 @@ import module [
buffer: insert buffer form reduce value
]

clip-str: func [str] [
; Keep string to one line.
unless string? str [str: mold/part/flat str max-desc-width]
replace/all str LF "^^/"
replace/all str CR "^^M"
if (length? str) > (max-desc-width - 1) [str: append copy/part str max-desc-width "..."]
str
]

interpunction: charset ";.?!"
dot: func[value [string!]][
unless find interpunction last value [append value #"."]
Expand Down Expand Up @@ -116,7 +107,8 @@ import module [
;none? :val [ mold/all val]
true [:val]
]
clip-str val
unless string? val [val: mold/part/flat val max-desc-width]
ellipsize/one-line val max-desc-width - 1
]

form-pad: func [val size] [
Expand Down

0 comments on commit e108cfc

Please sign in to comment.