aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2025-05-18 09:55:21 +0200
committerToni Uhlig <matzeton@googlemail.com>2025-05-20 09:55:21 +0200
commit9809ae4ea05ac7764ab3cf206bb9503c53e2d132 (patch)
treedf1e90f95e338616a64d7ca1a920d88fe1d1dccb
parent97387d0f1c163b43975e7dfe67fc6bbc67482e31 (diff)
rs-simple: improved readability and stability
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r--examples/rs-simple/src/main.rs51
1 files changed, 32 insertions, 19 deletions
diff --git a/examples/rs-simple/src/main.rs b/examples/rs-simple/src/main.rs
index 9efcb770a..7a19cf8c8 100644
--- a/examples/rs-simple/src/main.rs
+++ b/examples/rs-simple/src/main.rs
@@ -427,17 +427,21 @@ async fn main() {
let mut old_selected: Option<FlowKey> = None;
loop {
- let flows: Vec<(FlowKey, (FlowExpiration, FlowValue))> = flow_cache_rx.iter().map(|(k, v)| (k.as_ref().clone(), v.clone())).collect();
+ let flows: Vec<(FlowKey, (FlowExpiration, FlowValue))> = flow_cache_rx.iter().map(|(k, v)| (k.as_ref().clone(), v.clone()))
+ .take(1024)
+ .collect();
let mut table_selected = match table_state.selected() {
Some(mut table_index) => {
- if flows.len() > 0 && table_index >= flows.len() {
- flows.len() - 1
+ if table_index >= flows.len() {
+ flows.len().saturating_sub(1)
} else {
if let Some(ref old_flow_key_selected) = old_selected {
if let Some(old_index) = flows.iter().position(|x| x.0 == *old_flow_key_selected) {
if old_index != table_index {
table_index = old_index;
}
+ } else {
+ old_selected = Some(flows.get(table_index).unwrap().0.clone());
}
}
table_index
@@ -451,46 +455,55 @@ async fn main() {
Some(KeyCode::Char('q')) => break,
Some(KeyCode::Up) => {
table_selected = match table_selected {
- _ if flows.len() == 0 => 0,
- i if i == 0 => flows.len() - 1,
+ i if i == 0 => flows.len().saturating_sub(1),
i => i - 1,
};
- old_selected = Some(flows.get(table_selected).unwrap().0.clone());
+ if let Some(new_selected) = flows.get(table_selected) {
+ old_selected = Some(new_selected.0.clone());
+ }
},
Some(KeyCode::Down) => {
table_selected = match table_selected {
- i if flows.len() == 0 || i >= flows.len() - 1 => 0,
+ i if i >= flows.len().saturating_sub(1) => 0,
i => i + 1,
};
- old_selected = Some(flows.get(table_selected).unwrap().0.clone());
+ if let Some(new_selected) = flows.get(table_selected) {
+ old_selected = Some(new_selected.0.clone());
+ }
},
Some(KeyCode::PageUp) => {
table_selected = match table_selected {
- _ if flows.len() == 0 => 0,
- i if i == 0 => flows.len() - 1,
+ i if i == 0 => flows.len().saturating_sub(1),
i if i < 25 => 0,
i => i - 25,
};
- old_selected = Some(flows.get(table_selected).unwrap().0.clone());
+ if let Some(new_selected) = flows.get(table_selected) {
+ old_selected = Some(new_selected.0.clone());
+ }
},
Some(KeyCode::PageDown) => {
table_selected = match table_selected {
- i if flows.len() == 0 || i >= flows.len() - 1 => 0,
- i if flows.len() < 25 || i >= flows.len() - 25 => flows.len() - 1,
+ i if i >= flows.len().saturating_sub(1) => 0,
+ i if i >= flows.len().saturating_sub(25) => flows.len().saturating_sub(1),
i => i + 25,
};
- old_selected = Some(flows.get(table_selected).unwrap().0.clone());
+ if let Some(new_selected) = flows.get(table_selected) {
+ old_selected = Some(new_selected.0.clone());
+ }
},
Some(KeyCode::Home) => {
table_selected = 0;
- old_selected = Some(flows.get(table_selected).unwrap().0.clone());
+ if let Some(new_selected) = flows.get(table_selected) {
+ old_selected = Some(new_selected.0.clone());
+ }
},
Some(KeyCode::End) => {
table_selected = match table_selected {
- _ if flows.len() == 0 => 0,
- _ => flows.len() - 1,
+ _ => flows.len().saturating_sub(1),
};
- old_selected = Some(flows.get(table_selected).unwrap().0.clone());
+ if let Some(new_selected) = flows.get(table_selected) {
+ old_selected = Some(new_selected.0.clone());
+ }
},
Some(_) => (),
None => ()
@@ -518,7 +531,7 @@ async fn main() {
}
fn read_keypress() -> Option<KeyCode> {
- if event::poll(Duration::from_millis(500)).unwrap() {
+ if event::poll(Duration::from_millis(1000)).unwrap() {
if let event::Event::Key(KeyEvent { code, .. }) = event::read().unwrap() {
return Some(code);
}