1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-23 05:12:40 +03:00

Add ResetTerminal and pane:inject_output method

refs: https://github.com/wez/wezterm/discussions/2606
This commit is contained in:
Wez Furlong 2022-10-08 10:21:43 -07:00
parent 473316934b
commit 4e5945c061
9 changed files with 88 additions and 1 deletions

1
Cargo.lock generated
View File

@ -2662,6 +2662,7 @@ dependencies = [
"mux", "mux",
"portable-pty", "portable-pty",
"smol", "smol",
"termwiz",
"wezterm-dynamic", "wezterm-dynamic",
"wezterm-term", "wezterm-term",
] ]

View File

@ -531,6 +531,8 @@ pub enum KeyAssignment {
SplitPane(SplitPane), SplitPane(SplitPane),
PaneSelect(PaneSelectArguments), PaneSelect(PaneSelectArguments),
CharSelect(CharSelectArguments), CharSelect(CharSelectArguments),
ResetTerminal,
} }
impl_lua_conversion_dynamic!(KeyAssignment); impl_lua_conversion_dynamic!(KeyAssignment);

View File

@ -39,6 +39,8 @@ As features stabilize some brief notes about them will accumulate here.
[#2537](https://github.com/wez/wezterm/discussions/2537) [#2537](https://github.com/wez/wezterm/discussions/2537)
* [window-focus-changed](config/lua/window-events/window-focus-changed.md) * [window-focus-changed](config/lua/window-events/window-focus-changed.md)
event. event.
* [pane:inject_output](config/lua/pane/inject_output.md) method
* [ResetTerminal](config/lua/keyassignment/ResetTerminal.md) key assignment
#### Fixed #### Fixed
* Wayland: key repeat gets stuck after pressing two keys in quick succession. * Wayland: key repeat gets stuck after pressing two keys in quick succession.

View File

@ -0,0 +1,11 @@
# ResetTerminal
*since: nightly builds only*
Sends the `RIS` "Reset to Initial State" escape sequence (`ESC-c`) to the
output side of the current pane, causing the terminal emulator to reset its
state.
This will reset tab stops, margins, modes, graphic rendition, palette, activate
the primary screen, erase the display and move the cursor to the home position.

View File

@ -0,0 +1,34 @@
# `pane:inject_output(text)`
*Since: nightly builds only*
Sends text, which may include escape sequences, to the output side of the
current pane. The text will be evaluated by the terminal emulator and can thus
be used to inject/force the terminal to process escape sequences that adjust
the current mode, as well as sending human readable output to the terminal.
Note that if you move the cursor position as a result of using this method, you
should expect the display to change and for text UI programs to get confused.
In this contrived and useless example, pressing ALT-k will output `hello there`
in italics to the current pane:
```lua
local wezterm = require 'wezterm'
return {
keys = {
{
key = 'k',
mods = 'ALT',
action = wezterm.action_callback(function(window, pane)
pane:inject_output '\r\n\x1b[3mhello there\r\n'
end),
},
},
}
```
Not all panes support this method; at the time of writing, this works for local
panes but not for multiplexer panes.

View File

@ -15,4 +15,5 @@ log = "0.4"
luahelper = { path = "../../luahelper" } luahelper = { path = "../../luahelper" }
portable-pty = { path = "../../pty" } portable-pty = { path = "../../pty" }
smol = "1.2" smol = "1.2"
termwiz = { path = "../../termwiz" }
mux = { path = "../../mux" } mux = { path = "../../mux" }

View File

@ -196,6 +196,18 @@ impl UserData for MuxPane {
None => Ok("".to_string()), None => Ok("".to_string()),
} }
}); });
methods.add_method("inject_output", |_, this, text: String| {
let mux = get_mux()?;
let pane = this.resolve(&mux)?;
let mut parser = termwiz::escape::parser::Parser::new();
let mut actions = vec![];
parser.parse(text.as_bytes(), |action| actions.push(action));
pane.perform_actions(actions);
Ok(())
});
} }
} }

View File

@ -244,6 +244,25 @@ impl UserData for GuiWin {
Ok(result) Ok(result)
}); });
methods.add_async_method("active_pane", |_, this, _: ()| async move {
let (tx, rx) = smol::channel::bounded(1);
this.window
.notify(TermWindowNotif::Apply(Box::new(move |term_window| {
tx.try_send(
term_window
.get_active_pane_or_overlay()
.map(|pane| MuxPane(pane.pane_id())),
)
.ok();
})));
let result = rx
.recv()
.await
.map_err(|e| anyhow::anyhow!("{:#}", e))
.map_err(luaerr)?;
Ok(result)
});
methods.add_method("active_workspace", |_, _, _: ()| { methods.add_method("active_workspace", |_, _, _: ()| {
let mux = Mux::get() let mux = Mux::get()
.ok_or_else(|| anyhow::anyhow!("must be called on main thread")) .ok_or_else(|| anyhow::anyhow!("must be called on main thread"))

View File

@ -2719,6 +2719,11 @@ impl TermWindow {
let modal = crate::termwindow::charselect::CharSelector::new(self, args); let modal = crate::termwindow::charselect::CharSelector::new(self, args);
self.modal.borrow_mut().replace(Rc::new(modal)); self.modal.borrow_mut().replace(Rc::new(modal));
} }
ResetTerminal => {
pane.perform_actions(vec![termwiz::escape::Action::Esc(
termwiz::escape::Esc::Code(termwiz::escape::EscCode::FullReset),
)]);
}
}; };
Ok(PerformAssignmentResult::Handled) Ok(PerformAssignmentResult::Handled)
} }
@ -2953,7 +2958,7 @@ impl TermWindow {
/// then that will be returned instead. Otherwise, if the pane has /// then that will be returned instead. Otherwise, if the pane has
/// an active overlay (such as search or copy mode) then that will /// an active overlay (such as search or copy mode) then that will
/// be returned. /// be returned.
fn get_active_pane_or_overlay(&self) -> Option<Rc<dyn Pane>> { pub fn get_active_pane_or_overlay(&self) -> Option<Rc<dyn Pane>> {
let mux = Mux::get().unwrap(); let mux = Mux::get().unwrap();
let tab = match mux.get_active_tab_for_window(self.mux_window_id) { let tab = match mux.get_active_tab_for_window(self.mux_window_id) {
Some(tab) => tab, Some(tab) => tab,