Commit d41b55b470

d41b55b47090bb2b22676ffb505951ff87651288

parent: c8cdbac48b

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2020-10-17 09:23 UTC

commands: Allow the devices command to be run as a /matrix subcommand.

Layout: unified · split

src/commands/devices.rs +55 −44
@@ -1,4 +1,7 @@
1use clap::{App as Argparse, AppSettings as ArgParseSettings, Arg, SubCommand};
1use clap::{
2 App as Argparse, AppSettings as ArgParseSettings, Arg, ArgMatches,
3 SubCommand,
4};
25use matrix_sdk::identifiers::DeviceIdBox;
36
47use weechat::{
@@ -14,9 +17,12 @@ pub struct DevicesCommand {
1417}
1518
1619impl DevicesCommand {
20 pub const DESCRIPTION: &'static str =
21 "List, delete or rename Matrix devices";
22
1723 pub fn create(servers: &Servers) -> Result<Command, ()> {
1824 let settings = CommandSettings::new("devices")
19 .description("List, delete or rename Matrix devices.")
25 .description(Self::DESCRIPTION)
2026 .add_argument("list")
2127 .add_argument("delete <device-id>")
2228 .add_argument("set-name <device-id> <name>")
@@ -38,8 +44,8 @@ impl DevicesCommand {
3844 )
3945 }
4046
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);
4349
4450 if let Some(s) = server {
4551 let devices = || async move {
@@ -51,8 +57,8 @@ impl DevicesCommand {
5157 }
5258 }
5359
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);
5662
5763 if let Some(s) = server {
5864 let devices = || async move {
@@ -63,35 +69,57 @@ impl DevicesCommand {
6369 Weechat::print("Must be executed on Matrix buffer")
6470 }
6571 }
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 }
66112}
67113
68114impl CommandCallback for DevicesCommand {
69115 fn callback(&mut self, _: &Weechat, buffer: &Buffer, arguments: Args) {
70116 let argparse = Argparse::new("devices")
117 .about(Self::DESCRIPTION)
71118 .global_setting(ArgParseSettings::DisableHelpFlags)
72119 .global_setting(ArgParseSettings::DisableVersion)
73120 .global_setting(ArgParseSettings::VersionlessSubcommands)
74121 .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());
95123
96124 let matches = match argparse.get_matches_from_safe(arguments) {
97125 Ok(m) => m,
@@ -108,23 +136,6 @@ impl CommandCallback for DevicesCommand {
108136 }
109137 };
110138
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)
129140 }
130141}
src/commands/matrix.rs +19 −4
@@ -10,7 +10,10 @@ use weechat::{
1010 Args, Weechat,
1111};
1212
13use crate::{config::ConfigHandle, MatrixServer, Servers, PLUGIN_NAME};
13use crate::{
14 commands::DevicesCommand, config::ConfigHandle, MatrixServer, Servers,
15 PLUGIN_NAME,
16};
1417
1518pub struct MatrixCommand {
1619 servers: Servers,
@@ -27,18 +30,22 @@ impl MatrixCommand {
2730 .add_argument("server add <server-name> <hostname>[:<port>]")
2831 .add_argument("server delete|list|listfull <server-name>")
2932 .add_argument("connect <server-name>")
33 .add_argument("devices delete|list|set-name")
3034 .add_argument("disconnect <server-name>")
3135 .add_argument("reconnect <server-name>")
3236 .add_argument("help <matrix-command> [<matrix-subcommand>]")
33 .arguments_description(
37 .arguments_description(&format!(
3438 " server: List, add, or remove Matrix servers.
3539 connect: Connect to Matrix servers.
3640 disconnect: Disconnect from one or all Matrix servers.
3741 reconnect: Reconnect to server(s).
42 devices: {}
3843 help: Show detailed command help.\n
3944Use /matrix [command] help to find out more.\n",
40 )
45 DevicesCommand::DESCRIPTION
46 ))
4147 .add_completion("server |add|delete|list|listfull")
48 .add_completion("devices |list|delete|set-name")
4249 .add_completion("connect")
4350 .add_completion("disconnect")
4451 .add_completion("reconnect")
@@ -210,7 +217,7 @@ impl CommandCallback for MatrixCommand {
210217 fn callback(
211218 &mut self,
212219 _weechat: &Weechat,
213 _buffer: &Buffer,
220 buffer: &Buffer,
214221 arguments: Args,
215222 ) {
216223 let server_command = SubCommand::with_name("server")
@@ -254,6 +261,11 @@ impl CommandCallback for MatrixCommand {
254261 .global_setting(ArgParseSettings::VersionlessSubcommands)
255262 .setting(ArgParseSettings::SubcommandRequiredElseHelp)
256263 .subcommand(server_command)
264 .subcommand(
265 SubCommand::with_name("devices")
266 .about(DevicesCommand::DESCRIPTION)
267 .subcommands(DevicesCommand::subcommands()),
268 )
257269 .subcommand(
258270 SubCommand::with_name("connect")
259271 .about("Connect to Matrix servers.")
@@ -293,6 +305,9 @@ impl CommandCallback for MatrixCommand {
293305 ("connect", Some(subargs)) => self.connect_command(subargs),
294306 ("disconnect", Some(subargs)) => self.disconnect_command(subargs),
295307 ("server", Some(subargs)) => self.server_command(subargs),
308 ("devices", Some(subargs)) => {
309 DevicesCommand::run(buffer, &self.servers, subargs)
310 }
296311 _ => unreachable!(),
297312 }
298313 }