diff options
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..42614ec --- /dev/null +++ b/src/config.rs @@ -0,0 +1,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); + } +} |