Commit b3a639db78
b3a639db788b66bcefaaaa75504f9298127d4a51
parent: 4073028bba
Unsigned
Damir Jelić <poljar@termina.org.uk> · 2020-12-05 16:22 UTC
weechat-matrix: Add server completions for the connect/disconnect commands.
Layout: unified · split
src/commands/matrix.rs
+6 −4
| @@ -52,10 +52,12 @@ Use /matrix [command] help to find out more.\n", |
| 52 | 52 | .add_completion("server |add|delete|list|listfull") |
| 53 | 53 | .add_completion("devices |list|delete|set-name") |
| 54 | 54 | .add_completion(&format!("keys {}", KeysCommand::COMPLETION)) |
| 55 | | .add_completion("connect") |
| 56 | | .add_completion("disconnect") |
| 57 | | .add_completion("reconnect") |
| 58 | | .add_completion("help server|connect|disconnect|reconnect"); |
| 55 | .add_completion("connect %(matrix_servers)") |
| 56 | .add_completion("disconnect %(matrix_servers)") |
| 57 | .add_completion("reconnect %(matrix_servers)") |
| 58 | .add_completion( |
| 59 | "help server|connect|disconnect|reconnect|keys|devices", |
| 60 | ); |
| 59 | 61 | |
| 60 | 62 | Command::new( |
| 61 | 63 | matrix_settings, |
src/completions.rs
added
+59
| @@ -0,0 +1,59 @@ |
| 1 | use std::borrow::Cow; |
| 2 | |
| 3 | use weechat::{ |
| 4 | buffer::Buffer, |
| 5 | hooks::{ |
| 6 | Completion, CompletionCallback, CompletionHook, CompletionPosition, |
| 7 | }, |
| 8 | Weechat, |
| 9 | }; |
| 10 | |
| 11 | use crate::Servers; |
| 12 | |
| 13 | pub struct Completions { |
| 14 | #[used] |
| 15 | servers: CompletionHook, |
| 16 | } |
| 17 | |
| 18 | impl Completions { |
| 19 | pub fn hook_all(servers: Servers) -> Result<Self, ()> { |
| 20 | Ok(Self { |
| 21 | servers: ServersCompletion::create(servers)?, |
| 22 | }) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | struct ServersCompletion { |
| 27 | servers: Servers, |
| 28 | } |
| 29 | |
| 30 | impl ServersCompletion { |
| 31 | fn create(servers: Servers) -> Result<CompletionHook, ()> { |
| 32 | let comp = ServersCompletion { servers }; |
| 33 | |
| 34 | CompletionHook::new( |
| 35 | "matrix_servers", |
| 36 | "Completion for the list of added Matrix servers", |
| 37 | comp, |
| 38 | ) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | impl CompletionCallback for ServersCompletion { |
| 43 | fn callback( |
| 44 | &mut self, |
| 45 | _weechat: &Weechat, |
| 46 | _buffer: &Buffer, |
| 47 | _completion_name: Cow<str>, |
| 48 | completion: &Completion, |
| 49 | ) -> Result<(), ()> { |
| 50 | for server_name in self.servers.borrow().keys() { |
| 51 | completion.add_with_options( |
| 52 | server_name, |
| 53 | false, |
| 54 | CompletionPosition::Sorted, |
| 55 | ); |
| 56 | } |
| 57 | Ok(()) |
| 58 | } |
| 59 | } |
src/lib.rs
+7 −3
| @@ -2,6 +2,7 @@ |
| 2 | 2 | |
| 3 | 3 | mod bar_items; |
| 4 | 4 | mod commands; |
| 5 | mod completions; |
| 5 | 6 | mod config; |
| 6 | 7 | mod connection; |
| 7 | 8 | mod debug; |
| @@ -22,8 +23,8 @@ use weechat::{ |
| 22 | 23 | }; |
| 23 | 24 | |
| 24 | 25 | use crate::{ |
| 25 | | bar_items::BarItems, commands::Commands, config::ConfigHandle, |
| 26 | | room::RoomHandle, server::MatrixServer, |
| 26 | bar_items::BarItems, commands::Commands, completions::Completions, |
| 27 | config::ConfigHandle, room::RoomHandle, server::MatrixServer, |
| 27 | 28 | }; |
| 28 | 29 | |
| 29 | 30 | const PLUGIN_NAME: &str = "matrix"; |
| @@ -137,6 +138,8 @@ struct Matrix { |
| 137 | 138 | bar_items: BarItems, |
| 138 | 139 | #[used] |
| 139 | 140 | typing_notice_signal: SignalHook, |
| 141 | #[used] |
| 142 | completions: Completions, |
| 140 | 143 | debug_buffer: RefCell<Option<BufferHandle>>, |
| 141 | 144 | } |
| 142 | 145 | |
| @@ -182,8 +185,8 @@ impl Plugin for Matrix { |
| 182 | 185 | let config = ConfigHandle::new(&servers); |
| 183 | 186 | let commands = Commands::hook_all(&servers, &config)?; |
| 184 | 187 | |
| 185 | | // TODO move the bar creation into a separate file. |
| 186 | 188 | let bar_items = BarItems::hook_all(servers.clone())?; |
| 189 | let completions = Completions::hook_all(servers.clone())?; |
| 187 | 190 | |
| 188 | 191 | tracing_subscriber::fmt() |
| 189 | 192 | .with_writer(debug::Debug) |
| @@ -212,6 +215,7 @@ impl Plugin for Matrix { |
| 212 | 215 | commands, |
| 213 | 216 | config, |
| 214 | 217 | bar_items, |
| 218 | completions, |
| 215 | 219 | debug_buffer: RefCell::new(None), |
| 216 | 220 | typing_notice_signal: typing, |
| 217 | 221 | }; |