Commit d5c3f8fba3
d5c3f8fba3d90720674026809826504e7680192d
parent: af3550d08d
Unsigned
Damir Jelić <poljar@termina.org.uk> · 2021-08-15 07:49 UTC
feat(completions): Add a completion for E2EE enabled users
Layout: unified · split
src/commands/devices.rs
+1 −1
| @@ -33,7 +33,7 @@ impl DevicesCommand { |
| 33 | "device-id: The unique id of the device that should be deleted. |
33 | "device-id: The unique id of the device that should be deleted. |
| 34 | name: The name that the device name should be set to.", |
34 | name: The name that the device name should be set to.", |
| 35 | ) |
35 | ) |
| 36 | .add_completion("list") |
36 | .add_completion("list %(matrix-users)") |
| 37 | .add_completion("delete %(matrix-own-devices)") |
37 | .add_completion("delete %(matrix-own-devices)") |
| 38 | .add_completion("set-name %(matrix-own-devices)") |
38 | .add_completion("set-name %(matrix-own-devices)") |
| 39 | .add_completion("help list|delete|set-name"); |
39 | .add_completion("help list|delete|set-name"); |
src/commands/matrix.rs
+2 −2
| @@ -49,8 +49,8 @@ Use /matrix [command] help to find out more.\n", |
| 49 | DevicesCommand::DESCRIPTION, |
49 | DevicesCommand::DESCRIPTION, |
| 50 | KeysCommand::DESCRIPTION, |
50 | KeysCommand::DESCRIPTION, |
| 51 | )) |
51 | )) |
| 52 | .add_completion("server |add|delete|list|listfull") |
52 | .add_completion("server add|delete|list|listfull") |
| 53 | .add_completion("devices |list|delete|set-name") |
53 | .add_completion("devices list|delete|set-name %(matrix-users)") |
| 54 | .add_completion(&format!("keys {}", KeysCommand::COMPLETION)) |
54 | .add_completion(&format!("keys {}", KeysCommand::COMPLETION)) |
| 55 | .add_completion("connect %(matrix_servers)") |
55 | .add_completion("connect %(matrix_servers)") |
| 56 | .add_completion("disconnect %(matrix_servers)") |
56 | .add_completion("disconnect %(matrix_servers)") |
src/completions.rs
+48 −2
| @@ -1,5 +1,6 @@ |
| 1 | use std::borrow::Cow; |
1 | use std::borrow::Cow; |
| 2 | |
2 | |
| |
3 | use futures::executor::block_on; |
| 3 | use weechat::{ |
4 | use weechat::{ |
| 4 | buffer::Buffer, |
5 | buffer::Buffer, |
| 5 | hooks::{ |
6 | hooks::{ |
| @@ -10,15 +11,17 @@ use weechat::{ |
| 10 | |
11 | |
| 11 | use crate::Servers; |
12 | use crate::Servers; |
| 12 | |
13 | |
| |
14 | #[allow(dead_code)] |
| 13 | pub struct Completions { |
15 | pub struct Completions { |
| 14 | #[allow(dead_code)] |
| |
| 15 | servers: CompletionHook, |
16 | servers: CompletionHook, |
| |
17 | users: CompletionHook, |
| 16 | } |
18 | } |
| 17 | |
19 | |
| 18 | impl Completions { |
20 | impl Completions { |
| 19 | pub fn hook_all(servers: Servers) -> Result<Self, ()> { |
21 | pub fn hook_all(servers: Servers) -> Result<Self, ()> { |
| 20 | Ok(Self { |
22 | Ok(Self { |
| 21 | servers: ServersCompletion::create(servers)?, |
23 | servers: ServersCompletion::create(servers.clone())?, |
| |
24 | users: UsersCompletion::create(servers)?, |
| 22 | }) |
25 | }) |
| 23 | } |
26 | } |
| 24 | } |
27 | } |
| @@ -57,3 +60,46 @@ impl CompletionCallback for ServersCompletion { |
| 57 | Ok(()) |
60 | Ok(()) |
| 58 | } |
61 | } |
| 59 | } |
62 | } |
| |
63 | |
| |
64 | struct UsersCompletion { |
| |
65 | servers: Servers, |
| |
66 | } |
| |
67 | |
| |
68 | impl UsersCompletion { |
| |
69 | fn create(servers: Servers) -> Result<CompletionHook, ()> { |
| |
70 | let comp = UsersCompletion { servers }; |
| |
71 | |
| |
72 | CompletionHook::new( |
| |
73 | "matrix-users", |
| |
74 | "Completion for the list of Matrix users", |
| |
75 | comp, |
| |
76 | ) |
| |
77 | } |
| |
78 | } |
| |
79 | |
| |
80 | impl CompletionCallback for UsersCompletion { |
| |
81 | fn callback( |
| |
82 | &mut self, |
| |
83 | _: &Weechat, |
| |
84 | buffer: &Buffer, |
| |
85 | _: Cow<str>, |
| |
86 | completion: &Completion, |
| |
87 | ) -> Result<(), ()> { |
| |
88 | if let Some(server) = self.servers.find_server(buffer) { |
| |
89 | if let Some(connection) = server.connection() { |
| |
90 | let tracked_users = |
| |
91 | block_on(connection.client().tracked_users()); |
| |
92 | |
| |
93 | for user in tracked_users.into_iter() { |
| |
94 | completion.add_with_options( |
| |
95 | user.as_str(), |
| |
96 | true, |
| |
97 | CompletionPosition::Sorted, |
| |
98 | ) |
| |
99 | } |
| |
100 | } |
| |
101 | } |
| |
102 | |
| |
103 | Ok(()) |
| |
104 | } |
| |
105 | } |