|
| 1 | +use crate::view::{self, bold}; |
| 2 | + |
| 3 | +use ratatui::{ |
| 4 | + layout, |
| 5 | + text::{Span, Spans, Text}, |
| 6 | + widgets::{Paragraph, Widget}, |
| 7 | +}; |
| 8 | + |
| 9 | +/// A list of controls which are available in all views. |
| 10 | +const UNIVERSAL_CONTROLS: &[ControlDisplay] = &[ |
| 11 | + ControlDisplay { |
| 12 | + action: "toggle pause", |
| 13 | + keys: &[KeyDisplay { |
| 14 | + base: "space", |
| 15 | + utf8: None, |
| 16 | + }], |
| 17 | + }, |
| 18 | + ControlDisplay { |
| 19 | + action: "quit", |
| 20 | + keys: &[KeyDisplay { |
| 21 | + base: "q", |
| 22 | + utf8: None, |
| 23 | + }], |
| 24 | + }, |
| 25 | +]; |
| 26 | + |
| 27 | +/// Construct a widget to display the controls available to the user in the |
| 28 | +/// current view. |
| 29 | +pub(crate) struct Controls { |
| 30 | + paragraph: Paragraph<'static>, |
| 31 | + height: u16, |
| 32 | +} |
| 33 | + |
| 34 | +impl Controls { |
| 35 | + pub(in crate::view) fn new( |
| 36 | + view_controls: &'static [ControlDisplay], |
| 37 | + area: &layout::Rect, |
| 38 | + styles: &view::Styles, |
| 39 | + ) -> Self { |
| 40 | + let mut spans_controls = Vec::with_capacity(view_controls.len() + UNIVERSAL_CONTROLS.len()); |
| 41 | + spans_controls.extend(view_controls.iter().map(|c| c.to_spans(styles))); |
| 42 | + spans_controls.extend(UNIVERSAL_CONTROLS.iter().map(|c| c.to_spans(styles))); |
| 43 | + |
| 44 | + let mut lines = vec![Spans::from(vec![Span::from("controls: ")])]; |
| 45 | + let mut current_line = lines.last_mut().expect("This vector is never empty"); |
| 46 | + let separator = Span::from(", "); |
| 47 | + |
| 48 | + let controls_count: usize = spans_controls.len(); |
| 49 | + for (idx, spans) in spans_controls.into_iter().enumerate() { |
| 50 | + // If this is the first item on this line - or first item on the |
| 51 | + // first line, then always include it - even if it goes beyond the |
| 52 | + // line width, not much we can do anyway. |
| 53 | + if idx == 0 || current_line.width() == 0 { |
| 54 | + current_line.0.extend(spans.0); |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + // Include the width of our separator in the current item if we |
| 59 | + // aren't placing the last item. This is the separator after the |
| 60 | + // new element. |
| 61 | + let needed_trailing_separator_width = if idx == controls_count + 1 { |
| 62 | + separator.width() |
| 63 | + } else { |
| 64 | + 0 |
| 65 | + }; |
| 66 | + |
| 67 | + let total_width = current_line.width() |
| 68 | + + separator.width() |
| 69 | + + spans.width() |
| 70 | + + needed_trailing_separator_width; |
| 71 | + |
| 72 | + // If the current item fits on this line, append it. |
| 73 | + // Otherwise, append only the separator - we accounted for its |
| 74 | + // width in the previous loop iteration - and then create a new |
| 75 | + // line for the current item. |
| 76 | + if total_width <= area.width as usize { |
| 77 | + current_line.0.push(separator.clone()); |
| 78 | + current_line.0.extend(spans.0); |
| 79 | + } else { |
| 80 | + current_line.0.push(separator.clone()); |
| 81 | + lines.push(spans); |
| 82 | + current_line = lines.last_mut().expect("This vector is never empty"); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + let height = lines.len() as u16; |
| 87 | + let text = Text::from(lines); |
| 88 | + |
| 89 | + Self { |
| 90 | + paragraph: Paragraph::new(text), |
| 91 | + height, |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + pub(crate) fn height(&self) -> u16 { |
| 96 | + self.height |
| 97 | + } |
| 98 | + |
| 99 | + pub(crate) fn into_widget(self) -> impl Widget { |
| 100 | + self.paragraph |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +/// Construct span to display a control. |
| 105 | +/// |
| 106 | +/// A control is made up of an action and one or more keys that will trigger |
| 107 | +/// that action. |
| 108 | +#[derive(Clone)] |
| 109 | +pub(crate) struct ControlDisplay { |
| 110 | + pub(crate) action: &'static str, |
| 111 | + pub(crate) keys: &'static [KeyDisplay], |
| 112 | +} |
| 113 | + |
| 114 | +/// A key or keys which will be displayed to the user as part of spans |
| 115 | +/// constructed by `ControlDisplay`. |
| 116 | +/// |
| 117 | +/// The `base` description of the key should be ASCII only, more advanced |
| 118 | +/// descriptions can be supplied for that key in the `utf8` field. This |
| 119 | +/// allows the application to pick the best one to display at runtime |
| 120 | +/// based on the termainal being used. |
| 121 | +#[derive(Clone)] |
| 122 | +pub(crate) struct KeyDisplay { |
| 123 | + pub(crate) base: &'static str, |
| 124 | + pub(crate) utf8: Option<&'static str>, |
| 125 | +} |
| 126 | + |
| 127 | +impl ControlDisplay { |
| 128 | + pub(crate) fn to_spans(&self, styles: &view::Styles) -> Spans<'static> { |
| 129 | + let mut spans = Vec::new(); |
| 130 | + |
| 131 | + spans.push(Span::from(self.action)); |
| 132 | + spans.push(Span::from(" = ")); |
| 133 | + for (idx, key_display) in self.keys.iter().enumerate() { |
| 134 | + if idx > 0 { |
| 135 | + spans.push(Span::from(" or ")) |
| 136 | + } |
| 137 | + spans.push(bold(match key_display.utf8 { |
| 138 | + Some(utf8) => styles.if_utf8(utf8, key_display.base), |
| 139 | + None => key_display.base, |
| 140 | + })); |
| 141 | + } |
| 142 | + |
| 143 | + Spans::from(spans) |
| 144 | + } |
| 145 | +} |
0 commit comments