forked from sharkdp/pastel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
90 lines (81 loc) · 2.97 KB
/
mod.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use crate::config::Config;
use crate::error::Result;
use crate::output::Output;
use clap::ArgMatches;
mod color_commands;
mod colorcheck;
mod distinct;
mod format;
mod gradient;
mod gray;
mod imas;
mod io;
mod list;
mod paint;
mod pick;
mod prelude;
mod random;
mod show;
mod sort;
mod traits;
use traits::{ColorCommand, GenericCommand};
use colorcheck::ColorCheckCommand;
use distinct::DistinctCommand;
use format::FormatCommand;
use gradient::GradientCommand;
use gray::GrayCommand;
use imas::ImasCommand;
use list::ListCommand;
use paint::PaintCommand;
use pick::PickCommand;
use random::RandomCommand;
use sort::SortCommand;
use io::ColorArgIterator;
pub enum Command {
WithColor(Box<dyn ColorCommand>),
Generic(Box<dyn GenericCommand>),
}
impl Command {
pub fn from_string(command: &str) -> Command {
match command {
"color" => Command::WithColor(Box::new(show::ShowCommand)),
"saturate" => Command::WithColor(Box::new(color_commands::SaturateCommand)),
"desaturate" => Command::WithColor(Box::new(color_commands::DesaturateCommand)),
"lighten" => Command::WithColor(Box::new(color_commands::LightenCommand)),
"darken" => Command::WithColor(Box::new(color_commands::DarkenCommand)),
"rotate" => Command::WithColor(Box::new(color_commands::RotateCommand)),
"colorblind" => Command::WithColor(Box::new(color_commands::ColorblindCommand)),
"set" => Command::WithColor(Box::new(color_commands::SetCommand)),
"complement" => Command::WithColor(Box::new(color_commands::ComplementCommand)),
"mix" => Command::WithColor(Box::new(color_commands::MixCommand)),
"to-gray" => Command::WithColor(Box::new(color_commands::ToGrayCommand)),
"textcolor" => Command::WithColor(Box::new(color_commands::TextColorCommand)),
"pick" => Command::Generic(Box::new(PickCommand)),
"gray" => Command::Generic(Box::new(GrayCommand)),
"list" => Command::Generic(Box::new(ListCommand)),
"sort-by" => Command::Generic(Box::new(SortCommand)),
"random" => Command::Generic(Box::new(RandomCommand)),
"distinct" => Command::Generic(Box::new(DistinctCommand)),
"gradient" => Command::Generic(Box::new(GradientCommand)),
"paint" => Command::Generic(Box::new(PaintCommand)),
"format" => Command::WithColor(Box::new(FormatCommand)),
"colorcheck" => Command::Generic(Box::new(ColorCheckCommand)),
"imas" => Command::Generic(Box::new(ImasCommand)),
_ => unreachable!("Unknown subcommand"),
}
}
pub fn execute(&self, matches: &ArgMatches, config: &Config) -> Result<()> {
let stdout = std::io::stdout();
let mut stdout_lock = stdout.lock();
let mut out = Output::new(&mut stdout_lock);
match self {
Command::Generic(cmd) => cmd.run(&mut out, matches, config),
Command::WithColor(cmd) => {
for color in ColorArgIterator::from_args(config, matches.values_of("color"))? {
cmd.run(&mut out, matches, config, &color?)?;
}
Ok(())
}
}
}
}