Skip to content

Commit

Permalink
map carriage return '\r' to newline '\n', fix backspace bug
Browse files Browse the repository at this point in the history
  • Loading branch information
goolord committed Aug 4, 2020
1 parent 2086767 commit bb8add1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 14 deletions.
16 changes: 3 additions & 13 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,26 +176,16 @@ impl App {

pub fn on_key(&mut self, key: KeyCode) {
match key {
KeyCode::Char('\r') => self.put_char('\n'),
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)
}
}
KeyCode::Backspace => {
if self.data.input_cursor > 0
&& self.data.input_cursor < self.data.input.width() + 1
{
self.data.input_cursor = self.data.input_cursor.saturating_sub(1);
let idx = self
.data
.input
.chars()
.take(self.data.input_cursor)
.map(|c| c.len_utf8())
.sum();
self.data.input.remove(idx);
}
self.data.input_cursor = self.data.input_cursor.saturating_sub(1);
self.data.input.pop();
}
_ => {}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn draw_chat<B: Backend>(f: &mut Frame<B>, app: &mut App, area: Rect) {
lines.push(String::new());
}
match c {
'\r' => lines.push(String::new()),
'\n' => lines.push(String::new()),
_ => lines.last_mut().unwrap().push(c),
}
lines
Expand Down

0 comments on commit bb8add1

Please sign in to comment.