From bd345315d201f07c821be19ada03dfd5eaedb289 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= <omeragacan@gmail.com>
Date: Fri, 5 Apr 2024 16:58:01 +0200
Subject: [PATCH] Some renaming in TUI for consistency, documentation

---
 crates/libtiny_common/src/lib.rs        | 25 ++++++------
 crates/libtiny_tui/examples/chat.rs     |  2 +-
 crates/libtiny_tui/examples/tabs.rs     |  2 +-
 crates/libtiny_tui/src/exit_dialogue.rs |  2 +-
 crates/libtiny_tui/src/lib.rs           | 16 ++++----
 crates/libtiny_tui/src/tui.rs           | 52 ++++++++++++-------------
 crates/libtiny_tui/src/widget.rs        |  6 +--
 crates/tiny/src/ui.rs                   |  2 +-
 8 files changed, 53 insertions(+), 54 deletions(-)

diff --git a/crates/libtiny_common/src/lib.rs b/crates/libtiny_common/src/lib.rs
index ac9d2f08..616a5e43 100644
--- a/crates/libtiny_common/src/lib.rs
+++ b/crates/libtiny_common/src/lib.rs
@@ -236,22 +236,23 @@ pub enum TabStyle {
     Highlight,
 }
 
-/// UI events
+/// UI events.
 #[derive(Debug)]
 pub enum Event {
-    Abort {
-        msg: Option<String>,
-    },
-    Msg {
-        msg: String,
-        source: MsgSource,
-    },
+    /// User wants to quit.
+    Quit { msg: Option<String> },
+
+    /// A message was sent.
+    Msg { msg: String, source: MsgSource },
+
+    /// A multi-line message was sent.
+    ///
+    /// This can be done by pasting, or via the editor command (`C-x` by default).
     Lines {
         lines: Vec<String>,
         source: MsgSource,
     },
-    Cmd {
-        cmd: String,
-        source: MsgSource,
-    },
+
+    /// A command was submitted. `cmd` won't have an initial '/'.
+    Cmd { cmd: String, source: MsgSource },
 }
diff --git a/crates/libtiny_tui/examples/chat.rs b/crates/libtiny_tui/examples/chat.rs
index 92e90e68..81f804d2 100644
--- a/crates/libtiny_tui/examples/chat.rs
+++ b/crates/libtiny_tui/examples/chat.rs
@@ -96,7 +96,7 @@ fn handle_input_ev(ui: &TUI, ev: Event, abort: &mut mpsc::Sender<()>) {
                 ui.set_nick(SERV, new_nick);
             }
         }
-        Abort { .. } => {
+        Quit { .. } => {
             abort.try_send(()).unwrap();
         }
         Msg { .. } | Lines { .. } => {}
diff --git a/crates/libtiny_tui/examples/tabs.rs b/crates/libtiny_tui/examples/tabs.rs
index ee496b2b..3e9313d1 100644
--- a/crates/libtiny_tui/examples/tabs.rs
+++ b/crates/libtiny_tui/examples/tabs.rs
@@ -79,6 +79,6 @@ fn handle_input_ev(ui: &TUI, ev: Event) {
                 }
             }
         }
-        Abort { .. } | Msg { .. } | Lines { .. } => {}
+        Quit { .. } | Msg { .. } | Lines { .. } => {}
     }
 }
