Skip to content

Commit

Permalink
Add hacky logic for toggling tab config
Browse files Browse the repository at this point in the history
Fixed toggling the tab config (/ignore and /notify) when on a server tab
and also for when on a user tab.

The hack is how we are storing user nicks as channel names in TabConfig.
That can perhaps be refactored later to look nicer.
  • Loading branch information
trevarj committed Dec 15, 2022
1 parent 7a4a09e commit f673fd8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
3 changes: 2 additions & 1 deletion crates/libtiny_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,10 @@ impl<'a> MsgTarget<'a> {
}
}

pub fn chan_name(&self) -> Option<&ChanNameRef> {
pub fn chan_or_user_name(&self) -> Option<&ChanNameRef> {
match self {
MsgTarget::Chan { chan, .. } => Some(chan),
MsgTarget::User { nick, .. } => Some(ChanNameRef::new(nick)),
_ => None,
}
}
Expand Down
16 changes: 16 additions & 0 deletions crates/libtiny_tui/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@ impl TabConfigs {
};
self.0.insert(key, config);
}

pub(crate) fn set_by_server(&mut self, serv_name: &str, config: TabConfig) {
for c in self
.0
.iter_mut()
.filter(|entry| entry.0.starts_with(serv_name))
{
*c.1 = config;
}
}
}

impl From<&Config> for TabConfigs {
Expand Down Expand Up @@ -208,6 +218,12 @@ impl TabConfig {
notify: self.notify.or(config.notify),
}
}

pub(crate) fn toggle_ignore(&mut self) -> bool {
let ignore = self.ignore.get_or_insert(false);
*ignore = !&*ignore;
*ignore
}
}

impl FromStr for TabConfig {
Expand Down
54 changes: 35 additions & 19 deletions crates/libtiny_tui/src/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,33 @@ impl TUI {
}

fn ignore(&mut self, src: &MsgSource) {
match src {
let (target, ignore) = match src {
MsgSource::Serv { serv } => {
self.toggle_ignore(&MsgTarget::AllServTabs { serv });
let mut config = self.get_tab_config(serv, None);
let new_ignore = config.toggle_ignore();
self.tab_configs.set_by_server(serv, config);
(MsgTarget::AllServTabs { serv }, new_ignore)
}
MsgSource::Chan { serv, chan } => {
self.toggle_ignore(&MsgTarget::Chan { serv, chan });
let mut config = self.get_tab_config(serv, Some(chan));
let new_ignore = config.toggle_ignore();
self.tab_configs.set(serv, Some(chan), config);
(MsgTarget::Chan { serv, chan }, new_ignore)
}
MsgSource::User { serv, nick } => {
self.toggle_ignore(&MsgTarget::User { serv, nick });
let nick_chan = ChanNameRef::new(nick);
let mut config = self.get_tab_config(serv, Some(nick_chan));
let new_ignore = config.toggle_ignore();
self.tab_configs.set(serv, Some(nick_chan), config);
(MsgTarget::User { serv, nick }, new_ignore)
}
}
};
let msg = if ignore {
"Ignore enabled"
} else {
"Ignore disabled"
};
self.add_client_notify_msg(msg, &target);
}

fn notify(&mut self, words: &mut SplitWhitespace, src: &MsgSource) {
Expand Down Expand Up @@ -1261,7 +1277,10 @@ impl TUI {

pub(crate) fn set_tab_style(&mut self, style: TabStyle, target: &MsgTarget) {
let ignore = self
.get_tab_config(target.serv_name().unwrap_or_default(), target.chan_name())
.get_tab_config(
target.serv_name().unwrap_or_default(),
target.chan_or_user_name(),
)
.ignore
.unwrap_or_default();
self.apply_to_target(target, false, &mut |tab: &mut Tab, is_active: bool| {
Expand Down Expand Up @@ -1307,7 +1326,7 @@ impl TUI {
is_action: bool,
) {
let mut notifier = if let Some(serv) = target.serv_name() {
self.get_tab_config(serv, target.chan_name())
self.get_tab_config(serv, target.chan_or_user_name())
.notify
.unwrap_or_default()
} else {
Expand Down Expand Up @@ -1355,7 +1374,10 @@ impl TUI {

pub(crate) fn add_nick(&mut self, nick: &str, ts: Option<Tm>, target: &MsgTarget) {
let ignore = self
.get_tab_config(target.serv_name().unwrap_or_default(), target.chan_name())
.get_tab_config(
target.serv_name().unwrap_or_default(),
target.chan_or_user_name(),
)
.ignore
.unwrap_or_default();

Expand All @@ -1366,7 +1388,10 @@ impl TUI {

pub(crate) fn remove_nick(&mut self, nick: &str, ts: Option<Tm>, target: &MsgTarget) {
let ignore = self
.get_tab_config(target.serv_name().unwrap_or_default(), target.chan_name())
.get_tab_config(
target.serv_name().unwrap_or_default(),
target.chan_or_user_name(),
)
.ignore
.unwrap_or_default();

Expand Down Expand Up @@ -1405,15 +1430,6 @@ impl TUI {
self.apply_to_target(target, false, &mut |tab: &mut Tab, _| tab.widget.clear());
}

pub(crate) fn toggle_ignore(&mut self, target: &MsgTarget) {
let serv = target.serv_name().unwrap_or_default();
let chan = target.chan_name();
let mut config = self.get_tab_config(serv, chan);
let ignore = config.ignore.get_or_insert(false);
*ignore = !&*ignore;
self.set_tab_config(serv, chan, config)
}

// TODO: Maybe remove this and add a `create: bool` field to MsgTarget::User
pub(crate) fn user_tab_exists(&self, serv_: &str, nick_: &str) -> bool {
for tab in &self.tabs {
Expand All @@ -1428,7 +1444,7 @@ impl TUI {

pub(crate) fn set_notifier(&mut self, notifier: Notifier, target: &MsgTarget) {
if let Some(serv) = target.serv_name() {
if let Some(config) = self.tab_configs.get_mut(serv, target.chan_name()) {
if let Some(config) = self.tab_configs.get_mut(serv, target.chan_or_user_name()) {
config.notify = Some(notifier);
}
}
Expand Down

0 comments on commit f673fd8

Please sign in to comment.