Add /nick command

This commit is contained in:
Sijmen 2023-12-01 02:09:46 +01:00
parent 8f1a3fdb29
commit 22209bd133
Signed by: vijfhoek
GPG key ID: DAF7821E067D9C48
3 changed files with 37 additions and 10 deletions

View file

@ -1,4 +1,4 @@
use std::{cell::RefCell, collections::HashMap}; use std::{cell::RefCell, collections::HashMap, str::SplitWhitespace};
use chrono::{DateTime, Local}; use chrono::{DateTime, Local};
use iced::{ use iced::{
@ -18,7 +18,7 @@ use crate::{
irc_message::MessageDetail, irc_message::MessageDetail,
message_log::MessageLog, message_log::MessageLog,
ui_message::{self, UiMessage}, ui_message::{self, UiMessage},
util, util::{self, BATCH_SUB_COMMAND_CHATHISTORY},
}; };
static INPUT_ID: Lazy<text_input::Id> = Lazy::new(text_input::Id::unique); static INPUT_ID: Lazy<text_input::Id> = Lazy::new(text_input::Id::unique);
@ -63,13 +63,14 @@ impl Cri {
match command { match command {
"/join" | "/j" => self.handle_join_command(&mut tokens), "/join" | "/j" => self.handle_join_command(&mut tokens),
"/part" | "/p" => self.handle_part_command(&mut tokens), "/part" | "/p" => self.handle_part_command(&mut tokens),
"/query" | "/q" => self.handle_query_command(tokens), "/query" | "/q" => self.handle_query_command(&mut tokens),
"/nick" | "/n" => self.handle_nick_command(&mut tokens),
"/list" => self.handle_list_command(), "/list" => self.handle_list_command(),
_ => (), _ => (),
} }
} }
fn handle_part_command(&mut self, tokens: &mut std::str::SplitWhitespace<'_>) { fn handle_part_command(&mut self, tokens: &mut SplitWhitespace<'_>) {
let channel = tokens let channel = tokens
.next() .next()
.map(String::from) .map(String::from)
@ -98,7 +99,7 @@ impl Cri {
.unwrap(); .unwrap();
} }
fn handle_join_command(&mut self, tokens: &mut std::str::SplitWhitespace<'_>) { fn handle_join_command(&mut self, tokens: &mut SplitWhitespace<'_>) {
let channel = tokens let channel = tokens
.next() .next()
.map(String::from) .map(String::from)
@ -121,10 +122,19 @@ impl Cri {
self.message_log.set_active(Some(&channel)); self.message_log.set_active(Some(&channel));
} }
fn handle_query_command(&mut self, mut tokens: std::str::SplitWhitespace<'_>) { fn handle_query_command(&mut self, tokens: &mut SplitWhitespace<'_>) {
self.message_log.set_active(Some(tokens.next().unwrap())); self.message_log.set_active(Some(tokens.next().unwrap()));
} }
fn handle_nick_command(&self, tokens: &mut SplitWhitespace<'_>) {
if let Some(new_nick) = tokens.next() {
self.input_tx
.borrow()
.send(IrcCommand::NICK(new_nick.into()).into())
.unwrap();
}
}
fn handle_list_command(&self) { fn handle_list_command(&self) {
self.input_tx self.input_tx
.borrow() .borrow()
@ -239,6 +249,18 @@ impl Application for Cri {
IrcCommand::NICK(new) => { IrcCommand::NICK(new) => {
self.message_log self.message_log
.on_nick(&source_nickname, new, &timestamp, message_id); .on_nick(&source_nickname, new, &timestamp, message_id);
if let Some(batch) = batch {
let (subcommand, _) = &self.message_log.batch_channels[&batch];
if subcommand != &*BATCH_SUB_COMMAND_CHATHISTORY
&& source_nickname == self.nickname
{
self.nickname = new.into();
}
} else {
self.nickname = new.into();
}
} }
IrcCommand::QUIT(comment) => { IrcCommand::QUIT(comment) => {

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
irc_message::{IrcMessage, MessageDetail}, irc_message::{IrcMessage, MessageDetail},
util, util::{self, BATCH_SUB_COMMAND_CHATHISTORY},
}; };
use chrono::{DateTime, Local}; use chrono::{DateTime, Local};
@ -184,7 +184,7 @@ impl<'a> MessageLog {
) { ) {
if let Some(batch) = batch { if let Some(batch) = batch {
let (subcommand, channel_name) = &self.batch_channels[&batch]; let (subcommand, channel_name) = &self.batch_channels[&batch];
if subcommand == &BatchSubCommand::CUSTOM("CHATHISTORY".into()) { if subcommand == &*BATCH_SUB_COMMAND_CHATHISTORY {
if let Some(channel) = self.channels.get_mut(&Some(channel_name.to_owned())) { if let Some(channel) = self.channels.get_mut(&Some(channel_name.to_owned())) {
if message_id.is_some() if message_id.is_some()
&& !channel.message_ids.insert(message_id.unwrap().into()) && !channel.message_ids.insert(message_id.unwrap().into())
@ -246,7 +246,7 @@ impl<'a> MessageLog {
channel.is_multiline = true; channel.is_multiline = true;
channel.multiline_privmsgs = Some(Vec::new()); channel.multiline_privmsgs = Some(Vec::new());
channel.multiline_timestamp = Some(*timestamp); channel.multiline_timestamp = Some(*timestamp);
} else if subcommand == Some(&BatchSubCommand::CUSTOM("CHATHISTORY".into())) { } else if subcommand == Some(&*BATCH_SUB_COMMAND_CHATHISTORY) {
let channel_name = &params.unwrap()[0]; let channel_name = &params.unwrap()[0];
self.batch_channels.insert( self.batch_channels.insert(
tag.into(), tag.into(),
@ -277,7 +277,7 @@ impl<'a> MessageLog {
None, None,
&timestamp, &timestamp,
); );
} else if subcommand == BatchSubCommand::CUSTOM("CHATHISTORY".into()) { } else if subcommand == *BATCH_SUB_COMMAND_CHATHISTORY {
// TODO // TODO
} }
} }

View file

@ -1,4 +1,6 @@
use iced::Color; use iced::Color;
use irc::proto::BatchSubCommand;
use once_cell::sync::Lazy;
use regex::Regex; use regex::Regex;
pub const WHITE: Color = Color::WHITE; pub const WHITE: Color = Color::WHITE;
@ -44,6 +46,9 @@ pub const DARK_RED: Color = Color {
a: 1.0, a: 1.0,
}; };
pub static BATCH_SUB_COMMAND_CHATHISTORY: Lazy<BatchSubCommand> =
Lazy::new(|| BatchSubCommand::CUSTOM("CHATHISTORY".into()));
pub fn contains_word(needle: &str, haystack: &str) -> bool { pub fn contains_word(needle: &str, haystack: &str) -> bool {
let pattern = format!(r"\b{}\b", regex::escape(needle)); let pattern = format!(r"\b{}\b", regex::escape(needle));
let regex = Regex::new(&pattern).unwrap(); let regex = Regex::new(&pattern).unwrap();