wezterm: move Pane into its own file

This commit is contained in:
Wez Furlong committed 2020-10-03 11:15:57 -07:00
1 parent 233adb5fe9
commit 52aa9cf71b
19 files changed
+191 -171

No files matched your search

@@ -1,4 +1,5 @@
use crate::mux::tab::{PaneId, TabId};
use crate::mux::pane::PaneId;
use crate::mux::tab::TabId;
use crate::mux::window::WindowId;
use crate::mux::Mux;
use crate::termwiztermtab::TermWizTerminal;
+1 -1
View File
@@ -1,8 +1,8 @@
use crate::frontend::gui::selection::{SelectionCoordinate, SelectionRange};
use crate::frontend::gui::termwindow::TermWindow;
use crate::mux::domain::DomainId;
use crate::mux::pane::{Pane, PaneId};
use crate::mux::renderable::*;
use crate::mux::tab::{Pane, PaneId};
use portable_pty::PtySize;
use rangeset::RangeSet;
use std::cell::{RefCell, RefMut};
+2 -1
View File
@@ -1,5 +1,6 @@
use crate::frontend::gui::termwindow::TermWindow;
use crate::mux::tab::{Pane, PaneId, Tab, TabId};
use crate::mux::pane::{Pane, PaneId};
use crate::mux::tab::{Tab, TabId};
use crate::termwiztermtab::{allocate, TermWizTerminal};
use std::pin::Pin;
use std::rc::Rc;
+1 -2
View File
@@ -1,9 +1,8 @@
use crate::frontend::gui::selection::{SelectionCoordinate, SelectionRange};
use crate::frontend::gui::termwindow::TermWindow;
use crate::mux::domain::DomainId;
use crate::mux::pane::{Pane, PaneId, Pattern, SearchResult};
use crate::mux::renderable::*;
use crate::mux::tab::{Pane, PaneId};
use crate::mux::tab::{Pattern, SearchResult};
use portable_pty::PtySize;
use rangeset::RangeSet;
use std::cell::{RefCell, RefMut};
+2 -1
View File
@@ -19,8 +19,9 @@ use crate::keyassignment::{
InputMap, KeyAssignment, MouseEventTrigger, SpawnCommand, SpawnTabDomain,
};
use crate::mux::domain::{DomainId, DomainState};
use crate::mux::pane::{Pane, PaneId};
use crate::mux::renderable::{RenderableDimensions, StableCursorPosition};
use crate::mux::tab::{Pane, PaneId, PositionedPane, PositionedSplit, SplitDirection, Tab, TabId};
use crate::mux::tab::{PositionedPane, PositionedSplit, SplitDirection, Tab, TabId};
use crate::mux::window::WindowId as MuxWindowId;
use crate::mux::Mux;
use ::wezterm_term::input::MouseButton as TMB;
+1 -1
View File
@@ -2,7 +2,7 @@ use crate::config::configuration;
use crate::config::LeaderKey;
use crate::frontend::gui::SelectionMode;
use crate::mux::domain::DomainId;
use crate::mux::tab::Pattern;
use crate::mux::pane::Pattern;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
+1 -2
View File
@@ -1,7 +1,6 @@
use crate::mux::domain::DomainId;
use crate::mux::pane::{Pane, PaneId, Pattern, SearchResult};
use crate::mux::renderable::Renderable;
use crate::mux::tab::{Pane, PaneId};
use crate::mux::tab::{Pattern, SearchResult};
use anyhow::Error;
use async_trait::async_trait;
use portable_pty::{Child, MasterPty, PtySize};
+2 -1
View File
@@ -38,7 +38,8 @@ mod update;
use crate::frontend::activity::Activity;
use crate::frontend::{front_end, FrontEndSelection};
use crate::mux::domain::{Domain, LocalDomain};
use crate::mux::tab::{PaneId, SplitDirection};
use crate::mux::pane::PaneId;
use crate::mux::tab::SplitDirection;
use crate::mux::Mux;
use crate::server::client::{unix_connect_with_retry, Client};
use crate::server::domain::{ClientDomain, ClientDomainConfig};
+2 -1
View File
@@ -7,7 +7,8 @@
use crate::config::configuration;
use crate::localtab::LocalPane;
use crate::mux::tab::{alloc_pane_id, Pane, PaneId, SplitDirection, Tab, TabId};
use crate::mux::pane::{alloc_pane_id, Pane, PaneId};
use crate::mux::tab::{SplitDirection, Tab, TabId};
use crate::mux::window::WindowId;
use crate::mux::Mux;
use anyhow::{bail, Error};
+2 -1
View File
@@ -1,4 +1,4 @@
use crate::mux::tab::{Pane, PaneId};
use crate::mux::pane::{Pane, PaneId};
use crate::mux::tab::{Tab, TabId};
use crate::mux::window::{Window, WindowId};
use crate::ratelim::RateLimiter;
@@ -17,6 +17,7 @@ use std::thread;
use thiserror::*;
pub mod domain;
pub mod pane;
pub mod renderable;
pub mod tab;
pub mod window;
+154
View File
@@ -0,0 +1,154 @@
use crate::mux::domain::DomainId;
use crate::mux::renderable::Renderable;
use crate::mux::Mux;
use async_trait::async_trait;
use downcast_rs::{impl_downcast, Downcast};
use portable_pty::PtySize;
use serde::{Deserialize, Serialize};
use std::cell::RefMut;
use std::sync::{Arc, Mutex};
use url::Url;
use wezterm_term::color::ColorPalette;
use wezterm_term::{Clipboard, KeyCode, KeyModifiers, MouseEvent, StableRowIndex};
static PANE_ID: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::AtomicUsize::new(0);
pub type PaneId = usize;
pub fn alloc_pane_id() -> PaneId {
PANE_ID.fetch_add(1, ::std::sync::atomic::Ordering::Relaxed)
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub enum Pattern {
CaseSensitiveString(String),
CaseInSensitiveString(String),
Regex(String),
}
impl std::ops::Deref for Pattern {
type Target = String;
fn deref(&self) -> &String {
match self {
Pattern::CaseSensitiveString(s) => s,
Pattern::CaseInSensitiveString(s) => s,
Pattern::Regex(s) => s,
}
}
}
impl std::ops::DerefMut for Pattern {
fn deref_mut(&mut self) -> &mut String {
match self {
Pattern::CaseSensitiveString(s) => s,
Pattern::CaseInSensitiveString(s) => s,
Pattern::Regex(s) => s,
}
}
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct SearchResult {
pub start_y: StableRowIndex,
pub end_y: StableRowIndex,
/// The cell index into the line of the start of the match
pub start_x: usize,
/// The cell index into the line of the end of the match
pub end_x: usize,
}
const PASTE_CHUNK_SIZE: usize = 1024;
struct Paste {
pane_id: PaneId,
text: String,
offset: usize,
}
fn schedule_next_paste(paste: &Arc<Mutex<Paste>>) {
let paste = Arc::clone(paste);
promise::spawn::spawn(async move {
let mut locked = paste.lock().unwrap();
let mux = Mux::get().unwrap();
let pane = mux.get_pane(locked.pane_id).unwrap();
let remain = locked.text.len() - locked.offset;
let mut chunk = remain.min(PASTE_CHUNK_SIZE);
// Make sure we chunk at a char boundary, otherwise the
// slice operation below will panic
while !locked.text.is_char_boundary(locked.offset + chunk) && chunk < remain {
chunk += 1;
}
let text_slice = &locked.text[locked.offset..locked.offset + chunk];
pane.send_paste(text_slice).unwrap();
if chunk < remain {
// There is more to send
locked.offset += chunk;
schedule_next_paste(&paste);
}
});
}
/// A Pane represents a view on a terminal
#[async_trait(?Send)]
pub trait Pane: Downcast {
fn pane_id(&self) -> PaneId;
fn renderer(&self) -> RefMut<dyn Renderable>;
fn get_title(&self) -> String;
fn send_paste(&self, text: &str) -> anyhow::Result<()>;
fn reader(&self) -> anyhow::Result<Box<dyn std::io::Read + Send>>;
fn writer(&self) -> RefMut<dyn std::io::Write>;
fn resize(&self, size: PtySize) -> anyhow::Result<()>;
/// Called as a hint that the pane is being resized as part of
/// a zoom-to-fill-all-the-tab-space operation.
fn set_zoomed(&self, _zoomed: bool) {}
fn key_down(&self, key: KeyCode, mods: KeyModifiers) -> anyhow::Result<()>;
fn mouse_event(&self, event: MouseEvent) -> anyhow::Result<()>;
fn advance_bytes(&self, buf: &[u8]);
fn is_dead(&self) -> bool;
fn kill(&self) {}
fn palette(&self) -> ColorPalette;
fn domain_id(&self) -> DomainId;
fn erase_scrollback(&self) {}
/// Called to advise on whether this tab has focus
fn focus_changed(&self, _focused: bool) {}
/// Performs a search.
/// If the result is empty then there are no matches.
/// Otherwise, the result shall contain all possible matches.
async fn search(&self, _pattern: Pattern) -> anyhow::Result<Vec<SearchResult>> {
Ok(vec![])
}
/// Returns true if the terminal has grabbed the mouse and wants to
/// give the embedded application a chance to process events.
/// In practice this controls whether the gui will perform local
/// handling of clicks.
fn is_mouse_grabbed(&self) -> bool;
fn set_clipboard(&self, _clipboard: &Arc<dyn Clipboard>) {}
fn get_current_working_dir(&self) -> Option<Url>;
fn trickle_paste(&self, text: String) -> anyhow::Result<()> {
if text.len() <= PASTE_CHUNK_SIZE {
// Send it all now
self.send_paste(&text)?;
} else {
// It's pretty heavy, so we trickle it into the pty
self.send_paste(&text[0..PASTE_CHUNK_SIZE])?;
let paste = Arc::new(Mutex::new(Paste {
pane_id: self.pane_id(),
text,
offset: PASTE_CHUNK_SIZE,
}));
schedule_next_paste(&paste);
}
Ok(())
}
}
impl_downcast!(Pane);
+5 -149
View File
@@ -1,21 +1,15 @@
use crate::keyassignment::PaneDirection;
use crate::mux::domain::DomainId;
use crate::mux::renderable::Renderable;
use crate::mux::pane::*;
use crate::mux::{Mux, WindowId};
use crate::server::codec::{PaneEntry, PaneNode};
use async_trait::async_trait;
use bintree::PathBranch;
use downcast_rs::{impl_downcast, Downcast};
use portable_pty::PtySize;
use rangeset::range_intersection;
use serde::{Deserialize, Serialize};
use std::cell::{RefCell, RefMut};
use std::convert::TryInto;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use url::Url;
use wezterm_term::color::ColorPalette;
use wezterm_term::{Clipboard, KeyCode, KeyModifiers, MouseEvent, StableRowIndex};
pub type Tree = bintree::Tree<Rc<dyn Pane>, SplitDirectionAndSize>;
pub type Cursor = bintree::Cursor<Rc<dyn Pane>, SplitDirectionAndSize>;
@@ -23,85 +17,6 @@ pub type Cursor = bintree::Cursor<Rc<dyn Pane>, SplitDirectionAndSize>;
static TAB_ID: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::AtomicUsize::new(0);
pub type TabId = usize;
static PANE_ID: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::AtomicUsize::new(0);
pub type PaneId = usize;
pub fn alloc_pane_id() -> PaneId {
PANE_ID.fetch_add(1, ::std::sync::atomic::Ordering::Relaxed)
}
const PASTE_CHUNK_SIZE: usize = 1024;
struct Paste {
pane_id: PaneId,
text: String,
offset: usize,
}
fn schedule_next_paste(paste: &Arc<Mutex<Paste>>) {
let paste = Arc::clone(paste);
promise::spawn::spawn(async move {
let mut locked = paste.lock().unwrap();
let mux = Mux::get().unwrap();
let pane = mux.get_pane(locked.pane_id).unwrap();
let remain = locked.text.len() - locked.offset;
let mut chunk = remain.min(PASTE_CHUNK_SIZE);
// Make sure we chunk at a char boundary, otherwise the
// slice operation below will panic
while !locked.text.is_char_boundary(locked.offset + chunk) && chunk < remain {
chunk += 1;
}
let text_slice = &locked.text[locked.offset..locked.offset + chunk];
pane.send_paste(text_slice).unwrap();
if chunk < remain {
// There is more to send
locked.offset += chunk;
schedule_next_paste(&paste);
}
});
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub enum Pattern {
CaseSensitiveString(String),
CaseInSensitiveString(String),
Regex(String),
}
impl std::ops::Deref for Pattern {
type Target = String;
fn deref(&self) -> &String {
match self {
Pattern::CaseSensitiveString(s) => s,
Pattern::CaseInSensitiveString(s) => s,
Pattern::Regex(s) => s,
}
}
}
impl std::ops::DerefMut for Pattern {
fn deref_mut(&mut self) -> &mut String {
match self {
Pattern::CaseSensitiveString(s) => s,
Pattern::CaseInSensitiveString(s) => s,
Pattern::Regex(s) => s,
}
}
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct SearchResult {
pub start_y: StableRowIndex,
pub end_y: StableRowIndex,
/// The cell index into the line of the start of the match
pub start_x: usize,
/// The cell index into the line of the end of the match
pub end_x: usize,
}
/// A Tab is a container of Panes
pub struct Tab {
id: TabId,
@@ -1448,72 +1363,13 @@ impl Tab {
}
}
/// A Pane represents a view on a terminal
#[async_trait(?Send)]
pub trait Pane: Downcast {
fn pane_id(&self) -> PaneId;
fn renderer(&self) -> RefMut<dyn Renderable>;
fn get_title(&self) -> String;
fn send_paste(&self, text: &str) -> anyhow::Result<()>;
fn reader(&self) -> anyhow::Result<Box<dyn std::io::Read + Send>>;
fn writer(&self) -> RefMut<dyn std::io::Write>;
fn resize(&self, size: PtySize) -> anyhow::Result<()>;
/// Called as a hint that the pane is being resized as part of
/// a zoom-to-fill-all-the-tab-space operation.
fn set_zoomed(&self, _zoomed: bool) {}
fn key_down(&self, key: KeyCode, mods: KeyModifiers) -> anyhow::Result<()>;
fn mouse_event(&self, event: MouseEvent) -> anyhow::Result<()>;
fn advance_bytes(&self, buf: &[u8]);
fn is_dead(&self) -> bool;
fn kill(&self) {}
fn palette(&self) -> ColorPalette;
fn domain_id(&self) -> DomainId;
fn erase_scrollback(&self) {}
/// Called to advise on whether this tab has focus
fn focus_changed(&self, _focused: bool) {}
/// Performs a search.
/// If the result is empty then there are no matches.
/// Otherwise, the result shall contain all possible matches.
async fn search(&self, _pattern: Pattern) -> anyhow::Result<Vec<SearchResult>> {
Ok(vec![])
}
/// Returns true if the terminal has grabbed the mouse and wants to
/// give the embedded application a chance to process events.
/// In practice this controls whether the gui will perform local
/// handling of clicks.
fn is_mouse_grabbed(&self) -> bool;
fn set_clipboard(&self, _clipboard: &Arc<dyn Clipboard>) {}
fn get_current_working_dir(&self) -> Option<Url>;
fn trickle_paste(&self, text: String) -> anyhow::Result<()> {
if text.len() <= PASTE_CHUNK_SIZE {
// Send it all now
self.send_paste(&text)?;
} else {
// It's pretty heavy, so we trickle it into the pty
self.send_paste(&text[0..PASTE_CHUNK_SIZE])?;
let paste = Arc::new(Mutex::new(Paste {
pane_id: self.pane_id(),
text,
offset: PASTE_CHUNK_SIZE,
}));
schedule_next_paste(&paste);
}
Ok(())
}
}
impl_downcast!(Pane);
#[cfg(test)]
mod test {
use super::*;
use crate::mux::renderable::Renderable;
use url::Url;
use wezterm_term::color::ColorPalette;
use wezterm_term::{KeyCode, KeyModifiers, MouseEvent};
struct FakePane {
id: PaneId,
+1 -1
View File
@@ -1,7 +1,7 @@
use crate::config::{configuration, SshDomain, TlsDomainClient, UnixDomain};
use crate::connui::ConnectionUI;
use crate::mux::domain::{alloc_domain_id, DomainId};
use crate::mux::tab::PaneId;
use crate::mux::pane::PaneId;
use crate::mux::Mux;
use crate::server::codec::*;
use crate::server::domain::{ClientDomain, ClientDomainConfig};
+4 -3
View File
@@ -12,8 +12,9 @@
#![cfg_attr(feature = "cargo-clippy", allow(clippy::range_plus_one))]
use crate::mux::domain::DomainId;
use crate::mux::pane::PaneId;
use crate::mux::renderable::{RenderableDimensions, StableCursorPosition};
use crate::mux::tab::{PaneId, SplitDirection, SplitDirectionAndSize, TabId};
use crate::mux::tab::{SplitDirection, SplitDirectionAndSize, TabId};
use crate::mux::window::WindowId;
use anyhow::{bail, Context as _, Error};
use leb128;
@@ -771,12 +772,12 @@ pub struct GetLinesResponse {
#[derive(Deserialize, Serialize, PartialEq, Debug)]
pub struct SearchScrollbackRequest {
pub pane_id: PaneId,
pub pattern: crate::mux::tab::Pattern,
pub pattern: crate::mux::pane::Pattern,
}
#[derive(Deserialize, Serialize, PartialEq, Debug)]
pub struct SearchScrollbackResponse {
pub results: Vec<crate::mux::tab::SearchResult>,
pub results: Vec<crate::mux::pane::SearchResult>,
}
#[cfg(test)]
+2 -1
View File
@@ -4,7 +4,8 @@ use crate::font::FontConfiguration;
use crate::frontend::front_end;
use crate::keyassignment::SpawnTabDomain;
use crate::mux::domain::{alloc_domain_id, Domain, DomainId, DomainState};
use crate::mux::tab::{Pane, PaneId, SplitDirection, Tab, TabId};
use crate::mux::pane::{Pane, PaneId};
use crate::mux::tab::{SplitDirection, Tab, TabId};
use crate::mux::window::WindowId;
use crate::mux::Mux;
use crate::server::client::Client;
+3 -2
View File
@@ -1,6 +1,7 @@
use crate::keyassignment::SpawnTabDomain;
use crate::mux::pane::{Pane, PaneId};
use crate::mux::renderable::{RenderableDimensions, StableCursorPosition};
use crate::mux::tab::{Pane, PaneId, TabId};
use crate::mux::tab::TabId;
use crate::mux::Mux;
use crate::server::codec::*;
use crate::server::listener::PKI;
@@ -252,7 +253,7 @@ impl SessionHandler {
}
Pdu::SearchScrollbackRequest(SearchScrollbackRequest { pane_id, pattern }) => {
use crate::mux::tab::Pattern;
use crate::mux::pane::Pattern;
async fn do_search(pane_id: TabId, pattern: Pattern) -> anyhow::Result<Pdu> {
let mux = Mux::get().unwrap();
+2 -1
View File
@@ -1,7 +1,8 @@
use crate::config::configuration;
use crate::mux::domain::DomainId;
use crate::mux::pane::{alloc_pane_id, Pane, PaneId, Pattern, SearchResult};
use crate::mux::renderable::{Renderable, RenderableDimensions};
use crate::mux::tab::{alloc_pane_id, Pane, PaneId, Pattern, SearchResult, TabId};
use crate::mux::tab::TabId;
use crate::ratelim::RateLimiter;
use crate::server::codec::*;
use crate::server::domain::ClientInner;
+2 -1
View File
@@ -1,7 +1,8 @@
use crate::connui::ConnectionUI;
use crate::localtab::LocalPane;
use crate::mux::domain::{alloc_domain_id, Domain, DomainId, DomainState};
use crate::mux::tab::{alloc_pane_id, Pane, PaneId, SplitDirection, Tab, TabId};
use crate::mux::pane::{alloc_pane_id, Pane, PaneId};
use crate::mux::tab::{SplitDirection, Tab, TabId};
use crate::mux::window::WindowId;
use crate::mux::Mux;
use anyhow::{anyhow, bail, Context, Error};
+2 -1
View File
@@ -6,8 +6,9 @@
use crate::font::FontConfiguration;
use crate::frontend::front_end;
use crate::mux::domain::{alloc_domain_id, Domain, DomainId, DomainState};
use crate::mux::pane::{alloc_pane_id, Pane, PaneId};
use crate::mux::renderable::Renderable;
use crate::mux::tab::{alloc_pane_id, Pane, PaneId, SplitDirection, Tab, TabId};
use crate::mux::tab::{SplitDirection, Tab, TabId};
use crate::mux::window::WindowId;
use crate::mux::Mux;
use anyhow::{bail, Error};