diff options
author | Teddy Wing | 2016-08-06 05:38:02 -0400 |
---|---|---|
committer | Teddy Wing | 2016-08-06 05:38:02 -0400 |
commit | 833339151a14866ccf499dd57f1849766c7c3555 (patch) | |
tree | 48e1d80f79beb3d3bdc4e060cd48c681d81eecdb | |
parent | b4e150f01e9d282a6dab318ee7bb0ee9884b2263 (diff) | |
download | Passextract-833339151a14866ccf499dd57f1849766c7c3555.tar.bz2 |
Make selection arrow movement code DRY
Remove the duplication of these lines for the `j` and `k` commands by
extracting them to a function.
-rw-r--r-- | src/main.rs | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/src/main.rs b/src/main.rs index 23f808e..04f2738 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,15 @@ fn strip_key(line: &str) -> &str { strings[1] } +fn move_selection(term: &mut Terminal, selection: &mut Point, style: Cell, amount: isize) { + term.printline(selection.x, selection.y, " "); + + let y = selection.y as isize; + selection.y = (y + amount) as usize; + + term.printline_with_cell(selection.x, selection.y, "->", style); +} + fn main() { let mut options = Vec::new(); let stdin = io::stdin(); @@ -65,20 +74,12 @@ fn main() { } 'j' => { if selection.y < options.len() + 1 { - term.printline(selection.x, selection.y, " "); - - selection.y = selection.y + 1; - - term.printline_with_cell(selection.x, selection.y, "->", knockout_cell); + move_selection(&mut term, &mut selection, knockout_cell, 1) } } 'k' => { if selection.y > 2 { - term.printline(selection.x, selection.y, " "); - - selection.y = selection.y - 1; - - term.printline_with_cell(selection.x, selection.y, "->", knockout_cell); + move_selection(&mut term, &mut selection, knockout_cell, -1) } } '\x0D' => { |