Fix crash on changing selection with no ports

No ports always means no selection
This commit is contained in:
John Doty 2023-04-30 07:33:06 -07:00
parent 815ee5e86e
commit b85e3fa9a6

View file

@ -471,25 +471,37 @@ impl UI {
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.");
if i == 0 { if i == 0 {
self.ports.len() - 1 Some(self.ports.len() - 1)
} else { } else {
i - 1 Some(i - 1)
}
}
None => {
if self.ports.len() > 0 {
Some(0)
} else {
None
} }
} }
None => 0,
}; };
self.selection.select(Some(index)); self.selection.select(index);
} }
KeyEvent { code: KeyCode::Down, .. } KeyEvent { code: KeyCode::Down, .. }
| KeyEvent { code: KeyCode::Char('j'), .. } => { | 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."); assert!(self.ports.len() > 0, "We must have ports because we have a selection.");
(i + 1) % self.ports.len() Some((i + 1) % self.ports.len())
}
None => {
if self.ports.len() > 0 {
Some(0)
} else {
None
}
} }
None => 0,
}; };
self.selection.select(Some(index)); self.selection.select(index);
} }
KeyEvent { code: KeyCode::Enter, .. } => { KeyEvent { code: KeyCode::Enter, .. } => {
if let Some(p) = self.get_selected_port() { if let Some(p) = self.get_selected_port() {