termfmt/
lib.rs

1use std::collections::HashMap;
2
3use ::clap::Command;
4pub use {
5    clap::{TermFmtExt, TermFmtsExt},
6    color::{Bg, Fg, Style, TermStyle},
7    command::{CommandOutputError, CommandStatusError, TermCommandDefinition},
8    fmt::{BundleFmt, TermFmt},
9    output::{
10        termarrow, termarrow_fg, termerr, termh1, termh2, termh3, termh_fg, terminfo, termprefix1,
11        termprefix2, termprefix3,
12    },
13};
14
15pub mod chrono;
16
17mod clap;
18mod color;
19mod command;
20mod fmt;
21mod output;
22pub mod parse;
23
24pub struct TermCommand {
25    command: Command,
26    definitions: HashMap<String, Box<dyn TermCommandDefinition>>,
27}
28
29impl TermCommand {
30    pub fn new() -> Self {
31        let command = Command::new(env!("CARGO_PKG_NAME"))
32            .version(env!("CARGO_PKG_VERSION"))
33            .about(env!("CARGO_PKG_DESCRIPTION"));
34        Self {
35            command,
36            definitions: HashMap::new(),
37        }
38    }
39
40    pub fn configure(mut self, callback: impl Fn(Command) -> Command) -> Self {
41        self.command = callback(self.command);
42        self
43    }
44
45    pub fn add(mut self, definition: impl TermCommandDefinition + 'static) -> Self {
46        let command = definition.clap();
47        self.command = self.command.clone().subcommand(command.clone());
48        self.definitions
49            .insert(command.get_name().to_owned(), Box::new(definition));
50        self
51    }
52
53    pub fn run(self) {
54        _ = (self.command.get_matches(),);
55    }
56}