1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-24 07:46:59 +03:00

assign domains ids and associate tabs with them

The idea is that we can use this to spawn a tab in a
specific domain.  For example, "spawn tab in domain of current tab"
or "spawn tab in default domain".
This commit is contained in:
Wez Furlong 2019-06-08 20:39:55 -07:00
parent 5ebad9e63a
commit 87e7a7089c
4 changed files with 56 additions and 4 deletions

View File

@ -1,3 +1,4 @@
use crate::mux::domain::DomainId;
use crate::mux::renderable::Renderable;
use crate::mux::tab::{alloc_tab_id, Tab, TabId};
use failure::Error;
@ -11,6 +12,7 @@ pub struct LocalTab {
terminal: RefCell<Terminal>,
process: RefCell<Box<dyn Child>>,
pty: RefCell<Box<dyn MasterPty>>,
domain_id: DomainId,
}
impl Tab for LocalTab {
@ -85,16 +87,26 @@ impl Tab for LocalTab {
fn palette(&self) -> ColorPalette {
self.terminal.borrow().palette().clone()
}
fn domain_id(&self) -> DomainId {
self.domain_id
}
}
impl LocalTab {
pub fn new(terminal: Terminal, process: Box<dyn Child>, pty: Box<dyn MasterPty>) -> Self {
pub fn new(
terminal: Terminal,
process: Box<dyn Child>,
pty: Box<dyn MasterPty>,
domain_id: DomainId,
) -> Self {
let tab_id = alloc_tab_id();
Self {
tab_id,
terminal: RefCell::new(terminal),
process: RefCell::new(process),
pty: RefCell::new(pty),
domain_id,
}
}
}

View File

@ -16,21 +16,38 @@ use portable_pty::{PtySize, PtySystem};
use std::rc::Rc;
use std::sync::Arc;
static DOMAIN_ID: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::AtomicUsize::new(0);
pub type DomainId = usize;
pub fn alloc_domain_id() -> DomainId {
DOMAIN_ID.fetch_add(1, ::std::sync::atomic::Ordering::Relaxed)
}
pub trait Domain {
/// Spawn a new command within this domain
fn spawn(&self, size: PtySize, command: Option<CommandBuilder>) -> Result<Rc<dyn Tab>, Error>;
/// Returns the domain id, which is useful for obtaining
/// a handle on the domain later.
fn domain_id(&self) -> DomainId;
}
pub struct LocalDomain {
pty_system: Box<dyn PtySystem>,
config: Arc<Config>,
id: DomainId,
}
impl LocalDomain {
pub fn new(config: &Arc<Config>) -> Result<Self, Error> {
let config = Arc::clone(config);
let pty_system = config.pty.get()?;
Ok(Self { pty_system, config })
let id = alloc_domain_id();
Ok(Self {
pty_system,
config,
id,
})
}
}
@ -51,10 +68,14 @@ impl Domain for LocalDomain {
self.config.hyperlink_rules.clone(),
);
let tab: Rc<dyn Tab> = Rc::new(LocalTab::new(terminal, child, master));
let tab: Rc<dyn Tab> = Rc::new(LocalTab::new(terminal, child, master, self.id));
Mux::get().unwrap().add_tab(&tab)?;
Ok(tab)
}
fn domain_id(&self) -> DomainId {
self.id
}
}

View File

@ -21,13 +21,14 @@ pub mod window;
use crate::mux::tab::{Tab, TabId};
use crate::mux::window::{Window, WindowId};
use domain::Domain;
use domain::{Domain, DomainId};
pub struct Mux {
tabs: RefCell<HashMap<TabId, Rc<dyn Tab>>>,
windows: RefCell<HashMap<WindowId, Window>>,
config: Arc<Config>,
default_domain: Arc<dyn Domain>,
domains: RefCell<HashMap<DomainId, Arc<dyn Domain>>>,
}
fn read_from_tab_pty(tab_id: TabId, mut reader: Box<dyn std::io::Read>) {
@ -106,11 +107,15 @@ thread_local! {
impl Mux {
pub fn new(config: &Arc<Config>, default_domain: &Arc<dyn Domain>) -> Self {
let mut domains = HashMap::new();
domains.insert(default_domain.domain_id(), Arc::clone(default_domain));
Self {
tabs: RefCell::new(HashMap::new()),
windows: RefCell::new(HashMap::new()),
config: Arc::clone(config),
default_domain: Arc::clone(default_domain),
domains: RefCell::new(domains),
}
}
@ -118,6 +123,18 @@ impl Mux {
&self.default_domain
}
#[allow(dead_code)]
pub fn get_domain(&self, id: DomainId) -> Option<Arc<dyn Domain>> {
self.domains.borrow().get(&id).cloned()
}
#[allow(dead_code)]
pub fn add_domain(&self, domain: &Arc<dyn Domain>) {
self.domains
.borrow_mut()
.insert(domain.domain_id(), Arc::clone(domain));
}
pub fn config(&self) -> &Arc<Config> {
&self.config
}

View File

@ -1,3 +1,4 @@
use crate::mux::domain::DomainId;
use crate::mux::renderable::Renderable;
use failure::Error;
use std::cell::RefMut;
@ -30,4 +31,5 @@ pub trait Tab {
fn advance_bytes(&self, buf: &[u8], host: &mut dyn TerminalHost);
fn is_dead(&self) -> bool;
fn palette(&self) -> ColorPalette;
fn domain_id(&self) -> DomainId;
}