From 833339151a14866ccf499dd57f1849766c7c3555 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 6 Aug 2016 05:38:02 -0400 Subject: Make selection arrow movement code DRY Remove the duplication of these lines for the `j` and `k` commands by extracting them to a function. --- src/main.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'src') 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' => { -- cgit v1.2.3