lua: save and restore debug overlay repl history
This commit is contained in:
1 parent
a34b3ce777
commit
d41ed68b1a
1 file changed
+41
-5
@@ -5,6 +5,8 @@ use log::Level;
|
||||
use luahelper::ValuePrinter;
|
||||
use mlua::Value;
|
||||
use mux::termwiztermtab::TermWizTerminal;
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use termwiz::cell::{AttributeChange, CellAttributes, Intensity};
|
||||
use termwiz::color::AnsiColor;
|
||||
use termwiz::input::{InputEvent, KeyCode, KeyEvent};
|
||||
@@ -17,6 +19,43 @@ struct LuaReplHost {
|
||||
lua: mlua::Lua,
|
||||
}
|
||||
|
||||
fn history_file_name() -> PathBuf {
|
||||
config::RUNTIME_DIR.join("repl-history")
|
||||
}
|
||||
|
||||
impl LuaReplHost {
|
||||
fn new(lua: mlua::Lua) -> Self {
|
||||
let mut history = BasicHistory::default();
|
||||
if let Ok(data) = std::fs::read_to_string(history_file_name()) {
|
||||
for line in data.lines() {
|
||||
history.add(line);
|
||||
}
|
||||
}
|
||||
Self { history, lua }
|
||||
}
|
||||
|
||||
fn add_history(&mut self, line: &str) {
|
||||
if line.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(last) = self.history.last() {
|
||||
if self.history.get(last).as_deref() == Some(line) {
|
||||
// Don't add duplicate lines
|
||||
return;
|
||||
}
|
||||
}
|
||||
self.history.add(line);
|
||||
if let Ok(mut file) = std::fs::OpenOptions::new()
|
||||
.append(true)
|
||||
.create(true)
|
||||
.open(history_file_name())
|
||||
{
|
||||
writeln!(file, "{}", line).ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn format_lua_err(err: mlua::Error) -> String {
|
||||
match err {
|
||||
mlua::Error::SyntaxError {
|
||||
@@ -98,10 +137,7 @@ pub fn show_debug_overlay(mut term: TermWizTerminal, gui_win: GuiWin) -> anyhow:
|
||||
lua.globals().set("window", gui_win)?;
|
||||
|
||||
let mut latest_log_entry = None;
|
||||
let mut host = Some(LuaReplHost {
|
||||
history: BasicHistory::default(),
|
||||
lua,
|
||||
});
|
||||
let mut host = Some(LuaReplHost::new(lua));
|
||||
|
||||
term.render(&[Change::Title("Debug".to_string())])?;
|
||||
|
||||
@@ -154,7 +190,7 @@ pub fn show_debug_overlay(mut term: TermWizTerminal, gui_win: GuiWin) -> anyhow:
|
||||
if line.is_empty() {
|
||||
continue;
|
||||
}
|
||||
host.as_mut().unwrap().history().add(&line);
|
||||
host.as_mut().unwrap().add_history(&line);
|
||||
|
||||
let passed_host = host.take().unwrap();
|
||||
|
||||
|
||||
Reference in new issue
Block a user