summaryrefslogtreecommitdiffstats
path: root/src/config.rs
diff options
context:
space:
mode:
authorToby Vincent <tobyv13@gmail.com>2022-11-01 13:00:07 -0500
committerToby Vincent <tobyv13@gmail.com>2022-11-01 13:02:02 -0500
commit698d811059ebb6787305e8293a77837323fc9311 (patch)
tree218818bf54373848d49ec3e888e9d8c51bace845 /src/config.rs
parent61e7c46e5b805c2fda1b4724fb8389a187c75987 (diff)
feat: impl cli, config, and logging
via clap, figment, and tracing, respectively
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs114
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);
+ }
+}