use rmux_proto::{
KillWindowRequest, LastWindowRequest, LayoutName, LinkWindowRequest, ListWindowsRequest,
MoveWindowRequest, MoveWindowTarget, NewWindowRequest, NextWindowRequest,
PreviousWindowRequest, RenameWindowRequest, Request, Response, RotateWindowDirection,
RotateWindowRequest, SelectCustomLayoutRequest, SelectLayoutRequest, SelectLayoutTarget,
SelectWindowRequest, SessionName, SplitDirection, SplitWindowExtRequest, SplitWindowRequest,
SplitWindowTarget, SwapWindowRequest, UnlinkWindowRequest, WindowTarget,
};
use crate::{connection::Connection, ClientError};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SplitWindowOptions {
pub target: SplitWindowTarget,
pub direction: SplitDirection,
pub before: bool,
pub environment: Option<Vec<String>>,
pub command: Option<Vec<String>>,
}
impl Connection {
pub fn new_window(
&mut self,
target: rmux_proto::SessionName,
name: Option<String>,
detached: bool,
) -> Result<Response, ClientError> {
self.new_window_with_environment(target, name, detached, None, None, None)
}
pub fn new_window_with_environment(
&mut self,
target: rmux_proto::SessionName,
name: Option<String>,
detached: bool,
environment: Option<Vec<String>>,
start_directory: Option<std::path::PathBuf>,
command: Option<Vec<String>>,
) -> Result<Response, ClientError> {
self.new_window_at_with_environment(
target,
None,
name,
detached,
environment,
start_directory,
command,
false,
)
}
#[allow(clippy::too_many_arguments)]
pub fn new_window_at_with_environment(
&mut self,
target: rmux_proto::SessionName,
target_window_index: Option<u32>,
name: Option<String>,
detached: bool,
environment: Option<Vec<String>>,
start_directory: Option<std::path::PathBuf>,
command: Option<Vec<String>>,
insert_at_target: bool,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::NewWindow(NewWindowRequest {
target,
name,
detached,
start_directory,
environment,
command,
target_window_index,
insert_at_target,
}))
}
pub fn kill_window(
&mut self,
target: WindowTarget,
kill_others: bool,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::KillWindow(KillWindowRequest {
target,
kill_all_others: kill_others,
}))
}
pub fn select_window(&mut self, target: WindowTarget) -> Result<Response, ClientError> {
self.roundtrip(&Request::SelectWindow(SelectWindowRequest { target }))
}
pub fn rename_window(
&mut self,
target: WindowTarget,
new_name: String,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::RenameWindow(RenameWindowRequest {
target,
name: new_name,
}))
}
pub fn next_window(
&mut self,
target: SessionName,
alerts_only: bool,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::NextWindow(NextWindowRequest {
target,
alerts_only,
}))
}
pub fn previous_window(
&mut self,
target: SessionName,
alerts_only: bool,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::PreviousWindow(PreviousWindowRequest {
target,
alerts_only,
}))
}
pub fn last_window(&mut self, target: SessionName) -> Result<Response, ClientError> {
self.roundtrip(&Request::LastWindow(LastWindowRequest { target }))
}
pub fn list_windows(
&mut self,
target: SessionName,
format: Option<String>,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::ListWindows(ListWindowsRequest { target, format }))
}
pub fn link_window(
&mut self,
source: WindowTarget,
target: WindowTarget,
after: bool,
before: bool,
kill_destination: bool,
detached: bool,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::LinkWindow(LinkWindowRequest {
source,
target,
after,
before,
kill_destination,
detached,
}))
}
pub fn move_window(
&mut self,
source: Option<WindowTarget>,
target: MoveWindowTarget,
renumber: bool,
kill_destination: bool,
detached: bool,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::MoveWindow(MoveWindowRequest {
source,
target,
renumber,
kill_destination,
detached,
}))
}
pub fn swap_window(
&mut self,
source: WindowTarget,
target: WindowTarget,
detached: bool,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::SwapWindow(SwapWindowRequest {
source,
target,
detached,
}))
}
pub fn rotate_window(
&mut self,
target: WindowTarget,
direction: RotateWindowDirection,
) -> Result<Response, ClientError> {
self.rotate_window_with_zoom(target, direction, false)
}
pub fn rotate_window_with_zoom(
&mut self,
target: WindowTarget,
direction: RotateWindowDirection,
restore_zoom: bool,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::RotateWindow(RotateWindowRequest {
target,
direction,
restore_zoom,
}))
}
pub fn resize_window(
&mut self,
target: WindowTarget,
width: Option<u16>,
height: Option<u16>,
adjustment: Option<rmux_proto::ResizeWindowAdjustment>,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::ResizeWindow(rmux_proto::ResizeWindowRequest {
target,
width,
height,
adjustment,
}))
}
pub fn unlink_window(
&mut self,
target: WindowTarget,
kill_if_last: bool,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::UnlinkWindow(UnlinkWindowRequest {
target,
kill_if_last,
}))
}
pub fn respawn_window(
&mut self,
target: WindowTarget,
kill: bool,
) -> Result<Response, ClientError> {
self.respawn_window_with_environment(target, kill, None, None, None)
}
pub fn respawn_window_with_environment(
&mut self,
target: WindowTarget,
kill: bool,
environment: Option<Vec<String>>,
start_directory: Option<std::path::PathBuf>,
command: Option<Vec<String>>,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::RespawnWindow(rmux_proto::RespawnWindowRequest {
target,
kill,
start_directory,
environment,
command,
}))
}
pub fn split_window(&mut self, target: SplitWindowTarget) -> Result<Response, ClientError> {
self.split_window_with_direction(target, SplitDirection::Vertical)
}
pub fn split_window_with_direction(
&mut self,
target: SplitWindowTarget,
direction: SplitDirection,
) -> Result<Response, ClientError> {
self.split_window_with_direction_and_environment(target, direction, None)
}
pub fn split_window_with_direction_and_environment(
&mut self,
target: SplitWindowTarget,
direction: SplitDirection,
environment: Option<Vec<String>>,
) -> Result<Response, ClientError> {
self.split_window_with_spawn(target, direction, environment, None)
}
pub fn split_window_with_spawn(
&mut self,
target: SplitWindowTarget,
direction: SplitDirection,
environment: Option<Vec<String>>,
command: Option<Vec<String>>,
) -> Result<Response, ClientError> {
self.split_window_with_options(SplitWindowOptions {
target,
direction,
before: false,
environment,
command,
})
}
pub fn split_window_with_options(
&mut self,
options: SplitWindowOptions,
) -> Result<Response, ClientError> {
let SplitWindowOptions {
target,
direction,
before,
environment,
command,
} = options;
if command.is_some() {
return self.roundtrip(&Request::SplitWindowExt(SplitWindowExtRequest {
target,
direction,
before,
environment,
command,
}));
}
self.roundtrip(&Request::SplitWindow(SplitWindowRequest {
target,
direction,
before,
environment,
}))
}
pub fn select_layout(
&mut self,
target: SelectLayoutTarget,
layout: LayoutName,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::SelectLayout(SelectLayoutRequest {
target,
layout,
}))
}
pub fn select_custom_layout(
&mut self,
target: SelectLayoutTarget,
layout: String,
) -> Result<Response, ClientError> {
self.roundtrip(&Request::SelectCustomLayout(SelectCustomLayoutRequest {
target,
layout,
}))
}
pub fn next_layout(&mut self, target: WindowTarget) -> Result<Response, ClientError> {
self.roundtrip(&Request::NextLayout(rmux_proto::NextLayoutRequest {
target,
}))
}
pub fn previous_layout(&mut self, target: WindowTarget) -> Result<Response, ClientError> {
self.roundtrip(&Request::PreviousLayout(
rmux_proto::PreviousLayoutRequest { target },
))
}
}