char_coal/app/
cli.rs

1use std::path::PathBuf;
2
3use clap::{Args, Parser, Subcommand, ValueEnum};
4
5#[derive(Parser, Debug)]
6#[clap(version, about, long_about = None)]
7pub struct Cli {
8    #[clap(subcommand)]
9    pub commands: Commands,
10}
11
12impl Cli {
13    pub fn new() -> Commands {
14        Self::parse().commands
15    }
16}
17
18#[derive(Subcommand, Debug)]
19pub enum Commands {
20    /// Query words from online or offline
21    #[clap(aliases = &["q", "search", "s"])]
22    Query(QueryArgs),
23    /// Edit the configuration file
24    #[clap(aliases = &["e", "config"])]
25    Edit(EditArgs),
26    /// Cache commands
27    #[clap(aliases = &["c"])]
28    Cache {
29        #[clap(subcommand)]
30        commands: CacheCmds,
31    },
32}
33
34#[derive(Args, Debug)]
35pub struct QueryArgs {
36    /// The word to be queried
37    #[clap(value_parser)]
38    pub query: Vec<String>,
39    /// Speak aloud
40    #[clap(value_parser, short, long)]
41    pub speak: bool,
42    /// Mute (overloads speak)
43    #[clap(value_parser, short = 'q', long)]
44    pub mute: bool,
45    /// Whether to speak aloud
46    #[clap(value_parser, long)]
47    pub speak_as: Option<Toggle>,
48    /// Be concise
49    #[clap(value_parser, short, long)]
50    pub concise: bool,
51    /// Whether to be concise
52    #[clap(value_parser, long)]
53    pub concise_as: Option<Toggle>,
54}
55
56impl QueryArgs {
57    pub fn query(&self) -> String {
58        self.query.join(" ")
59    }
60}
61
62#[derive(Args, Debug)]
63pub struct EditArgs {
64    /// A fresh start
65    #[clap(value_parser, long)]
66    pub reset: bool,
67}
68
69#[derive(Subcommand, Debug)]
70pub enum CacheCmds {
71    /// Show cache location
72    #[clap(aliases = &["ls"])]
73    Show,
74    /// Clean cache
75    #[clap(aliases = &["destroy"])]
76    Clean,
77    /// Import
78    #[clap(aliases = &["in"])]
79    Import {
80        #[clap(value_parser)]
81        dir: PathBuf,
82    },
83    /// Export
84    #[clap(aliases = &["out"])]
85    Export {
86        #[clap(value_parser)]
87        dir: PathBuf,
88    },
89}
90
91#[derive(Clone, Debug, ValueEnum)]
92pub enum Toggle {
93    /// True
94    True,
95    /// False
96    False,
97    /// Flip
98    Flip,
99}
100
101impl Toggle {
102    pub fn twitch(&self, b: &mut bool) {
103        match self {
104            Toggle::True => *b = true,
105            Toggle::False => *b = false,
106            Toggle::Flip => *b = !*b,
107        }
108    }
109    pub fn counter_twitch(&self, b: &mut bool) {
110        match self {
111            Toggle::True => *b = false,
112            Toggle::False => *b = true,
113            Toggle::Flip => *b = !*b,
114        }
115    }
116}