aboutsummaryrefslogtreecommitdiffstats
path: root/src/config.rs
blob: 42614ecbaa149e5fec2bb971c58d7c663f3d4639 (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use crate::{Error, Result};
use clap::{Args, Parser};
use figment::{
    providers::{Env, Format, Serialized, Toml},
    Figment,
};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

/// Simple program to manage projects and ssh hosts using tmux
#[derive(Debug, Parser, Serialize, Deserialize)]
#[command(author, version, about)]
pub struct Config {
    /// Path to search for directories
    pub(crate) path: Vec<PathBuf>,

    /// Stand alone directory. Can be specified multiple times
    #[arg(short, long)]
    pub(crate) project: Vec<PathBuf>,

    /// Allows traversal into hidden directories when searching
    #[arg(long)]
    pub(crate) hidden: bool,

    #[arg(skip)]
    pub(crate) finder: Finder,

    #[arg(skip)]
    pub(crate) enable_logging: bool,

    #[arg(skip)]
    pub(crate) log_file: PathBuf,
}

impl Config {
    pub fn extract(&self) -> Result<Config> {
        Figment::from(Serialized::defaults(self))
            .merge(Toml::file("tmuxr.toml"))
            .merge(Env::prefixed("TMUXR_"))
            .extract()
            .map_err(Error::from)
    }
}

impl Default for Config {
    fn default() -> Self {
        Self {
            path: Default::default(),
            project: Default::default(),
            hidden: Default::default(),
            finder: Default::default(),
            enable_logging: Default::default(),
            log_file: dirs::cache_dir()
                .map(|p| p.join("tmuxr"))
                .unwrap_or_default()
                .join("tmuxr.log"),
        }
    }
}

#[derive(Debug, Args, Serialize, Deserialize)]
pub(crate) struct Finder {
    pub(crate) program: String,
    pub(crate) args: Vec<String>,
}

impl Default for Finder {
    fn default() -> Self {
        Self {
            program: "fzf-tmux".into(),
            args: vec![
                "--".into(),
                "--multi".into(),
                "--print-query".into(),
                "-d/".into(),
                "--preview-window='right,75%,<80(up,75%,border-bottom)'".into(),
                "--preview='sel={}; less ${sel:-{q}} 2>/dev/null'".into(),
            ],
        }
    }
}

#[derive(Debug, Args, Serialize, Deserialize)]
pub(crate) struct Directory {
    pub(crate) path: PathBuf,
    pub(crate) root: bool,
    pub(crate) hidden: bool,
}

#[derive(Debug, Default)]
pub(crate) struct Directories {
    search_dirs: Vec<PathBuf>,
    root_dirs: Vec<PathBuf>,
}

impl From<Vec<Directory>> for Directories {
    fn from(value: Vec<Directory>) -> Self {
        value.iter().fold(Directories::default(), |mut acc, d| {
            match d.root {
                true => acc.root_dirs.push(d.path.to_owned()),
                false => acc.search_dirs.push(d.path.to_owned()),
            };
            acc
        })
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_start() {
        assert_eq!(1, 1);
    }
}