Skip to content

Commit

Permalink
Limit loop + fix crashes on non-ascii input
Browse files Browse the repository at this point in the history
  • Loading branch information
Paddyk45 committed Dec 11, 2023
1 parent 2c7447e commit 969434f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::time::Duration;

#[derive(Default)]
pub struct App {
pub input: String,
pub input: Vec<char>,
pub cursor_pos: usize,
pub messages: Vec<String>,
pub message_queue: Vec<String>,
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ pub fn initialize_panic_handler() {
std::panic::set_hook(Box::new(|panic_info| {
execute!(std::io::stderr(), crossterm::terminal::LeaveAlternateScreen).unwrap();
disable_raw_mode().unwrap();
Terminal::new(CrosstermBackend::new(stdout())).unwrap().show_cursor().unwrap();
Settings::auto()
.most_recent_first(false)
.most_recent_first(true)
.lineno_suffix(true)
.create_panic_handler()(panic_info);
}));
Expand All @@ -39,5 +40,6 @@ fn main() -> io::Result<()> {
app.run(&mut terminal);
execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
disable_raw_mode()?;
terminal.show_cursor()?;
Ok(())
}
3 changes: 3 additions & 0 deletions src/net_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use serde_json::Value;
use std::io::Write;
use std::net::TcpStream;
use std::sync::{Arc, RwLock};
use std::thread::sleep;
use std::time::Duration;
use crate::app::App;

pub fn handler_s2c(app: Arc<RwLock<App>>, stream: TcpStream) {
Expand Down Expand Up @@ -56,5 +58,6 @@ pub fn handler_c2s(app: Arc<RwLock<App>>, mut stream: TcpStream) {
.expect("Failed to write to stream");
app.write().unwrap().message_queue.pop();
}
sleep(Duration::from_millis(40));
}
}
21 changes: 10 additions & 11 deletions src/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn ui(frame: &mut Frame, app: Arc<RwLock<App>>) {
frame.render_widget(esc_info, chunks[0]);

let state = app.read().unwrap();
let input = Paragraph::new(state.input.as_str())
let input = Paragraph::new(state.input.iter().collect::<String>())
.block(Block::default().borders(Borders::ALL).title("Message"));
frame.render_widget(input, chunks[1]);

Expand All @@ -40,36 +40,35 @@ pub fn ui(frame: &mut Frame, app: Arc<RwLock<App>>) {


impl App {
pub fn move_cursor_left(&mut self) {
let moved = self.cursor_pos.saturating_sub(1);
pub fn move_cursor_left(&mut self, amount: usize) {
let moved = self.cursor_pos.saturating_sub(amount);
self.cursor_pos = moved.clamp(0, self.input.len());
}
pub fn move_cursor_right(&mut self) {
let moved = self.cursor_pos + 1;
pub fn move_cursor_right(&mut self, amount: usize) {
let moved = self.cursor_pos + amount;
self.cursor_pos = moved.clamp(0, self.input.len());
}

pub fn enter(&mut self, ch: char) {
self.input.insert(self.cursor_pos, ch);
self.move_cursor_right();
self.move_cursor_right(1);
}

pub fn delete(&mut self) {
if self.cursor_pos == 0 {
return;
}
let char_to_delete_pos = self.cursor_pos - 1;
self.input.remove(char_to_delete_pos);
self.move_cursor_left();
self.input.remove(self.cursor_pos - 1);
self.move_cursor_left(1);
}

pub fn reset_cursor(&mut self) {
self.cursor_pos = 0;
}

pub fn send(&mut self) {
self.message_queue.push(self.input.clone());
self.input = String::new();
self.message_queue.push(self.input.iter().collect());
self.input.clear();
self.reset_cursor();
}
}

0 comments on commit 969434f

Please sign in to comment.