fix russian in chat

This commit is contained in:
MeexReay 2025-02-12 02:57:45 +03:00
parent 0111c27389
commit 1b9d890fc8
2 changed files with 23 additions and 10 deletions

View File

@ -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 crossterm::{cursor::{MoveLeft, MoveRight}, event::{self, Event, KeyCode, KeyModifiers, MouseEventKind}, execute, terminal::{self, disable_raw_mode, enable_raw_mode}};
use rand::random; 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}; 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<Context>) -> Result<(), Box<dyn Error>> {
continue continue
} }
let len = input.read().unwrap().chars().count(); 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(); *input.write().unwrap() = history[history_cursor].clone();
replace_input_left(cursor, len, &history[history_cursor], cursor-1); replace_input_left(cursor, len, &history[history_cursor], cursor-1);
cursor -= 1; cursor -= 1;
@ -332,7 +333,8 @@ fn poll_events(ctx: Arc<Context>) -> Result<(), Box<dyn Error>> {
continue continue
} }
let len = input.read().unwrap().chars().count(); 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(); *input.write().unwrap() = history[history_cursor].clone();
replace_input_left(cursor, len, &history[history_cursor], cursor); replace_input_left(cursor, len, &history[history_cursor], cursor);
} }
@ -388,11 +390,12 @@ fn poll_events(ctx: Arc<Context>) -> Result<(), Box<dyn Error>> {
on_close(); on_close();
break; break;
} }
history[history_cursor].insert(cursor, c); let i = char_index_to_byte_index(&history[history_cursor], cursor);
input.write().unwrap().insert(cursor, c); history[history_cursor].insert(i, c);
input.write().unwrap().insert(i, c);
write!(stdout(), "{}{}", write!(stdout(), "{}{}",
history[history_cursor][cursor..].to_string(), history[history_cursor][i..].to_string(),
MoveLeft(1).to_string().repeat(history[history_cursor].len()-cursor-1) MoveLeft(1).to_string().repeat(history[history_cursor].chars().count()-cursor-1)
).unwrap(); ).unwrap();
stdout().lock().flush().unwrap(); stdout().lock().flush().unwrap();
cursor += 1; cursor += 1;
@ -401,9 +404,15 @@ fn poll_events(ctx: Arc<Context>) -> Result<(), Box<dyn Error>> {
} }
}, },
Event::Paste(data) => { Event::Paste(data) => {
input.write().unwrap().push_str(&data); let i = char_index_to_byte_index(&history[history_cursor], cursor);
write!(stdout(), "{}", &data).unwrap(); 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(); stdout().lock().flush().unwrap();
cursor += data.len();
}, },
Event::Resize(_, _) => { Event::Resize(_, _) => {
print_console( print_console(

View File

@ -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 lazy_static::lazy_static;
use regex::Regex; use regex::Regex;
@ -12,6 +12,10 @@ fn get_matches(regex: &Regex, text: &str) -> Vec<Range<usize>> {
regex.find_iter(text).map(|mat| mat.range()).collect() 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)> { pub fn string_chunks(text: &str, width: usize) -> Vec<(String, usize)> {
let mut norm: Vec<bool> = vec![true; text.chars().count()]; let mut norm: Vec<bool> = vec![true; text.chars().count()];