Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
metiftikci committed Oct 25, 2023
1 parent 7930131 commit fc07e69
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
19 changes: 13 additions & 6 deletions src/core/editable_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@ pub struct EditableText {
}

impl EditableText {
pub fn new() -> Self {
Self {
text: String::new(),
cursor_x: 0,
}
}

pub fn insert_char(&mut self, ch: char) {
self.text.insert(self.cursor_x, ch);
self.move_right();
}

pub fn delete_char(&mut self) {
pub fn delete_char_previous(&mut self) {
if self.cursor_x > 0 {
self.text.remove(self.cursor_x - 1);
self.move_left();
}
}

pub fn delete_char_after(&mut self) {
pub fn delete_char(&mut self) {
if self.cursor_x < self.text.len() {
self.text.remove(self.cursor_x);
}
Expand All @@ -43,9 +50,9 @@ impl EditableText {
match key {
"left" => self.move_left(),
"right" => self.move_right(),
"<c-h>" => self.delete_char(),
"backspace" => self.delete_char(),
"delete" => self.delete_char_after(),
"<c-h>" => self.delete_char_previous(),
"backspace" => self.delete_char_previous(),
"delete" => self.delete_char(),
other => {
if other.len() == 1 {
self.insert_char(other.chars().nth(0).unwrap());
Expand All @@ -71,7 +78,7 @@ mod tests {
assert_eq!("", editable.text);
assert_eq!(0, editable.cursor_x);

editable.delete_char();
editable.delete_char_previous();
editable.move_left();
editable.move_right();
editable.insert_char('b');
Expand Down
5 changes: 1 addition & 4 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ impl Editor {
status_area: Rectangle::zero(),
tabs: vec![],
active_tab: 0,
input: EditableText {
text: String::new(),
cursor_x: 0,
},
input: EditableText::new(),
};

editor.set_size(area);
Expand Down

0 comments on commit fc07e69

Please sign in to comment.