|
1 | 1 | //! The UI-specific parts of [`super::TuiMonitor`]
|
2 |
| -use alloc::{string::ToString, sync::Arc, vec::Vec}; |
| 2 | +use alloc::{sync::Arc, vec::Vec}; |
3 | 3 | use core::cmp::{max, min};
|
4 | 4 | use std::sync::RwLock;
|
5 | 5 |
|
@@ -27,13 +27,27 @@ pub struct TuiUi {
|
27 | 27 | enhanced_graphics: bool,
|
28 | 28 | show_logs: bool,
|
29 | 29 | client_idx: usize,
|
30 |
| - clients: usize, |
| 30 | + clients: Vec<usize>, |
31 | 31 | charts_tab_idx: usize,
|
32 | 32 | graph_data: Vec<(f64, f64)>,
|
33 | 33 |
|
34 | 34 | pub should_quit: bool,
|
35 | 35 | }
|
36 | 36 |
|
| 37 | +fn next_larger(sorted: &[usize], value: usize) -> Option<usize> { |
| 38 | + if let Some(index) = sorted.iter().position(|x| *x > value) { |
| 39 | + return Some(index); |
| 40 | + } |
| 41 | + None |
| 42 | +} |
| 43 | + |
| 44 | +fn next_smaller(sorted: &[usize], value: usize) -> Option<usize> { |
| 45 | + if let Some(index) = sorted.iter().rposition(|x| *x < value) { |
| 46 | + return Some(index); |
| 47 | + } |
| 48 | + None |
| 49 | +} |
| 50 | + |
37 | 51 | impl TuiUi {
|
38 | 52 | #[must_use]
|
39 | 53 | pub fn new(title: String, enhanced_graphics: bool) -> Self {
|
@@ -68,20 +82,31 @@ impl TuiUi {
|
68 | 82 | }
|
69 | 83 |
|
70 | 84 | pub fn on_right(&mut self) {
|
71 |
| - if self.clients > 0 && self.client_idx < self.clients - 1 { |
72 |
| - self.client_idx += 1; |
| 85 | + if let Some(idx) = next_larger(&self.clients, self.client_idx) { |
| 86 | + self.client_idx = self.clients[idx]; |
73 | 87 | }
|
74 | 88 | }
|
75 | 89 |
|
76 | 90 | pub fn on_left(&mut self) {
|
77 |
| - if self.client_idx > 0 { |
78 |
| - self.client_idx -= 1; |
| 91 | + if let Some(idx) = next_smaller(&self.clients, self.client_idx) { |
| 92 | + self.client_idx = self.clients[idx]; |
79 | 93 | }
|
80 | 94 | }
|
81 | 95 |
|
82 | 96 | /// Draw the current TUI context
|
83 | 97 | pub fn draw(&mut self, f: &mut Frame, app: &Arc<RwLock<TuiContext>>) {
|
84 |
| - self.clients = app.read().unwrap().clients_num; |
| 98 | + let new = app.read().unwrap().clients_num; |
| 99 | + if new != self.clients.len() { |
| 100 | + // get the list of all clients |
| 101 | + let mut all: Vec<usize> = app.read().unwrap().clients.keys().copied().collect(); |
| 102 | + all.sort_unstable(); |
| 103 | + |
| 104 | + // move the current client to the first one |
| 105 | + self.client_idx = all[0]; |
| 106 | + |
| 107 | + // move the vector holding all clients ids |
| 108 | + self.clients = all; |
| 109 | + } |
85 | 110 |
|
86 | 111 | let body = Layout::default()
|
87 | 112 | .constraints(if self.show_logs {
|
@@ -417,7 +442,7 @@ impl TuiUi {
|
417 | 442 | let empty_geometry: ItemGeometry = ItemGeometry::new();
|
418 | 443 | let item_geometry: &ItemGeometry = if is_overall {
|
419 | 444 | &tui_context.total_item_geometry
|
420 |
| - } else if self.clients == 0 { |
| 445 | + } else if self.clients.is_empty() { |
421 | 446 | &empty_geometry
|
422 | 447 | } else {
|
423 | 448 | let clients = &tui_context.clients;
|
@@ -494,7 +519,7 @@ impl TuiUi {
|
494 | 519 | let empty_timing: ProcessTiming = ProcessTiming::new();
|
495 | 520 | let tup: (Duration, &ProcessTiming) = if is_overall {
|
496 | 521 | (tui_context.start_time, &tui_context.total_process_timing)
|
497 |
| - } else if self.clients == 0 { |
| 522 | + } else if self.clients.is_empty() { |
498 | 523 | (current_time(), &empty_timing)
|
499 | 524 | } else {
|
500 | 525 | let clients = &tui_context.clients;
|
@@ -570,11 +595,9 @@ impl TuiUi {
|
570 | 595 | vec![
|
571 | 596 | Row::new(vec![
|
572 | 597 | Cell::from(Span::raw("clients")),
|
573 |
| - Cell::from(Span::raw(format!("{}", self.clients))), |
| 598 | + Cell::from(Span::raw(format!("{}", self.clients.len()))), |
574 | 599 | Cell::from(Span::raw("total execs")),
|
575 | 600 | Cell::from(Span::raw(format_big_number(app.total_execs))),
|
576 |
| - Cell::from(Span::raw("map density")), |
577 |
| - Cell::from(Span::raw(app.total_map_density.to_string())), |
578 | 601 | ]),
|
579 | 602 | Row::new(vec![
|
580 | 603 | Cell::from(Span::raw("solutions")),
|
@@ -683,14 +706,6 @@ impl TuiUi {
|
683 | 706 | .map_or(0, |x| x.executions),
|
684 | 707 | ))),
|
685 | 708 | ]),
|
686 |
| - Row::new(vec![ |
687 |
| - Cell::from(Span::raw("map density")), |
688 |
| - Cell::from(Span::raw( |
689 |
| - app.clients |
690 |
| - .get(&self.client_idx) |
691 |
| - .map_or("0%".to_string(), |x| x.map_density.to_string()), |
692 |
| - )), |
693 |
| - ]), |
694 | 709 | ]
|
695 | 710 | };
|
696 | 711 |
|
|
0 commit comments