Name | Executed | Routines | % | Executed | Lines | % | Unexecuted |
/home/matt/eu/rds/include/std/pretty.e | 8 | 8 | 100.00% | 114 | 130 | 87.69% | 16 |
Routine | Executed | Lines | Unexecuted | |
rPrint() | 43 | 51 | 84.31% | 8 |
esc_char() | 4 | 8 | 50.00% | 4 |
pretty_out() | 7 | 10 | 70.00% | 3 |
indent() | 10 | 11 | 90.91% | 1 |
cut_line() | 8 | 8 | 100.00% | 0 |
pretty() | 20 | 20 | 100.00% | 0 |
pretty_print() | 6 | 6 | 100.00% | 0 |
pretty_sprint() | 4 | 4 | 100.00% | 0 |
# | Executed | |
1 | namespace pretty | |
2 | ||
3 | --**** | |
4 | -- == Pretty Printing | |
5 | -- | |
6 | -- < | |
7 | -- | |
8 | ||
9 | -- pretty print variables | |
10 | integer pretty_end_col, pretty_chars, pretty_start_col, pretty_level, | |
11 | pretty_file, pretty_ascii, pretty_indent, pretty_ascii_min, | |
12 | pretty_ascii_max, pretty_line_count, pretty_line_max, pretty_dots, | |
13 | pretty_line_breaks, pretty_printing | |
14 | sequence pretty_fp_format, pretty_int_format, pretty_line | |
15 | ||
16 | 7486 | |
17 | -- Output text, keeping track of line length. | |
18 | -- Buffering lines speeds up Windows console output. | |
19 | 7486 | pretty_line &= text |
20 | 7486 | if equal(text, '\n') and pretty_printing then |
21 | 0 | puts(pretty_file, pretty_line) |
22 | 0 | pretty_line = "" |
23 | 0 | pretty_line_count += 1 |
24 | end if | |
25 | 7486 | if atom(text) then |
26 | 4288 | pretty_chars += 1 |
27 | else | |
28 | 3198 | pretty_chars += length(text) |
29 | end if | |
30 | 7486 | end procedure |
31 | ||
32 | 3084 | |
33 | -- check for time to do line break | |
34 | 3084 | if not pretty_line_breaks then |
35 | 32 | pretty_chars = 0 |
36 | 32 | return |
37 | end if | |
38 | 3052 | if pretty_chars + n > pretty_end_col then |
39 | 160 | pretty_out('\n') |
40 | 160 | pretty_chars = 0 |
41 | end if | |
42 | 3052 | end procedure |
43 | ||
44 | 453 | |
45 | -- indent the display of a sequence | |
46 | 453 | if pretty_line_breaks = 0 then |
47 | 13 | pretty_chars = 0 |
48 | 13 | return |
49 | 440 | elsif pretty_line_breaks = -1 then |
50 | ||
51 | 0 | cut_line( 0 ) |
52 | ||
53 | else | |
54 | 440 | if pretty_chars > 0 then |
55 | 430 | pretty_out('\n') |
56 | 430 | pretty_chars = 0 |
57 | end if | |
58 | 440 | pretty_out(repeat(' ', (pretty_start_col-1) + |
59 | pretty_level * pretty_indent)) | |
60 | end if | |
61 | ||
62 | 440 | end procedure |
63 | ||
64 | 233 | |
65 | -- show escaped characters | |
66 | 233 | switch a do |
67 | case'\t' then | |
68 | 0 | return `\t` |
69 | ||
70 | case'\n' then | |
71 | 0 | return `\n` |
72 | ||
73 | case'\r' then | |
74 | 0 | return `\r` |
75 | ||
76 | case'\\' then | |
77 | 2 | return `\\` |
78 | ||
79 | case'"' then | |
80 | 0 | return `\"` |
81 | ||
82 | case else | |
83 | 231 | return a |
84 | end switch | |
85 | end function | |
86 | ||
87 | 3139 | |
88 | -- recursively print a Euphoria object | |
89 | sequence sbuff | |
90 | integer multi_line, all_ascii | |
91 | ||
92 | 3139 | if atom(a) then |
93 | 2756 | if integer(a) then |
94 | 2721 | sbuff = sprintf(pretty_int_format, a) |
95 | 2721 | if pretty_ascii then |
96 | 2721 | if pretty_ascii >= 3 then |
97 | -- replace number with display character? | |
98 | 0 | if (a >= pretty_ascii_min and a <= pretty_ascii_max) then |
99 | 0 | sbuff = '\'' & a & '\'' -- display char only |
100 | ||
101 | 0 | elsif find(a, "\t\n\r\\") then |
102 | 0 | sbuff = '\'' & esc_char(a) & '\'' -- display char only |
103 | ||
104 | end if | |
105 | else -- pretty ascii 1 or 2 | |
106 | -- add display character to number? | |
107 | 2721 | if (a >= pretty_ascii_min and a <= pretty_ascii_max) and pretty_ascii < 2 then |
108 | 2680 | sbuff &= '\'' & a & '\'' -- add to numeric display |
109 | end if | |
110 | end if | |
111 | end if | |
112 | else | |
113 | 35 | sbuff = sprintf(pretty_fp_format, a) |
114 | end if | |
115 | 2756 | pretty_out(sbuff) |
116 | ||
117 | else | |
118 | -- sequence | |
119 | 383 | cut_line(1) |
120 | 383 | multi_line = 0 |
121 | 383 | all_ascii = pretty_ascii > 1 |
122 | 383 | for i = 1 to length(a) do |
123 | 3062 | if sequence(a[i]) and length(a[i]) > 0 then |
124 | 114 | multi_line = 1 |
125 | 114 | all_ascii = 0 |
126 | 114 | exit |
127 | end if | |
128 | 2948 | if not integer(a[i]) or |
129 | (a[i] < pretty_ascii_min and | |
130 | (pretty_ascii < 2 or not find(a[i], "\t\r\n\\"))) or | |
131 | a[i] > pretty_ascii_max then | |
132 | 32 | all_ascii = 0 |
133 | end if | |
134 | 2948 | end for |
135 | ||
136 | 383 | if all_ascii then |
137 | 32 | pretty_out('\"') |
138 | else | |
139 | 351 | pretty_out('{') |
140 | end if | |
141 | 383 | pretty_level += 1 |
142 | 383 | for i = 1 to length(a) do |
143 | 3285 | if multi_line then |
144 | 339 | indent() |
145 | end if | |
146 | 3285 | if all_ascii then |
147 | 233 | pretty_out(esc_char(a[i])) |
148 | else | |
149 | 3052 | rPrint(a[i]) |
150 | end if | |
151 | 3285 | if pretty_line_count >= pretty_line_max then |
152 | 0 | if not pretty_dots then |
153 | 0 | pretty_out(" ...") |
154 | end if | |
155 | 0 | pretty_dots = 1 |
156 | 0 | return |
157 | end if | |
158 | 3285 | if i != length(a) and not all_ascii then |
159 | 2701 | pretty_out(',') |
160 | 2701 | cut_line(6) |
161 | end if | |
162 | 3285 | end for |
163 | 383 | pretty_level -= 1 |
164 | 383 | if multi_line then |
165 | 114 | indent() |
166 | end if | |
167 | 383 | if all_ascii then |
168 | 32 | pretty_out('\"') |
169 | else | |
170 | 351 | pretty_out('}') |
171 | end if | |
172 | end if | |
173 | 3139 | end procedure |
174 | ||
175 | 101 | ifdef UNIX then |
176 | 101 | public constant PRETTY_DEFAULT = {1, 2, 1, 78, "%d", "%.10g", 32, 126, 1000000000, 1} |
177 | elsedef | |
178 | public constant PRETTY_DEFAULT = {1, 2, 1, 78, "%d", "%.10g", 32, 127, 1000000000, 1} | |
179 | end ifdef | |
180 | ||
181 | public enum | |
182 | 101 | DISPLAY_ASCII = 1, |
183 | 101 | INDENT, |
184 | 101 | START_COLUMN, |
185 | 101 | WRAP, |
186 | 101 | INT_FORMAT, |
187 | 101 | FP_FORMAT, |
188 | 101 | MIN_ASCII, |
189 | 101 | MAX_ASCII, |
190 | 101 | MAX_LINES, |
191 | 101 | LINE_BREAKS |
192 | ||
193 | --**** | |
194 | -- === Routines | |
195 | ||
196 | 87 | |
197 | 87 | if length(options) < length( PRETTY_DEFAULT ) then |
198 | 52 | options &= PRETTY_DEFAULT[length(options)+1..$] |
199 | end if | |
200 | ||
201 | -- set options | |
202 | 87 | pretty_ascii = options[DISPLAY_ASCII] |
203 | 87 | pretty_indent = options[INDENT] |
204 | 87 | pretty_start_col = options[START_COLUMN] |
205 | 87 | pretty_end_col = options[WRAP] |
206 | 87 | pretty_int_format = options[INT_FORMAT] |
207 | 87 | pretty_fp_format = options[FP_FORMAT] |
208 | 87 | pretty_ascii_min = options[MIN_ASCII] |
209 | 87 | pretty_ascii_max = options[MAX_ASCII] |
210 | 87 | pretty_line_max = options[MAX_LINES] |
211 | 87 | pretty_line_breaks = options[LINE_BREAKS] |
212 | ||
213 | 87 | pretty_chars = pretty_start_col |
214 | ||
215 | 87 | pretty_level = 0 |
216 | 87 | pretty_line = "" |
217 | 87 | pretty_line_count = 0 |
218 | 87 | pretty_dots = 0 |
219 | 87 | rPrint(x) |
220 | 87 | end procedure |
221 | ||
222 | --** | |
223 | -- Print an object to a file or device, using braces { , , , }, indentation, and multiple lines | |
224 | -- to show the structure. | |
225 | -- | |
226 | -- Parameters: | |
227 | -- # ##fn## : an integer, the file/device number to write to | |
228 | -- # ##x## : the object to display/convert to printable form | |
229 | -- # ##options## : is an (up to) 10-element options sequence. | |
230 | -- | |
231 | -- Comments: | |
232 | -- | |
233 | -- Pass {} in ##options## to select the defaults, or set options as below: | |
234 | -- # display ASCII characters: | |
235 | -- ** 0 ~-- never | |
236 | -- ** 1 ~-- alongside any integers in printable ASCII range (default) | |
237 | -- ** 2 ~-- display as "string" when all integers of a sequence | |
238 | -- are in ASCII range | |
239 | -- ** 3 ~-- show strings, and quoted characters (only) for any integers | |
240 | -- in ASCII range as well as the characters: \t \r \n | |
241 | -- # amount to indent for each level of sequence nesting ~-- default: 2 | |
242 | -- # column we are starting at ~-- default: 1 | |
243 | -- # approximate column to wrap at ~-- default: 78 | |
244 | -- # format to use for integers ~-- default: "%d" | |
245 | -- # format to use for floating-point numbers ~-- default: "%.10g" | |
246 | -- # minimum value for printable ASCII ~-- default 32 | |
247 | -- # maximum value for printable ASCII ~-- default 127 | |
248 | -- # maximum number of lines to output | |
249 | -- # line breaks between elements ~-- default 1 (0 = no line breaks, -1 = line breaks to wrap only) | |
250 | -- | |
251 | -- If the length is less than 10, unspecified options at | |
252 | -- the end of the sequence will keep the default values. | |
253 | -- e.g. {0, 5} will choose "never display ASCII", | |
254 | -- plus 5-character indentation, with defaults for everything else. | |
255 | -- | |
256 | -- The default options can be applied using the public constant ##PRETTY_DEFAULT##, and the | |
257 | -- elements may be accessed using the following public enum~: | |
258 | -- | |
259 | -- # ##DISPLAY_ASCII## | |
260 | -- # ##INDENT## | |
261 | -- # ##START_COLUMN## | |
262 | -- # ##WRAP## | |
263 | -- # ##INT_FORMAT## | |
264 | -- # ##FP_FORMAT## | |
265 | -- # ##MIN_ASCII## | |
266 | -- # ##MAX_ASCII## | |
267 | -- # ##MAX_LINES## | |
268 | -- # ##LINE_BREAKS## | |
269 | -- | |
270 | -- The display will start at the current cursor position. Normally you will want to call | |
271 | -- ##pretty_print##() when the cursor is in column 1 (after printing a \n character). | |
272 | -- If you want to start in a different column, you should call ##position##() and specify a value | |
273 | -- for option [3]. This will ensure that the first and last braces in a sequence line up | |
274 | -- vertically. | |
275 | -- | |
276 | -- When specifying the format to use for integers and floating-point numbers, you can add | |
277 | -- some decoration, e.g. "(%d)" or "$ %.2f" | |
278 | -- | |
279 | -- Example 1: | |
280 | -- | |
281 | -- pretty_print(1, "ABC", {}) | |
282 | -- | |
283 | -- {65'A',66'B',67'C'} | |
284 | -- | |
285 | -- | |
286 | -- Example 2: | |
287 | -- | |
288 | -- pretty_print(1, {{1,2,3}, {4,5,6}}, {}) | |
289 | -- | |
290 | -- { | |
291 | -- {1,2,3}, | |
292 | -- {4,5,6} | |
293 | -- } | |
294 | -- | |
295 | -- | |
296 | -- Example 3: | |
297 | -- | |
298 | -- pretty_print(1, {"Euphoria", "Programming", "Language"}, {2}) | |
299 | -- | |
300 | -- { | |
301 | -- "Euphoria", | |
302 | -- "Programming", | |
303 | -- "Language" | |
304 | -- } | |
305 | -- | |
306 | -- | |
307 | -- Example 4: | |
308 | -- | |
309 | -- puts(1, "word_list = ") -- moves cursor to column 13 | |
310 | -- pretty_print(1, | |
311 | -- {{"Euphoria", 8, 5.3}, | |
312 | -- {"Programming", 11, -2.9}, | |
313 | -- {"Language", 8, 9.8}}, | |
314 | -- {2, 4, 13, 78, "%03d", "%.3f"}) -- first 6 of 8 options | |
315 | -- | |
316 | -- word_list = { | |
317 | -- { | |
318 | -- "Euphoria", | |
319 | -- 008, | |
320 | -- 5.300 | |
321 | -- }, | |
322 | -- { | |
323 | -- "Programming", | |
324 | -- 011, | |
325 | -- -2.900 | |
326 | -- }, | |
327 | -- { | |
328 | -- "Language", | |
329 | -- 008, | |
330 | -- 9.800 | |
331 | -- } | |
332 | -- } | |
333 | -- | |
334 | -- | |
335 | -- See Also: | |
336 | -- [[:print]], [[:sprint]], [[:printf]], [[:sprintf]], [[:pretty_sprint]] | |
337 | -- | |
338 | 16 | |
339 | 16 | pretty_printing = 1 |
340 | 16 | pretty_file = fn |
341 | 16 | pretty( x, options ) |
342 | 16 | puts(pretty_file, pretty_line) |
343 | 16 | end procedure |
344 | ||
345 | --** | |
346 | -- Format an object using braces { , , , }, indentation, and multiple lines to show the structure. | |
347 | -- | |
348 | -- Parameters: | |
349 | -- # ##x## : the object to display | |
350 | -- # ##options## : is an (up to) 10-element options sequence: Pass {} to select the defaults, or | |
351 | -- set options | |
352 | -- | |
353 | -- Returns: | |
354 | -- A **sequence**, of printable characters, representing ##x## in an human-readable form. | |
355 | -- | |
356 | -- Comments: | |
357 | -- | |
358 | -- This function formats objects the same as [[:pretty_print]](), but returns the sequence obtained instead of sending it to some file.. | |
359 | -- | |
360 | -- See Also: | |
361 | -- [[:pretty_print]], [[:sprint]] | |
362 | ||
363 | 71 | |
364 | 71 | pretty_printing = 0 |
365 | 71 | pretty( x, options ) |
366 | 71 | return pretty_line |
367 | end function |