manen

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

commit 63c65c28925f2c9a10364eaf3ce67a08380510a4
parent c3950a8031d85b19ca2479ef357ec9f738994079
Author: Sylvia Ivory <git@sivory.net>
Date:   Fri, 20 Jun 2025 22:55:23 -0700

Add help command

Diffstat:
Msrc/editor.rs | 56+++++++++++++++++++++++++++++++-------------------------
Msrc/format.rs | 33++++++++++++++-------------------
Msrc/highlight.rs | 38++++++++++----------------------------
Msrc/main.rs | 2+-
4 files changed, 56 insertions(+), 73 deletions(-)

diff --git a/src/editor.rs b/src/editor.rs @@ -1,7 +1,10 @@ use mlua::prelude::*; use reedline::{DefaultPrompt, DefaultPromptSegment, Reedline, Signal}; -use crate::{format::{lua_to_string, TableFormat}, highlight::LuaHighlighter}; +use crate::{ + format::{TableFormat, lua_to_string}, + highlight::LuaHighlighter, +}; pub struct Editor { prompt: DefaultPrompt, @@ -21,8 +24,7 @@ impl Editor { DefaultPromptSegment::Empty, ); - let editor = Reedline::create() - .with_highlighter(Box::new(LuaHighlighter::new())); + let editor = Reedline::create().with_highlighter(Box::new(LuaHighlighter::new())); Ok(Self { prompt, @@ -52,41 +54,45 @@ impl Editor { } } // TODO; this should cancel the current Lua execution if possible - Ok(Signal::CtrlC) | Ok(Signal::CtrlD) => { - break - }, + Ok(Signal::CtrlC) | Ok(Signal::CtrlD) => break, _ => {} } } } + // .help // .format <format> [true/false] fn eval_special(&mut self, line: &str) -> LuaResult<()> { - let mut split = line - .strip_prefix(".") - .unwrap() - .split_whitespace(); + let mut split = line.strip_prefix(".").unwrap().split_whitespace(); let cmd = split.next(); match cmd { - Some("format") => { - match split.next() { - Some("inspect") => { - self.table_format = TableFormat::Inspect; - }, - Some("address") => { - self.table_format = TableFormat::Address; - }, - Some("comfytable") => { - let nested = split.next().unwrap_or("").parse::<bool>().unwrap_or_default(); - - self.table_format = TableFormat::ComfyTable(nested); - }, - _ => println!("unknown subcommand") + Some("help") => { + println!(".help\tPrint this message"); + println!( + ".format <inspect|address|comfytable> [true|false]\tConfigure table printing, boolean configures nesting" + ); + } + Some("format") => match split.next() { + Some("inspect") => { + self.table_format = TableFormat::Inspect; + } + Some("address") => { + self.table_format = TableFormat::Address; + } + Some("comfytable") => { + let nested = split + .next() + .unwrap_or("") + .parse::<bool>() + .unwrap_or_default(); + + self.table_format = TableFormat::ComfyTable(nested); } + _ => println!("unknown subcommand"), }, - _ => println!("unknown command") + _ => println!("unknown command"), } Ok(()) diff --git a/src/format.rs b/src/format.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use comfy_table::{presets::UTF8_FULL_CONDENSED, Table}; +use comfy_table::{Table, presets::UTF8_FULL_CONDENSED}; use mlua::prelude::*; const INSPECT_CODE: &str = include_str!("inspect.lua"); @@ -29,7 +29,7 @@ pub fn lua_to_string(value: &LuaValue) -> LuaResult<String> { match value { LuaValue::String(string) => Ok(convert_string(string)), LuaValue::Table(tbl) => Ok(addr_tbl(tbl)), - value => value.to_string() + value => value.to_string(), } } @@ -68,20 +68,24 @@ fn print_array(tbl: &LuaTable) -> LuaResult<String> { Ok(format!("{{ {} }}", buff.join(", "))) } -fn comfy_table(tbl: &LuaTable, recursive: bool, visited: &mut HashMap<String, usize>) -> LuaResult<String> { +fn comfy_table( + tbl: &LuaTable, + recursive: bool, + visited: &mut HashMap<String, usize>, +) -> LuaResult<String> { let addr = addr_tbl(tbl); if let Some(id) = visited.get(&addr) { - return Ok(format!("<table {id}>")) + return Ok(format!("<table {id}>")); } - + let id = visited.len(); visited.insert(addr.clone(), id); let (is_array, has_table) = is_array(tbl)?; if is_array && !has_table { - return print_array(tbl) + return print_array(tbl); } let mut table = Table::new(); @@ -91,21 +95,12 @@ fn comfy_table(tbl: &LuaTable, recursive: bool, visited: &mut HashMap<String, us for (key, value) in tbl.pairs::<LuaValue, LuaValue>().flatten() { let (key_str, value_str) = if let LuaValue::Table(sub) = value { if recursive { - ( - lua_to_string(&key)?, - comfy_table(&sub, recursive, visited)? - ) + (lua_to_string(&key)?, comfy_table(&sub, recursive, visited)?) } else { - ( - lua_to_string(&key)?, - addr.clone(), - ) + (lua_to_string(&key)?, addr.clone()) } } else { - ( - lua_to_string(&key)?, - lua_to_string(&value)?, - ) + (lua_to_string(&key)?, lua_to_string(&value)?) }; table.add_row(vec![key_str, value_str]); @@ -131,7 +126,7 @@ impl TableFormat { self.format(lua, tbl) } - }, + } TableFormat::ComfyTable(recursive) => { let mut visited = HashMap::new(); comfy_table(tbl, *recursive, &mut visited) diff --git a/src/highlight.rs b/src/highlight.rs @@ -9,30 +9,23 @@ const LUA_HIGHLIGHT_NAMES: &[&str] = &[ "keyword.return", "keyword.function", "keyword.operator", - "punctuation.delimiter", "punctuation.bracket", - "variable", "variable.builtin", - "constant", "constant.builtin", - "function", "function.call", "function.builtin", "method", "parameter", - "string", "string.escape", "boolean", "number", - "field", "constructor", - "label", "repeat", "conditional", @@ -61,30 +54,23 @@ const STYLES: &[Style] = &[ style_fg(Color::Purple), style_fg(Color::Purple), style_fg(Color::Purple), - style_fg(Color::LightGray), style_fg(Color::LightRed), - style_fg(Color::LightGray), style_fg(Color::Red), - style_fg(Color::Magenta), style_fg(Color::Magenta), - style_fg(Color::LightBlue), style_fg(Color::LightBlue), style_fg(Color::LightBlue), style_fg(Color::LightBlue), style_fg(Color::LightRed), - style_fg(Color::Green), style_fg(Color::Cyan), style_fg(Color::Yellow), style_fg(Color::Yellow), - style_fg(Color::LightGray), style_fg(Color::LightRed), - style_fg(Color::LightGray), style_fg(Color::Purple), style_fg(Color::Purple), @@ -105,14 +91,15 @@ impl LuaHighlighter { "lua", tree_sitter_lua::HIGHLIGHTS_QUERY, tree_sitter_lua::INJECTIONS_QUERY, - tree_sitter_lua::LOCALS_QUERY - ).unwrap(); + tree_sitter_lua::LOCALS_QUERY, + ) + .unwrap(); config.configure(LUA_HIGHLIGHT_NAMES); Self { highlighter: RefCell::new(highlighter), - config + config, } } } @@ -120,12 +107,7 @@ impl LuaHighlighter { impl reedline::Highlighter for LuaHighlighter { fn highlight(&self, line: &str, _cursor: usize) -> StyledText { let mut binding = self.highlighter.borrow_mut(); - let highlights = binding.highlight( - &self.config, - line.as_bytes(), - None, - |_| None - ); + let highlights = binding.highlight(&self.config, line.as_bytes(), None, |_| None); let mut text = StyledText::new(); @@ -134,19 +116,19 @@ impl reedline::Highlighter for LuaHighlighter { } else { text.push((Style::new(), line.to_string())); - return text + return text; }; let mut style = Style::new(); for event in highlights.flatten() { match event { - HighlightEvent::Source {start, end} => { - text.push((style, line[start..end].to_string())) - }, + HighlightEvent::Source { start, end } => { + text.push((style, line[start..end].to_string())) + } HighlightEvent::HighlightStart(s) => { style = STYLES[s.0]; - }, + } HighlightEvent::HighlightEnd => {} } } diff --git a/src/main.rs b/src/main.rs @@ -1,8 +1,8 @@ use editor::Editor; mod editor; -mod highlight; mod format; +mod highlight; fn main() -> color_eyre::Result<()> { Editor::new()?.run();