1
//! The selectable list shared by the tabs that show one plist per row.
2

            
3
use ratatui::layout::Constraint;
4
use scripting::nomiscript::Value;
5

            
6
use tvui::RowWindow;
7

            
8
use crate::tabs::fetch::Fetch;
9
use crate::tabs::plist::parse_rows;
10
use crate::tabs::seq::Seq;
11

            
12
/// A row type a [`SelectList`] can fetch, select by key and draw.
13
pub 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
23
    fn has_key(&self, key: &str) -> bool {
25
23
        self.key() == key
26
23
    }
27
}
28

            
29
/// The rows of one list reply with the selection on one of them.
30
pub 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

            
40
impl<R: SelectRow> SelectList<R> {
41
    #[must_use]
42
10319
    pub fn new() -> Self {
43
10319
        Self {
44
10319
            state: Fetch::Idle,
45
10319
            rows: Vec::new(),
46
10319
            follow: None,
47
10319
            selected: 0,
48
10319
            window: RowWindow::default(),
49
10319
            seq: Seq::default(),
50
10319
        }
51
10319
    }
52

            
53
    #[must_use]
54
464
    pub fn is_idle(&self) -> bool {
55
464
        self.state == Fetch::Idle
56
464
    }
57

            
58
    #[must_use]
59
135
    pub fn any_loading(&self) -> bool {
60
135
        matches!(self.state, Fetch::Loading { .. })
61
135
    }
62

            
63
197
    pub fn set_loading(&mut self, id: i64) {
64
197
        self.state = Fetch::Loading { id };
65
197
    }
66

            
67
    /// Back to `Idle` so the next tab switch re-fetches; the rows stay visible.
68
64
    pub fn reset(&mut self) {
69
64
        self.state = Fetch::Idle;
70
64
    }
71

            
72
    /// Parse a list reply; the selection stays on its row.
73
177
    pub fn on_reply(&mut self, wire: &str) {
74
177
        let target = self
75
177
            .follow
76
177
            .take()
77
167
            .or_else(|| self.selected_key().map(str::to_string));
78
177
        match parse_rows(wire, R::parse) {
79
            Ok(listed) => {
80
158
                self.rows = listed;
81
158
                self.state = Fetch::Loaded(());
82
            }
83
19
            Err(message) => self.state = Fetch::Error(message),
84
        }
85
177
        let found = target.and_then(|key| self.rows.iter().position(|row| row.has_key(&key)));
86
177
        self.selected = found
87
177
            .unwrap_or(self.selected)
88
177
            .min(self.rows.len().saturating_sub(1));
89
177
    }
90

            
91
    #[must_use]
92
182
    pub fn rows(&self) -> &[R] {
93
182
        &self.rows
94
182
    }
95

            
96
    #[must_use]
97
266
    pub fn selected_row(&self) -> Option<&R> {
98
266
        self.rows.get(self.selected)
99
266
    }
100

            
101
    #[must_use]
102
224
    pub fn selected_key(&self) -> Option<&str> {
103
224
        self.selected_row().map(R::key)
104
224
    }
105

            
106
122
    pub fn select_next(&mut self) {
107
122
        if self.selected + 1 < self.rows.len() {
108
18
            self.selected += 1;
109
104
        }
110
122
    }
111

            
112
5
    pub fn select_prev(&mut self) {
113
5
        self.selected = self.selected.saturating_sub(1);
114
5
    }
115

            
116
    /// Move the selection a view's height down or up.
117
3
    pub fn select_view(&mut self, down: bool) {
118
3
        self.selected = self.window.paged(self.selected, self.rows.len(), down);
119
3
    }
120

            
121
5
    pub fn select_first(&mut self) {
122
5
        self.selected = 0;
123
5
    }
124

            
125
    /// Selects row `index` when the list holds it.
126
25
    pub fn select_row(&mut self, index: usize) {
127
25
        if index < self.rows.len() {
128
24
            self.selected = index;
129
1
        }
130
25
    }
131

            
132
7
    pub fn select_last(&mut self) {
133
7
        self.selected = self.rows.len().saturating_sub(1);
134
7
    }
135
}
136

            
137
impl<R: SelectRow> Default for SelectList<R> {
138
    fn default() -> Self {
139
        Self::new()
140
    }
141
}
142

            
143
#[cfg(test)]
144
mod tests;