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