From 9a3421dcf983931e4413be672c84d94fb388853e Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Sat, 24 Feb 2024 22:39:49 +0530 Subject: [PATCH] fix: handle formatting for cyclic tables --- src/lune/util/formatting.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/lune/util/formatting.rs b/src/lune/util/formatting.rs index 2107c14..720dd7f 100644 --- a/src/lune/util/formatting.rs +++ b/src/lune/util/formatting.rs @@ -103,9 +103,9 @@ pub fn style_from_style_str>(s: S) -> LuaResult, depth: usize, ) -> std::fmt::Result { - // TODO: Handle tables with cyclic references match &value { LuaValue::Nil => write!(buffer, "nil")?, LuaValue::Boolean(true) => write!(buffer, "{}", COLOR_YELLOW.apply_to("true"))?, @@ -127,6 +127,8 @@ pub fn pretty_format_value( write!(buffer, "{}", STYLE_DIM.apply_to("{ ... }"))?; } else if let Some(s) = call_table_tostring_metamethod(tab) { write!(buffer, "{s}")?; + } else if depth >= 1 && parent_table_addr.eq(&Some(format!("{:p}", tab))) { + write!(buffer, "{}", STYLE_DIM.apply_to(""))? } else { let mut is_empty = false; let depth_indent = INDENT.repeat(depth); @@ -144,11 +146,11 @@ pub fn pretty_format_value( )?, _ => { write!(buffer, "\n{depth_indent}{INDENT}[")?; - pretty_format_value(buffer, &key, depth)?; + pretty_format_value(buffer, &key, parent_table_addr.clone(), depth)?; write!(buffer, "] {} ", STYLE_DIM.apply_to("="))?; } } - pretty_format_value(buffer, &value, depth + 1)?; + pretty_format_value(buffer, &value, parent_table_addr.clone(), depth + 1)?; write!(buffer, "{}", STYLE_DIM.apply_to(","))?; is_empty = false; } @@ -192,7 +194,7 @@ pub fn pretty_format_multi_value(multi: &LuaMultiValue) -> LuaResult { if let LuaValue::String(s) = value { write!(buffer, "{}", s.to_string_lossy()).into_lua_err()?; } else { - pretty_format_value(&mut buffer, value, 0).into_lua_err()?; + pretty_format_value(&mut buffer, value, Some(format!("{:p}", value)), 0).into_lua_err()?; } if counter < multi.len() { write!(&mut buffer, " ").into_lua_err()?;