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 iced::{
@ -18,7 +18,7 @@ use crate::{
irc_message::MessageDetail,
message_log::MessageLog,
ui_message::{self, UiMessage},
util,
util::{self, BATCH_SUB_COMMAND_CHATHISTORY},
};
static INPUT_ID: Lazy<text_input::Id> = Lazy::new(text_input::Id::unique);
@ -63,13 +63,14 @@ impl Cri {
match command {
"/join" | "/j" => self.handle_join_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(),
_ => (),
}
}
fn handle_part_command(&mut self, tokens: &mut std::str::SplitWhitespace<'_>) {
fn handle_part_command(&mut self, tokens: &mut SplitWhitespace<'_>) {
let channel = tokens
.next()
.map(String::from)
@ -98,7 +99,7 @@ impl Cri {
.unwrap();
}
fn handle_join_command(&mut self, tokens: &mut std::str::SplitWhitespace<'_>) {
fn handle_join_command(&mut self, tokens: &mut SplitWhitespace<'_>) {
let channel = tokens
.next()
.map(String::from)
@ -121,10 +122,19 @@ impl Cri {
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()));
}
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) {
self.input_tx
.borrow()
@ -239,6 +249,18 @@ impl Application for Cri {
IrcCommand::NICK(new) => {
self.message_log
.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) => {

View file

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

View file

@ -1,4 +1,6 @@
use iced::Color;
use irc::proto::BatchSubCommand;
use once_cell::sync::Lazy;
use regex::Regex;
pub const WHITE: Color = Color::WHITE;
@ -44,6 +46,9 @@ pub const DARK_RED: Color = Color {
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 {
let pattern = format!(r"\b{}\b", regex::escape(needle));
let regex = Regex::new(&pattern).unwrap();