diff --git a/crates/libtiny_tui/src/exit_dialogue.rs b/crates/libtiny_tui/src/exit_dialogue.rs
index bb17a463..1fa7cfdd 100644
--- a/crates/libtiny_tui/src/exit_dialogue.rs
+++ b/crates/libtiny_tui/src/exit_dialogue.rs
@@ -50,7 +50,7 @@ impl ExitDialogue {
 
     pub(crate) fn keypressed(&self, key_action: &KeyAction) -> WidgetRet {
         match key_action {
-            KeyAction::Input('y') | KeyAction::InputSend => WidgetRet::Abort,
+            KeyAction::Input('y') | KeyAction::InputSend => WidgetRet::Quit,
             _ => WidgetRet::Remove,
         }
     }
diff --git a/crates/libtiny_tui/src/lib.rs b/crates/libtiny_tui/src/lib.rs
index e6e93b53..e7ff91a3 100644
--- a/crates/libtiny_tui/src/lib.rs
+++ b/crates/libtiny_tui/src/lib.rs
@@ -172,8 +172,8 @@ async fn input_handler<S>(
             Some(Ok(ev)) => {
                 let tui_ret = tui.borrow_mut().handle_input_event(ev, &mut rcv_editor_ret);
                 match tui_ret {
-                    Some(TUIRet::Abort) => {
-                        snd_ev.try_send(Event::Abort { msg: None }).unwrap();
+                    Some(TUIRet::Quit) => {
+                        snd_ev.try_send(Event::Quit { msg: None }).unwrap();
                         let _ = snd_abort.try_send(());
                         return;
                     }
@@ -181,12 +181,12 @@ async fn input_handler<S>(
                     Some(TUIRet::KeyCommand { cmd, from }) => {
                         let result = tui.borrow_mut().try_handle_cmd(&cmd, &from);
                         match result {
-                            CmdResult::Ok => {}
-                            CmdResult::Continue => {
+                            CmdResult::Handled => {}
+                            CmdResult::Pass => {
                                 snd_ev.try_send(Event::Cmd { cmd, source: from }).unwrap()
                             }
                             CmdResult::Quit(msg) => {
-                                snd_ev.try_send(Event::Abort { msg }).unwrap();
+                                snd_ev.try_send(Event::Quit { msg }).unwrap();
                                 let _ = snd_abort.try_send(());
                                 return;
                             }
@@ -199,12 +199,12 @@ async fn input_handler<S>(
                             let cmd: String = msg[1..].iter().collect();
                             let result = tui.borrow_mut().try_handle_cmd(&cmd, &from);
                             match result {
-                                CmdResult::Ok => {}
-                                CmdResult::Continue => {
+                                CmdResult::Handled => {}
+                                CmdResult::Pass => {
                                     snd_ev.try_send(Event::Cmd { cmd, source: from }).unwrap()
                                 }
                                 CmdResult::Quit(msg) => {
-                                    snd_ev.try_send(Event::Abort { msg }).unwrap();
+                                    snd_ev.try_send(Event::Quit { msg }).unwrap();
                                     let _ = snd_abort.try_send(());
                                     return;
                                 }
diff --git a/crates/libtiny_tui/src/tui.rs b/crates/libtiny_tui/src/tui.rs
index df8cafc4..52257493 100644
--- a/crates/libtiny_tui/src/tui.rs
+++ b/crates/libtiny_tui/src/tui.rs
@@ -28,20 +28,16 @@ use termbox_simple::{CellBuf, Termbox};
 
 #[derive(Debug)]
 pub(crate) enum TUIRet {
-    Abort,
-
-    KeyCommand {
-        cmd: String,
-        from: MsgSource,
-    },
-
-    /// INVARIANT: The vec will have at least one char.
-    // Can't make MsgSource a ref because of this weird error:
-    // https://users.rust-lang.org/t/borrow-checker-bug/5165
-    Input {
-        msg: Vec<char>,
-        from: MsgSource,
-    },
+    /// User wants to quit.
+    Quit,
+
+    /// A command was submitted, either directly, or via a key bound to a command.
+    ///
+    /// `cmd` won't have an initial '/'.
+    KeyCommand { cmd: String, from: MsgSource },
+
+    /// A message was sent. `msg` will have at least one character.
+    Input { msg: Vec<char>, from: MsgSource },
 }
 
 const LEFT_ARROW: char = '<';
@@ -108,11 +104,13 @@ pub struct TUI {
 }
 
 pub(crate) enum CmdResult {
-    /// Command executed successfully
-    Ok,
-    /// Pass command through to cmd.rs for further handling
-    Continue,
-    /// Quit command was executed, with the payload as a quit message
+    /// Command executed successfully by the TUI.
+    Handled,
+
+    /// Pass command through to the caller.
+    Pass,
+
+    /// Quit command was executed, with the payload as the quit message.
     Quit(Option<String>),
 }
 
@@ -283,15 +281,15 @@ impl TUI {
         match words.next() {
             Some("clear") => {
                 self.clear(&src.to_target());
-                CmdResult::Ok
+                CmdResult::Handled
             }
             Some("ignore") => {
                 self.ignore(src);
-                CmdResult::Ok
+                CmdResult::Handled
             }
             Some("notify") => {
                 self.notify(&mut words, src);
-                CmdResult::Ok
+                CmdResult::Handled
             }
             Some("switch") => {
                 match words.next() {
@@ -301,12 +299,12 @@ impl TUI {
                         &MsgTarget::CurrentTab,
                     ),
                 }
-                CmdResult::Ok
+                CmdResult::Handled
             }
             Some("reload") => {
                 self.reload_config();
                 self.add_client_notify_msg("Reloaded config file.", &MsgTarget::CurrentTab);
-                CmdResult::Ok
+                CmdResult::Handled
             }
             Some("help") => {
                 self.add_client_msg("TUI Commands: ", &MsgTarget::CurrentTab);
@@ -320,7 +318,7 @@ impl TUI {
                     );
                 }
                 // Fall through to print help for cmd.rs commands
-                CmdResult::Continue
+                CmdResult::Pass
             }
             Some("quit") => {
                 // Note: `SplitWhitespace::as_str` could be used here instead, when it gets stabilized.
@@ -332,7 +330,7 @@ impl TUI {
                     CmdResult::Quit(Some(reason))
                 }
             }
-            _ => CmdResult::Continue,
+            _ => CmdResult::Pass,
         }
     }
 
@@ -693,7 +691,7 @@ impl TUI {
 
             WidgetRet::Remove => unimplemented!(),
 
-            WidgetRet::Abort => Some(TUIRet::Abort),
+            WidgetRet::Quit => Some(TUIRet::Quit),
         }
     }
 
diff --git a/crates/libtiny_tui/src/widget.rs b/crates/libtiny_tui/src/widget.rs
index f3a2b170..051b4a56 100644
--- a/crates/libtiny_tui/src/widget.rs
+++ b/crates/libtiny_tui/src/widget.rs
@@ -8,12 +8,12 @@ pub(crate) enum WidgetRet {
     /// An input is submitted.
     Input(Vec<char>),
 
-    /// A command is ran
+    /// A command is ran.
     Command(String),
 
     /// Remove the widget. E.g. close the tab, hide the dialogue etc.
     Remove,
 
-    /// An exit event happened.
-    Abort,
+    /// User wants to quit, i.e. pressed `C-c <enter>` or a key bound to the `/quit` command.
+    Quit,
 }
diff --git a/crates/tiny/src/ui.rs b/crates/tiny/src/ui.rs
index 11e30307..a9933222 100644
--- a/crates/tiny/src/ui.rs
+++ b/crates/tiny/src/ui.rs
@@ -119,7 +119,7 @@ fn handle_input_ev(
 ) {
     use libtiny_common::Event::*;
     match ev {
-        Abort { msg } => {
+        Quit { msg } => {
             for client in clients {
                 client.quit(msg.clone());
             }