use super::tmux_interface::*;
use super::tmux_interface_error::TmuxInterfaceError;
use std::process::Output;
#[derive(Default)]
pub struct AttachSession<'a> {
pub detach_other: Option<bool>,
pub not_update_env: Option<bool>,
pub read_only: Option<bool>,
pub cwd: Option<&'a str>,
pub target_session: Option<&'a str>,
}
impl<'a> AttachSession<'a> {
pub fn new() -> Self {
Default::default()
}
}
#[derive(Default)]
pub struct DetachClient<'a> {
pub all: Option<bool>,
pub parent_sighup: Option<bool>,
pub shell_command: Option<&'a str>,
pub target_session: Option<&'a str>,
pub target_client: Option<&'a str>
}
impl<'a> DetachClient<'a> {
pub fn new() -> Self {
Default::default()
}
}
#[derive(Default)]
pub struct NewSession<'a> {
pub attach: Option<bool>,
pub detached: Option<bool>,
pub detach_other: Option<bool>,
pub not_update_env: Option<bool>,
pub print: Option<bool>,
pub cwd: Option<&'a str>,
pub format: Option<&'a str>,
pub window_name: Option<&'a str>,
pub session_name: Option<&'a str>,
pub group_name: Option<&'a str>,
pub width: Option<usize>,
pub height: Option<usize>,
pub shell_command: Option<&'a str>
}
impl<'a> NewSession<'a> {
pub fn new() -> Self {
Default::default()
}
}
#[derive(Default)]
pub struct RefreshClient<'a> {
pub tracking_cursor: Option<bool>,
pub down: Option<bool>,
pub request_clipboard: Option<bool>,
pub left: Option<bool>,
pub right: Option<bool>,
pub status_line: Option<bool>,
pub up: Option<bool>,
pub size: Option<(usize, usize)>,
pub target_client: Option<&'a str>,
pub adjustment: Option<usize>
}
impl<'a> RefreshClient<'a> {
pub fn new() -> Self {
Default::default()
}
}
#[derive(Default)]
pub struct SwitchClient<'a> {
pub not_update_env: Option<bool>,
pub last: Option<bool>,
pub next: Option<bool>,
pub previous: Option<bool>,
pub read_only: Option<bool>,
pub target_client: Option<&'a str>,
pub target_session: Option<&'a str>,
pub key_table: Option<&'a str>,
}
impl<'a> SwitchClient<'a> {
pub fn new() -> Self {
Default::default()
}
}
impl<'a> TmuxInterface<'a> {
const ATTACH_SESSION: &'static str = "attach-session";
const DETACH_CLIENT: &'static str = "detach-client";
const HAS_SESSION: &'static str = "has-session";
const KILL_SERVER: &'static str = "kill-server";
const KILL_SESSION: &'static str = "kill-session";
const LIST_CLIENTS: &'static str = "list-clients";
const LIST_COMMANDS: &'static str = "list-commands";
const LIST_SESSIONS: &'static str = "list-sessions";
const LOCK_CLIENT: &'static str = "lock-client";
const LOCK_SESSION: &'static str = "lock-session";
const NEW_SESSION: &'static str = "new-session";
const REFRESH_CLIENT: &'static str = "refresh-client";
const RENAME_SESSION: &'static str = "rename-session";
const SHOW_MESSAGES: &'static str = "show-messages";
const SOURCE_FILE: &'static str = "source-file";
const START_SERVER: &'static str = "start-server";
const SUSPEND_CLIENT: &'static str = "suspend-client";
const SWITCH_CLIENT: &'static str = "switch-client";
pub fn attach_session(&self,
attach_session: &AttachSession
) -> Result<Output, TmuxInterfaceError> {
let mut args: Vec<&str> = Vec::new();
if attach_session.detach_other.unwrap_or(false) { args.push(d_KEY); }
if attach_session.not_update_env.unwrap_or(false) { args.push(E_KEY); }
if attach_session.read_only.unwrap_or(false) { args.push(r_KEY); }
attach_session.cwd.and_then(|s| Some(args.extend_from_slice(&[c_KEY, &s])));
attach_session.target_session.and_then(|s| Some(args.extend_from_slice(&[t_KEY, &s])));
let output = self.subcommand(TmuxInterface::ATTACH_SESSION, &args)?;
Ok(output)
}
pub fn detach_client(&self,
detach_client: &DetachClient
) -> Result<Output, TmuxInterfaceError> {
let mut args: Vec<&str> = Vec::new();
if detach_client.all.unwrap_or(false) { args.push(a_KEY); }
if detach_client.parent_sighup.unwrap_or(false) { args.push(P_KEY); }
detach_client.shell_command.and_then(|s| Some(args.extend_from_slice(&[E_KEY, &s])));
detach_client.target_session.and_then(|s| Some(args.extend_from_slice(&[s_KEY, &s])));
detach_client.target_client.and_then(|s| Some(args.extend_from_slice(&[t_KEY, &s])));
let output = self.subcommand(TmuxInterface::DETACH_CLIENT, &args)?;
Ok(output)
}
pub fn has_session(&self, target_session: Option<&str>) -> Result<bool, TmuxInterfaceError> {
let mut args: Vec<&str> = Vec::new();
target_session.and_then(|s| Some(args.extend_from_slice(&[t_KEY, s])));
let output = self.subcommand(TmuxInterface::HAS_SESSION, &args)?;
Ok(output.status.success())
}
pub fn kill_server(&self) -> Result<Output, TmuxInterfaceError> {
let output = self.subcommand(TmuxInterface::KILL_SERVER, &[""])?;
Ok(output)
}
pub fn kill_session(&self,
all: Option<bool>,
clear_alerts: Option<bool>,
target_session: Option<&str>
) -> Result<Output, TmuxInterfaceError> {
let mut args: Vec<&str> = Vec::new();
if all.unwrap_or(false) { args.push(a_KEY); }
if clear_alerts.unwrap_or(false) { args.push(C_KEY); }
target_session.and_then(|s| Some(args.extend_from_slice(&[t_KEY, &s])));
let output = self.subcommand(TmuxInterface::KILL_SESSION, &args)?;
Ok(output)
}
pub fn list_clients(&self,
format: Option<&str>,
target_session: Option<&str>
) -> Result<Output, TmuxInterfaceError> {
let mut args: Vec<&str> = Vec::new();
format.and_then(|s| Some(args.extend_from_slice(&[F_KEY, &s])));
target_session.and_then(|s| Some(args.extend_from_slice(&[t_KEY, &s])));
let output = self.subcommand(TmuxInterface::LIST_CLIENTS, &args)?;
Ok(output)
}
pub fn list_commands(&self, format: Option<&str>) -> Result<Output, TmuxInterfaceError> {
let mut args: Vec<&str> = Vec::new();
format.and_then(|s| Some(args.extend_from_slice(&[F_KEY, &s])));
let output = self.subcommand(TmuxInterface::LIST_COMMANDS, &args)?;
Ok(output)
}
pub fn list_sessions(&self, format: Option<&str>) -> Result<String, TmuxInterfaceError> {
let mut args: Vec<&str> = Vec::new();
format.and_then(|s| Some(args.extend_from_slice(&[F_KEY, s])));
let output = self.subcommand(TmuxInterface::LIST_SESSIONS, &args)?;
let stdout = String::from_utf8_lossy(&output.stdout.as_slice());
Ok(stdout.to_string())
}
pub fn lock_client(&self, target_client: Option<&str>) -> Result<Output, TmuxInterfaceError> {
let mut args: Vec<&str> = Vec::new();
target_client.and_then(|s| Some(args.extend_from_slice(&[t_KEY, s])));
let output = self.subcommand(TmuxInterface::LOCK_CLIENT, &args)?;
Ok(output)
}
pub fn lock_session(&self, target_session: Option<&str>) -> Result<Output, TmuxInterfaceError> {
let mut args: Vec<&str> = Vec::new();
target_session.and_then(|s| Some(args.extend_from_slice(&[t_KEY, s])));
let output = self.subcommand(TmuxInterface::LOCK_SESSION, &[""])?;
Ok(output)
}
pub fn new_session(&self, new_session: &NewSession) -> Result<Output, TmuxInterfaceError> {
let mut args: Vec<&str> = Vec::new();
if new_session.attach.unwrap_or(false) { args.push(A_KEY); }
if new_session.detached.unwrap_or(false) { args.push(d_KEY); }
if new_session.detach_other.unwrap_or(false) { args.push(D_KEY); }
if new_session.not_update_env.unwrap_or(false) { args.push(E_KEY); }
if new_session.print.unwrap_or(false) { args.push(P_KEY); }
new_session.cwd.and_then(|s| Some(args.extend_from_slice(&[c_KEY, &s])));
new_session.format.and_then(|s| Some(args.extend_from_slice(&[F_KEY, &s])));
new_session.window_name.and_then(|s| Some(args.extend_from_slice(&[n_KEY, &s])));
new_session.session_name.and_then(|s| Some(args.extend_from_slice(&[s_KEY, &s])));
new_session.group_name.and_then(|s| Some(args.extend_from_slice(&[t_KEY, &s])));
let x;
if let Some(width) = new_session.width {
x = width.to_string();
args.extend_from_slice(&[x_KEY, &x]);
}
let y;
if let Some(height) = new_session.height {
y = height.to_string();
args.extend_from_slice(&[y_KEY, &y]);
}
new_session.shell_command.and_then(|s| Some(args.push(&s)));
let output = self.subcommand(TmuxInterface::NEW_SESSION, &args)?;
Ok(output)
}
pub fn refresh_client(&self,
refresh_client: &RefreshClient
) -> Result<Output, TmuxInterfaceError> {
let mut args: Vec<&str> = Vec::new();
if refresh_client.tracking_cursor.unwrap_or(false) { args.push(c_KEY); }
if refresh_client.down.unwrap_or(false) { args.push(D_KEY); }
if refresh_client.request_clipboard.unwrap_or(false) { args.push(l_KEY); }
if refresh_client.left.unwrap_or(false) { args.push(L_KEY); }
if refresh_client.right.unwrap_or(false) { args.push(R_KEY); }
if refresh_client.status_line.unwrap_or(false) { args.push(S_KEY); }
let s;
if let Some(size) = refresh_client.size {
s = format!("{},{}", size.0, size.1);
args.extend_from_slice(&[C_KEY, &s]);
}
refresh_client.target_client.and_then(|s| Some(args.extend_from_slice(&[t_KEY, &s])));
let n;
if let Some(adjustment) = refresh_client.adjustment {
n = adjustment.to_string();
args.push(&n);
}
let output = self.subcommand(TmuxInterface::REFRESH_CLIENT, &args)?;
Ok(output)
}
pub fn rename_session(&self,
target_session: Option<&str>,
new_name: &str
) -> Result<Output, TmuxInterfaceError> {
let mut args: Vec<&str> = Vec::new();
target_session.and_then(|s| Some(args.extend_from_slice(&[t_KEY, &s])));
args.push(new_name);
let output = self.subcommand(TmuxInterface::RENAME_SESSION, &args)?;
Ok(output)
}
pub fn show_messages(&self,
jobs: Option<bool>,
terminal: Option<bool>,
target_client: Option<&str>
) -> Result<Output, TmuxInterfaceError> {
let mut args: Vec<&str> = Vec::new();
if jobs.unwrap_or(false) { args.push(J_KEY); }
if terminal.unwrap_or(false) { args.push(T_KEY); }
target_client.and_then(|s| Some(args.extend_from_slice(&[t_KEY, &s])));
let output = self.subcommand(TmuxInterface::SHOW_MESSAGES, &args)?;
Ok(output)
}
pub fn source_file(&self,
quite: Option<bool>,
path: &str
) -> Result<Output, TmuxInterfaceError> {
let mut args: Vec<&str> = Vec::new();
if quite.unwrap_or(false) { args.push(q_KEY); }
args.push(path);
let output = self.subcommand(TmuxInterface::SOURCE_FILE, &args)?;
Ok(output)
}
pub fn start_server(&self) -> Result<Output, TmuxInterfaceError> {
let output = self.subcommand(TmuxInterface::START_SERVER, &[""])?;
Ok(output)
}
pub fn suspend_client(&self,
target_client: Option<&str>
) -> Result<Output, TmuxInterfaceError> {
let mut args: Vec<&str> = Vec::new();
target_client.and_then(|s| Some(args.extend_from_slice(&[t_KEY, &s])));
let output = self.subcommand(TmuxInterface::SUSPEND_CLIENT, &args)?;
Ok(output)
}
pub fn switch_client(&self,
switch_client: &SwitchClient
) -> Result<Output, TmuxInterfaceError> {
let mut args: Vec<&str> = Vec::new();
if switch_client.not_update_env.unwrap_or(false) { args.push(E_KEY); }
if switch_client.last.unwrap_or(false) { args.push(l_KEY); }
if switch_client.next.unwrap_or(false) { args.push(n_KEY); }
if switch_client.previous.unwrap_or(false) { args.push(p_KEY); }
if switch_client.read_only.unwrap_or(false) { args.push(r_KEY); }
switch_client.target_client.and_then(|s| Some(args.extend_from_slice(&[c_KEY, &s])));
switch_client.target_session.and_then(|s| Some(args.extend_from_slice(&[t_KEY, &s])));
switch_client.key_table.and_then(|s| Some(args.extend_from_slice(&[T_KEY, &s])));
let output = self.subcommand(TmuxInterface::SWITCH_CLIENT, &args)?;
Ok(output)
}
}