Skip to content

Commit

Permalink
util, feat: support encode_string option in util.inspect.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Aug 19, 2021
1 parent 52f8d08 commit 700ce7b
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 69 deletions.
4 changes: 2 additions & 2 deletions fibjs/src/console/console.cpp
Expand Up @@ -112,7 +112,7 @@ class color_initer {
} s_color_initer;

exlib::string json_format(v8::Local<v8::Value> obj, bool color);
exlib::string table_format(v8::Local<v8::Value> obj, v8::Local<v8::Array> fields, bool color);
exlib::string table_format(v8::Local<v8::Value> obj, v8::Local<v8::Array> fields, bool color, bool encode_string);
result_t util_format(exlib::string fmt, OptArgs args, bool color, exlib::string& retVal);

extern std_logger* s_std;
Expand Down Expand Up @@ -267,7 +267,7 @@ result_t console_base::table(v8::Local<v8::Value> obj)

result_t console_base::table(v8::Local<v8::Value> obj, v8::Local<v8::Array> fields)
{
exlib::string strBuffer = table_format(obj, fields, colors(C_INFO));
exlib::string strBuffer = table_format(obj, fields, colors(C_INFO), true);
asyncLog(C_INFO, strBuffer);
return 0;
}
Expand Down
23 changes: 17 additions & 6 deletions fibjs/src/util/util_format_table.cpp
Expand Up @@ -355,7 +355,7 @@ exlib::string object_format(v8::Local<v8::Value> v, bool color, bool l2 = false)
return buf.str();
}

