use crate::options::{
GetLocalSessionOption, GetLocalSessionOptionValue, SessionOptionsCtl, SetLocalSessionOption,
SetLocalSessionOptions,
};
use crate::{Error, Tmux, TmuxCommand, TmuxOutput};
use std::borrow::Cow;
pub struct LocalSessionOptionsCtl<'a> {
pub invoker: &'a dyn Fn(TmuxCommand<'a>) -> Result<TmuxOutput, Error>,
pub target: Option<Cow<'a, str>>,
}
impl<'a> Default for LocalSessionOptionsCtl<'a> {
fn default() -> Self {
Self {
invoker: &|cmd| Tmux::with_command(cmd).output(),
target: None,
}
}
}
impl<'a> LocalSessionOptionsCtl<'a> {
pub fn new<S: Into<Cow<'a, str>>>(
target: Option<S>,
invoker: &'a dyn Fn(TmuxCommand<'a>) -> Result<TmuxOutput, Error>,
) -> Self {
Self {
invoker,
target: target.map(|s| s.into()),
}
}
pub fn with_target<S: Into<Cow<'a, str>>>(target: Option<S>) -> Self {
Self {
target: target.map(|s| s.into()),
..Default::default()
}
}
pub fn with_invoker(invoker: &'a dyn Fn(TmuxCommand<'a>) -> Result<TmuxOutput, Error>) -> Self {
Self {
invoker,
..Default::default()
}
}
}
impl<'a> SessionOptionsCtl<'a> for LocalSessionOptionsCtl<'a> {
type Getter = GetLocalSessionOptionValue;
type Setter = SetLocalSessionOption;
type GetterAll = GetLocalSessionOption;
type SetterMultiple = SetLocalSessionOptions<'a>;
fn target(&self) -> Option<Cow<'a, str>> {
self.target.to_owned()
}
fn invoker(&self) -> &'a dyn Fn(TmuxCommand<'a>) -> Result<TmuxOutput, Error> {
self.invoker
}
}