diff --git a/src/lua.rs b/src/lua.rs index f78741c..149dce6 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -2,15 +2,15 @@ use std::{cell::RefCell, rc::Rc}; use mlua::{DeserializeOptions, Function, Lua, LuaSerdeExt, Value}; -pub fn try_eval(lua: &Lua, src: &str) -> mlua::Result<(String, String)> { +pub fn try_eval(lua: &Lua, src: &str, fancy: bool) -> mlua::Result<(String, String)> { lua.scope(|s| { let output = Rc::new(RefCell::new(String::new())); + let tostring = lua.globals().get::<_, Function>("tostring")?; + + let tostring_ = tostring.clone(); let output_ = Rc::clone(&output); - let print = s.create_function(move |lua, x: Value| { - let s = lua - .globals() - .get::<_, Function>("tostring")? - .call::<_, String>(x)?; + let print = s.create_function(move |_lua, x: Value| { + let s = tostring_.call::<_, String>(x)?; let mut output = output_.borrow_mut(); output.push_str(&s); @@ -21,13 +21,19 @@ pub fn try_eval(lua: &Lua, src: &str) -> mlua::Result<(String, String)> { lua.globals().set("print", print)?; - let res = lua.from_value_with::( - lua.load(src).eval()?, - DeserializeOptions::new() - .deny_unsupported_types(false) - .deny_recursive_tables(false), - )?; - let res = serde_json::to_string_pretty(&res).unwrap(); + let eval_res = lua.load(src).eval()?; + + let res = if fancy { + let val = lua.from_value_with::( + eval_res, + DeserializeOptions::new() + .deny_unsupported_types(false) + .deny_recursive_tables(false), + )?; + serde_json::to_string_pretty(&val).unwrap() + } else { + tostring.call(eval_res)? + }; Ok((output.take(), res)) }) diff --git a/src/main.rs b/src/main.rs index b693b25..f8a9a86 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use relm4::{gtk, RelmApp}; use std::cell::RefCell; -use tree_sitter_highlight::Highlighter; use tree_sitter::Language; +use tree_sitter_highlight::Highlighter; mod hl; mod lua; diff --git a/src/ui/entry.rs b/src/ui/entry.rs index cbe167d..961c55b 100644 --- a/src/ui/entry.rs +++ b/src/ui/entry.rs @@ -13,6 +13,7 @@ pub enum Entry { src: TextBuffer, out: String, result: TextBuffer, + fancy: bool, }, } @@ -26,9 +27,11 @@ impl FactoryPrototype for Entry { fn position(&self, _key: &DynamicIndex) {} fn init_view(&self, _key: &DynamicIndex, _sender: relm4::Sender) -> Self::Widgets { - let (first_buf, second_buf) = match self { - Self::Error(e) => (e, None), - Self::LuaData { src, result, .. } => (src, Some(result)), + let (first_buf, second_buf, fancy) = match self { + Self::Error(e) => (e, None, false), + Self::LuaData { + src, result, fancy, .. + } => (src, Some(result), *fancy), }; if let Some(json) = second_buf { @@ -38,11 +41,13 @@ impl FactoryPrototype for Entry { hl::LUA_HL_QUERY, ); - highlight_text_buffer( - json, - unsafe { crate::tree_sitter_json() }, - hl::JSON_HL_QUERY, - ); + if fancy { + highlight_text_buffer( + json, + unsafe { crate::tree_sitter_json() }, + hl::JSON_HL_QUERY, + ); + } } else { first_buf.apply_tag( &first_buf.tag_table().lookup("error").unwrap(), diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 6fd9a15..5eb8dc9 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -29,6 +29,7 @@ pub struct AppModel { loading: bool, history: Vec, history_idx: usize, + fancy: bool, } impl AppModel { @@ -70,6 +71,7 @@ impl AppModel { loading: false, history: Vec::new(), history_idx: 0, + fancy: true, }) } } @@ -89,7 +91,8 @@ impl AppUpdate for AppModel { .text(&self.input.start_iter(), &self.input.end_iter(), false) .to_string(); let lua = Arc::clone(&self.lua); - thread::spawn(move || match try_eval(&*lua.lock().unwrap(), &src) { + let fancy = self.fancy; + thread::spawn(move || match try_eval(&*lua.lock().unwrap(), &src, fancy) { Ok((out, r)) => { send!( sender, @@ -97,6 +100,7 @@ impl AppUpdate for AppModel { src, result: r, out, + fancy, }) ); send!(sender, AppMsg::ClearInput); @@ -113,7 +117,12 @@ impl AppUpdate for AppModel { .text(&e) .build(), ), - StringEntry::LuaData { src, out, result } => Entry::LuaData { + StringEntry::LuaData { + src, + out, + result, + fancy, + } => Entry::LuaData { src: gtk::TextBuffer::builder() .tag_table(&self.input.tag_table()) .text(&src) @@ -123,6 +132,7 @@ impl AppUpdate for AppModel { .text(&result) .build(), out, + fancy, }, }); self.loading = false; @@ -177,6 +187,7 @@ impl AppUpdate for AppModel { } send!(sender, AppMsg::InputUpdate); }, + AppMsg::FancyToggle(s) => self.fancy = s, } true } @@ -194,6 +205,7 @@ pub enum StringEntry { src: String, out: String, result: String, + fancy: bool, }, } @@ -204,6 +216,7 @@ pub enum AppMsg { ClearEntries, InputUpdate, History(HistoryChange), + FancyToggle(bool), } pub enum HistoryChange { @@ -274,8 +287,18 @@ impl Widgets for AppWidgets { append = >k::Button { set_label: "Clear List", - connect_clicked(sender) => move |_| send!(sender, AppMsg::ClearEntries), + connect_clicked(sender) => move |_| { + send!(sender, AppMsg::ClearEntries); + } }, + + append = >k::CheckButton { + set_label: Some("Fancy"), + set_active: true, + connect_toggled(sender) => move |btn| { + send!(sender, AppMsg::FancyToggle(btn.is_active())); + } + } }, }, }