From a7ac6ad7c535608cca072107413011dacf21293a Mon Sep 17 00:00:00 2001 From: Zachary Churchill Date: Sun, 2 Aug 2020 14:07:23 -0400 Subject: [PATCH] Alt+Enter newline shortcut --- src/app.rs | 18 ++++++++++-------- src/ui.rs | 35 +++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/app.rs b/src/app.rs index ad6e7e4..4528a4e 100644 --- a/src/app.rs +++ b/src/app.rs @@ -165,16 +165,18 @@ impl App { }) } + pub fn put_char(&mut self, c: char) { + let mut idx = self.data.input_cursor; + while !self.data.input.is_char_boundary(idx) { + idx += 1; + } + self.data.input.insert(idx, c); + self.data.input_cursor += 1; + } + pub fn on_key(&mut self, key: KeyCode) { match key { - KeyCode::Char(c) => { - let mut idx = self.data.input_cursor; - while !self.data.input.is_char_boundary(idx) { - idx += 1; - } - self.data.input.insert(idx, c); - self.data.input_cursor += 1; - } + KeyCode::Char(c) => self.put_char(c), KeyCode::Enter if !self.data.input.is_empty() => { if let Some(idx) = self.data.channels.state.selected() { self.send_input(idx) diff --git a/src/ui.rs b/src/ui.rs index 8ce5be3..f5cf829 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -56,18 +56,25 @@ pub fn draw(f: &mut Frame, app: &mut App) { fn draw_chat(f: &mut Frame, app: &mut App, area: Rect) { let text_width = area.width.saturating_sub(2) as usize; - let lines = app - .data - .input - .chars() - .enumerate() - .fold(Vec::new(), |mut lines, (idx, c)| { - if idx % text_width == 0 { - lines.push(String::new()) - } - lines.last_mut().unwrap().push(c); - lines - }); + let lines: Vec = + app.data + .input + .chars() + .enumerate() + .fold(Vec::new(), |mut lines, (idx, c)| { + if idx % text_width == 0 { + lines.push(String::new()); + } + match c { + '\r' => lines.push(String::new()), + _ => lines.last_mut().unwrap().push(c), + } + lines + }); + let chars_since_newline = match lines.last() { + Some(last) => last.len(), + None => 0, + }; let num_input_lines = lines.len().max(1); let input: Vec = lines.into_iter().map(Spans::from).collect(); let extra_cursor_line = if app.data.input_cursor > 0 && app.data.input_cursor % text_width == 0 @@ -92,9 +99,9 @@ fn draw_chat(f: &mut Frame, app: &mut App, area: Rect) { f.render_widget(input, chunks[1]); f.set_cursor( // Put cursor past the end of the input text - chunks[1].x + ((app.data.input_cursor as u16) % text_width as u16) + 1, + chunks[1].x + ((chars_since_newline as u16) % text_width as u16) + 1, // Move one line down, from the border to the input line - chunks[1].y + (app.data.input_cursor as u16 / (text_width as u16)) + 1, + chunks[1].y + (chars_since_newline as u16 / (text_width as u16)) + num_input_lines as u16, ); }