add option to disable fancy output
This commit is contained in:
parent
9dcb326a78
commit
0916af8a83
4 changed files with 59 additions and 25 deletions
32
src/lua.rs
32
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::<serde_json::Value>(
|
||||
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::<serde_json::Value>(
|
||||
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))
|
||||
})
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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::Msg>) -> 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(),
|
||||
|
|
|
@ -29,6 +29,7 @@ pub struct AppModel {
|
|||
loading: bool,
|
||||
history: Vec<String>,
|
||||
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<AppModel, ()> 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()));
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue