Skip to content

Commit

Permalink
incremental search added
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacperacy committed Mar 20, 2024
1 parent 75574da commit e37c096
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub struct Editor {
filename: Option<String>,
dirty: bool,
quit_times: u8,
find_num: u32,
}

impl Editor {
Expand All @@ -56,6 +57,7 @@ impl Editor {
status_time: Duration::new(0, 0),
dirty: false,
quit_times: QUIT_TIMES,
find_num: 0,
}
}

Expand Down Expand Up @@ -288,14 +290,20 @@ impl Editor {
}

fn find_callback(&mut self, query: &str) {
let mut num = self.find_num;
for i in 0..self.rows.len() {
if let Some(pos) = self.rows[i].find(query) {
self.cursor.y = i;
self.cursor.x = pos;
self.offset.rows = self.rows.len();
break;

if num == 0 {
break;
}
num -= 1;
}
}
self.find_num -= num;
}

fn find(&mut self) {
Expand Down Expand Up @@ -338,6 +346,16 @@ impl Editor {
if let Some(callback) = callback {
callback(self, &input);
}
} else if c.code == KeyCode::Up || c.code == KeyCode::Left {
if let Some(callback) = callback {
self.find_num = self.find_num.saturating_sub(1);
callback(self, &input);
}
} else if c.code == KeyCode::Down || c.code == KeyCode::Right {
if let Some(callback) = callback {
self.find_num += 1;
callback(self, &input);
}
}
}
}
Expand Down

0 comments on commit e37c096

Please sign in to comment.