1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-22 22:42:48 +03:00

repl: improve printing of various lua types

We can now delegate to the new mlua helpers for a lot of this stuff.
We do still take first dibs on printing userdata so that we can use
our dynamic data interpretation when available, but otherwise, we
can now show reasonable information about things like builtin file
objects, and include the pointer address of things like functions
to help disambiguate them when printing them.

refs: #3849
refs: https://github.com/khvzak/mlua/issues/291
This commit is contained in:
Wez Furlong 2023-07-12 14:15:08 -07:00
parent 750f49f0ae
commit 4924fd5137
No known key found for this signature in database
GPG Key ID: 7A7F66A31EC9B387

View File

@ -307,14 +307,6 @@ impl<'lua> ValuePrinterHelper<'lua> {
impl<'lua> std::fmt::Debug for ValuePrinterHelper<'lua> { impl<'lua> std::fmt::Debug for ValuePrinterHelper<'lua> {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> { fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> {
match &self.value { match &self.value {
LuaValue::Nil => fmt.write_str("nil"),
LuaValue::Boolean(b) => fmt.write_str(if *b { "true" } else { "false" }),
LuaValue::Integer(i) => fmt.write_fmt(format_args!("{}", i)),
LuaValue::Number(i) => fmt.write_fmt(format_args!("{}", i)),
LuaValue::String(s) => match s.to_str() {
Ok(s) => fmt.write_fmt(format_args!("{:?}", s)),
Err(_) => fmt.write_fmt(format_args!("{:?}", s.as_bytes())),
},
LuaValue::Table(t) => { LuaValue::Table(t) => {
self.visited self.visited
.borrow_mut() .borrow_mut()
@ -379,8 +371,8 @@ impl<'lua> std::fmt::Debug for ValuePrinterHelper<'lua> {
fmt.debug_map().entries(&map).finish() fmt.debug_map().entries(&map).finish()
} }
} }
LuaValue::UserData(ud) => match ud.get_metatable() { LuaValue::UserData(ud) => {
Ok(mt) => { if let Ok(mt) = ud.get_metatable() {
if let Ok(to_dynamic) = mt.get::<mlua::Function>("__wezterm_to_dynamic") { if let Ok(to_dynamic) = mt.get::<mlua::Function>("__wezterm_to_dynamic") {
return match to_dynamic.call(LuaValue::UserData(ud.clone())) { return match to_dynamic.call(LuaValue::UserData(ud.clone())) {
Ok(value) => Self { Ok(value) => Self {
@ -391,28 +383,17 @@ impl<'lua> std::fmt::Debug for ValuePrinterHelper<'lua> {
Err(err) => write!(fmt, "Error calling __wezterm_to_dynamic: {err}"), Err(err) => write!(fmt, "Error calling __wezterm_to_dynamic: {err}"),
}; };
} }
match mt.get::<mlua::Function>(mlua::MetaMethod::ToString) {
Ok(to_string) => match to_string.call(LuaValue::UserData(ud.clone())) {
Ok(value) => Self {
visited: Rc::clone(&self.visited),
value,
}
.fmt(fmt),
Err(err) => {
write!(fmt, "Error calling tostring: {err:#}")
}
},
Err(err) => {
write!(fmt, "Error getting tostring: {err:#}")
}
}
} }
Err(_) => fmt.write_str("userdata"), match self.value.to_string() {
}, Ok(s) => fmt.write_str(&s),
LuaValue::LightUserData(_) => fmt.write_str("userdata"), Err(err) => write!(fmt, "userdata ({err:#})"),
LuaValue::Thread(_) => fmt.write_str("thread"), }
LuaValue::Function(_) => fmt.write_str("function"), }
LuaValue::Error(e) => fmt.write_fmt(format_args!("error {}", e)), LuaValue::Error(e) => fmt.write_fmt(format_args!("error {}", e)),
_ => match self.value.to_string() {
Ok(s) => fmt.write_str(&s),
Err(err) => write!(fmt, "({err:#})"),
},
} }
} }
} }