diff options
author | 2023-09-27 04:47:37 +0200 | |
---|---|---|
committer | 2023-09-26 19:47:37 -0700 | |
commit | d7b43f8ea17694641a355d5659c00563e60d0acf (patch) | |
tree | 5a4115c423ebd6c404fc4e1407914d7ff64acf76 /src | |
parent | 648d5aecf3980997c0672746374068033e632e1d (diff) | |
download | bun-d7b43f8ea17694641a355d5659c00563e60d0acf.tar.gz bun-d7b43f8ea17694641a355d5659c00563e60d0acf.tar.zst bun-d7b43f8ea17694641a355d5659c00563e60d0acf.zip |
fix: support console.dir options object correctly (#6059)
* fix: support console,dir options object correctly
`console.dir` can be passed a second argument which is a object of options.
This implements that logic with the currently supported properties: `depth`
and `colors`.
I used node as a reference for implementation details.
Fixes: https://github.com/oven-sh/bun/issues/6039
* style: format zig file
* fix: implement changes from review
Implements changes requested from review, like adding
more test cases and refactoring code style.
Diffstat (limited to 'src')
-rw-r--r-- | src/bun.js/bindings/exports.zig | 36 |
1 files changed, 28 insertions, 8 deletions
diff --git a/src/bun.js/bindings/exports.zig b/src/bun.js/bindings/exports.zig index 10326bc7c..784a6289c 100644 --- a/src/bun.js/bindings/exports.zig +++ b/src/bun.js/bindings/exports.zig @@ -983,22 +983,42 @@ pub const ZigConsoleClient = struct { else console.writer; var writer = buffered_writer.writer(); - const Writer = @TypeOf(writer); - if (len > 0) + + var print_length = len; + var print_options: FormatOptions = .{ + .enable_colors = enable_colors, + .add_newline = true, + .flush = true, + }; + + if (message_type == .Dir and len >= 2) { + print_length = 1; + var opts = vals[1]; + if (opts.isObject()) { + if (opts.get(global, "depth")) |depth_prop| { + if (depth_prop.isNumber()) + print_options.max_depth = depth_prop.toU16(); + if (depth_prop.isNull() or depth_prop.toInt64() == std.math.maxInt(i64)) + print_options.max_depth = std.math.maxInt(u16); + } + if (opts.get(global, "colors")) |colors_prop| { + if (colors_prop.isBoolean()) + print_options.enable_colors = colors_prop.toBoolean(); + } + } + } + + if (print_length > 0) format( level, global, vals, - len, + print_length, @TypeOf(buffered_writer.unbuffered_writer.context), Writer, writer, - .{ - .enable_colors = enable_colors, - .add_newline = true, - .flush = true, - }, + print_options, ) else if (message_type == .Log) { _ = console.writer.write("\n") catch 0; |