Files
LLeMbas-TTY/wezterm/src/scripting/pane.rs
T
Wez Furlong ab2b2cff45 Add pane:get_lines_as_text() lua method
Allows this:

```lua
local wezterm = require 'wezterm';
local io = require 'io';
local os = require 'os';

wezterm.on("trigger-vim-with-scrollback", function(window, pane)
  -- Retrieve the current viewport's text.
  -- Pass an optional number of lines (eg: 2000) to retrieve
  -- that number of lines starting from the bottom of the viewport
  local scrollback = pane:get_lines_as_text();

  -- Create a temporary file to pass to vim
  local name = os.tmpname();
  local f = io.open(name, "w+");
  f:write(scrollback);
  f:flush();
  f:close();

  -- Open a new window running vim and tell it to open the file
  window:perform_action(wezterm.action{SpawnCommandInNewWindow={
    args={"vim", name}}
  }, pane)

  -- After vim is up and running we should remove the file,
  -- but we can't do this right now as the window and processing
  -- spawning are asynchronous and we'll probably delete the
  -- file while vim is starting up :-/
  -- os.remove(name);
end)

return {
  keys = {
    {key="E", mods="CTRL",
      action=wezterm.action{EmitEvent="trigger-vim-with-scrollback"}},
  }
}
```

refs: #222
2020-10-09 16:30:04 -07:00

78 lines
2.8 KiB
Rust

//! PaneObject represents a Mux Pane instance in lua code
use super::luaerr;
use anyhow::anyhow;
use mlua::{UserData, UserDataMethods};
use mux::pane::{Pane, PaneId};
use mux::Mux;
use std::rc::Rc;
#[derive(Clone)]
pub struct PaneObject {
pane: PaneId,
}
impl PaneObject {
pub fn new(pane: &Rc<dyn Pane>) -> Self {
Self {
pane: pane.pane_id(),
}
}
pub fn pane(&self) -> mlua::Result<Rc<dyn Pane>> {
let mux = Mux::get()
.ok_or_else(|| anyhow!("must be called on main thread"))
.map_err(luaerr)?;
mux.get_pane(self.pane)
.ok_or_else(|| anyhow!("pane id {} is not valid", self.pane))
.map_err(luaerr)
}
}
impl UserData for PaneObject {
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_method("pane_id", |_, this, _: ()| Ok(this.pane()?.pane_id()));
methods.add_method("get_title", |_, this, _: ()| Ok(this.pane()?.get_title()));
methods.add_method("get_current_working_dir", |_, this, _: ()| {
Ok(this
.pane()?
.get_current_working_dir()
.map(|u| u.to_string()))
});
methods.add_method("paste", |_, this, text: String| {
this.pane()?.send_paste(&text).map_err(luaerr)?;
Ok(())
});
methods.add_method("get_cursor_position", |_, this, _: ()| {
Ok(this.pane()?.renderer().get_cursor_position())
});
methods.add_method("get_dimensions", |_, this, _: ()| {
Ok(this.pane()?.renderer().get_dimensions())
});
// When called with no arguments, returns the lines from the
// viewport as plain text (no escape sequences).
// When called with an optional integer argument, returns the
// last nlines lines of the terminal output.
// The returned string will have trailing whitespace trimmed.
methods.add_method("get_lines_as_text", |_, this, nlines: Option<usize>| {
let pane = this.pane()?;
let mut render = pane.renderer();
let dims = render.get_dimensions();
let nlines = nlines.unwrap_or(dims.viewport_rows);
let bottom_row = dims.physical_top + dims.viewport_rows as isize;
let top_row = bottom_row.saturating_sub(nlines as isize);
let (_first_row, lines) = render.get_lines(top_row..bottom_row);
let mut text = String::new();
for line in lines {
for (_, cell) in line.visible_cells() {
text.push_str(cell.str());
}
let trimmed = text.trim_end().len();
text.truncate(trimmed);
text.push('\n');
}
Ok(text)
});
}
}