From baf990e65c62f42e45fe25ac385db9536d3f1788 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 27 Apr 2025 16:49:38 -0400 Subject: Update stdlib to use `print` instead of `printf` in all cases. This means bringing in fpconv to do float-to-string conversion and a few updates to integer and number methods for string formatting. --- src/stdlib/stdlib.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'src/stdlib/stdlib.c') diff --git a/src/stdlib/stdlib.c b/src/stdlib/stdlib.c index 7ee249ab..71257b8e 100644 --- a/src/stdlib/stdlib.c +++ b/src/stdlib/stdlib.c @@ -465,26 +465,33 @@ public void start_inspect(const char *filename, int64_t start, int64_t end) file = load_file(filename); if (file) { - const char *spaces = " "; - int64_t first_line_len = (int64_t)strcspn(file->text + start, "\r\n"); + size_t first_line_len = strcspn(file->text + start, "\r\n"); const char *slash = strrchr(filename, '/'); const char *file_base = slash ? slash + 1 : filename; int64_t line_num = get_line_number(file, file->text + start); - fprintf(stdout, USE_COLOR ? "%.*s\x1b[33;1m>> \x1b[m%.*s %.*s\x1b[32;2m[%s:%ld]\x1b[m\n" : "%.*s>> %.*s %.*s[%s:%ld]\n", - 3*_inspect_depth, spaces, first_line_len, file->text + start, - MAX(0, 35-first_line_len-3*_inspect_depth), spaces, file_base, line_num); + if (USE_COLOR) { + print(repeated_char(' ', 3*_inspect_depth), "\x1b[33;1m>> \x1b[m", + string_slice(file->text + start, first_line_len), + " ", repeated_char(' ', MAX(0, 35-(int64_t)first_line_len-3*_inspect_depth)), + "\x1b[32;2m[", file_base, ":", line_num, "]\x1b[m"); + } else { + print(repeated_char(' ', 3*_inspect_depth), ">> ", + string_slice(file->text + start, first_line_len), + " ", repeated_char(' ', MAX(0, 35-(int64_t)first_line_len-3*_inspect_depth)), + "[", file_base, ":", line_num, "]"); + } // For multi-line expressions, dedent each and print it on a new line with ".. " in front: - if (end > start + first_line_len) { + if (end > start + (int64_t)first_line_len) { const char *line_start = get_line(file, line_num); int64_t indent_len = (int64_t)strspn(line_start, " \t"); for (const char *line = file->text + start + first_line_len; line < file->text + end; line += strcspn(line, "\r\n")) { line += strspn(line, "\r\n"); if ((int64_t)strspn(line, " \t") >= indent_len) line += indent_len; - fprintf(stdout, USE_COLOR ? "%.*s\x1b[33m.. \x1b[m%.*s\n" : "%.*s.. %.*s\n", - 3*_inspect_depth, spaces, strcspn(line, "\r\n"), line); + print(repeated_char(' ', 3*_inspect_depth), USE_COLOR ? "\x1b[33m.. " : ".. ", + string_slice(line, strcspn(line, "\r\n"))); } } } -- cgit v1.2.3