manen

Fancy Lua REPL
Log | Files | Refs | README | LICENSE

commit 9aa724953d5ce5de3b70315cfbd22613ad3e3036
parent a0e92848960307401dbeba76ebbd24298cfb3d67
Author: Sylvia Ivory <git@sivory.net>
Date:   Fri, 20 Jun 2025 22:18:53 -0700

Print arrays prettier in ComfyTable

Diffstat:
Msrc/editor.rs | 45+++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 43 insertions(+), 2 deletions(-)

diff --git a/src/editor.rs b/src/editor.rs @@ -1,4 +1,4 @@ -use std::collections::{HashMap, HashSet}; +use std::collections::HashMap; use comfy_table::{presets::UTF8_FULL, Table}; use mlua::prelude::*; @@ -43,7 +43,7 @@ impl Editor { editor, lua, - table_format: TableFormat::Inspect, + table_format: TableFormat::ComfyTable(true), }) } @@ -89,6 +89,41 @@ impl Editor { } } + fn is_array(tbl: &LuaTable) -> LuaResult<(bool, bool)> { + let mut is_array = true; + let mut has_table = false; + + for (key, value) in tbl.pairs::<LuaValue, LuaValue>().flatten() { + if !(key.is_integer() || key.is_number()) { + is_array = false; + } + + if let LuaValue::Table(inner) = value { + let (is_array, has_tbl) = Self::is_array(&inner)?; + + if !is_array || has_tbl { + has_table = true; + } + } + } + + Ok((is_array, has_table)) + } + + fn print_array(tbl: &LuaTable) -> LuaResult<String> { + let mut buff = Vec::new(); + + for (_, value) in tbl.pairs::<LuaValue, LuaValue>().flatten() { + if let LuaValue::Table(inner) = value { + buff.push(Self::print_array(&inner)?); + } else { + buff.push(Self::lua_to_string(&value)?); + } + } + + Ok(format!("{{ {} }}", buff.join(", "))) + } + fn pretty_table(tbl: &LuaTable, recursive: bool, visited: &mut HashMap<String, usize>) -> LuaResult<String> { let addr = Self::addr_tbl(tbl); @@ -99,6 +134,12 @@ impl Editor { let id = visited.len(); visited.insert(addr.clone(), id); + let (is_array, has_table) = Self::is_array(tbl)?; + + if is_array && !has_table { + return Self::print_array(tbl) + } + let mut table = Table::new(); table.load_preset(UTF8_FULL); table.set_header(vec![format!("<table {id}>")]);