Skip to content

Commit

Permalink
Some renaming in TUI for consistency, documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
osa1 committed Apr 6, 2024
1 parent 2c06e4c commit bd34531
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 54 deletions.
25 changes: 13 additions & 12 deletions crates/libtiny_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
}
2 changes: 1 addition & 1 deletion crates/libtiny_tui/examples/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 { .. } => {}
Expand Down
2 changes: 1 addition & 1 deletion crates/libtiny_tui/examples/tabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ fn handle_input_ev(ui: &TUI, ev: Event) {
}
}
}
Abort { .. } | Msg { .. } | Lines { .. } => {}
Quit { .. } | Msg { .. } | Lines { .. } => {}
}
}
2 changes: 1 addition & 1 deletion crates/libtiny_tui/src/exit_dialogue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
16 changes: 8 additions & 8 deletions crates/libtiny_tui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,21 @@ 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;
}

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;
}
Expand All @@ -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;
}
Expand Down
52 changes: 25 additions & 27 deletions crates/libtiny_tui/src/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<';
Expand Down Expand Up @@ -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>),
}

Expand Down Expand Up @@ -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() {
Expand All @@ -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);
Expand All @@ -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.
Expand All @@ -332,7 +330,7 @@ impl TUI {
CmdResult::Quit(Some(reason))
}
}
_ => CmdResult::Continue,
_ => CmdResult::Pass,
}
}

Expand Down Expand Up @@ -693,7 +691,7 @@ impl TUI {

WidgetRet::Remove => unimplemented!(),

WidgetRet::Abort => Some(TUIRet::Abort),
WidgetRet::Quit => Some(TUIRet::Quit),
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/libtiny_tui/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
2 changes: 1 addition & 1 deletion crates/tiny/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit bd34531

Please sign in to comment.