From 1b9d890fc809fb981a5e4fac161daa85547e4b3c Mon Sep 17 00:00:00 2001 From: MeexReay Date: Wed, 12 Feb 2025 02:57:45 +0300 Subject: [PATCH] fix russian in chat --- src/chat.rs | 27 ++++++++++++++++++--------- src/util.rs | 6 +++++- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index 83e7e38..0426516 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -4,7 +4,7 @@ use colored::{Color, Colorize}; use crossterm::{cursor::{MoveLeft, MoveRight}, event::{self, Event, KeyCode, KeyModifiers, MouseEventKind}, execute, terminal::{self, disable_raw_mode, enable_raw_mode}}; use rand::random; -use crate::{proto::send_message_auth, util::string_chunks, IP_REGEX}; +use crate::{proto::send_message_auth, util::{char_index_to_byte_index, string_chunks}, IP_REGEX}; use super::{proto::read_messages, util::sanitize_text, COLORED_USERNAMES, DATE_REGEX, config::Context, proto::send_message}; @@ -322,7 +322,8 @@ fn poll_events(ctx: Arc) -> Result<(), Box> { continue } let len = input.read().unwrap().chars().count(); - history[history_cursor].remove(cursor-1); + let i = char_index_to_byte_index(&history[history_cursor], cursor-1); + history[history_cursor].remove(i); *input.write().unwrap() = history[history_cursor].clone(); replace_input_left(cursor, len, &history[history_cursor], cursor-1); cursor -= 1; @@ -332,7 +333,8 @@ fn poll_events(ctx: Arc) -> Result<(), Box> { continue } let len = input.read().unwrap().chars().count(); - history[history_cursor].remove(cursor); + let i = char_index_to_byte_index(&history[history_cursor], cursor); + history[history_cursor].remove(i); *input.write().unwrap() = history[history_cursor].clone(); replace_input_left(cursor, len, &history[history_cursor], cursor); } @@ -388,11 +390,12 @@ fn poll_events(ctx: Arc) -> Result<(), Box> { on_close(); break; } - history[history_cursor].insert(cursor, c); - input.write().unwrap().insert(cursor, c); + let i = char_index_to_byte_index(&history[history_cursor], cursor); + history[history_cursor].insert(i, c); + input.write().unwrap().insert(i, c); write!(stdout(), "{}{}", - history[history_cursor][cursor..].to_string(), - MoveLeft(1).to_string().repeat(history[history_cursor].len()-cursor-1) + history[history_cursor][i..].to_string(), + MoveLeft(1).to_string().repeat(history[history_cursor].chars().count()-cursor-1) ).unwrap(); stdout().lock().flush().unwrap(); cursor += 1; @@ -401,9 +404,15 @@ fn poll_events(ctx: Arc) -> Result<(), Box> { } }, Event::Paste(data) => { - input.write().unwrap().push_str(&data); - write!(stdout(), "{}", &data).unwrap(); + let i = char_index_to_byte_index(&history[history_cursor], cursor); + history[history_cursor].insert_str(i, &data); + input.write().unwrap().insert_str(i, &data); + write!(stdout(), "{}{}", + history[history_cursor][cursor..].to_string(), + MoveLeft(1).to_string().repeat(history[history_cursor].len()-cursor-1) + ).unwrap(); stdout().lock().flush().unwrap(); + cursor += data.len(); }, Event::Resize(_, _) => { print_console( diff --git a/src/util.rs b/src/util.rs index 9197ddc..56eebf5 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,4 +1,4 @@ -use std::{collections::HashSet, io::{stdin, stdout, BufRead, Write}, ops::Range}; +use std::{io::{stdin, stdout, BufRead, Write}, ops::Range}; use lazy_static::lazy_static; use regex::Regex; @@ -12,6 +12,10 @@ fn get_matches(regex: &Regex, text: &str) -> Vec> { regex.find_iter(text).map(|mat| mat.range()).collect() } +pub fn char_index_to_byte_index(text: &str, char_index: usize) -> usize { + text.char_indices().skip(char_index).next().map(|o| o.0).unwrap_or(text.len()) +} + pub fn string_chunks(text: &str, width: usize) -> Vec<(String, usize)> { let mut norm: Vec = vec![true; text.chars().count()];