manen

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

commit 38afe4005ef57754ea4204a1d0833b51b8ce06cb
parent 6aa6f9b74ff472e22886bc1dcba67b1f10e591fa
Author: Sylvia Ivory <git@sivory.net>
Date:   Sun, 29 Jun 2025 00:24:29 -0700

Reorganize code

Diffstat:
Msrc/editor.rs | 2+-
Dsrc/format.rs | 93-------------------------------------------------------------------------------
Msrc/hinter.rs | 14+++++++++-----
Msrc/inspect.rs | 87+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/main.rs | 4+---
Msrc/validator.rs | 1+
6 files changed, 99 insertions(+), 102 deletions(-)

diff --git a/src/editor.rs b/src/editor.rs @@ -15,7 +15,7 @@ use reedline::{ }; use crate::{ - completion::LuaCompleter, format::TableFormat, hinter::LuaHinter, inspect::display_basic, + completion::LuaCompleter, hinter::LuaHinter, inspect::{display_basic, TableFormat}, parse::LuaHighlighter, validator::LuaValidator, }; diff --git a/src/format.rs b/src/format.rs @@ -1,93 +0,0 @@ -use std::{collections::HashMap, sync::Arc}; - -use comfy_table::{Table, presets::UTF8_FULL_CONDENSED}; -use mlua::prelude::*; -use nu_ansi_term::Color; - -use crate::inspect::{display_basic, display_table, is_short_printable, print_array}; - -#[derive(Clone, Copy)] -pub enum TableFormat { - ComfyTable(bool), - Inspect, - Address, -} - -fn comfy_table_inner( - tbl: &LuaTable, - recursive: bool, - visited: &mut HashMap<usize, usize>, -) -> LuaResult<String> { - let addr = tbl.to_pointer() as usize; - - if let Some(id) = visited.get(&addr) { - return Ok(format!("<table {id}>")); - } - - let id = visited.len(); - visited.insert(addr, id); - - let printable = is_short_printable(tbl); - - if printable { - return Ok(print_array(tbl, false)); - } - - let mut table = Table::new(); - table.load_preset(UTF8_FULL_CONDENSED); - table.set_header(vec![format!("<table {id}>")]); - - for (key, value) in tbl.pairs::<LuaValue, LuaValue>().flatten() { - let (key_str, value_str) = if let LuaValue::Table(sub) = value { - if recursive { - ( - display_basic(&key, false), - comfy_table_inner(&sub, recursive, visited)?, - ) - } else { - ( - display_basic(&key, false), - display_basic(&LuaValue::Table(sub), false), - ) - } - } else { - (display_basic(&key, false), display_basic(&value, false)) - }; - - table.add_row(vec![key_str, value_str]); - } - - if table.is_empty() { - Ok(String::from("{}")) - } else { - Ok(table.to_string()) - } -} - -pub fn comfy_table(tbl: &LuaTable, recursive: bool) -> LuaResult<String> { - let mut visited = HashMap::new(); - comfy_table_inner(tbl, recursive, &mut visited) -} - -impl TableFormat { - pub fn format(&self, tbl: &LuaTable, colorize: bool) -> LuaResult<String> { - match self { - TableFormat::Address => { - if colorize { - Ok(format!( - "{}{}{}", - Color::LightBlue.paint("table"), - Color::Default.paint("@"), - Color::LightYellow.paint(format!("{:?}", tbl.to_pointer())) - )) - } else { - Ok(format!("table@{:?}", tbl.to_pointer())) - } - } - TableFormat::Inspect => { - display_table(tbl, colorize).map_err(|e| LuaError::ExternalError(Arc::new(e))) - } - TableFormat::ComfyTable(recursive) => comfy_table(tbl, *recursive), - } - } -} diff --git a/src/hinter.rs b/src/hinter.rs @@ -36,7 +36,7 @@ impl Hinter for LuaHinter { line: &str, _pos: usize, _history: &dyn History, - _use_ansi_coloring: bool, + use_ansi_coloring: bool, _cwd: &str, ) -> String { let lua = burner_lua(); @@ -56,11 +56,15 @@ impl Hinter for LuaHinter { return String::new(); } - let style = Style::new().fg(Color::DarkGray); + let s = format!(" ({})", display_basic(&value, false)); - style - .paint(format!(" ({})", display_basic(&value, false))) - .to_string() + if use_ansi_coloring { + Color::DarkGray + .paint(s) + .to_string() + } else { + s + } } fn complete_hint(&self) -> String { diff --git a/src/inspect.rs b/src/inspect.rs @@ -5,6 +5,7 @@ use std::{ }; use aho_corasick::AhoCorasick; +use comfy_table::{presets::UTF8_FULL_CONDENSED, Table}; use lazy_static::lazy_static; use mlua::prelude::*; use nu_ansi_term::{AnsiString, AnsiStrings, Color}; @@ -360,3 +361,89 @@ pub fn inspect(value: &LuaValue, colorize: bool) -> LuaResult<String> { value => Ok(display_basic(value, colorize)), } } + +#[derive(Clone, Copy)] +pub enum TableFormat { + ComfyTable(bool), + Inspect, + Address, +} + +fn comfy_table_inner( + tbl: &LuaTable, + recursive: bool, + visited: &mut HashMap<usize, usize>, +) -> LuaResult<String> { + let addr = tbl.to_pointer() as usize; + + if let Some(id) = visited.get(&addr) { + return Ok(format!("<table {id}>")); + } + + let id = visited.len(); + visited.insert(addr, id); + + let printable = is_short_printable(tbl); + + if printable { + return Ok(print_array(tbl, false)); + } + + let mut table = Table::new(); + table.load_preset(UTF8_FULL_CONDENSED); + table.set_header(vec![format!("<table {id}>")]); + + for (key, value) in tbl.pairs::<LuaValue, LuaValue>().flatten() { + let (key_str, value_str) = if let LuaValue::Table(sub) = value { + if recursive { + ( + display_basic(&key, false), + comfy_table_inner(&sub, recursive, visited)?, + ) + } else { + ( + display_basic(&key, false), + display_basic(&LuaValue::Table(sub), false), + ) + } + } else { + (display_basic(&key, false), display_basic(&value, false)) + }; + + table.add_row(vec![key_str, value_str]); + } + + if table.is_empty() { + Ok(String::from("{}")) + } else { + Ok(table.to_string()) + } +} + +pub fn comfy_table(tbl: &LuaTable, recursive: bool) -> LuaResult<String> { + let mut visited = HashMap::new(); + comfy_table_inner(tbl, recursive, &mut visited) +} + +impl TableFormat { + pub fn format(&self, tbl: &LuaTable, colorize: bool) -> LuaResult<String> { + match self { + TableFormat::Address => { + if colorize { + Ok(format!( + "{}{}{}", + Color::LightBlue.paint("table"), + Color::Default.paint("@"), + Color::LightYellow.paint(format!("{:?}", tbl.to_pointer())) + )) + } else { + Ok(format!("table@{:?}", tbl.to_pointer())) + } + } + TableFormat::Inspect => { + display_table(tbl, colorize).map_err(|e| LuaError::ExternalError(Arc::new(e))) + } + TableFormat::ComfyTable(recursive) => comfy_table(tbl, *recursive), + } + } +} diff --git a/src/main.rs b/src/main.rs @@ -11,13 +11,11 @@ use emmylua_parser::{LuaParser, ParserConfig}; use mlua::prelude::*; use reedline::Highlighter; -use format::comfy_table; -use inspect::inspect; +use inspect::{inspect, comfy_table}; use parse::LuaHighlighter; mod completion; mod editor; -mod format; mod hinter; mod inspect; mod parse; diff --git a/src/validator.rs b/src/validator.rs @@ -1,6 +1,7 @@ use mlua::prelude::*; use reedline::{ValidationResult, Validator}; +// TODO; we should instead rely on the parser to determine incomplete input pub struct LuaValidator { lua: Lua, }