exlib::string table_format(v8::Local<v8::Value> obj, v8::Local<v8::Array> fields, bool color)
exlib::string table_format(v8::Local<v8::Value> obj, v8::Local<v8::Array> fields, bool color, bool encode_string)
{
if (isSimpleValue(obj))
return json_format(obj, color);
Expand Down Expand Up @@ -402,7 +402,12 @@ exlib::string table_format(v8::Local<v8::Value> obj, v8::Local<v8::Array> fields
if (isSimpleValue(v)) {
if (!b_has_prop) {
value_cols.resize(i);
value_cols.append(json_format(v, color));
if (!encode_string && (v->IsString() || v->IsStringObject())) {
exlib::string val;
GetArgumentValue(isolate->m_isolate, v, val);
value_cols.append(val);
} else
value_cols.append(json_format(v, color));
}
} else {
v8::Local<v8::Object> ro = v8::Local<v8::Object>::Cast(v);
Expand All @@ -424,9 +429,12 @@ exlib::string table_format(v8::Local<v8::Value> obj, v8::Local<v8::Array> fields

JSValue rv = ro->Get(_context, isolate->NewString(row_key));
exlib::string row_value;
if (isSimpleValue(rv))
row_value = json_format(rv, color);
else
if (isSimpleValue(rv)) {
if (!encode_string && (rv->IsString() || rv->IsStringObject()))
GetArgumentValue(isolate->m_isolate, rv, row_value);
else
row_value = json_format(rv, color);
} else
row_value = object_format(rv, color);

it->second.resize(i);
Expand Down Expand Up @@ -526,7 +534,10 @@ result_t util_base::inspect(v8::Local<v8::Value> obj, v8::Local<v8::Object> opti
v8::Local<v8::Array> fields;
GetConfigValue(isolate->m_isolate, options, "fields", fields, true);

retVal = table_format(obj, fields, colors);
bool encode_string = true;
GetConfigValue(isolate->m_isolate, options, "encode_string", encode_string, true);

retVal = table_format(obj, fields, colors, encode_string);
} else
retVal = json_format(obj, colors);
return 0;
Expand Down
1 change: 1 addition & 0 deletions idl/zh-cn/util.idl
Expand Up @@ -30,6 +30,7 @@ module util
{
"colors": false, // 指定是否输出 ansi 作色字符串,缺省为 false
"table": false, // 指定输出 table 格式,缺省为 false
"encode_string": true, // 指定表格中的字符串是否编码,缺省为 true
"fields": [], // 当 table 为 true 时指定显示字段
}
```
Expand Down
166 changes: 105 additions & 61 deletions test/util_test.js
Expand Up @@ -1139,27 +1139,45 @@ describe('util', () => {
assert.equal(util.format('%%%s%%%%', 'hi'), '%hi%%');
});

it("fix: crash on error.", () => {
util.format(new mq.Message());
});

it("table", () => {
function test_table(data, only, expected) {
if (arguments.length === 2) {
expected = only;
only = undefined;
}
var result = util.inspect(data, {
table: true,
fields: only
});
assert.equal('\n' + result + '\n', expected);
it("fix: crash on %d.", () => {
util.format("%d", 2n);
});
});


describe("table", () => {
function test_table(data, only, expected) {
if (arguments.length === 2) {
expected = only;
only = undefined;
}
var result = util.inspect(data, {
table: true,
fields: only
});
assert.equal('\n' + result + '\n', expected);
}

function test_table1(data, opt, expected) {
opt.table = true;
var result = util.inspect(data, opt);
assert.equal('\n' + result + '\n', expected);
}

it("simple value", () => {
test_table(null, '\nnull\n');
test_table(undefined, '\nundefined\n');
test_table(false, '\nfalse\n');
test_table('hi', '\n"hi"\n');
test_table(Symbol(), '\nSymbol()\n');
test_table(util.inspect, '\n[Function inspect]\n');
});

it("simple table", () => {
test_table([1, 2, 3], `
┌─────────┬────────┐
│ (index) │ Values │
Expand Down Expand Up @@ -1206,6 +1224,14 @@ describe('util', () => {
│ b │ │ Symbol() │
│ c │ 10 │ │
└─────────┴────┴──────────┘
`);

test_table({ a: { a: 1, b: 2, c: 3 } }, `
┌─────────┬───┬───┬───┐
│ (index) │ a │ b │ c │
├─────────┼───┼───┼───┤
│ a │ 1 │ 2 │ 3 │
└─────────┴───┴───┴───┘
`);

// test_table(new Map([['a', 1], [Symbol(), [2]]]), `
Expand All @@ -1227,24 +1253,6 @@ describe('util', () => {
// └───────────────────┴──────────┘
// `);

test_table({ a: 1, b: 2 }, ['a'], `
┌─────────┬───┐
│ (index) │ a │
├─────────┼───┤
│ a │ │
│ b │ │
└─────────┴───┘
`);

test_table([{ a: 1, b: 2 }, { a: 3, c: 4 }], ['a'], `
┌─────────┬───┐
│ (index) │ a │
├─────────┼───┤
│ 0 │ 1 │
│ 1 │ 3 │
└─────────┴───┘
`);

// test_table(new Map([[1, 1], [2, 2], [3, 3]]).entries(), `
// ┌───────────────────┬─────┬────────┐
// │ (iteration index) │ Key │ Values │
Expand Down Expand Up @@ -1284,16 +1292,30 @@ describe('util', () => {
// │ 2 │ 3 │
// └───────────────────┴────────┘
// `);
});

it("fields", () => {
test_table({ a: 1, b: 2 }, ['a'], `
┌─────────┬───┐
│ (index) │ a │
├─────────┼───┤
│ a │ │
│ b │ │
└─────────┴───┘
`);

test_table({ a: { a: 1, b: 2, c: 3 } }, `
┌─────────┬───┬───┬───┐
│ (index) │ a │ b │ c │
├─────────┼───┼───┼───┤
│ a │ 1 │ 2 │ 3 │
└─────────┴───┴───┴───┘
test_table([{ a: 1, b: 2 }, { a: 3, c: 4 }], ['a'], `
┌─────────┬───┐
│ (index) │ a │
├─────────┼───┤
│ 0 │ 1 │
│ 1 │ 3 │
└─────────┴───┘
`);

});

it("fields", () => {
test_table({ a: { a: [] } }, `
┌─────────┬────┐
│ (index) │ a │
Expand All @@ -1309,7 +1331,9 @@ describe('util', () => {
│ a │ {} │
└─────────┴────┘
`);
});

it("object/array", () => {
test_table({ a: { a: { a: 1, b: 2, c: 3 } } }, `
┌─────────┬──────────┐
│ (index) │ a │
Expand Down Expand Up @@ -1397,7 +1421,9 @@ describe('util', () => {
│ a │ <Buffer 01 02 03 04 05> │
└─────────┴─────────────────────────┘
`);
});

it("field order", () => {
test_table({ a: [1, 2] }, `
┌─────────┬───┬───┐
│ (index) │ 0 │ 1 │
Expand All @@ -1415,7 +1441,51 @@ describe('util', () => {
│ c │ │ │ │ │ │ 5 │ │
└─────────┴───┴───┴───┴───┴───┴───┴────────┘
`);
});

it("unicode", () => {
test_table({ foo: '¥', bar: '¥' }, `
┌─────────┬────────┐
│ (index) │ Values │
├─────────┼────────┤
│ foo │ "¥" │
│ bar │ "¥" │
└─────────┴────────┘
`);

test_table({ foo: '你好', bar: 'hello' }, `
┌─────────┬─────────┐
│ (index) │ Values │
├─────────┼─────────┤
│ foo │ "你好" │
│ bar │ "hello" │
└─────────┴─────────┘
`);
});

it("encode_string", () => {
test_table1([1, 2, "asd"], { encode_string: false }, `
┌─────────┬────────┐
│ (index) │ Values │
├─────────┼────────┤
│ 0 │ 1 │
│ 1 │ 2 │
│ 2 │ asd │
└─────────┴────────┘
`);

test_table1([1, 2, ["asd"]], { encode_string: false }, `
┌─────────┬─────┬────────┐
│ (index) │ 0 │ Values │
├─────────┼─────┼────────┤
│ 0 │ │ 1 │
│ 1 │ │ 2 │
│ 2 │ asd │ │
└─────────┴─────┴────────┘
`);
});

it("other", () => {
test_table(new Uint8Array([1, 2, 3]), `
┌─────────┬────────┐
│ (index) │ Values │
Expand Down Expand Up @@ -1480,32 +1550,6 @@ describe('util', () => {
└─────────┴──${line}──┘
`);
}

test_table({ foo: '¥', bar: '¥' }, `
┌─────────┬────────┐
│ (index) │ Values │
├─────────┼────────┤
│ foo │ "¥" │
│ bar │ "¥" │
└─────────┴────────┘
`);

test_table({ foo: '你好', bar: 'hello' }, `
┌─────────┬─────────┐
│ (index) │ Values │
├─────────┼─────────┤
│ foo │ "你好" │
│ bar │ "hello" │
└─────────┴─────────┘
`);
});

it("fix: crash on error.", () => {
util.format(new mq.Message());
});

it("fix: crash on %d.", () => {
util.format("%d", 2n);
});
});

Expand Down

0 comments on commit 700ce7b

Please sign in to comment.