1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use crate::{
    Error, GetLocalWindowOption, GetLocalWindowOptionValue, SetLocalWindowOption,
    SetLocalWindowOptions, Tmux, TmuxCommand, TmuxOutput, WindowOptionsCtl,
};
use std::borrow::Cow;

// XXX: rename WindowOptionCtl?
// trait top level options, then server session window pane
pub struct LocalWindowOptionsCtl<'a> {
    // TODO: comment/doc
    //
    // function used for executing the given option get/set command
    //
    // ```
    // let tmux = Tmux::new();
    // ```
    pub invoker: &'a dyn Fn(TmuxCommand<'a>) -> Result<TmuxOutput, Error>,
    pub target: Option<Cow<'a, str>>,
}

impl<'a> Default for LocalWindowOptionsCtl<'a> {
    fn default() -> Self {
        Self {
            invoker: &|cmd| Tmux::with_command(cmd).output(),
            target: None,
        }
    }
}

impl<'a> LocalWindowOptionsCtl<'a> {
    pub fn new(invoker: &'a dyn Fn(TmuxCommand<'a>) -> Result<TmuxOutput, Error>) -> Self {
        Self {
            invoker,
            target: None,
        }
    }

    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> WindowOptionsCtl<'a> for LocalWindowOptionsCtl<'a> {
    type Getter = GetLocalWindowOptionValue;
    type Setter = SetLocalWindowOption;
    type GetterAll = GetLocalWindowOption;
    type SetterMultiple = SetLocalWindowOptions<'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
    }
}