Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 49 additions & 41 deletions tokio-console/src/view/controls.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::view::{self, bold};

use once_cell::sync::OnceCell;
use tui::{
layout,
text::{Span, Spans, Text},
Expand All @@ -16,7 +15,7 @@ pub(crate) struct Controls {

impl Controls {
pub(in crate::view) fn new(
view_controls: &Vec<ControlDisplay>,
view_controls: &'static [ControlDisplay],
area: &layout::Rect,
styles: &view::Styles,
) -> Self {
Expand All @@ -32,28 +31,39 @@ impl Controls {

let controls_count: usize = spans_controls.len();
for (idx, spans) in spans_controls.into_iter().enumerate() {
// If this is the first item on this line - or first item on the
// first line, then always include it - even if it goes beyond the
// line width, not much we can do anyway.
if idx == 0 || current_line.width() == 0 {
current_line.0.extend(spans.0);
continue;
}

// Include the width of our separator in the current item if we
// aren't placing the last item. This is the separator after the
// new element.
let needed_trailing_separator_width = if idx == controls_count + 1 {
separator.width()
} else {
0
};

let total_width = current_line.width()
+ separator.width()
+ spans.width()
+ needed_trailing_separator_width;

// If the current item fits on this line, append it.
// Otherwise, append only the separator - we accounted for its
// width in the previous loop iteration - and then create a new
// line for the current item.
if total_width <= area.width as usize {
current_line.0.push(separator.clone());
current_line.0.extend(spans.0);
} else {
let needed_trailing_separator_width = if idx == controls_count + 1 {
separator.width()
} else {
0
};

let total_width = current_line.width()
+ separator.width()
+ spans.width()
+ needed_trailing_separator_width;

if total_width <= area.width as usize {
current_line.0.push(separator.clone());
current_line.0.extend(spans.0);
} else {
current_line.0.push(separator.clone());
lines.push(spans);
current_line = lines.last_mut().expect("This vector is never empty");
}
current_line.0.push(separator.clone());
lines.push(spans);
current_line = lines.last_mut().expect("This vector is never empty");
}
}

Expand Down Expand Up @@ -99,17 +109,7 @@ pub(crate) struct KeyDisplay {
}

impl ControlDisplay {
pub(crate) fn new_simple(action: &'static str, key: &'static str) -> Self {
ControlDisplay {
action,
keys: vec![KeyDisplay {
base: key,
utf8: None,
}],
}
}

pub fn to_spans(&self, styles: &view::Styles) -> Spans<'static> {
pub(crate) fn to_spans(&self, styles: &view::Styles) -> Spans<'static> {
let mut spans = Vec::new();

spans.push(Span::from(self.action));
Expand All @@ -129,13 +129,21 @@ impl ControlDisplay {
}

/// Returns a list of controls which are available in all views.
pub(crate) fn universal_controls() -> &'static Vec<ControlDisplay> {
static UNIVERSAL_CONTROLS: OnceCell<Vec<ControlDisplay>> = OnceCell::new();

UNIVERSAL_CONTROLS.get_or_init(|| {
vec![
ControlDisplay::new_simple("toggle pause", "space"),
ControlDisplay::new_simple("quit", "q"),
]
})
const fn universal_controls() -> &'static [ControlDisplay] {
&[
ControlDisplay {
action: "toggle pause",
keys: &[KeyDisplay {
base: "space",
utf8: None,
}],
},
ControlDisplay {
action: "quit",
keys: &[KeyDisplay {
base: "q",
utf8: None,
}],
},
]
}
10 changes: 4 additions & 6 deletions tokio-console/src/view/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,17 @@ impl ResourceView {
}
}

fn view_controls() -> &'static Vec<ControlDisplay> {
fn view_controls() -> &'static [ControlDisplay] {
static VIEW_CONTROLS: OnceCell<Vec<ControlDisplay>> = OnceCell::new();

VIEW_CONTROLS.get_or_init(|| {
let mut resource_controls = vec![ControlDisplay {
let resource_controls = &[ControlDisplay {
action: "return to task list",
keys: vec![KeyDisplay {
keys: &[KeyDisplay {
base: "esc",
utf8: Some("\u{238B} esc"),
}],
}];
resource_controls.extend(async_ops::view_controls().to_owned());

resource_controls
[resource_controls, async_ops::view_controls()].concat()
})
}
101 changes: 57 additions & 44 deletions tokio-console/src/view/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::{
controls::{ControlDisplay, KeyDisplay},
},
};
use once_cell::sync::OnceCell;
use tui::{layout, widgets::TableState};

use std::cell::RefCell;
Expand Down Expand Up @@ -195,47 +194,61 @@ where
}
}

pub(crate) fn view_controls() -> &'static Vec<ControlDisplay> {
static VIEW_CONTROLS: OnceCell<Vec<ControlDisplay>> = OnceCell::new();

VIEW_CONTROLS.get_or_init(|| {
vec![
ControlDisplay {
action: "select column (sort)",
keys: vec![
KeyDisplay {
base: "left, right",
utf8: Some("\u{2190}\u{2192}"),
},
KeyDisplay {
base: "h, l",
utf8: None,
},
],
},
ControlDisplay {
action: "scroll",
keys: vec![
KeyDisplay {
base: "up, down",
utf8: Some("\u{2191}\u{2193}"),
},
KeyDisplay {
base: "k, j",
utf8: None,
},
],
},
ControlDisplay {
action: "view details",
keys: vec![KeyDisplay {
base: "enter",
utf8: Some("\u{21B5}"),
}],
},
ControlDisplay::new_simple("invert sort (highest/lowest)", "i"),
ControlDisplay::new_simple("scroll to top", "gg"),
ControlDisplay::new_simple("scroll to bottom", "G"),
]
})
pub(crate) const fn view_controls() -> &'static [ControlDisplay] {
&[
ControlDisplay {
action: "select column (sort)",
keys: &[
KeyDisplay {
base: "left, right",
utf8: Some("\u{2190}\u{2192}"),
},
KeyDisplay {
base: "h, l",
utf8: None,
},
],
},
ControlDisplay {
action: "scroll",
keys: &[
KeyDisplay {
base: "up, down",
utf8: Some("\u{2191}\u{2193}"),
},
KeyDisplay {
base: "k, j",
utf8: None,
},
],
},
ControlDisplay {
action: "view details",
keys: &[KeyDisplay {
base: "enter",
utf8: Some("\u{21B5}"),
}],
},
ControlDisplay {
action: "invert sort (highest/lowest)",
keys: &[KeyDisplay {
base: "i",
utf8: None,
}],
},
ControlDisplay {
action: "scroll to top",
keys: &[KeyDisplay {
base: "gg",
utf8: None,
}],
},
ControlDisplay {
action: "scroll to bottom",
keys: &[KeyDisplay {
base: "G",
utf8: None,
}],
},
]
}
21 changes: 8 additions & 13 deletions tokio-console/src/view/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::{
durations::Durations,
},
};
use once_cell::sync::OnceCell;
use std::{
cell::RefCell,
cmp,
Expand Down Expand Up @@ -254,16 +253,12 @@ impl TaskView {
}
}

fn view_controls() -> &'static Vec<ControlDisplay> {
static VIEW_CONTROLS: OnceCell<Vec<ControlDisplay>> = OnceCell::new();

VIEW_CONTROLS.get_or_init(|| {
vec![ControlDisplay {
action: "return to task list",
keys: vec![KeyDisplay {
base: "esc",
utf8: Some("\u{238B} esc"),
}],
}]
})
const fn view_controls() -> &'static [ControlDisplay] {
&[ControlDisplay {
action: "return to task list",
keys: &[KeyDisplay {
base: "esc",
utf8: Some("\u{238B} esc"),
}],
}]
}