1
1
mirror of https://github.com/wez/wezterm.git synced 2024-10-26 15:52:29 +03:00

ValuePrinter: improve array style table detection

before:

```
> { a = {'a'}, 1, {2} }
[
    1,
    [
        2,
    ],
]
```

after:

```
> { a = {'a'}, 1, {2} }
{
    "a": [
        "a",
    ],
    1: 1,
    2: [
        2,
    ],
}
```

refs: https://github.com/wez/wezterm/pull/4336
This commit is contained in:
Wez Furlong 2023-12-02 08:05:47 -07:00
parent 6d58e5196b
commit 5190c3e6cb
No known key found for this signature in database
GPG Key ID: 7A7F66A31EC9B387

View File

@ -3,7 +3,7 @@
pub use mlua;
use mlua::{IntoLua, Value as LuaValue};
use std::cell::RefCell;
use std::collections::{BTreeMap, HashSet};
use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::rc::Rc;
use wezterm_dynamic::{FromDynamic, ToDynamic, Value as DynValue};
@ -306,6 +306,33 @@ impl<'lua> ValuePrinterHelper<'lua> {
}
}
fn is_array_style_table(t: &mlua::Table) -> bool {
let mut keys = BTreeSet::new();
for pair in t.clone().pairs::<LuaValue, LuaValue>() {
match pair {
Ok((key, _)) => match key {
LuaValue::Integer(i) if i >= 1 => {
keys.insert(i);
}
_ => return false,
},
Err(_) => return false,
}
}
// Now see if we have contiguous keys.
// The BTreeSet will iterate the keys in ascending order.
let mut expect = 1;
for key in keys {
if key != expect {
return false;
}
expect += 1;
}
true
}
impl<'lua> std::fmt::Debug for ValuePrinterHelper<'lua> {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> {
match &self.value {
@ -316,7 +343,7 @@ impl<'lua> std::fmt::Debug for ValuePrinterHelper<'lua> {
self.visited
.borrow_mut()
.insert(self.value.to_pointer() as usize);
if let Ok(true) = t.contains_key(1) {
if is_array_style_table(&t) {
// Treat as list
let mut list = fmt.debug_list();
for value in t.clone().sequence_values() {