From 85dc2f07076c4536bf133ea5cee4e92355e40a7c Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Sat, 18 Feb 2023 11:27:23 -0800 Subject: [PATCH 1/4] ui: Switch j and k keys to match vi -- j means down, k means up --- src/client/ui.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client/ui.rs b/src/client/ui.rs index a6da4e6..8957ad0 100644 --- a/src/client/ui.rs +++ b/src/client/ui.rs @@ -383,7 +383,7 @@ impl UI { } } KeyEvent { code: KeyCode::Up, .. } - | KeyEvent { code: KeyCode::Char('j'), .. } => { + | KeyEvent { code: KeyCode::Char('k'), .. } => { let index = match self.selection.selected() { Some(i) => { if i == 0 { @@ -397,7 +397,7 @@ impl UI { self.selection.select(Some(index)); } KeyEvent { code: KeyCode::Down, .. } - | KeyEvent { code: KeyCode::Char('k'), .. } => { + | KeyEvent { code: KeyCode::Char('j'), .. } => { let index = match self.selection.selected() { Some(i) => { assert!(self.ports.len() > 0, "We must have ports because we have a selection."); From 3060032a958f7c787111b06b056a03137c8f26d7 Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Sat, 18 Feb 2023 11:27:58 -0800 Subject: [PATCH 2/4] ui: Wrap around list when at the endoflife Pressing "up"/"k" at the top of the UI will now go to the end of the list. And same in reverse. --- src/client/ui.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/client/ui.rs b/src/client/ui.rs index 8957ad0..610baff 100644 --- a/src/client/ui.rs +++ b/src/client/ui.rs @@ -387,7 +387,7 @@ impl UI { let index = match self.selection.selected() { Some(i) => { if i == 0 { - 0 + self.ports.len() - 1 } else { i - 1 } @@ -400,8 +400,12 @@ impl UI { | KeyEvent { code: KeyCode::Char('j'), .. } => { let index = match self.selection.selected() { Some(i) => { - assert!(self.ports.len() > 0, "We must have ports because we have a selection."); - (i + 1).min(self.ports.len() - 1) + let max = self.ports.len() - 1; + if i >= max { + 0 + } else { + i + 1 + } } None => 0, }; From f174f364a4058091f42ca6e5e1bea598c64edd80 Mon Sep 17 00:00:00 2001 From: John Doty Date: Tue, 21 Mar 2023 19:45:17 -0700 Subject: [PATCH 3/4] Re-add assertions and simplify wrapping --- src/client/ui.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/client/ui.rs b/src/client/ui.rs index 610baff..63bc79b 100644 --- a/src/client/ui.rs +++ b/src/client/ui.rs @@ -386,6 +386,7 @@ impl UI { | KeyEvent { code: KeyCode::Char('k'), .. } => { let index = match self.selection.selected() { Some(i) => { + assert!(self.ports.len() > 0, "We must have ports because we have a selection."); if i == 0 { self.ports.len() - 1 } else { @@ -400,12 +401,8 @@ impl UI { | KeyEvent { code: KeyCode::Char('j'), .. } => { let index = match self.selection.selected() { Some(i) => { - let max = self.ports.len() - 1; - if i >= max { - 0 - } else { - i + 1 - } + assert!(self.ports.len() > 0, "We must have ports because we have a selection."); + (i + 1) % self.ports.len() } None => 0, }; From 7410ec51434e0d929ed2874da14dd151dcf99756 Mon Sep 17 00:00:00 2001 From: John Doty Date: Tue, 21 Mar 2023 22:48:06 -0700 Subject: [PATCH 4/4] Add tests for keypresses --- src/client/ui.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/src/client/ui.rs b/src/client/ui.rs index 63bc79b..3a1f3d4 100644 --- a/src/client/ui.rs +++ b/src/client/ui.rs @@ -573,6 +573,73 @@ mod tests { assert_eq!(ui.ports.len(), 2); 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); }