Commit 10d1cef849
Unsigned
Layout: unified · split
src/commands/devices.rs +4 −16
| @@ -10,6 +10,7 @@ use weechat::{ | |||
| 10 | Args, Weechat, | 10 | Args, Weechat, |
| 11 | }; | 11 | }; |
| 12 | 12 | ||
| 13 | use super::parse_and_run; | ||
| 13 | use crate::Servers; | 14 | use crate::Servers; |
| 14 | 15 | ||
| 15 | pub struct DevicesCommand { | 16 | pub struct DevicesCommand { |
| @@ -120,21 +121,8 @@ impl CommandCallback for DevicesCommand { | |||
| 120 | .setting(ArgParseSettings::SubcommandRequiredElseHelp) | 121 | .setting(ArgParseSettings::SubcommandRequiredElseHelp) |
| 121 | .subcommands(Self::subcommands()); | 122 | .subcommands(Self::subcommands()); |
| 122 | 123 | ||
| 123 | let matches = match argparse.get_matches_from_safe(arguments) { | 124 | parse_and_run(argparse, arguments, |matches| { |
| 124 | Ok(m) => m, | 125 | Self::run(buffer, &self.servers, &matches) |
| 125 | Err(e) => { | 126 | }); |
| 126 | Weechat::print( | ||
| 127 | &Weechat::execute_modifier( | ||
| 128 | "color_decode_ansi", | ||
| 129 | "1", | ||
| 130 | &e.to_string(), | ||
| 131 | ) | ||
| 132 | .unwrap(), | ||
| 133 | ); | ||
| 134 | return; | ||
| 135 | } | ||
| 136 | }; | ||
| 137 | |||
| 138 | Self::run(buffer, &self.servers, &matches) | ||
| 139 | } | 127 | } |
| 140 | } | 128 | } |
src/commands/keys.rs +4 −16
| @@ -11,6 +11,7 @@ use weechat::{ | |||
| 11 | Args, Weechat, | 11 | Args, Weechat, |
| 12 | }; | 12 | }; |
| 13 | 13 | ||
| 14 | use super::parse_and_run; | ||
| 14 | use crate::{MatrixServer, Servers}; | 15 | use crate::{MatrixServer, Servers}; |
| 15 | 16 | ||
| 16 | pub struct KeysCommand { | 17 | pub struct KeysCommand { |
| @@ -121,21 +122,8 @@ impl CommandCallback for KeysCommand { | |||
| 121 | .setting(ArgParseSettings::SubcommandRequiredElseHelp) | 122 | .setting(ArgParseSettings::SubcommandRequiredElseHelp) |
| 122 | .subcommands(Self::subcommands()); | 123 | .subcommands(Self::subcommands()); |
| 123 | 124 | ||
| 124 | let matches = match argparse.get_matches_from_safe(arguments) { | 125 | parse_and_run(argparse, arguments, |matches| { |
| 125 | Ok(m) => m, | 126 | Self::run(buffer, &self.servers, matches) |
| 126 | Err(e) => { | 127 | }); |
| 127 | Weechat::print( | ||
| 128 | &Weechat::execute_modifier( | ||
| 129 | "color_decode_ansi", | ||
| 130 | "1", | ||
| 131 | &e.to_string(), | ||
| 132 | ) | ||
| 133 | .unwrap(), | ||
| 134 | ); | ||
| 135 | return; | ||
| 136 | } | ||
| 137 | }; | ||
| 138 | |||
| 139 | Self::run(buffer, &self.servers, &matches) | ||
| 140 | } | 128 | } |
| 141 | } | 129 | } |
src/commands/matrix.rs +17 −27
| @@ -10,6 +10,7 @@ use weechat::{ | |||
| 10 | Args, Weechat, | 10 | Args, Weechat, |
| 11 | }; | 11 | }; |
| 12 | 12 | ||
| 13 | use super::parse_and_run; | ||
| 13 | use crate::{ | 14 | use crate::{ |
| 14 | commands::{DevicesCommand, KeysCommand}, | 15 | commands::{DevicesCommand, KeysCommand}, |
| 15 | config::ConfigHandle, | 16 | config::ConfigHandle, |
| @@ -216,6 +217,21 @@ Use /matrix [command] help to find out more.\n", | |||
| 216 | self.server_not_found(server_name) | 217 | self.server_not_found(server_name) |
| 217 | } | 218 | } |
| 218 | } | 219 | } |
| 220 | |||
| 221 | fn run(&self, buffer: &Buffer, args: &ArgMatches) { | ||
| 222 | match args.subcommand() { | ||
| 223 | ("connect", Some(subargs)) => self.connect_command(subargs), | ||
| 224 | ("disconnect", Some(subargs)) => self.disconnect_command(subargs), | ||
| 225 | ("server", Some(subargs)) => self.server_command(subargs), | ||
| 226 | ("devices", Some(subargs)) => { | ||
| 227 | DevicesCommand::run(buffer, &self.servers, subargs) | ||
| 228 | } | ||
| 229 | ("keys", Some(subargs)) => { | ||
| 230 | KeysCommand::run(buffer, &self.servers, subargs) | ||
| 231 | } | ||
| 232 | _ => unreachable!(), | ||
| 233 | } | ||
| 234 | } | ||
| 219 | } | 235 | } |
| 220 | 236 | ||
| 221 | impl CommandCallback for MatrixCommand { | 237 | impl CommandCallback for MatrixCommand { |
| @@ -296,32 +312,6 @@ impl CommandCallback for MatrixCommand { | |||
| 296 | ), | 312 | ), |
| 297 | ); | 313 | ); |
| 298 | 314 | ||
| 299 | let matches = match argparse.get_matches_from_safe(arguments) { | 315 | parse_and_run(argparse, arguments, |args| self.run(buffer, args)); |
| 300 | Ok(m) => m, | ||
| 301 | Err(e) => { | ||
| 302 | Weechat::print( | ||
| 303 | &Weechat::execute_modifier( | ||
| 304 | "color_decode_ansi", | ||
| 305 | "1", | ||
| 306 | &e.to_string(), | ||
| 307 | ) | ||
| 308 | .unwrap(), | ||
| 309 | ); | ||
| 310 | return; | ||
| 311 | } | ||
| 312 | }; | ||
| 313 | |||
| 314 | match matches.subcommand() { | ||
| 315 | ("connect", Some(subargs)) => self.connect_command(subargs), | ||
| 316 | ("disconnect", Some(subargs)) => self.disconnect_command(subargs), | ||
| 317 | ("server", Some(subargs)) => self.server_command(subargs), | ||
| 318 | ("devices", Some(subargs)) => { | ||
| 319 | DevicesCommand::run(buffer, &self.servers, subargs) | ||
| 320 | } | ||
| 321 | ("keys", Some(subargs)) => { | ||
| 322 | KeysCommand::run(buffer, &self.servers, subargs) | ||
| 323 | } | ||
| 324 | _ => unreachable!(), | ||
| 325 | } | ||
| 326 | } | 316 | } |
| 327 | } | 317 | } |
src/commands/mod.rs +22 −1
| @@ -1,5 +1,7 @@ | |||
| 1 | use clap::{App, ArgMatches}; | ||
| 2 | use weechat::{hooks::Command, Args, Weechat}; | ||
| 3 | |||
| 1 | use crate::{config::ConfigHandle, Servers}; | 4 | use crate::{config::ConfigHandle, Servers}; |
| 2 | use weechat::hooks::Command; | ||
| 3 | 5 | ||
| 4 | mod devices; | 6 | mod devices; |
| 5 | mod keys; | 7 | mod keys; |
| @@ -27,3 +29,22 @@ impl Commands { | |||
| 27 | }) | 29 | }) |
| 28 | } | 30 | } |
| 29 | } | 31 | } |
| 32 | |||
| 33 | fn parse_and_run( | ||
| 34 | parser: App, | ||
| 35 | arguments: Args, | ||
| 36 | command: impl FnOnce(&ArgMatches), | ||
| 37 | ) { | ||
| 38 | match parser.get_matches_from_safe(arguments) { | ||
| 39 | Ok(m) => command(&m), | ||
| 40 | Err(e) => { | ||
| 41 | let error = Weechat::execute_modifier( | ||
| 42 | "color_decode_ansi", | ||
| 43 | "1", | ||
| 44 | &e.to_string(), | ||
| 45 | ) | ||
| 46 | .expect("Can't color decode ansi string"); | ||
| 47 | Weechat::print(&error); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | } | ||