ayun_console/
lib.rs

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
use ayun_core::{Callback, Result};
use clap::{
    builder::{
        styling::{AnsiColor, Effects},
        IntoResettable, Str, StyledStr, Styles,
    },
    ArgMatches, Command,
};
use std::collections::{BTreeMap, HashMap};

pub mod command;
pub mod commands;
mod service;
pub mod traits;

#[derive(Clone)]
pub struct Console {
    inner: Command,
    commands: BTreeMap<String, Command>,
    closures: HashMap<String, Callback<ArgMatches, Result<()>>>,
}

pub const STYLES: Styles = Styles::styled()
    .header(AnsiColor::Yellow.on_default().effects(Effects::BOLD))
    .usage(AnsiColor::Yellow.on_default().effects(Effects::BOLD))
    .literal(AnsiColor::Green.on_default())
    .placeholder(AnsiColor::Cyan.on_default())
    .error(AnsiColor::Red.on_default().effects(Effects::BOLD))
    .valid(AnsiColor::Cyan.on_default().effects(Effects::BOLD))
    .invalid(AnsiColor::Yellow.on_default().effects(Effects::BOLD));

impl Console {
    pub fn name(mut self, name: impl Into<Str>) -> Self {
        self.inner = self.inner.name(name);
        self
    }

    pub fn version(mut self, ver: impl IntoResettable<Str>) -> Self {
        self.inner = self.inner.version(ver);
        self
    }

    pub fn about(mut self, about: impl IntoResettable<StyledStr>) -> Self {
        self.inner = self.inner.about(about);
        self
    }
}

const LOGO: &str = r"
░░      ░░░  ░░░░  ░░  ░░░░  ░░   ░░░  ░
▒  ▒▒▒▒  ▒▒▒  ▒▒  ▒▒▒  ▒▒▒▒  ▒▒    ▒▒  ▒
▓  ▓▓▓▓  ▓▓▓▓    ▓▓▓▓  ▓▓▓▓  ▓▓  ▓  ▓  ▓
█        █████  █████  ████  ██  ██    █
█  ████  █████  ██████      ███  ███   █
";

impl Default for Console {
    fn default() -> Self {
        let command = Command::default()
            .styles(STYLES)
            .name("ayun")
            .version(env!("CARGO_PKG_VERSION"))
            .about(format!("\n {} \n {}", LOGO, env!("CARGO_PKG_DESCRIPTION")));

        Self {
            inner: command,
            commands: BTreeMap::new(),
            closures: HashMap::new(),
        }
    }
}