summaryrefslogtreecommitdiffstats
path: root/src/cli.rs
blob: 68bc665ccec881aa5ab6a7269398c4d4c84ff90e (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
use crate::{config::Paths, Config, Result};
use clap::{Args, Parser};
use std::path::PathBuf;
use tracing_subscriber::{filter::LevelFilter, Layer, Registry};

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

    /// Add additional directory to search results. Can be specified multiple times
    #[arg(short, long)]
    pub(crate) directory: Vec<PathBuf>,

    #[command(flatten)]
    pub verbose: Verbosity,

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

    /// Connect to ssh host
    #[arg(short, long)]
    pub ssh: Option<String>,
}

impl Cli {
    pub fn as_layer(&self) -> Result<Vec<Box<dyn Layer<Registry> + Send + Sync>>> {
        let mut layers = Vec::new();

        let fmt_layer = tracing_subscriber::fmt::layer()
            .pretty()
            .with_filter(self.verbose.into_filter())
            .boxed();

        layers.push(fmt_layer);

        Ok(layers)
    }

    pub fn as_config(&self) -> Config {
        Config {
            paths: Paths {
                search: self.path.to_owned(),
                add: self.directory.to_owned(),
                hidden: self.hidden,
            },
            finder: Default::default(),
            logging: Default::default(),
        }
    }
}

#[derive(Debug, Default, 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 into_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_start() {
        assert_eq!(1, 1);
    }
}