src/commands/mod.rs

d6c7becfdedcaae0c782a0dce8290b3047e01c0a
weechat-matrix-rs/src/commands/mod.rs history · blame · raw

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 buffer_clear;
10mod devices;
11mod keys;
12mod matrix;
13mod page_up;
14
15use buffer_clear::BufferClearCommand;
16use devices::DevicesCommand;
17use keys::KeysCommand;
18use matrix::MatrixCommand;
19use page_up::PageUpCommand;
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}