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
93
94
95
96
97
98
99
100
101
102
103
104
105
use clap::{Parser, Subcommand};
use crossterm::{
    execute,
    style::{Print, Stylize},
};
use std::{
    io::{stderr, stdout},
    path::PathBuf,
};

use crate::{config::Configuration, render, Error};

#[derive(Parser)]
#[command(name = "tmux-fzy")]
pub struct Cli {
    #[command(subcommand)]
    command: Option<Commands>,
}

#[derive(Subcommand, Debug)]
enum Commands {
    Add {
        #[arg(long, default_value_t = 0)]
        maxdepth: usize,
        #[arg(long, default_value_t = 0)]
        mindepth: usize,
        paths: Vec<PathBuf>,
    },

    List,

    Del {
        paths: Vec<PathBuf>,
    },
}

pub fn start(config: &mut Configuration) -> Result<(), Error> {
    let cli = Cli::parse();

    match cli.command {
        None => {
            let res = render(config);
            if let Err(err) = res {
                execute!(
                    stderr(),
                    Print(crossterm::style::Stylize::bold(
                        crossterm::style::Stylize::red("Error: ")
                    )),
                    Print(format!("{:?}", err))
                )
                .map_err(|e| Error::UnexpectedError(e.into()))?
            }

            Ok(())
        }
        Some(cmd) => match cmd {
            Commands::Add {
                maxdepth,
                mindepth,
                paths,
            } => {
                for path in paths {
                    let path = path
                        .canonicalize()
                        .map_err(|e| Error::UnexpectedError(e.into()))?
                        .to_str()
                        .unwrap()
                        .to_string();

                    config.insert_row(path, mindepth, maxdepth);
                }
                config.save_configuration().expect("Failed to save");
                Ok(())
            }
            Commands::Del { paths } => {
                for path in paths {
                    let path = path
                        .canonicalize()
                        .map_err(|e| Error::UnexpectedError(e.into()))?;
                    let path = path.to_str().unwrap();
                    _ = config.0.remove(path);
                    config.save_configuration()?;
                }
                Ok(())
            }
            Commands::List => {
                for (i, (path, values)) in config.0.iter().enumerate() {
                    execute!(
                        stdout(),
                        Print(format!("{}: ", i + 1).blue()),
                        Print(path),
                        Print("\tsearch_depth:[".blue()),
                        Print("min:"),
                        Print(values.min_depth),
                        Print(", max:"),
                        Print(values.max_depth),
                        Print("]".blue()),
                    )
                    .map_err(|e| Error::UnexpectedError(e.into()))?;
                }
                Ok(())
            }
        },
    }
}