Commit d41b55b470
Unsigned
Layout: unified · split
src/commands/devices.rs +55 −44
| @@ -1,4 +1,7 @@ | ||
| 1 | use clap::{App as Argparse, AppSettings as ArgParseSettings, Arg, SubCommand}; | |
| 1 | use clap::{ | |
| 2 | App as Argparse, AppSettings as ArgParseSettings, Arg, ArgMatches, | |
| 3 | SubCommand, | |
| 4 | }; | |
| 2 | 5 | use matrix_sdk::identifiers::DeviceIdBox; |
| 3 | 6 | |
| 4 | 7 | use weechat::{ |
| @@ -14,9 +17,12 @@ pub struct DevicesCommand { | ||
| 14 | 17 | } |
| 15 | 18 | |
| 16 | 19 | impl DevicesCommand { |
| 20 | pub const DESCRIPTION: &'static str = | |
| 21 | "List, delete or rename Matrix devices"; | |
| 22 | ||
| 17 | 23 | pub fn create(servers: &Servers) -> Result<Command, ()> { |
| 18 | 24 | let settings = CommandSettings::new("devices") |
| 19 | .description("List, delete or rename Matrix devices.") | |
| 25 | .description(Self::DESCRIPTION) | |
| 20 | 26 | .add_argument("list") |
| 21 | 27 | .add_argument("delete <device-id>") |
| 22 | 28 | .add_argument("set-name <device-id> <name>") |
| @@ -38,8 +44,8 @@ impl DevicesCommand { | ||
| 38 | 44 | ) |
| 39 | 45 | } |
| 40 | 46 | |
| 41 | fn delete(&self, buffer: &Buffer, devices: Vec<DeviceIdBox>) { | |
| 42 | let server = self.servers.find_server(buffer); | |
| 47 | fn delete(servers: &Servers, buffer: &Buffer, devices: Vec<DeviceIdBox>) { | |
| 48 | let server = servers.find_server(buffer); | |
| 43 | 49 | |
| 44 | 50 | if let Some(s) = server { |
| 45 | 51 | let devices = || async move { |
| @@ -51,8 +57,8 @@ impl DevicesCommand { | ||
| 51 | 57 | } |
| 52 | 58 | } |
| 53 | 59 | |
| 54 | fn list(&self, buffer: &Buffer) { | |
| 55 | let server = self.servers.find_server(buffer); | |
| 60 | fn list(servers: &Servers, buffer: &Buffer) { | |
| 61 | let server = servers.find_server(buffer); | |
| 56 | 62 | |
| 57 | 63 | if let Some(s) = server { |
| 58 | 64 | let devices = || async move { |
| @@ -63,35 +69,57 @@ impl DevicesCommand { | ||
| 63 | 69 | Weechat::print("Must be executed on Matrix buffer") |
| 64 | 70 | } |
| 65 | 71 | } |
| 72 | ||
| 73 | pub fn run(buffer: &Buffer, servers: &Servers, args: &ArgMatches) { | |
| 74 | match args.subcommand() { | |
| 75 | ("list", _) => Self::list(servers, buffer), | |
| 76 | ("delete", args) => { | |
| 77 | let devices = args.unwrap().args.get("device-id").unwrap(); | |
| 78 | let devices: Vec<DeviceIdBox> = devices | |
| 79 | .vals | |
| 80 | .iter() | |
| 81 | .filter_map(|d| { | |
| 82 | d.clone().into_string().ok().map(|d| d.into()) | |
| 83 | }) | |
| 84 | .collect(); | |
| 85 | Self::delete(servers, buffer, devices); | |
| 86 | } | |
| 87 | _ => Weechat::print(&format!( | |
| 88 | "{}Subcommand isn't implemented", | |
| 89 | Weechat::prefix("error") | |
| 90 | )), | |
| 91 | } | |
| 92 | } | |
| 93 | ||
| 94 | pub fn subcommands() -> Vec<Argparse<'static, 'static>> { | |
| 95 | vec![ | |
| 96 | SubCommand::with_name("list") | |
| 97 | .about("List your own Matrix devices on the server."), | |
| 98 | SubCommand::with_name("delete") | |
| 99 | .about("Delete the given device") | |
| 100 | .arg( | |
| 101 | Arg::with_name("device-id") | |
| 102 | .require_delimiter(true) | |
| 103 | .multiple(true) | |
| 104 | .required(true), | |
| 105 | ), | |
| 106 | SubCommand::with_name("set-name") | |
| 107 | .about("Set the human readable name of the given device") | |
| 108 | .arg(Arg::with_name("device-id").required(true)) | |
| 109 | .arg(Arg::with_name("name").required(true)), | |
| 110 | ] | |
| 111 | } | |
| 66 | 112 | } |
| 67 | 113 | |
| 68 | 114 | impl CommandCallback for DevicesCommand { |
| 69 | 115 | fn callback(&mut self, _: &Weechat, buffer: &Buffer, arguments: Args) { |
| 70 | 116 | let argparse = Argparse::new("devices") |
| 117 | .about(Self::DESCRIPTION) | |
| 71 | 118 | .global_setting(ArgParseSettings::DisableHelpFlags) |
| 72 | 119 | .global_setting(ArgParseSettings::DisableVersion) |
| 73 | 120 | .global_setting(ArgParseSettings::VersionlessSubcommands) |
| 74 | 121 | .setting(ArgParseSettings::SubcommandRequiredElseHelp) |
| 75 | .subcommand( | |
| 76 | SubCommand::with_name("list") | |
| 77 | .about("List your own Matrix devices on the server."), | |
| 78 | ) | |
| 79 | .subcommand( | |
| 80 | SubCommand::with_name("delete") | |
| 81 | .about("Delete the given device") | |
| 82 | .arg( | |
| 83 | Arg::with_name("device-id") | |
| 84 | .require_delimiter(true) | |
| 85 | .multiple(true) | |
| 86 | .required(true), | |
| 87 | ), | |
| 88 | ) | |
| 89 | .subcommand( | |
| 90 | SubCommand::with_name("set-name") | |
| 91 | .about("Set the human readable name of the given device") | |
| 92 | .arg(Arg::with_name("device-id").required(true)) | |
| 93 | .arg(Arg::with_name("name").required(true)), | |
| 94 | ); | |
| 122 | .subcommands(Self::subcommands()); | |
| 95 | 123 | |
| 96 | 124 | let matches = match argparse.get_matches_from_safe(arguments) { |
| 97 | 125 | Ok(m) => m, |
| @@ -108,23 +136,6 @@ impl CommandCallback for DevicesCommand { | ||
| 108 | 136 | } |
| 109 | 137 | }; |
| 110 | 138 | |
| 111 | match matches.subcommand() { | |
| 112 | ("list", _) => self.list(buffer), | |
| 113 | ("delete", args) => { | |
| 114 | let devices = args.unwrap().args.get("device-id").unwrap(); | |
| 115 | let devices: Vec<DeviceIdBox> = devices | |
| 116 | .vals | |
| 117 | .iter() | |
| 118 | .filter_map(|d| { | |
| 119 | d.clone().into_string().ok().map(|d| d.into()) | |
| 120 | }) | |
| 121 | .collect(); | |
| 122 | self.delete(buffer, devices); | |
| 123 | } | |
| 124 | _ => Weechat::print(&format!( | |
| 125 | "{}Subcommand isn't implemented", | |
| 126 | Weechat::prefix("error") | |
| 127 | )), | |
| 128 | } | |
| 139 | Self::run(buffer, &self.servers, &matches) | |
| 129 | 140 | } |
| 130 | 141 | } |
src/commands/matrix.rs +19 −4
| @@ -10,7 +10,10 @@ use weechat::{ | ||
| 10 | 10 | Args, Weechat, |
| 11 | 11 | }; |
| 12 | 12 | |
| 13 | use crate::{config::ConfigHandle, MatrixServer, Servers, PLUGIN_NAME}; | |
| 13 | use crate::{ | |
| 14 | commands::DevicesCommand, config::ConfigHandle, MatrixServer, Servers, | |
| 15 | PLUGIN_NAME, | |
| 16 | }; | |
| 14 | 17 | |
| 15 | 18 | pub struct MatrixCommand { |
| 16 | 19 | servers: Servers, |
| @@ -27,18 +30,22 @@ impl MatrixCommand { | ||
| 27 | 30 | .add_argument("server add <server-name> <hostname>[:<port>]") |
| 28 | 31 | .add_argument("server delete|list|listfull <server-name>") |
| 29 | 32 | .add_argument("connect <server-name>") |
| 33 | .add_argument("devices delete|list|set-name") | |
| 30 | 34 | .add_argument("disconnect <server-name>") |
| 31 | 35 | .add_argument("reconnect <server-name>") |
| 32 | 36 | .add_argument("help <matrix-command> [<matrix-subcommand>]") |
| 33 | .arguments_description( | |
| 37 | .arguments_description(&format!( | |
| 34 | 38 | " server: List, add, or remove Matrix servers. |
| 35 | 39 | connect: Connect to Matrix servers. |
| 36 | 40 | disconnect: Disconnect from one or all Matrix servers. |
| 37 | 41 | reconnect: Reconnect to server(s). |
| 42 | devices: {} | |
| 38 | 43 | help: Show detailed command help.\n |
| 39 | 44 | Use /matrix [command] help to find out more.\n", |
| 40 | ) | |
| 45 | DevicesCommand::DESCRIPTION | |
| 46 | )) | |
| 41 | 47 | .add_completion("server |add|delete|list|listfull") |
| 48 | .add_completion("devices |list|delete|set-name") | |
| 42 | 49 | .add_completion("connect") |
| 43 | 50 | .add_completion("disconnect") |
| 44 | 51 | .add_completion("reconnect") |
| @@ -210,7 +217,7 @@ impl CommandCallback for MatrixCommand { | ||
| 210 | 217 | fn callback( |
| 211 | 218 | &mut self, |
| 212 | 219 | _weechat: &Weechat, |
| 213 | _buffer: &Buffer, | |
| 220 | buffer: &Buffer, | |
| 214 | 221 | arguments: Args, |
| 215 | 222 | ) { |
| 216 | 223 | let server_command = SubCommand::with_name("server") |
| @@ -254,6 +261,11 @@ impl CommandCallback for MatrixCommand { | ||
| 254 | 261 | .global_setting(ArgParseSettings::VersionlessSubcommands) |
| 255 | 262 | .setting(ArgParseSettings::SubcommandRequiredElseHelp) |
| 256 | 263 | .subcommand(server_command) |
| 264 | .subcommand( | |
| 265 | SubCommand::with_name("devices") | |
| 266 | .about(DevicesCommand::DESCRIPTION) | |
| 267 | .subcommands(DevicesCommand::subcommands()), | |
| 268 | ) | |
| 257 | 269 | .subcommand( |
| 258 | 270 | SubCommand::with_name("connect") |
| 259 | 271 | .about("Connect to Matrix servers.") |
| @@ -293,6 +305,9 @@ impl CommandCallback for MatrixCommand { | ||
| 293 | 305 | ("connect", Some(subargs)) => self.connect_command(subargs), |
| 294 | 306 | ("disconnect", Some(subargs)) => self.disconnect_command(subargs), |
| 295 | 307 | ("server", Some(subargs)) => self.server_command(subargs), |
| 308 | ("devices", Some(subargs)) => { | |
| 309 | DevicesCommand::run(buffer, &self.servers, subargs) | |
| 310 | } | |
| 296 | 311 | _ => unreachable!(), |
| 297 | 312 | } |
| 298 | 313 | } |