Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
enable unix shortcut key
  • Loading branch information
ksk001100 committed Jun 29, 2020
commit 3e15fdb31a3e2d0d1293eea1dd8ac37360030fc0
25 changes: 21 additions & 4 deletions src/handlers/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ pub fn handler(key: Key, app: &mut App) {
app.input_idx = 0;
app.input_cursor_position = 0;
}
Key::Left => {
Key::Left | Key::Ctrl('b') => {
if !app.input.is_empty() && app.input_idx > 0 {
let last_c = app.input[app.input_idx - 1];
app.input_idx -= 1;
app.input_cursor_position -= compute_character_width(last_c);
}
}
Key::Right => {
Key::Right | Key::Ctrl('f') => {
if app.input_idx < app.input.len() {
let next_c = app.input[app.input_idx];
app.input_idx += 1;
Expand Down Expand Up @@ -105,14 +105,14 @@ pub fn handler(key: Key, app: &mut App) {
app.input_idx += 1;
app.input_cursor_position += compute_character_width(c);
}
Key::Backspace => {
Key::Backspace | Key::Ctrl('h') => {
if !app.input.is_empty() && app.input_idx > 0 {
let last_c = app.input.remove(app.input_idx - 1);
app.input_idx -= 1;
app.input_cursor_position -= compute_character_width(last_c);
}
}
Key::Delete => {
Key::Delete | Key::Ctrl('d') => {
if !app.input.is_empty() && app.input_idx < app.input.len() {
app.input.remove(app.input_idx);
}
Expand Down Expand Up @@ -268,6 +268,12 @@ mod tests {

handler(Key::Backspace, &mut app);
assert_eq!(app.input, str_to_vec_char("M tex"));

app.input_idx = 1;
app.input_cursor_position = 1;

handler(Key::Ctrl('h'), &mut app);
assert_eq!(app.input, str_to_vec_char(" tex"));
}

#[test]
Expand All @@ -287,6 +293,13 @@ mod tests {

handler(Key::Delete, &mut app);
assert_eq!(app.input, str_to_vec_char("ラト"));

app.input = str_to_vec_char("Rust");
app.input_idx = 2;
app.input_cursor_position = 2;

handler(Key::Ctrl('d'), &mut app);
assert_eq!(app.input, str_to_vec_char("Rut"));
}

#[test]
Expand All @@ -304,6 +317,10 @@ mod tests {
assert_eq!(app.input_cursor_position, input_len - 2);
handler(Key::Left, &mut app);
assert_eq!(app.input_cursor_position, input_len - 3);
handler(Key::Ctrl('b'), &mut app);
assert_eq!(app.input_cursor_position, input_len - 4);
handler(Key::Ctrl('b'), &mut app);
assert_eq!(app.input_cursor_position, input_len - 5);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding tests 👍


// Pretend to smash the left event to test the we have no out-of-bounds crash
for _ in 0..20 {
Expand Down