use clap::{Args, Parser}; use figment::{providers::Serialized, value, Metadata, Profile, Provider}; use serde::Serialize; use std::path::PathBuf; use tracing_subscriber::{filter::LevelFilter, Layer, Registry}; use crate::{paths::PathEntry, Config}; /// Simple program to manage projects and ssh hosts using tmux #[derive(Debug, Clone, Default, Parser, Serialize)] #[command(author, version, about)] #[serde(into = "Config")] pub struct Cli { /// Path to directories pub(crate) paths: Vec, /// Max depth to recurse. /// /// By default, no limit is set. Setting to 0 will only use the supplied directory. #[arg(short = 'd', long)] pub(crate) max_depth: Option, /// Recurse into hidden directories. /// /// Include hidden directories when traversing directories. (default: hidden directories /// are skipped). A Directory is considered to be hidden if its name starts with a `.` /// sign (dot). If `max-depth` is set to 0, this has no effect (As no recursion happens). #[arg(long)] pub(crate) hidden: bool, #[command(flatten)] pub verbose: Verbosity, /// Connect to ssh host #[arg(short, long)] pub ssh: Option, } impl Cli { pub fn as_layer(&self) -> Vec + Send + Sync>> { let fmt_layer = tracing_subscriber::fmt::layer() .pretty() .with_filter(self.verbose.as_filter()) .boxed(); vec![fmt_layer] } } impl From for Config { fn from(value: Cli) -> Self { Config { paths: crate::paths::Config { paths: value .paths .iter() .cloned() .map(|p| PathEntry { path: p, hidden: value.hidden, recurse: value.max_depth, }) .collect(), }, ..Default::default() } } } impl Provider for Cli { fn metadata(&self) -> Metadata { Metadata::named("Tmuxr cli provider") } fn data(&self) -> figment::error::Result> { Serialized::defaults(Self::default()).data() } } #[derive(Debug, Default, Clone, Args)] pub struct Verbosity { /// Print additional information per occurrence #[arg(short, long, action = clap::ArgAction::Count, conflicts_with = "quiet")] pub verbose: u8, /// Suppress all output #[arg(short, long, global = true, conflicts_with = "verbose")] pub quiet: bool, } impl Verbosity { pub fn as_filter(&self) -> LevelFilter { self.into() } } impl From<&Verbosity> for LevelFilter { fn from(value: &Verbosity) -> Self { match value.verbose + 1 - u8::from(value.quiet) { 0 => LevelFilter::OFF, 1 => LevelFilter::ERROR, 2 => LevelFilter::WARN, 3 => LevelFilter::INFO, 4 => LevelFilter::DEBUG, _ => LevelFilter::TRACE, } } } #[cfg(test)] mod tests { #[test] fn test_cli_parse() { assert_eq!(1, 1); } }