use crate::{
Error, GetServerOptionTr, GetServerOptionValue, GetUserOption, ServerOptions, SetClipboard,
SetServerOption, SetServerOptionTr, SetServerOptions, SetServerOptionsTr, SetUserOption,
ShowOptions, Switch, Tmux, TmuxCommand, TmuxOutput,
};
use std::borrow::Cow;
use std::str::FromStr;
pub struct ServerOptionsCtl<'a> {
pub invoker: &'a dyn Fn(TmuxCommand<'a>) -> Result<TmuxOutput, Error>,
}
impl<'a> Default for ServerOptionsCtl<'a> {
fn default() -> Self {
Self {
invoker: &|cmd| Tmux::with_command(cmd).output(),
}
}
}
impl<'a> ServerOptionsCtl<'a> {
pub fn new(invoker: &'a dyn Fn(TmuxCommand<'a>) -> Result<TmuxOutput, Error>) -> Self {
Self { invoker }
}
pub fn with_invoker(invoker: &'a dyn Fn(TmuxCommand<'a>) -> Result<TmuxOutput, Error>) -> Self {
Self { invoker }
}
pub fn invoker(&self) -> &'a dyn Fn(TmuxCommand<'a>) -> Result<TmuxOutput, Error> {
self.invoker
}
pub fn get_all(&self) -> Result<ServerOptions<'a>, Error> {
Self::get_all_ext(self.invoker)
}
pub fn get_all_ext(
invoker: impl FnOnce(TmuxCommand<'a>) -> Result<TmuxOutput, Error>,
) -> Result<ServerOptions<'a>, Error> {
let cmd = ShowOptions::new().server().build();
let output = invoker(cmd)?.to_string();
ServerOptions::from_str(&output)
}
pub fn set_all(&self, server_options: ServerOptions<'a>) -> Result<TmuxOutput, Error> {
Self::set_all_ext(self.invoker(), server_options)
}
pub fn set_all_ext(
invoker: impl FnOnce(TmuxCommand<'a>) -> Result<TmuxOutput, Error>,
server_options: ServerOptions<'a>,
) -> Result<TmuxOutput, Error> {
let cmds = SetServerOptions::new();
#[cfg(feature = "tmux_3_1")]
let cmds = cmds.backspace(server_options.backspace);
#[cfg(feature = "tmux_1_5")]
let cmds = cmds.buffer_limit(server_options.buffer_limit);
#[cfg(feature = "tmux_2_4")]
let cmds = cmds.command_alias(server_options.command_alias);
#[cfg(feature = "tmux_3_2")]
let cmds = cmds.copy_command(server_options.copy_command);
#[cfg(feature = "tmux_2_1")]
let cmds = cmds.default_terminal(server_options.default_terminal);
#[cfg(feature = "tmux_1_2")]
let cmds = cmds.escape_time(server_options.escape_time);
#[cfg(feature = "tmux_3_2")]
let cmds = cmds.editor(server_options.editor);
#[cfg(feature = "tmux_2_7")]
let cmds = cmds.exit_empty(server_options.exit_empty);
#[cfg(feature = "tmux_1_4")]
let cmds = cmds.exit_unattached(server_options.exit_unattached);
#[cfg(feature = "tmux_3_2")]
let cmds = cmds.extended_keys(server_options.extended_keys);
#[cfg(feature = "tmux_1_9")]
let cmds = cmds.focus_events(server_options.focus_events);
#[cfg(feature = "tmux_2_1")]
let cmds = cmds.history_file(server_options.history_file);
#[cfg(feature = "tmux_2_0")]
let cmds = cmds.message_limit(server_options.message_limit);
#[cfg(feature = "tmux_3_3")]
let cmds = cmds.prompt_history_limit(server_options.prompt_history_limit);
#[cfg(feature = "tmux_1_5")]
let cmds = cmds.set_clipboard(server_options.set_clipboard);
#[cfg(feature = "tmux_3_2")]
let cmds = cmds.terminal_features(server_options.terminal_features);
#[cfg(feature = "tmux_2_0")]
let cmds = cmds.terminal_overrides(server_options.terminal_overrides);
#[cfg(feature = "tmux_3_0")]
let cmds = cmds.user_keys(server_options.user_keys);
#[cfg(all(feature = "tmux_1_2", not(feature = "tmux_2_0")))]
let cmds = cmds.quiet(server_options.quiet);
#[cfg(all(feature = "tmux_1_3", not(feature = "tmux_1_4")))]
let cmds = cmds.detach_on_destroy(server_options.detach_on_destroy);
let cmd = TmuxCommand::with_cmds(cmds.build());
invoker(cmd)
}
pub fn get<T: std::str::FromStr>(&self, cmd: TmuxCommand<'a>) -> Result<Option<T>, Error> {
Ok((self.invoker)(cmd)?.to_string().trim().parse::<T>().ok())
}
pub fn set(&self, cmd: TmuxCommand<'a>) -> Result<TmuxOutput, Error> {
(self.invoker)(cmd)
}
pub fn get_array(&self, get_option_cmd: TmuxCommand<'a>) -> Result<Option<Vec<String>>, Error> {
let output = (self.invoker)(get_option_cmd)?;
let v: Vec<String> = output
.to_string()
.lines()
.map(|s| s.trim().into())
.collect();
let result = match v.is_empty() {
true => None,
false => Some(v),
};
Ok(result)
}
}
impl<'a> ServerOptionsCtl<'a> {
#[cfg(feature = "tmux_3_1")]
pub fn get_backspace(&self) -> Result<Option<String>, Error> {
self.get(GetServerOptionValue::backspace())
}
#[cfg(feature = "tmux_3_1")]
pub fn set_backspace(&self, key: Option<String>) -> Result<TmuxOutput, Error> {
self.set(SetServerOption::backspace(key))
}
#[cfg(feature = "tmux_1_5")]
pub fn get_buffer_limit(&self) -> Result<Option<usize>, Error> {
self.get(GetServerOptionValue::buffer_limit())
}
#[cfg(feature = "tmux_1_5")]
pub fn set_buffer_limit(&self, buffer_limit: Option<usize>) -> Result<TmuxOutput, Error> {
self.set(SetServerOption::buffer_limit(buffer_limit))
}
#[cfg(feature = "tmux_2_4")]
pub fn get_command_alias(&self) -> Result<Option<Vec<String>>, Error> {
self.get_array(GetServerOptionValue::command_alias())
}
#[cfg(feature = "tmux_2_4")]
pub fn set_command_alias<I, S>(&self, command_alias: Option<I>) -> Result<TmuxOutput, Error>
where
I: IntoIterator<Item = S>,
S: Into<Cow<'a, str>>,
{
self.set(TmuxCommand::with_cmds(SetServerOption::command_alias(
command_alias,
)))
}
#[cfg(feature = "tmux_3_2")]
pub fn get_copy_command(&self) -> Result<Option<String>, Error> {
self.get(GetServerOptionValue::copy_command())
}
#[cfg(feature = "tmux_3_2")]
pub fn set_copy_command(&self, copy_command: Option<String>) -> Result<TmuxOutput, Error> {
self.set(SetServerOption::copy_command(copy_command))
}
#[cfg(feature = "tmux_2_1")]
pub fn get_default_terminal(&self) -> Result<Option<String>, Error> {
self.get(GetServerOptionValue::default_terminal())
}
#[cfg(feature = "tmux_2_1")]
pub fn set_default_terminal(
&self,
default_terminal: Option<String>,
) -> Result<TmuxOutput, Error> {
self.set(SetServerOption::default_terminal(default_terminal))
}
#[cfg(feature = "tmux_1_2")]
pub fn get_escape_time(&self) -> Result<Option<usize>, Error> {
self.get(GetServerOptionValue::escape_time())
}
#[cfg(feature = "tmux_1_2")]
pub fn set_escape_time(&self, escape_time: Option<usize>) -> Result<TmuxOutput, Error> {
self.set(SetServerOption::escape_time(escape_time))
}
#[cfg(feature = "tmux_3_2")]
pub fn get_editor(&self) -> Result<Option<String>, Error> {
self.get(GetServerOptionValue::editor())
}
#[cfg(feature = "tmux_3_2")]
pub fn set_editor<S>(&self, editor: Option<S>) -> Result<TmuxOutput, Error>
where
S: Into<Cow<'a, str>>,
{
self.set(SetServerOption::editor(editor))
}
#[cfg(feature = "tmux_2_7")]
pub fn get_exit_empty(&self) -> Result<Option<Switch>, Error> {
self.get(GetServerOptionValue::exit_empty())
}
#[cfg(feature = "tmux_2_7")]
pub fn set_exit_empty(&self, exit_empty: Option<Switch>) -> Result<TmuxOutput, Error> {
self.set(SetServerOption::exit_empty(exit_empty))
}
#[cfg(feature = "tmux_1_4")]
pub fn get_exit_unattached(&self) -> Result<Option<Switch>, Error> {
self.get(GetServerOptionValue::exit_unattached())
}
#[cfg(feature = "tmux_1_4")]
pub fn set_exit_unattached(
&self,
exit_unattached: Option<Switch>,
) -> Result<TmuxOutput, Error> {
self.set(SetServerOption::exit_unattached(exit_unattached))
}
#[cfg(feature = "tmux_3_2")]
pub fn get_extended_keys(&self) -> Result<Option<Switch>, Error> {
self.get(GetServerOptionValue::extended_keys())
}
#[cfg(feature = "tmux_3_2")]
pub fn set_extended_keys(&self, extended_keys: Option<Switch>) -> Result<TmuxOutput, Error> {
self.set(SetServerOption::extended_keys(extended_keys))
}
#[cfg(feature = "tmux_1_9")]
pub fn get_focus_events(&self) -> Result<Option<Switch>, Error> {
self.get(GetServerOptionValue::focus_events())
}
#[cfg(feature = "tmux_1_9")]
pub fn set_focus_events(&self, focus_events: Option<Switch>) -> Result<TmuxOutput, Error> {
self.set(SetServerOption::focus_events(focus_events))
}
#[cfg(feature = "tmux_2_1")]
pub fn get_history_file(&self) -> Result<Option<String>, Error> {
self.get(GetServerOptionValue::history_file())
}
#[cfg(feature = "tmux_2_1")]
pub fn set_history_file(&self, history_file: Option<String>) -> Result<TmuxOutput, Error> {
self.set(SetServerOption::history_file(history_file))
}
#[cfg(feature = "tmux_2_0")]
pub fn get_message_limit(&self) -> Result<Option<usize>, Error> {
self.get(GetServerOptionValue::message_limit())
}
#[cfg(feature = "tmux_2_0")]
pub fn set_message_limit(&self, message_limit: Option<usize>) -> Result<TmuxOutput, Error> {
self.set(SetServerOption::message_limit(message_limit))
}
#[cfg(feature = "tmux_3_3")]
pub fn get_prompt_history_limit(&self) -> Result<Option<usize>, Error> {
self.get(GetServerOptionValue::prompt_history_limit())
}
#[cfg(feature = "tmux_3_3")]
pub fn set_prompt_history_limit(
&self,
prompt_history_limit: Option<usize>,
) -> Result<TmuxOutput, Error> {
self.set(SetServerOption::prompt_history_limit(prompt_history_limit))
}
#[cfg(feature = "tmux_1_5")]
pub fn get_set_clipboard(&self) -> Result<Option<SetClipboard>, Error> {
self.get(GetServerOptionValue::set_clipboard())
}
#[cfg(feature = "tmux_1_5")]
pub fn set_set_clipboard(
&self,
set_clipboard: Option<SetClipboard>,
) -> Result<TmuxOutput, Error> {
self.set(SetServerOption::set_clipboard(set_clipboard))
}
#[cfg(feature = "tmux_3_2")]
pub fn get_terminal_features(&self) -> Result<Option<Vec<String>>, Error> {
self.get_array(GetServerOptionValue::terminal_features())
}
#[cfg(feature = "tmux_3_2")]
pub fn set_terminal_features<I, S>(
&self,
terminal_features: Option<I>,
) -> Result<TmuxOutput, Error>
where
I: IntoIterator<Item = S>,
S: Into<Cow<'a, str>>,
{
self.set(TmuxCommand::with_cmds(SetServerOption::terminal_features(
terminal_features,
)))
}
#[cfg(feature = "tmux_2_0")]
pub fn get_terminal_overrides(&self) -> Result<Option<Vec<String>>, Error> {
self.get_array(GetServerOptionValue::terminal_overrides())
}
#[cfg(feature = "tmux_2_0")]
pub fn set_terminal_overrides<I, S>(
&self,
terminal_overrides: Option<I>,
) -> Result<TmuxOutput, Error>
where
I: IntoIterator<Item = S>,
S: Into<Cow<'a, str>>,
{
self.set(TmuxCommand::with_cmds(SetServerOption::terminal_overrides(
terminal_overrides,
)))
}
#[cfg(feature = "tmux_3_0")]
pub fn get_user_keys(&self) -> Result<Option<Vec<String>>, Error> {
self.get_array(GetServerOptionValue::user_keys())
}
#[cfg(feature = "tmux_3_0")]
pub fn set_user_keys<I, S>(&self, user_keys: Option<I>) -> Result<TmuxOutput, Error>
where
I: IntoIterator<Item = S>,
S: Into<Cow<'a, str>>,
{
self.set(TmuxCommand::with_cmds(SetServerOption::user_keys(
user_keys,
)))
}
#[cfg(all(feature = "tmux_1_2", not(feature = "tmux_2_0")))]
pub fn get_quiet(&self) -> Result<Option<Switch>, Error> {
self.get(GetServerOptionValue::quiet())
}
#[cfg(all(feature = "tmux_1_2", not(feature = "tmux_2_0")))]
pub fn set_quiet(&self, quiet: Option<Switch>) -> Result<TmuxOutput, Error> {
self.set(SetServerOption::quiet(quiet))
}
#[cfg(all(feature = "tmux_1_3", not(feature = "tmux_1_4")))]
pub fn get_detach_on_destroy(&self) -> Result<Option<Switch>, Error> {
self.get(GetServerOptionValue::detach_on_destroy())
}
#[cfg(all(feature = "tmux_1_3", not(feature = "tmux_1_4")))]
pub fn set_detach_on_destroy(
&self,
detach_on_destroy: Option<Switch>,
) -> Result<TmuxOutput, Error> {
self.set(SetServerOption::detach_on_destroy(detach_on_destroy))
}
pub fn get_user_option<S: Into<Cow<'a, str>>>(&self, name: S) -> Result<Option<String>, Error> {
self.get(GetServerOptionValue::user_option(name))
}
pub fn set_user_option<S: Into<Cow<'a, str>>, T: Into<Cow<'a, str>>>(
&self,
name: S,
value: Option<T>,
) -> Result<TmuxOutput, Error> {
self.set(SetServerOption::user_option(name, value))
}
}