tmux_interface/options/common/
mod.rs

1pub mod constants;
2pub mod status_keys;
3pub mod switch;
4pub mod terminal_features;
5pub mod user_option;
6
7pub use constants::*;
8pub use status_keys::StatusKeys;
9pub use switch::Switch;
10pub use terminal_features::*;
11pub use user_option::*;
12
13// command_alias[0] = "alias1" => command_alias["alias1"]
14// command_alias[1] = "alias2" => command_alias["alias2"]
15// ...
16// command_alias[n] = "aliasN" => command_alias["aliasN"]
17// TODO: optimization, merge server, session, window, pane?
18
19const SEPARATOR: &str = " ";
20
21use std::borrow::Cow;
22use std::fmt;
23
24pub fn option_to_string<S: fmt::Display>(v: &mut Vec<String>, name: &str, value: &Option<S>) {
25    if let Some(data) = value {
26        v.push(format!("{} {}", name, data))
27    }
28}
29
30pub fn option_array_to_string<S: fmt::Display>(
31    v: &mut Vec<String>,
32    name: &str,
33    value: &Option<Vec<S>>,
34) {
35    if let Some(data) = value {
36        for item in data {
37            v.push(format!("{} {}", name, item))
38        }
39    }
40}
41
42pub fn array_insert(v: &mut Option<Vec<Cow<'_, str>>>, i: Option<usize>, value: Option<String>) {
43    if let Some(i) = i {
44        match value {
45            Some(data) => v.get_or_insert(Vec::new()).insert(i, data.into()),
46            None => *v = None,
47        }
48    }
49}
50
51pub fn cow_parse<'a>(value: Option<&str>) -> Option<Cow<'a, str>> {
52    value.map(|s| Cow::Owned(s.into()))
53}
54
55// split string in 3 parts, name, index (if option is an array) and value
56// TODO: rename
57pub fn get_parts(s: &str) -> Option<(&str, Option<usize>, Option<&str>)> {
58    let v: Vec<&str> = s.trim().splitn(2, SEPARATOR).collect();
59    let value = v.get(1).copied();
60    match v.first() {
61        Some(name) => {
62            let v: Vec<&str> = name.split(|c| c == '[' || c == ']').collect();
63            match v.first() {
64                Some(name) => {
65                    let index = v.get(1).and_then(|i| i.parse().ok());
66                    Some((name, index, value))
67                }
68                None => None,
69            }
70        }
71        None => None,
72    }
73}
74
75#[cfg(test)]
76#[path = "."]
77mod common_tests {
78    pub mod status_keys_tests;
79    pub mod switch_tests;
80}