Commit a5c25f2487

a5c25f248741927b352463df7d2e9d08354edaa3

parent: 50023bad40

Unregistered key

Ulyssa <git@ulyssa.dev> · 2023-04-28 23:52 UTC

Support leaving rooms (#45)

Layout: unified · split

Cargo.lock +62 −17
@@ -625,6 +625,22 @@ dependencies = [
625625 "winapi",
626626]
627627
628[[package]]
629name = "crossterm"
630version = "0.26.1"
631source = "registry+https://github.com/rust-lang/crates.io-index"
632checksum = "a84cda67535339806297f1b331d6dd6320470d2a0fe65381e79ee9e156dd3d13"
633dependencies = [
634 "bitflags 1.3.2",
635 "crossterm_winapi",
636 "libc",
637 "mio",
638 "parking_lot 0.12.1",
639 "signal-hook",
640 "signal-hook-mio",
641 "winapi",
642]
643
628644[[package]]
629645name = "crossterm_winapi"
630646version = "0.9.0"
@@ -2006,24 +2022,26 @@ dependencies = [
20062022
20072023[[package]]
20082024name = "modalkit"
2009version = "0.0.14"
2025version = "0.0.15"
20102026source = "registry+https://github.com/rust-lang/crates.io-index"
2011checksum = "5c48c7d7e6d764a09435b43a7e4d342ba2d2e026626ca773b16a5ba34b90b933"
2027checksum = "b44af1b5a7737da948719b907c870b4c852f1d98300d873bd12568f4028d908a"
20122028dependencies = [
20132029 "anymap2",
20142030 "arboard",
20152031 "bitflags 1.3.2",
2016 "crossterm",
2032 "crossterm 0.25.0",
20172033 "derive_more",
20182034 "intervaltree",
20192035 "libc",
20202036 "nom",
20212037 "radix_trie",
2038 "ratatui",
20222039 "regex",
20232040 "ropey",
2041 "textwrap",
20242042 "thiserror",
2025 "tui",
20262043 "unicode-segmentation",
2044 "unicode-width",
20272045]
20282046
20292047[[package]]
@@ -2699,6 +2717,19 @@ dependencies = [
26992717 "rand_core 0.3.1",
27002718]
27012719
2720[[package]]
2721name = "ratatui"
2722version = "0.20.1"
2723source = "registry+https://github.com/rust-lang/crates.io-index"
2724checksum = "dcc0d032bccba900ee32151ec0265667535c230169f5a011154cdcd984e16829"
2725dependencies = [
2726 "bitflags 1.3.2",
2727 "cassowary",
2728 "crossterm 0.26.1",
2729 "unicode-segmentation",
2730 "unicode-width",
2731]
2732
27022733[[package]]
27032734name = "rdrand"
27042735version = "0.4.0"
@@ -3209,6 +3240,12 @@ version = "1.10.0"
32093240source = "registry+https://github.com/rust-lang/crates.io-index"
32103241checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0"
32113242
3243[[package]]
3244name = "smawk"
3245version = "0.3.1"
3246source = "registry+https://github.com/rust-lang/crates.io-index"
3247checksum = "f67ad224767faa3c7d8b6d91985b78e70a1324408abcb1cfcc2be4c06bc06043"
3248
32123249[[package]]
32133250name = "socket2"
32143251version = "0.4.9"
@@ -3383,6 +3420,17 @@ dependencies = [
33833420 "windows-sys 0.48.0",
33843421]
33853422
3423[[package]]
3424name = "textwrap"
3425version = "0.16.0"
3426source = "registry+https://github.com/rust-lang/crates.io-index"
3427checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d"
3428dependencies = [
3429 "smawk",
3430 "unicode-linebreak",
3431 "unicode-width",
3432]
3433
33863434[[package]]
33873435name = "thiserror"
33883436version = "1.0.40"
@@ -3638,19 +3686,6 @@ version = "0.2.4"
36383686source = "registry+https://github.com/rust-lang/crates.io-index"
36393687checksum = "3528ecfd12c466c6f163363caf2d02a71161dd5e1cc6ae7b34207ea2d42d81ed"
36403688
3641[[package]]
3642name = "tui"
3643version = "0.19.0"
3644source = "registry+https://github.com/rust-lang/crates.io-index"
3645checksum = "ccdd26cbd674007e649a272da4475fb666d3aa0ad0531da7136db6fab0e5bad1"
3646dependencies = [
3647 "bitflags 1.3.2",
3648 "cassowary",
3649 "crossterm",
3650 "unicode-segmentation",
3651 "unicode-width",
3652]
3653
36543689[[package]]
36553690name = "typed-arena"
36563691version = "2.0.2"
@@ -3684,6 +3719,16 @@ version = "1.0.8"
36843719source = "registry+https://github.com/rust-lang/crates.io-index"
36853720checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4"
36863721
3722[[package]]
3723name = "unicode-linebreak"
3724version = "0.1.4"
3725source = "registry+https://github.com/rust-lang/crates.io-index"
3726checksum = "c5faade31a542b8b35855fff6e8def199853b2da8da256da52f52f1316ee3137"
3727dependencies = [
3728 "hashbrown",
3729 "regex",
3730]
3731
36873732[[package]]
36883733name = "unicode-normalization"
36893734version = "0.1.22"
Cargo.toml +1 −1
@@ -40,7 +40,7 @@ unicode-width = "0.1.10"
4040url = {version = "^2.2.2", features = ["serde"]}
4141
4242[dependencies.modalkit]
43version = "0.0.14"
43version = "0.0.15"
4444
4545[dependencies.matrix-sdk]
4646version = "0.6"
src/base.rs +4 −1
@@ -118,7 +118,9 @@ pub enum MessageAction {
118118 React(String),
119119
120120 /// Redact a message, with an optional reason.
121 Redact(Option<String>),
121 ///
122 /// The [bool] argument indicates whether to skip confirmation.
123 Redact(Option<String>, bool),
122124
123125 /// Reply to a message.
124126 Reply,
@@ -178,6 +180,7 @@ pub enum RoomAction {
178180 InviteAccept,
179181 InviteReject,
180182 InviteSend(OwnedUserId),
183 Leave(bool),
181184 Members(Box<CommandContext<ProgramContext>>),
182185 Set(RoomField, String),
183186 Unset(RoomField),
src/commands.rs +25 −4
@@ -161,6 +161,17 @@ fn iamb_members(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
161161 return Ok(step);
162162}
163163
164fn iamb_leave(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
165 if !desc.arg.text.is_empty() {
166 return Result::Err(CommandError::InvalidArgument);
167 }
168
169 let leave = IambAction::Room(RoomAction::Leave(desc.bang));
170 let step = CommandStep::Continue(leave.into(), ctx.context.take());
171
172 return Ok(step);
173}
174
164175fn iamb_cancel(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
165176 if !desc.arg.text.is_empty() {
166177 return Result::Err(CommandError::InvalidArgument);
@@ -237,7 +248,8 @@ fn iamb_redact(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
237248 return Result::Err(CommandError::InvalidArgument);
238249 }
239250
240 let ract = IambAction::from(MessageAction::Redact(args.into_iter().next()));
251 let reason = args.into_iter().next();
252 let ract = IambAction::from(MessageAction::Redact(reason, desc.bang));
241253 let step = CommandStep::Continue(ract.into(), ctx.context.take());
242254
243255 return Ok(step);
@@ -469,6 +481,11 @@ fn add_iamb_commands(cmds: &mut ProgramCommands) {
469481 f: iamb_invite,
470482 });
471483 cmds.add_command(ProgramCommand { name: "join".into(), aliases: vec![], f: iamb_join });
484 cmds.add_command(ProgramCommand {
485 name: "leave".into(),
486 aliases: vec![],
487 f: iamb_leave,
488 });
472489 cmds.add_command(ProgramCommand {
473490 name: "members".into(),
474491 aliases: vec![],
@@ -870,15 +887,19 @@ mod tests {
870887 let ctx = ProgramContext::default();
871888
872889 let res = cmds.input_cmd("redact", ctx.clone()).unwrap();
873 let act = IambAction::Message(MessageAction::Redact(None));
890 let act = IambAction::Message(MessageAction::Redact(None, false));
891 assert_eq!(res, vec![(act.into(), ctx.clone())]);
892
893 let res = cmds.input_cmd("redact!", ctx.clone()).unwrap();
894 let act = IambAction::Message(MessageAction::Redact(None, true));
874895 assert_eq!(res, vec![(act.into(), ctx.clone())]);
875896
876897 let res = cmds.input_cmd("redact Removed", ctx.clone()).unwrap();
877 let act = IambAction::Message(MessageAction::Redact(Some("Removed".into())));
898 let act = IambAction::Message(MessageAction::Redact(Some("Removed".into()), false));
878899 assert_eq!(res, vec![(act.into(), ctx.clone())]);
879900
880901 let res = cmds.input_cmd("redact \"Removed\"", ctx.clone()).unwrap();
881 let act = IambAction::Message(MessageAction::Redact(Some("Removed".into())));
902 let act = IambAction::Message(MessageAction::Redact(Some("Removed".into()), false));
882903 assert_eq!(res, vec![(act.into(), ctx.clone())]);
883904
884905 let res = cmds.input_cmd("redact Removed Removed", ctx.clone());
src/main.rs +44 −7
@@ -76,12 +76,14 @@ use modalkit::{
7676 EditInfo,
7777 Editable,
7878 EditorAction,
79 InfoMessage,
7980 InsertTextAction,
8081 Jumpable,
8182 Promptable,
8283 Scrollable,
8384 TabContainer,
8485 TabCount,
86 UIError,
8587 WindowAction,
8688 WindowContainer,
8789 },
@@ -90,7 +92,7 @@ use modalkit::{
9092 key::KeyManager,
9193 store::Store,
9294 },
93 input::{bindings::BindingMachine, key::TerminalKey},
95 input::{bindings::BindingMachine, dialog::Pager, key::TerminalKey},
9496 widgets::{
9597 cmdbar::CommandBarState,
9698 screen::{Screen, ScreenState},
@@ -156,8 +158,7 @@ impl Application {
156158 }
157159
158160 fn redraw(&mut self, full: bool, store: &mut ProgramStore) -> Result<(), std::io::Error> {
159 let modestr = self.bindings.showmode();
160 let cursor = self.bindings.get_cursor_indicator();
161 let bindings = &mut self.bindings;
161162 let sstate = &mut self.screen;
162163 let term = &mut self.terminal;
163164
@@ -168,9 +169,20 @@ impl Application {
168169 term.draw(|f| {
169170 let area = f.size();
170171
171 let screen = Screen::new(store).showmode(modestr).borders(true);
172 let modestr = bindings.show_mode();
173 let cursor = bindings.get_cursor_indicator();
174 let dialogstr = bindings.show_dialog(area.height as usize, area.width as usize);
175
176 // Don't show terminal cursor when we show a dialog.
177 let hide_cursor = !dialogstr.is_empty();
178
179 let screen = Screen::new(store).show_dialog(dialogstr).show_mode(modestr).borders(true);
172180 f.render_stateful_widget(screen, area, sstate);
173181
182 if hide_cursor {
183 return;
184 }
185
174186 if let Some((cx, cy)) = sstate.get_term_cursor() {
175187 if let Some(c) = cursor {
176188 let style = Style::default().fg(Color::Green);
@@ -215,7 +227,8 @@ impl Application {
215227 match self.screen.editor_command(&act, &ctx, store.deref_mut()) {
216228 Ok(None) => {},
217229 Ok(Some(info)) => {
218 self.screen.push_info(info);
230 drop(store);
231 self.handle_info(info);
219232 },
220233 Err(e) => {
221234 self.screen.push_error(e);
@@ -279,7 +292,7 @@ impl Application {
279292 Action::CommandBar(act) => self.screen.command_bar(&act, &ctx)?,
280293 Action::Macro(act) => self.bindings.macro_command(&act, &ctx, store)?,
281294 Action::Scroll(style) => self.screen.scroll(&style, &ctx, store)?,
282 Action::Suspend => self.terminal.program_suspend()?,
295 Action::ShowInfoMessage(info) => Some(info),
283296 Action::Tab(cmd) => self.screen.tab_command(&cmd, &ctx, store)?,
284297 Action::Window(cmd) => self.screen.window_command(&cmd, &ctx, store)?,
285298
@@ -289,6 +302,11 @@ impl Application {
289302
290303 None
291304 },
305 Action::Suspend => {
306 self.terminal.program_suspend()?;
307
308 None
309 },
292310
293311 // UI actions.
294312 Action::RedrawScreen => {
@@ -402,6 +420,18 @@ impl Application {
402420 }
403421 }
404422
423 fn handle_info(&mut self, info: InfoMessage) {
424 match info {
425 InfoMessage::Message(info) => {
426 self.screen.push_info(info);
427 },
428 InfoMessage::Pager(text) => {
429 let pager = Box::new(Pager::new(text, vec![]));
430 self.bindings.run_dialog(pager);
431 },
432 }
433 }
434
405435 pub async fn run(&mut self) -> Result<(), std::io::Error> {
406436 self.terminal.clear()?;
407437
@@ -422,11 +452,18 @@ impl Application {
422452 continue;
423453 },
424454 Ok(Some(info)) => {
425 self.screen.push_info(info);
455 self.handle_info(info);
426456
427457 // Continue processing; we'll redraw later.
428458 continue;
429459 },
460 Err(
461 UIError::NeedConfirm(dialog) |
462 UIError::EditingFailure(EditError::NeedConfirm(dialog)),
463 ) => {
464 self.bindings.run_dialog(dialog);
465 continue;
466 },
430467 Err(e) => {
431468 self.screen.push_error(e);
432469
src/windows/mod.rs +3 −3
@@ -199,7 +199,7 @@ fn room_prompt(
199199
200200 Err(err)
201201 },
202 PromptAction::Recall(_, _) => {
202 PromptAction::Recall(..) => {
203203 let msg = "Cannot recall history inside a list";
204204 let err = EditError::Failure(msg.into());
205205
@@ -1043,7 +1043,7 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for VerifyItem {
10431043
10441044 Err(err)
10451045 },
1046 PromptAction::Recall(_, _) => {
1046 PromptAction::Recall(..) => {
10471047 let msg = "Cannot recall history inside a list";
10481048 let err = EditError::Failure(msg.into());
10491049
@@ -1120,7 +1120,7 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for MemberItem {
11201120
11211121 Err(err)
11221122 },
1123 PromptAction::Recall(_, _) => {
1123 PromptAction::Recall(..) => {
11241124 let msg = "Cannot recall history inside a list";
11251125 let err = EditError::Failure(msg.into());
11261126
src/windows/room/chat.rs +17 −3
@@ -27,6 +27,7 @@ use matrix_sdk::{
2727};
2828
2929use modalkit::{
30 input::dialog::PromptYesNo,
3031 tui::{
3132 buffer::Buffer,
3233 layout::Rect,
@@ -40,6 +41,7 @@ use modalkit::{
4041
4142use modalkit::editing::{
4243 action::{
44 Action,
4345 EditError,
4446 EditInfo,
4547 EditResult,
@@ -310,7 +312,16 @@ impl ChatState {
310312
311313 Ok(None)
312314 },
313 MessageAction::Redact(reason) => {
315 MessageAction::Redact(reason, skip_confirm) => {
316 if !skip_confirm {
317 let msg = "Are you sure you want to redact this message?";
318 let act = IambAction::Message(MessageAction::Redact(reason, true));
319 let prompt = PromptYesNo::new(msg, vec![Action::from(act)]);
320 let prompt = Box::new(prompt);
321
322 return Err(UIError::NeedConfirm(prompt));
323 }
324
314325 let room = self.get_joined(&store.application.worker)?;
315326 let event_id = match &msg.event {
316327 MessageEvent::EncryptedOriginal(ev) => ev.event_id.clone(),
@@ -672,13 +683,14 @@ impl PromptActions<ProgramContext, ProgramStore, IambInfo> for ChatState {
672683 &mut self,
673684 dir: &MoveDir1D,
674685 count: &Count,
686 prefixed: bool,
675687 ctx: &ProgramContext,
676688 _: &mut ProgramStore,
677689 ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
678690 let count = ctx.resolve(count);
679691 let rope = self.tbox.get();
680692
681 let text = self.sent.recall(&rope, &mut self.sent_scrollback, *dir, count);
693 let text = self.sent.recall(&rope, &mut self.sent_scrollback, *dir, prefixed, count);
682694
683695 if let Some(text) = text {
684696 self.tbox.set_text(text);
@@ -702,7 +714,9 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for ChatState {
702714 match act {
703715 PromptAction::Submit => self.submit(ctx, store),
704716 PromptAction::Abort(empty) => self.abort(*empty, ctx, store),
705 PromptAction::Recall(dir, count) => self.recall(dir, count, ctx, store),
717 PromptAction::Recall(dir, count, prefixed) => {
718 self.recall(dir, count, *prefixed, ctx, store)
719 },
706720 _ => Err(EditError::Unimplemented("unknown prompt action".to_string())),
707721 }
708722 }
src/windows/room/mod.rs +20
@@ -43,11 +43,13 @@ use modalkit::{
4343 WriteFlags,
4444 },
4545 editing::completion::CompletionList,
46 input::dialog::PromptYesNo,
4647 input::InputContext,
4748 widgets::{TermOffset, TerminalCursor, WindowOps},
4849};
4950
5051use crate::base::{
52 IambAction,
5153 IambError,
5254 IambId,
5355 IambInfo,
@@ -218,6 +220,24 @@ impl RoomState {
218220 Err(IambError::NotJoined.into())
219221 }
220222 },
223 RoomAction::Leave(skip_confirm) => {
224 if let Some(room) = store.application.worker.client.get_joined_room(self.id()) {
225 if skip_confirm {
226 room.leave().await.map_err(IambError::from)?;
227
228 Ok(vec![])
229 } else {
230 let msg = "Do you really want to leave this room?";
231 let leave = IambAction::Room(RoomAction::Leave(true));
232 let prompt = PromptYesNo::new(msg, vec![Action::from(leave)]);
233 let prompt = Box::new(prompt);
234
235 Err(UIError::NeedConfirm(prompt))
236 }
237 } else {
238 Err(IambError::NotJoined.into())
239 }
240 },
221241 RoomAction::Members(mut cmd) => {
222242 let width = Count::Exact(30);
223243 let act =
src/worker.rs +1 −1
@@ -1215,7 +1215,7 @@ impl ClientWorker {
12151215 let _req = request.await.map_err(IambError::from)?;
12161216 let info = format!("Sent verification request to {user_id}");
12171217
1218 Ok(InfoMessage::from(info).into())
1218 Ok(Some(InfoMessage::from(info)))
12191219 },
12201220 None => {
12211221 let msg = format!("Could not find identity information for {user_id}");