add lua repl to the debug overlay
`wezterm` is pre-imported. The repl prints the result of the expressions to the overlay. refs: https://github.com/wez/wezterm/issues/641
This commit is contained in:
1 parent
8b71a57513
commit
09f0421b48
5 files changed
+127
-26
No files matched your search
+4
-4
@@ -1227,9 +1227,9 @@ impl Default for Config {
|
||||
}
|
||||
|
||||
pub struct LoadedConfig {
|
||||
config: Config,
|
||||
file_name: Option<PathBuf>,
|
||||
lua: Option<mlua::Lua>,
|
||||
pub config: Config,
|
||||
pub file_name: Option<PathBuf>,
|
||||
pub lua: Option<mlua::Lua>,
|
||||
}
|
||||
|
||||
struct PathPossibility {
|
||||
@@ -1340,7 +1340,7 @@ impl Config {
|
||||
Ok(LoadedConfig {
|
||||
config: Self::default().compute_extra_defaults(None),
|
||||
file_name: None,
|
||||
lua: None,
|
||||
lua: Some(make_lua_context(Path::new(""))?),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -36,6 +36,8 @@ macro_rules! impl_lua_conversion {
|
||||
};
|
||||
}
|
||||
|
||||
pub use serde_lua::ValueWrapper;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct JsonLua(pub serde_json::Value);
|
||||
|
||||
@@ -66,7 +66,7 @@ impl From<Error> for mlua::Error {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ValueWrapper<'lua>(Value<'lua>);
|
||||
pub struct ValueWrapper<'lua>(pub Value<'lua>);
|
||||
|
||||
impl<'lua> PartialEq for ValueWrapper<'lua> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
|
||||
@@ -25,7 +25,7 @@ use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use termwiz::caps::{Capabilities, ColorLevel, ProbeHints};
|
||||
use termwiz::input::{InputEvent, KeyEvent, MouseEvent as TermWizMouseEvent};
|
||||
use termwiz::input::{InputEvent, KeyEvent, Modifiers, MouseEvent as TermWizMouseEvent};
|
||||
use termwiz::render::terminfo::TerminfoRenderer;
|
||||
use termwiz::surface::Change;
|
||||
use termwiz::surface::Line;
|
||||
@@ -269,6 +269,13 @@ pub struct TermWizTerminal {
|
||||
render_tx: TermWizTerminalRenderTty,
|
||||
input_rx: Receiver<InputEvent>,
|
||||
renderer: TerminfoRenderer,
|
||||
grab_mouse: bool,
|
||||
}
|
||||
|
||||
impl TermWizTerminal {
|
||||
pub fn no_grab_mouse_in_raw_mode(&mut self) {
|
||||
self.grab_mouse = false;
|
||||
}
|
||||
}
|
||||
|
||||
struct TermWizTerminalRenderTty {
|
||||
@@ -328,8 +335,10 @@ impl termwiz::terminal::Terminal for TermWizTerminal {
|
||||
}
|
||||
|
||||
decset!(BracketedPaste);
|
||||
decset!(AnyEventMouse);
|
||||
decset!(SGRMouse);
|
||||
if self.grab_mouse {
|
||||
decset!(AnyEventMouse);
|
||||
decset!(SGRMouse);
|
||||
}
|
||||
self.flush()?;
|
||||
|
||||
Ok(())
|
||||
@@ -371,7 +380,19 @@ impl termwiz::terminal::Terminal for TermWizTerminal {
|
||||
self.render_tx.screen_size.cols = *cols;
|
||||
self.render_tx.screen_size.rows = *rows;
|
||||
}
|
||||
i
|
||||
match i {
|
||||
// Urgh, we get normalized-to-lowercase CTRL-c,
|
||||
// but eg: termwiz and other terminal input expect
|
||||
// to get CTRL-C instead. Adjust for that here.
|
||||
Some(InputEvent::Key(KeyEvent {
|
||||
key: KeyCode::Char(c),
|
||||
modifiers: Modifiers::CTRL,
|
||||
})) if c.is_ascii_lowercase() => Some(InputEvent::Key(KeyEvent {
|
||||
key: KeyCode::Char(c.to_ascii_uppercase()),
|
||||
modifiers: Modifiers::CTRL,
|
||||
})),
|
||||
i @ _ => i,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -401,6 +422,7 @@ pub fn allocate(size: PtySize) -> (TermWizTerminal, Rc<dyn Pane>) {
|
||||
},
|
||||
input_rx,
|
||||
renderer,
|
||||
grab_mouse: true,
|
||||
};
|
||||
|
||||
let domain_id = 0;
|
||||
@@ -465,6 +487,7 @@ pub async fn run<
|
||||
},
|
||||
input_rx,
|
||||
renderer,
|
||||
grab_mouse: true,
|
||||
};
|
||||
|
||||
async fn register_tab(
|
||||
|
||||
@@ -1,16 +1,82 @@
|
||||
use chrono::prelude::*;
|
||||
use log::Level;
|
||||
use luahelper::ValueWrapper;
|
||||
use mlua::Value;
|
||||
use mux::termwiztermtab::TermWizTerminal;
|
||||
use std::time::Duration;
|
||||
use termwiz::cell::{AttributeChange, CellAttributes, Intensity};
|
||||
use termwiz::color::AnsiColor;
|
||||
use termwiz::input::{InputEvent, KeyCode, KeyEvent};
|
||||
use termwiz::lineedit::*;
|
||||
use termwiz::surface::Change;
|
||||
use termwiz::terminal::Terminal;
|
||||
|
||||
struct LuaReplHost {
|
||||
history: BasicHistory,
|
||||
lua: mlua::Lua,
|
||||
}
|
||||
|
||||
impl LineEditorHost for LuaReplHost {
|
||||
fn history(&mut self) -> &mut dyn History {
|
||||
&mut self.history
|
||||
}
|
||||
|
||||
fn resolve_action(
|
||||
&mut self,
|
||||
event: &InputEvent,
|
||||
editor: &mut LineEditor<'_>,
|
||||
) -> Option<Action> {
|
||||
let (line, _cursor) = editor.get_line_and_cursor();
|
||||
if line.is_empty()
|
||||
&& matches!(
|
||||
event,
|
||||
InputEvent::Key(KeyEvent {
|
||||
key: KeyCode::Escape,
|
||||
..
|
||||
})
|
||||
)
|
||||
{
|
||||
Some(Action::Cancel)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn render_preview(&self, line: &str) -> Vec<OutputElement> {
|
||||
let expr = format!("return {}", line);
|
||||
let mut preview = vec![];
|
||||
|
||||
let chunk = self.lua.load(&expr);
|
||||
match chunk.into_function() {
|
||||
Ok(_) => {}
|
||||
Err(err) => {
|
||||
let text = match &err {
|
||||
mlua::Error::SyntaxError {
|
||||
incomplete_input: true,
|
||||
..
|
||||
} => "...".to_string(),
|
||||
_ => format!("{:#}", err),
|
||||
};
|
||||
preview.push(OutputElement::Text(text));
|
||||
}
|
||||
}
|
||||
|
||||
preview
|
||||
}
|
||||
}
|
||||
|
||||
pub fn show_debug_overlay(mut term: TermWizTerminal) -> anyhow::Result<()> {
|
||||
let log_interval = Duration::from_secs(1);
|
||||
term.no_grab_mouse_in_raw_mode();
|
||||
|
||||
let lua = config::Config::load()?
|
||||
.lua
|
||||
.ok_or_else(|| anyhow::anyhow!("failed to setup lua context"))?;
|
||||
lua.load("wezterm = require 'wezterm'").exec()?;
|
||||
|
||||
let mut latest_log_entry = None;
|
||||
let mut host = LuaReplHost {
|
||||
history: BasicHistory::default(),
|
||||
lua,
|
||||
};
|
||||
|
||||
term.render(&[Change::Title("Debug".to_string())])?;
|
||||
|
||||
@@ -61,24 +127,34 @@ pub fn show_debug_overlay(mut term: TermWizTerminal) -> anyhow::Result<()> {
|
||||
term.render(&changes)
|
||||
}
|
||||
|
||||
print_new_log_entries(&mut term, &mut latest_log_entry)?;
|
||||
loop {
|
||||
print_new_log_entries(&mut term, &mut latest_log_entry)?;
|
||||
|
||||
while let Ok(res) = term.poll_input(Some(log_interval)) {
|
||||
match res {
|
||||
Some(event) => match event {
|
||||
InputEvent::Key(KeyEvent {
|
||||
key: KeyCode::Escape,
|
||||
..
|
||||
}) => {
|
||||
break;
|
||||
let mut editor = LineEditor::new(&mut term);
|
||||
editor.set_prompt("> ");
|
||||
if let Some(line) = editor.read_line(&mut host)? {
|
||||
host.history().add(&line);
|
||||
|
||||
let expr = format!("return {}", line);
|
||||
let chunk = host.lua.load(&expr);
|
||||
match chunk.eval::<Value>() {
|
||||
Ok(result) => {
|
||||
let text = format!("{:?}", ValueWrapper(result));
|
||||
term.render(&[Change::Text(format!("{}\r\n", text.replace("\n", "\r\n")))])?;
|
||||
}
|
||||
Err(err) => {
|
||||
let text = match &err {
|
||||
mlua::Error::SyntaxError {
|
||||
incomplete_input: true,
|
||||
..
|
||||
} => "...".to_string(),
|
||||
_ => format!("{:#}", err),
|
||||
};
|
||||
term.render(&[Change::Text(format!("{}\r\n", text.replace("\n", "\r\n")))])?;
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
None => {
|
||||
print_new_log_entries(&mut term, &mut latest_log_entry)?;
|
||||
}
|
||||
} else {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user