From 6f3e0a8d25f06c664a41c6b8597e92b810596217 Mon Sep 17 00:00:00 2001 From: Jaromir Obr Date: Mon, 27 Dec 2021 13:04:00 +0100 Subject: [PATCH] Fixed a crash of editor when deleting multiple bytes characters in prompt --- src/editor.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/editor.rs b/src/editor.rs index af10def..6726070 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -6,6 +6,7 @@ use std::time::Duration; use std::time::Instant; use termion::color; use termion::event::Key; +use unicode_segmentation::UnicodeSegmentation; const STATUS_FG_COLOR: color::Rgb = color::Rgb(63, 63, 63); const STATUS_BG_COLOR: color::Rgb = color::Rgb(239, 239, 239); @@ -388,7 +389,13 @@ impl Editor { self.refresh_screen()?; let key = Terminal::read_key()?; match key { - Key::Backspace => result.truncate(result.len().saturating_sub(1)), + Key::Backspace => { + let graphemes_cnt = result.graphemes(true).count(); + result = result + .graphemes(true) + .take(graphemes_cnt.saturating_sub(1)) + .collect(); + } Key::Char('\n') => break, Key::Char(c) => { if !c.is_control() {