use rmux_proto::request::{
AttachSessionExt2Request, AttachSessionExt3Request, AttachSessionExtRequest,
CreateSessionLeaseRequest, DetachClientExtRequest, ListClientsRequest, NewSessionExtRequest,
RefreshClientRequest, ReleaseSessionLeaseRequest, RenewSessionLeaseRequest, Request,
SuspendClientRequest, SwitchClientExt2Request, SwitchClientExt3Request,
};
use rmux_proto::{
AttachSessionRequest, DetachClientRequest, HasSessionRequest, KillSessionRequest,
ListPanesRequest, ListSessionsRequest, NewSessionRequest, RenameSessionRequest, Response,
SessionName, SwitchClientExtRequest, SwitchClientRequest, TerminalSize,
CAPABILITY_SDK_SESSION_LEASE,
};
use crate::{
connection::{AttachTransition, Connection},
ClientError,
};
impl Connection {
pub fn new_session(
&mut self,
session_name: SessionName,
detached: bool,
size: Option<TerminalSize>,
) -> Result<Response, ClientError> {
self.new_session_with_environment(session_name, detached, size, None)
}
pub fn new_session_with_environment(
&mut self,
session_name: SessionName,
detached: bool,
size: Option<TerminalSize>,
environment: Option<Vec<String>>,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::NewSession(NewSessionRequest {
session_name,
detached,
size,
environment,
}))
}
pub fn new_session_extended(
&mut self,
request: NewSessionExtRequest,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::NewSessionExt(Box::new(request)))
}
pub fn has_session(&mut self, target: SessionName) -> Result<Response, ClientError> {
self.roundtrip(&Request::HasSession(HasSessionRequest { target }))
}
pub fn kill_session(&mut self, request: KillSessionRequest) -> Result<Response, ClientError> {
self.roundtrip(&Request::KillSession(request))
}
pub fn rename_session(
&mut self,
target: SessionName,
new_name: SessionName,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::RenameSession(RenameSessionRequest {
target,
new_name,
}))
}
pub fn create_session_lease(
&mut self,
session_name: SessionName,
ttl_millis: u64,
) -> Result<Response, ClientError> {
if !self.supports_capability(CAPABILITY_SDK_SESSION_LEASE)? {
return Err(ClientError::Protocol(
rmux_proto::RmuxError::UnsupportedCapability {
feature: CAPABILITY_SDK_SESSION_LEASE.to_owned(),
supported: Vec::new(),
},
));
}
self.roundtrip(&Request::CreateSessionLease(CreateSessionLeaseRequest {
session_name,
ttl_millis,
}))
}
pub fn renew_session_lease(
&mut self,
session_name: SessionName,
token: u64,
ttl_millis: u64,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::RenewSessionLease(RenewSessionLeaseRequest {
session_name,
token,
ttl_millis,
}))
}
pub fn release_session_lease(
&mut self,
session_name: SessionName,
token: u64,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::ReleaseSessionLease(ReleaseSessionLeaseRequest {
session_name,
token,
}))
}
pub fn list_sessions(&mut self, request: ListSessionsRequest) -> Result<Response, ClientError> {
self.roundtrip(&Request::ListSessions(request))
}
pub fn list_panes(
&mut self,
target: SessionName,
format: Option<String>,
) -> Result<Response, ClientError> {
self.list_panes_in_window(target, None, format)
}
pub fn list_panes_in_window(
&mut self,
target: SessionName,
target_window_index: Option<u32>,
format: Option<String>,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::ListPanes(ListPanesRequest {
target,
target_window_index,
format,
}))
}
pub fn switch_client(&mut self, target: SessionName) -> Result<Response, ClientError> {
self.roundtrip(&Request::SwitchClient(SwitchClientRequest { target }))
}
pub fn switch_client_extended(
&mut self,
target: Option<SessionName>,
key_table: Option<String>,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::SwitchClientExt(SwitchClientExtRequest {
target,
key_table,
}))
}
pub fn switch_client_with_session_flags(
&mut self,
request: SwitchClientExt2Request,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::SwitchClientExt2(Box::new(request)))
}
pub fn switch_client_with_target_selector(
&mut self,
request: SwitchClientExt3Request,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::SwitchClientExt3(Box::new(request)))
}
pub fn detach_client(&mut self) -> Result<Response, ClientError> {
self.roundtrip(&Request::DetachClient(DetachClientRequest))
}
pub fn detach_client_extended(
&mut self,
request: DetachClientExtRequest,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::DetachClientExt(request))
}
pub fn refresh_client(
&mut self,
request: RefreshClientRequest,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::RefreshClient(Box::new(request)))
}
pub fn list_clients(&mut self, request: ListClientsRequest) -> Result<Response, ClientError> {
self.roundtrip(&Request::ListClients(Box::new(request)))
}
pub fn suspend_client(
&mut self,
request: SuspendClientRequest,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::SuspendClient(request))
}
pub fn begin_attach(mut self, target: SessionName) -> Result<AttachTransition, ClientError> {
self.write_request(&Request::AttachSession(AttachSessionRequest { target }))?;
let response = self.read_response()?;
match response {
Response::AttachSession(response) => Ok(AttachTransition::Upgraded(
self.into_attach_upgrade(response)?,
)),
other => Ok(AttachTransition::Rejected(other)),
}
}
pub fn begin_attach_extended(
mut self,
request: AttachSessionExtRequest,
) -> Result<AttachTransition, ClientError> {
self.write_request(&Request::AttachSessionExt(request))?;
let response = self.read_response()?;
match response {
Response::AttachSession(response) => Ok(AttachTransition::Upgraded(
self.into_attach_upgrade(response)?,
)),
other => Ok(AttachTransition::Rejected(other)),
}
}
pub fn begin_attach_with_target_spec(
mut self,
request: AttachSessionExt2Request,
) -> Result<AttachTransition, ClientError> {
self.write_request(&Request::AttachSessionExt2(Box::new(request)))?;
let response = self.read_response()?;
match response {
Response::AttachSession(response) => Ok(AttachTransition::Upgraded(
self.into_attach_upgrade(response)?,
)),
other => Ok(AttachTransition::Rejected(other)),
}
}
pub fn begin_attach_with_capabilities(
mut self,
request: AttachSessionExt3Request,
) -> Result<AttachTransition, ClientError> {
self.write_request(&Request::AttachSessionExt3(Box::new(request)))?;
let response = self.read_response()?;
match response {
Response::AttachSession(response) => Ok(AttachTransition::Upgraded(
self.into_attach_upgrade(response)?,
)),
other => Ok(AttachTransition::Rejected(other)),
}
}
pub fn attach_session_with_target_spec_detached(
&mut self,
request: AttachSessionExt2Request,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::AttachSessionExt2(Box::new(request)))
}
}