add option to disable fancy output

This commit is contained in:
LordMZTE 2022-02-06 21:30:16 +01:00
parent 9dcb326a78
commit 0916af8a83
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
4 changed files with 59 additions and 25 deletions

View file

@ -2,15 +2,15 @@ use std::{cell::RefCell, rc::Rc};
use mlua::{DeserializeOptions, Function, Lua, LuaSerdeExt, Value}; 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| { lua.scope(|s| {
let output = Rc::new(RefCell::new(String::new())); 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 output_ = Rc::clone(&output);
let print = s.create_function(move |lua, x: Value| { let print = s.create_function(move |_lua, x: Value| {
let s = lua let s = tostring_.call::<_, String>(x)?;
.globals()
.get::<_, Function>("tostring")?
.call::<_, String>(x)?;
let mut output = output_.borrow_mut(); let mut output = output_.borrow_mut();
output.push_str(&s); 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)?; lua.globals().set("print", print)?;
let res = lua.from_value_with::<serde_json::Value>( let eval_res = lua.load(src).eval()?;
lua.load(src).eval()?,
DeserializeOptions::new() let res = if fancy {
.deny_unsupported_types(false) let val = lua.from_value_with::<serde_json::Value>(
.deny_recursive_tables(false), eval_res,
)?; DeserializeOptions::new()
let res = serde_json::to_string_pretty(&res).unwrap(); .deny_unsupported_types(false)
.deny_recursive_tables(false),
)?;
serde_json::to_string_pretty(&val).unwrap()
} else {
tostring.call(eval_res)?
};
Ok((output.take(), res)) Ok((output.take(), res))
}) })

View file

@ -1,7 +1,7 @@
use relm4::{gtk, RelmApp}; use relm4::{gtk, RelmApp};
use std::cell::RefCell; use std::cell::RefCell;
use tree_sitter_highlight::Highlighter;
use tree_sitter::Language; use tree_sitter::Language;
use tree_sitter_highlight::Highlighter;
mod hl; mod hl;
mod lua; mod lua;

View file

@ -13,6 +13,7 @@ pub enum Entry {
src: TextBuffer, src: TextBuffer,
out: String, out: String,
result: TextBuffer, result: TextBuffer,
fancy: bool,
}, },
} }
@ -26,9 +27,11 @@ impl FactoryPrototype for Entry {
fn position(&self, _key: &DynamicIndex) {} fn position(&self, _key: &DynamicIndex) {}
fn init_view(&self, _key: &DynamicIndex, _sender: relm4::Sender<Self::Msg>) -> Self::Widgets { fn init_view(&self, _key: &DynamicIndex, _sender: relm4::Sender<Self::Msg>) -> Self::Widgets {
let (first_buf, second_buf) = match self { let (first_buf, second_buf, fancy) = match self {
Self::Error(e) => (e, None), Self::Error(e) => (e, None, false),
Self::LuaData { src, result, .. } => (src, Some(result)), Self::LuaData {
src, result, fancy, ..
} => (src, Some(result), *fancy),
}; };
if let Some(json) = second_buf { if let Some(json) = second_buf {
@ -38,11 +41,13 @@ impl FactoryPrototype for Entry {
hl::LUA_HL_QUERY, hl::LUA_HL_QUERY,
); );
highlight_text_buffer( if fancy {
json, highlight_text_buffer(
unsafe { crate::tree_sitter_json() }, json,
hl::JSON_HL_QUERY, unsafe { crate::tree_sitter_json() },
); hl::JSON_HL_QUERY,
);
}
} else { } else {
first_buf.apply_tag( first_buf.apply_tag(
&first_buf.tag_table().lookup("error").unwrap(), &first_buf.tag_table().lookup("error").unwrap(),

View file

@ -29,6 +29,7 @@ pub struct AppModel {
loading: bool, loading: bool,
history: Vec<String>, history: Vec<String>,
history_idx: usize, history_idx: usize,
fancy: bool,
} }
impl AppModel { impl AppModel {
@ -70,6 +71,7 @@ impl AppModel {
loading: false, loading: false,
history: Vec::new(), history: Vec::new(),
history_idx: 0, history_idx: 0,
fancy: true,
}) })
} }
} }
@ -89,7 +91,8 @@ impl AppUpdate for AppModel {
.text(&self.input.start_iter(), &self.input.end_iter(), false) .text(&self.input.start_iter(), &self.input.end_iter(), false)
.to_string(); .to_string();
let lua = Arc::clone(&self.lua); 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)) => { Ok((out, r)) => {
send!( send!(
sender, sender,
@ -97,6 +100,7 @@ impl AppUpdate for AppModel {
src, src,
result: r, result: r,
out, out,
fancy,
}) })
); );
send!(sender, AppMsg::ClearInput); send!(sender, AppMsg::ClearInput);
@ -113,7 +117,12 @@ impl AppUpdate for AppModel {
.text(&e) .text(&e)
.build(), .build(),
), ),
StringEntry::LuaData { src, out, result } => Entry::LuaData { StringEntry::LuaData {
src,
out,
result,
fancy,
} => Entry::LuaData {
src: gtk::TextBuffer::builder() src: gtk::TextBuffer::builder()
.tag_table(&self.input.tag_table()) .tag_table(&self.input.tag_table())
.text(&src) .text(&src)
@ -123,6 +132,7 @@ impl AppUpdate for AppModel {
.text(&result) .text(&result)
.build(), .build(),
out, out,
fancy,
}, },
}); });
self.loading = false; self.loading = false;
@ -177,6 +187,7 @@ impl AppUpdate for AppModel {
} }
send!(sender, AppMsg::InputUpdate); send!(sender, AppMsg::InputUpdate);
}, },
AppMsg::FancyToggle(s) => self.fancy = s,
} }
true true
} }
@ -194,6 +205,7 @@ pub enum StringEntry {
src: String, src: String,
out: String, out: String,
result: String, result: String,
fancy: bool,
}, },
} }
@ -204,6 +216,7 @@ pub enum AppMsg {
ClearEntries, ClearEntries,
InputUpdate, InputUpdate,
History(HistoryChange), History(HistoryChange),
FancyToggle(bool),
} }
pub enum HistoryChange { pub enum HistoryChange {
@ -274,8 +287,18 @@ impl Widgets<AppModel, ()> for AppWidgets {
append = &gtk::Button { append = &gtk::Button {
set_label: "Clear List", set_label: "Clear List",
connect_clicked(sender) => move |_| send!(sender, AppMsg::ClearEntries), connect_clicked(sender) => move |_| {
send!(sender, AppMsg::ClearEntries);
}
}, },
append = &gtk::CheckButton {
set_label: Some("Fancy"),
set_active: true,
connect_toggled(sender) => move |btn| {
send!(sender, AppMsg::FancyToggle(btn.is_active()));
}
}
}, },
}, },
} }