From 698d811059ebb6787305e8293a77837323fc9311 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Tue, 1 Nov 2022 13:00:07 -0500 Subject: feat: impl cli, config, and logging via clap, figment, and tracing, respectively --- src/config.rs | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 src/config.rs (limited to 'src/config.rs') 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, + + /// Stand alone directory. Can be specified multiple times + #[arg(short, long)] + pub(crate) project: Vec, + + /// 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 { + 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, +} + +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, + root_dirs: Vec, +} + +impl From> for Directories { + fn from(value: Vec) -> 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); + } +} -- cgit v1.2.3-70-g09d2