Skip to main content

tui/tabs/
select_list.rs

1//! The selectable list shared by the tabs that show one plist per row.
2
3use ratatui::layout::Constraint;
4use scripting::nomiscript::Value;
5
6use tvui::RowWindow;
7
8use crate::tabs::fetch::Fetch;
9use crate::tabs::plist::parse_rows;
10use crate::tabs::seq::Seq;
11
12/// A row type a [`SelectList`] can fetch, select by key and draw.
13pub trait SelectRow: Sized {
14    const HEADERS: &'static [&'static str];
15    const WIDTHS: &'static [Constraint];
16
17    fn parse(plist: &Value) -> Result<Self, String>;
18
19    /// Identity of the row, kept selected across reloads.
20    fn key(&self) -> &str;
21
22    fn cells(&self) -> Vec<String>;
23
24    fn has_key(&self, key: &str) -> bool {
25        self.key() == key
26    }
27}
28
29/// The rows of one list reply with the selection on one of them.
30pub struct SelectList<R> {
31    pub state: Fetch<()>,
32    rows: Vec<R>,
33    /// Key the selection moves to after the next reply.
34    pub follow: Option<String>,
35    pub selected: usize,
36    pub window: RowWindow,
37    pub seq: Seq,
38}
39
40impl<R: SelectRow> SelectList<R> {
41    #[must_use]
42    pub fn new() -> Self {
43        Self {
44            state: Fetch::Idle,
45            rows: Vec::new(),
46            follow: None,
47            selected: 0,
48            window: RowWindow::default(),
49            seq: Seq::default(),
50        }
51    }
52
53    #[must_use]
54    pub fn is_idle(&self) -> bool {
55        self.state == Fetch::Idle
56    }
57
58    #[must_use]
59    pub fn any_loading(&self) -> bool {
60        matches!(self.state, Fetch::Loading { .. })
61    }
62
63    pub fn set_loading(&mut self, id: i64) {
64        self.state = Fetch::Loading { id };
65    }
66
67    /// Back to `Idle` so the next tab switch re-fetches; the rows stay visible.
68    pub fn reset(&mut self) {
69        self.state = Fetch::Idle;
70    }
71
72    /// Parse a list reply; the selection stays on its row.
73    pub fn on_reply(&mut self, wire: &str) {
74        let target = self
75            .follow
76            .take()
77            .or_else(|| self.selected_key().map(str::to_string));
78        match parse_rows(wire, R::parse) {
79            Ok(listed) => {
80                self.rows = listed;
81                self.state = Fetch::Loaded(());
82            }
83            Err(message) => self.state = Fetch::Error(message),
84        }
85        let found = target.and_then(|key| self.rows.iter().position(|row| row.has_key(&key)));
86        self.selected = found
87            .unwrap_or(self.selected)
88            .min(self.rows.len().saturating_sub(1));
89    }
90
91    #[must_use]
92    pub fn rows(&self) -> &[R] {
93        &self.rows
94    }
95
96    #[must_use]
97    pub fn selected_row(&self) -> Option<&R> {
98        self.rows.get(self.selected)
99    }
100
101    #[must_use]
102    pub fn selected_key(&self) -> Option<&str> {
103        self.selected_row().map(R::key)
104    }
105
106    pub fn select_next(&mut self) {
107        if self.selected + 1 < self.rows.len() {
108            self.selected += 1;
109        }
110    }
111
112    pub fn select_prev(&mut self) {
113        self.selected = self.selected.saturating_sub(1);
114    }
115
116    /// Move the selection a view's height down or up.
117    pub fn select_view(&mut self, down: bool) {
118        self.selected = self.window.paged(self.selected, self.rows.len(), down);
119    }
120
121    pub fn select_first(&mut self) {
122        self.selected = 0;
123    }
124
125    /// Selects row `index` when the list holds it.
126    pub fn select_row(&mut self, index: usize) {
127        if index < self.rows.len() {
128            self.selected = index;
129        }
130    }
131
132    pub fn select_last(&mut self) {
133        self.selected = self.rows.len().saturating_sub(1);
134    }
135}
136
137impl<R: SelectRow> Default for SelectList<R> {
138    fn default() -> Self {
139        Self::new()
140    }
141}
142
143#[cfg(test)]
144mod tests;