Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce ServerId type, use it in MsgSource and MsgTarget #378

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 68 additions & 10 deletions crates/libtiny_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,23 +142,48 @@ impl Hash for ChanNameRef {
}
}

#[derive(Debug)]
pub struct ServerIdGen {
next: ServerId,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ServerId(usize);

impl ServerIdGen {
pub fn new() -> Self {
Self { next: ServerId(0) }
}

pub fn next(&mut self) -> ServerId {
let next = self.next;
self.next = ServerId(self.next.0 + 1);
next
}
}

/// Target of a message to be shown in a UI.
#[derive(Debug)]
pub enum MsgTarget<'a> {
/// Show the message in the server tab.
Server { serv: &'a str },
Server { serv_id: ServerId, serv: &'a str },

/// Show the message in the channel tab.
Chan {
serv_id: ServerId,
serv: &'a str,
chan: &'a ChanNameRef,
},

/// Show the message in the privmsg tab.
User { serv: &'a str, nick: &'a str },
User {
serv_id: ServerId,
serv: &'a str,
nick: &'a str,
},

/// Show the message in all tabs of a server.
AllServTabs { serv: &'a str },
AllServTabs { serv_id: ServerId, serv: &'a str },

/// Show the message in currently active tab.
CurrentTab,
Expand All @@ -168,32 +193,65 @@ pub enum MsgTarget<'a> {
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MsgSource {
/// Message sent in a server tab.
Serv { serv: String },
Serv { serv_id: ServerId, serv: String },

/// Message sent in a channel tab.
Chan { serv: String, chan: ChanName },
Chan {
serv_id: ServerId,
serv: String,
chan: ChanName,
},

/// Message sent in a privmsg tab.
User { serv: String, nick: String },
User {
serv_id: ServerId,
serv: String,
nick: String,
},
}

impl MsgSource {
pub fn serv_id(&self) -> ServerId {
match self {
MsgSource::Serv { serv_id, .. }
| MsgSource::Chan { serv_id, .. }
| MsgSource::User { serv_id, .. } => *serv_id,
}
}

// TODO: Most uses of this method should be replaced by `serv_id`
pub fn serv_name(&self) -> &str {
match self {
MsgSource::Serv { serv }
MsgSource::Serv { serv, .. }
| MsgSource::Chan { serv, .. }
| MsgSource::User { serv, .. } => serv,
}
}

pub fn to_target(&self) -> MsgTarget {
match self {
MsgSource::Serv { serv } => MsgTarget::Server { serv },
MsgSource::Chan { serv, chan } => MsgTarget::Chan {
MsgSource::Serv { serv_id, serv } => MsgTarget::Server {
serv_id: *serv_id,
serv,
},
MsgSource::Chan {
serv_id,
serv,
chan,
} => MsgTarget::Chan {
serv_id: *serv_id,
serv,
chan: chan.borrow(),
},
MsgSource::User { serv, nick } => MsgTarget::User { serv, nick },
MsgSource::User {
serv_id,
serv,
nick,
} => MsgTarget::User {
serv_id: *serv_id,
serv,
nick,
},
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions crates/libtiny_tui/examples/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ fn main() {

let local = tokio::task::LocalSet::new();

let tab = MsgTarget::Server { serv: "mentions" };

local.block_on(&runtime, async move {
let (tui, rcv_ev) = TUI::run(PathBuf::from("../tiny/config.yml"));

let serv_id = tui.mentions_serv_id();

let tab = MsgTarget::Server {
serv_id,
serv: "mentions",
};

let mut text = String::new();
let mut file = File::open("test/lipsum.txt").unwrap();
file.read_to_string(&mut text).unwrap();
Expand Down
27 changes: 18 additions & 9 deletions crates/libtiny_tui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mod widget;
mod tests;

use crate::tui::{CmdResult, TUIRet};
use libtiny_common::{ChanNameRef, Event, MsgSource, MsgTarget, TabStyle};
use libtiny_common::{ChanNameRef, Event, MsgSource, MsgTarget, ServerId, TabStyle};
use term_input::Input;

use std::cell::RefCell;
Expand Down Expand Up @@ -222,16 +222,20 @@ macro_rules! delegate {
impl TUI {
delegate!(draw());
delegate!(new_server_tab(serv_name: &str, alias: Option<String>,));
delegate!(close_server_tab(serv_name: &str,));
delegate!(new_chan_tab(serv_name: &str, chan: &ChanNameRef,));
delegate!(close_chan_tab(serv_name: &str, chan: &ChanNameRef,));
delegate!(close_user_tab(serv_name: &str, nick: &str,));
delegate!(close_server_tab(serv_id: ServerId,));
delegate!(new_chan_tab(
serv_id: ServerId,
serv_name: &str,
chan: &ChanNameRef,
));
delegate!(close_chan_tab(serv_id: ServerId, chan: &ChanNameRef,));
delegate!(close_user_tab(serv_id: ServerId, nick: &str,));
delegate!(add_client_msg(msg: &str, target: &MsgTarget,));
delegate!(add_msg(msg: &str, ts: Tm, target: &MsgTarget,));
delegate!(add_err_msg(msg: &str, ts: Tm, target: &MsgTarget,));
delegate!(add_client_err_msg(msg: &str, target: &MsgTarget,));
delegate!(clear_nicks(serv_name: &str,));
delegate!(set_nick(serv_name: &str, new_nick: &str,));
delegate!(clear_nicks(serv_id: ServerId, serv_name: &str,));
delegate!(set_nick(serv_id: ServerId, serv_name: &str, new_nick: &str,));
delegate!(add_privmsg(
sender: &str,
msg: &str,
Expand All @@ -251,14 +255,19 @@ impl TUI {
delegate!(set_topic(
topic: &str,
ts: Tm,
serv_id: ServerId,
serv_name: &str,
chan_name: &ChanNameRef,
));
delegate!(set_tab_style(style: TabStyle, target: &MsgTarget,));

pub fn user_tab_exists(&self, serv_name: &str, nick: &str) -> bool {
pub fn mentions_serv_id(&self) -> ServerId {
self.inner.upgrade().unwrap().borrow().mentions_serv_id()
}

pub fn user_tab_exists(&self, serv_id: ServerId, nick: &str) -> bool {
match self.inner.upgrade() {
Some(tui) => tui.borrow().user_tab_exists(serv_name, nick),
Some(tui) => tui.borrow().user_tab_exists(serv_id, nick),
None => false,
}
}
Expand Down
36 changes: 24 additions & 12 deletions crates/libtiny_tui/src/tests/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ fn test_join_part_overflow() {
let mut tui = TUI::new_test(21, 4);
let serv = "irc.server_1.org";
let chan = ChanNameRef::new("#chan");
tui.new_server_tab(serv, None);
tui.set_nick(serv, "osa1");
tui.new_chan_tab(serv, chan);
let (serv_id, _) = tui.new_server_tab(serv, None);
tui.set_nick(serv_id, serv, "osa1");
tui.new_chan_tab(serv_id, serv, chan);
tui.next_tab();
tui.next_tab();

let target = MsgTarget::Chan { serv, chan };
let target = MsgTarget::Chan {
serv_id,
serv,
chan,
};
let ts = time::at_utc(time::Timespec::new(0, 0));
tui.add_nick("123456", Some(ts), &target);
tui.add_nick("abcdef", Some(ts), &target);
Expand All @@ -40,13 +44,17 @@ fn test_alignment_long_string() {
tui.set_layout(Layout::Aligned { max_nick_len: 12 });
let serv = "irc.server_1.org";
let chan = ChanNameRef::new("#chan");
tui.new_server_tab(serv, None);
tui.set_nick(serv, "osa1");
tui.new_chan_tab(serv, chan);
let (serv_id, _) = tui.new_server_tab(serv, None);
tui.set_nick(serv_id, serv, "osa1");
tui.new_chan_tab(serv_id, serv, chan);
tui.next_tab();
tui.next_tab();

let target = MsgTarget::Chan { serv, chan };
let target = MsgTarget::Chan {
serv_id,
serv,
chan,
};
let ts = time::at_utc(time::Timespec::new(0, 0));
tui.add_privmsg(
"osa1",
Expand All @@ -72,10 +80,14 @@ fn test_alignment_long_string() {
#[test]
fn test_mnemonic_generation() {
let mut tui = TUI::new_test(10, 10);
tui.new_chan_tab("s1", ChanNameRef::new("#ab"));
tui.new_chan_tab("s2", ChanNameRef::new("#ab"));
tui.new_chan_tab("s3", ChanNameRef::new("#ab"));
tui.new_chan_tab("s4", ChanNameRef::new("#ab"));
let (s1, _) = tui.new_server_tab("s1", None);
tui.new_chan_tab(s1, "s1", ChanNameRef::new("#ab"));
let (s2, _) = tui.new_server_tab("s2", None);
tui.new_chan_tab(s2, "s2", ChanNameRef::new("#ab"));
let (s3, _) = tui.new_server_tab("s3", None);
tui.new_chan_tab(s3, "s3", ChanNameRef::new("#ab"));
let (s4, _) = tui.new_server_tab("s4", None);
tui.new_chan_tab(s4, "s4", ChanNameRef::new("#ab"));
let tabs = tui.get_tabs();
assert_eq!(tabs.len(), 9); // mentions, 4 servers, 4 channels
assert_eq!(tabs[2].switch, Some('a'));
Expand Down
44 changes: 26 additions & 18 deletions crates/libtiny_tui/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ fn init_screen() {
fn close_rightmost_tab() {
// After closing right-most tab the tab bar should scroll left.
let mut tui = TUI::new_test(20, 4);
tui.new_server_tab("irc.server_1.org", None);
tui.new_server_tab("irc.server_2.org", None);
let (serv1_id, _) = tui.new_server_tab("irc.server_1.org", None);
let (serv2_id, _) = tui.new_server_tab("irc.server_2.org", None);
tui.next_tab();
tui.next_tab();
tui.draw();
Expand All @@ -49,7 +49,7 @@ fn close_rightmost_tab() {

// Should scroll left when the server tab is closed. Left arrow should still be visible as
// there are still tabs to the left.
tui.close_server_tab("irc.server_2.org");
tui.close_server_tab(serv2_id);
tui.draw();

#[rustfmt::skip]
Expand All @@ -61,7 +61,7 @@ fn close_rightmost_tab() {
expect_screen(screen, &tui.get_front_buffer(), 20, 4, Location::caller());

// Scroll left again, left arrow should disappear this time.
tui.close_server_tab("irc.server_1.org");
tui.close_server_tab(serv1_id);
tui.draw();

#[rustfmt::skip]
Expand All @@ -78,13 +78,17 @@ fn small_screen_1() {
let mut tui = TUI::new_test(21, 3);
let serv = "irc.server_1.org";
let chan = ChanNameRef::new("#chan");
tui.new_server_tab(serv, None);
tui.set_nick(serv, "osa1");
tui.new_chan_tab(serv, chan);
let (serv_id, _) = tui.new_server_tab(serv, None);
tui.set_nick(serv_id, serv, "osa1");
tui.new_chan_tab(serv_id, serv, chan);
tui.next_tab();
tui.next_tab();

let target = MsgTarget::Chan { serv, chan };
let target = MsgTarget::Chan {
serv_id,
serv,
chan,
};
let ts = time::at_utc(time::Timespec::new(0, 0));
tui.add_nick("123456", Some(ts), &target);
tui.add_nick("abcdef", Some(ts), &target);
Expand Down Expand Up @@ -127,15 +131,19 @@ fn small_screen_2() {
let mut tui = TUI::new_test(21, 4);
let serv = "irc.server_1.org";
let chan = ChanNameRef::new("#chan");
tui.new_server_tab(serv, None);
tui.set_nick(serv, "osa1");
tui.new_chan_tab(serv, chan);
let (serv_id, _) = tui.new_server_tab(serv, None);
tui.set_nick(serv_id, serv, "osa1");
tui.new_chan_tab(serv_id, serv, chan);
tui.next_tab();
tui.next_tab();

let target = MsgTarget::Chan { serv, chan };
let target = MsgTarget::Chan {
serv_id,
serv,
chan,
};
let ts = time::at_utc(time::Timespec::new(0, 0));
tui.set_topic("Blah blah blah-", ts.clone(), serv, chan);
tui.set_topic("Blah blah blah-", ts.clone(), serv_id, serv, chan);

tui.draw();

Expand Down Expand Up @@ -164,9 +172,9 @@ fn ctrl_w() {
let mut tui = TUI::new_test(30, 3);
let serv = "irc.server_1.org";
let chan = ChanNameRef::new("#chan");
tui.new_server_tab(serv, None);
tui.set_nick(serv, "osa1");
tui.new_chan_tab(serv, chan);
let (serv_id, _) = tui.new_server_tab(serv, None);
tui.set_nick(serv_id, serv, "osa1");
tui.new_chan_tab(serv_id, serv, chan);
tui.next_tab();
tui.next_tab();

Expand Down Expand Up @@ -233,8 +241,8 @@ fn test_text_field_wrap() {
let mut tui = TUI::new_test(40, 8);

let server = "chat.freenode.net";
tui.new_server_tab(server, None);
tui.set_nick(server, "x");
let (serv_id, _) = tui.new_server_tab(server, None);
tui.set_nick(serv_id, server, "x");

// Switch to server tab
tui.next_tab();
Expand Down
Loading