Compare commits

..

No commits in common. "7410ec51434e0d929ed2874da14dd151dcf99756" and "5fb0410eee6fb307e40b8efcb4730bf31ea905c3" have entirely different histories.

View file

@ -383,12 +383,11 @@ impl UI {
} }
} }
KeyEvent { code: KeyCode::Up, .. } KeyEvent { code: KeyCode::Up, .. }
| KeyEvent { code: KeyCode::Char('k'), .. } => { | KeyEvent { code: KeyCode::Char('j'), .. } => {
let index = match self.selection.selected() { let index = match self.selection.selected() {
Some(i) => { Some(i) => {
assert!(self.ports.len() > 0, "We must have ports because we have a selection.");
if i == 0 { if i == 0 {
self.ports.len() - 1 0
} else { } else {
i - 1 i - 1
} }
@ -398,11 +397,11 @@ impl UI {
self.selection.select(Some(index)); self.selection.select(Some(index));
} }
KeyEvent { code: KeyCode::Down, .. } KeyEvent { code: KeyCode::Down, .. }
| KeyEvent { code: KeyCode::Char('j'), .. } => { | KeyEvent { code: KeyCode::Char('k'), .. } => {
let index = match self.selection.selected() { let index = match self.selection.selected() {
Some(i) => { Some(i) => {
assert!(self.ports.len() > 0, "We must have ports because we have a selection."); assert!(self.ports.len() > 0, "We must have ports because we have a selection.");
(i + 1) % self.ports.len() (i + 1).min(self.ports.len() - 1)
} }
None => 0, None => 0,
}; };
@ -573,73 +572,6 @@ mod tests {
assert_eq!(ui.ports.len(), 2); assert_eq!(ui.ports.len(), 2);
assert_matches!(ui.selection.selected(), Some(0)); assert_matches!(ui.selection.selected(), Some(0));
// Move selection up => wraps around the length
ui.handle_console_event(Some(Ok(Event::Key(KeyEvent::new(
KeyCode::Up,
KeyModifiers::empty(),
)))));
assert_matches!(ui.selection.selected(), Some(1));
ui.handle_console_event(Some(Ok(Event::Key(KeyEvent::new(
KeyCode::Up,
KeyModifiers::empty(),
)))));
assert_matches!(ui.selection.selected(), Some(0));
ui.handle_console_event(Some(Ok(Event::Key(KeyEvent::new(
KeyCode::Up,
KeyModifiers::empty(),
)))));
assert_matches!(ui.selection.selected(), Some(1));
// Move selection down => wraps around the length
ui.handle_console_event(Some(Ok(Event::Key(KeyEvent::new(
KeyCode::Down,
KeyModifiers::empty(),
)))));
assert_matches!(ui.selection.selected(), Some(0));
ui.handle_console_event(Some(Ok(Event::Key(KeyEvent::new(
KeyCode::Down,
KeyModifiers::empty(),
)))));
assert_matches!(ui.selection.selected(), Some(1));
ui.handle_console_event(Some(Ok(Event::Key(KeyEvent::new(
KeyCode::Down,
KeyModifiers::empty(),
)))));
assert_matches!(ui.selection.selected(), Some(0));
// J and K move the correct direction
ui.handle_internal_event(Some(UIEvent::Ports(vec![
PortDesc {
port: 8080,
desc: "my-service".to_string(),
},
PortDesc {
port: 8081,
desc: "my-service".to_string(),
},
PortDesc {
port: 8082,
desc: "my-service".to_string(),
},
])));
assert_eq!(ui.ports.len(), 3);
// J is down
ui.selection.select(Some(1));
ui.handle_console_event(Some(Ok(Event::Key(KeyEvent::new(
KeyCode::Char('j'),
KeyModifiers::empty(),
)))));
assert_matches!(ui.selection.selected(), Some(2));
// K is up
ui.selection.select(Some(1));
ui.handle_console_event(Some(Ok(Event::Key(KeyEvent::new(
KeyCode::Char('k'),
KeyModifiers::empty(),
)))));
assert_matches!(ui.selection.selected(), Some(0));
drop(sender); drop(sender);
} }