src/commands/mod.rs
61 lines · 1428 bytes
1use clap::{App, ArgMatches};
2use weechat::{
3 hooks::{Command, CommandRun},
4 Args, Weechat,
5};
6
7use crate::{config::ConfigHandle, Servers};
8
9mod devices;
10mod keys;
11mod matrix;
12mod page_up;
13mod buffer_clear;
14
15use devices::DevicesCommand;
16use keys::KeysCommand;
17use matrix::MatrixCommand;
18use page_up::PageUpCommand;
19use buffer_clear::BufferClearCommand;
20
21pub struct Commands {
22 _matrix: Command,
23 _keys: Command,
24 _devices: Command,
25 _page_up: CommandRun,
26 _buffer_clear: CommandRun,
27}
28
29impl Commands {
30 pub fn hook_all(
31 servers: &Servers,
32 config: &ConfigHandle,
33 ) -> Result<Commands, ()> {
34 Ok(Commands {
35 _matrix: MatrixCommand::create(servers, config)?,
36 _devices: DevicesCommand::create(servers)?,
37 _keys: KeysCommand::create(servers)?,
38 _page_up: PageUpCommand::create(servers)?,
39 _buffer_clear: BufferClearCommand::create(servers)?,
40 })
41 }
42}
43
44fn parse_and_run(
45 parser: App,
46 arguments: Args,
47 command: impl FnOnce(&ArgMatches),
48) {
49 match parser.get_matches_from_safe(arguments) {
50 Ok(m) => command(&m),
51 Err(e) => {
52 let error = Weechat::execute_modifier(
53 "color_decode_ansi",
54 "1",
55 &e.to_string(),
56 )
57 .expect("Can't color decode ansi string");
58 Weechat::print(&error);
59 }
60 }
61}