Files
LLeMbas-TTY/src/mux/tab.rs
T
Wez Furlong 4e6357b218 term: enable DECSET 1004 Focus Reporting
This is enabled only for local terminals for now; the multiplexer
protocol will need to be extended to carry this signal.
2020-06-20 22:01:56 -07:00

144 lines
4.4 KiB
Rust

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 TAB_ID: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::AtomicUsize::new(0);
pub type TabId = usize;
pub fn alloc_tab_id() -> TabId {
TAB_ID.fetch_add(1, ::std::sync::atomic::Ordering::Relaxed)
}
const PASTE_CHUNK_SIZE: usize = 1024;
struct Paste {
tab_id: TabId,
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 tab = mux.get_tab(locked.tab_id).unwrap();
let remain = locked.text.len() - locked.offset;
let chunk = remain.min(PASTE_CHUNK_SIZE);
let text_slice = &locked.text[locked.offset..locked.offset + chunk];
tab.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,
}
#[async_trait(?Send)]
pub trait Tab: Downcast {
fn tab_id(&self) -> TabId;
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<()>;
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 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 {
tab_id: self.tab_id(),
text,
offset: PASTE_CHUNK_SIZE,
}));
schedule_next_paste(&paste);
}
Ok(())
}
}
impl_downcast!(Tab);