@@ -53,7 +53,16 @@ impl Prompt {
5353 }
5454
5555 pub fn insert_char(&mut self, c: char) {
56− self.line.insert(self.cursor, c);
56+ let pos = if self.line.is_empty() {
57+ 0
58+ } else {
59+ self.line
60+ .char_indices()
61+ .nth(self.cursor)
62+ .map(|(pos, _)| pos)
63+ .unwrap_or_else(|| self.line.len())
64+ };
65+ self.line.insert(pos, c);
5766 self.cursor += 1;
5867 self.completion = (self.completion_fn)(&self.line);
5968 self.exit_selection();
@@ -79,7 +88,13 @@ impl Prompt {
7988
8089 pub fn delete_char_backwards(&mut self) {
8190 if self.cursor > 0 {
82− self.line.remove(self.cursor - 1);
91+ let pos = self
92+ .line
93+ .char_indices()
94+ .nth(self.cursor - 1)
95+ .map(|(pos, _)| pos)
96+ .expect("line is not empty");
97+ self.line.remove(pos);
8398 self.cursor -= 1;
8499 self.completion = (self.completion_fn)(&self.line);
85100 }