ratagrid 0.1.0

A mouse-first interactive data grid for Ratatui
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
use ratatui_core::style::Style;
use std::{
    any::Any,
    cmp::Ordering,
    collections::{BTreeMap, BTreeSet},
    rc::Rc,
};

type RowKey<T> = Box<dyn Fn(&T) -> Box<dyn Any>>;
type KeyMatches<T> = Box<dyn Fn(&T, &dyn Any) -> bool>;

struct RowIdentity<T> {
    key: RowKey<T>,
    matches: KeyMatches<T>,
}

type Formatter<T> = Box<dyn Fn(&T) -> String>;
type CellStyle<T> = Box<dyn Fn(&T) -> Style>;
type RowFilter<T> = Box<dyn Fn(&T) -> bool>;

type Comparator<T> = Box<dyn Fn(&T, &T) -> Ordering>;

/// A typed column. Sorting compares records, independently of their display text.
pub struct Column<T> {
    pub(crate) title: String,
    pub(crate) width: u16,
    pub(crate) format: Formatter<T>,
    pub(crate) compare: Option<Comparator<T>>,
    pub(crate) external_sortable: bool,
    pub(crate) cell_style: Option<CellStyle<T>>,
}

impl<T> Column<T> {
    /// Create a column with a width in terminal cells (minimum four).
    pub fn new(
        title: impl Into<String>,
        width: u16,
        format: impl Fn(&T) -> String + 'static,
    ) -> Self {
        Self {
            title: title.into(),
            width: width.max(4),
            format: Box::new(format),
            compare: None,
            external_sortable: false,
            cell_style: None,
        }
    }

    /// Style data cells using the underlying record, independently of display text.
    /// This patches the base cell style; selection, hover, flashes and the cell cursor apply afterward.
    /// The callback runs only for visible cells and should remain inexpensive.
    pub fn cell_style(mut self, style: impl Fn(&T) -> Style + 'static) -> Self {
        self.cell_style = Some(Box::new(style));
        self
    }

    /// Enable sorting with a comparator over the underlying records.
    pub fn sortable(mut self, compare: impl Fn(&T, &T) -> Ordering + 'static) -> Self {
        self.compare = Some(Box::new(compare));
        self
    }

    /// Enable source-side sorting in an externally paginated grid, without an
    /// unused local comparator. Owned grids treat this column as unsortable.
    pub fn sortable_external(mut self) -> Self {
        self.external_sortable = true;
        self
    }
}

/// The direction of an active sort.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SortDirection {
    Ascending,
    Descending,
}

/// Single-column sort state. `None` restores insertion order.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Sort {
    pub column: usize,
    pub direction: SortDirection,
}

/// Framework-independent ordering and selection over owned records.
///
/// Selection refers to an insertion index, so sorting never selects a different record.
pub struct GridModel<T> {
    pub(crate) columns: Vec<Column<T>>,
    rows: Vec<T>,
    pub(crate) order: Vec<usize>,
    selected: Option<usize>,
    selected_position: Option<usize>,
    sort: Option<Sort>,
    identity: Option<RowIdentity<T>>,
    selected_id: Option<Box<dyn Any>>,
    marked: BTreeSet<usize>,
    marked_ids: BTreeMap<usize, Box<dyn Any>>,
    filter: Option<RowFilter<T>>,
    search: String,
}

impl<T> GridModel<T> {
    pub fn new(columns: Vec<Column<T>>, rows: Vec<T>) -> Self {
        let order = (0..rows.len()).collect();
        Self {
            columns,
            rows,
            order,
            selected: None,
            selected_position: None,
            sort: None,
            identity: None,
            selected_id: None,
            marked: BTreeSet::new(),
            marked_ids: BTreeMap::new(),
            filter: None,
            search: String::new(),
        }
    }

    /// Preserve selection across replacements using an application-owned row ID.
    /// IDs must be stable and unique. A missing or duplicated selected ID clears
    /// selection. Without this callback, replacements clear selection as before.
    /// Only active/marked IDs are retained; no per-row ID index is allocated.
    pub fn with_row_id<K: Eq + 'static>(mut self, row_id: impl Fn(&T) -> K + 'static) -> Self {
        let row_id = Rc::new(row_id);
        let extract = Rc::clone(&row_id);
        self.identity = Some(RowIdentity {
            key: Box::new(move |row| Box::new(extract(row))),
            matches: Box::new(move |row, key| {
                key.downcast_ref::<K>()
                    .is_some_and(|key| row_id(row) == *key)
            }),
        });
        self.refresh_selected_id();
        self.refresh_marked_ids();
        self
    }

    fn refresh_selected_id(&mut self) {
        self.selected_id = self.selected.and_then(|index| {
            self.identity
                .as_ref()
                .map(|identity| (identity.key)(&self.rows[index]))
        });
    }

    fn restore_selection(&mut self) {
        self.selected = None;
        self.selected_position = None;
        if let (Some(identity), Some(key)) = (&self.identity, &self.selected_id) {
            let mut matches = self
                .rows
                .iter()
                .enumerate()
                .filter(|(_, row)| (identity.matches)(row, key.as_ref()));
            if let Some((index, _)) = matches.next()
                && matches.next().is_none()
            {
                self.selected = Some(index);
            }
        }
        if self.selected.is_none() {
            self.selected_id = None;
        }
        self.marked.clear();
        if let Some(identity) = &self.identity {
            for key in self.marked_ids.values() {
                let mut matches = self
                    .rows
                    .iter()
                    .enumerate()
                    .filter(|(_, row)| (identity.matches)(row, key.as_ref()));
                if let Some((index, _)) = matches.next()
                    && matches.next().is_none()
                {
                    self.marked.insert(index);
                }
            }
        }
        self.refresh_marked_ids();
    }

    fn refresh_marked_ids(&mut self) {
        self.marked_ids.clear();
        if let Some(identity) = &self.identity {
            for &index in &self.marked {
                self.marked_ids
                    .insert(index, (identity.key)(&self.rows[index]));
            }
        }
    }

    /// Resident insertion indices explicitly marked for bulk actions, in insertion order.
    /// The active cursor row is independent of this set.
    pub fn selected_indices(&self) -> impl Iterator<Item = usize> + '_ {
        self.marked.iter().copied()
    }

    pub fn is_row_selected(&self, index: usize) -> bool {
        self.marked.contains(&index)
    }

    /// Replace the bulk selection. Invalid indices are ignored.
    pub fn set_selected_indices(&mut self, indices: impl IntoIterator<Item = usize>) -> bool {
        let marked = indices
            .into_iter()
            .filter(|&i| i < self.rows.len())
            .collect();
        if self.marked == marked {
            return false;
        }
        self.marked = marked;
        self.refresh_marked_ids();
        true
    }

    pub fn toggle_row_selection(&mut self, index: usize) -> bool {
        if index >= self.rows.len() {
            return false;
        }
        if !self.marked.remove(&index) {
            self.marked.insert(index);
        }
        if let Some(identity) = &self.identity {
            if self.marked.contains(&index) {
                self.marked_ids
                    .insert(index, (identity.key)(&self.rows[index]));
            } else {
                self.marked_ids.remove(&index);
            }
        }
        true
    }

    pub fn clear_row_selection(&mut self) -> bool {
        let changed = !self.marked.is_empty();
        self.marked.clear();
        self.marked_ids.clear();
        changed
    }

    /// Number of rows after filtering, independently of resident storage size.
    pub fn visible_len(&self) -> usize {
        self.order.len()
    }

    /// Filter owned rows using actual record values. Search and this predicate combine.
    pub fn set_filter(&mut self, filter: impl Fn(&T) -> bool + 'static) {
        self.filter = Some(Box::new(filter));
        self.apply_sort();
    }

    pub fn clear_filter(&mut self) {
        self.filter = None;
        self.apply_sort();
    }

    /// Case-insensitive substring search across all defined formatted columns,
    /// removing terminal controls as rendering and copying do.
    /// Formatting is evaluated for all resident rows when the query changes.
    pub fn set_search(&mut self, query: impl Into<String>) {
        self.search = query
            .into()
            .chars()
            .filter(|c| !c.is_control())
            .collect::<String>()
            .to_lowercase();
        self.apply_sort();
    }

    fn matches_filter(&self, row: &T) -> bool {
        self.filter.as_ref().is_none_or(|filter| filter(row))
            && (self.search.is_empty()
                || self.columns.iter().any(|column| {
                    (column.format)(row)
                        .chars()
                        .filter(|c| !c.is_control())
                        .collect::<String>()
                        .to_lowercase()
                        .contains(&self.search)
                }))
    }

    /// Discard resident records while retaining a keyed selection during a fetch.
    pub(crate) fn clear_rows_for_request(&mut self) {
        self.rows.clear();
        self.order.clear();
        self.selected = None;
        self.selected_position = None;
        self.marked.clear();
    }

    pub fn rows(&self) -> &[T] {
        &self.rows
    }
    pub fn sort(&self) -> Option<Sort> {
        self.sort
    }
    pub fn selected(&self) -> Option<&T> {
        self.selected.map(|i| &self.rows[i])
    }
    pub fn selected_index(&self) -> Option<usize> {
        self.selected
    }
    pub fn selected_position(&self) -> Option<usize> {
        self.selected_position
    }
    pub fn row_at(&self, position: usize) -> Option<&T> {
        self.index_at(position).map(|i| &self.rows[i])
    }
    /// Resolve a sorted position to the resident record's insertion index.
    pub fn index_at(&self, position: usize) -> Option<usize> {
        self.order.get(position).copied()
    }

    /// Edit one resident record, maintaining stable sorting and selection.
    /// Returns false for an invalid index without calling `update`.
    /// Only the edited row is repositioned: logarithmic comparisons, with up to
    /// linear index lookup/movement. The full dataset is never re-sorted.
    pub fn update_row(&mut self, index: usize, update: impl FnOnce(&mut T)) -> bool {
        if !self.update_row_in_place(index, update) {
            return false;
        }
        if self.filter.is_some() || !self.search.is_empty() {
            self.apply_sort();
            return true;
        }
        let Some(sort) = self.sort else {
            return true;
        };
        let old = if self.selected == Some(index) {
            self.selected_position.expect("selected position")
        } else {
            self.order
                .iter()
                .position(|&i| i == index)
                .expect("resident row")
        };
        let compare = self.columns[sort.column]
            .compare
            .as_ref()
            .expect("sortable column");
        let ordered = |a: usize, b: usize| {
            let result = compare(&self.rows[a], &self.rows[b]);
            let result = match sort.direction {
                SortDirection::Ascending => result,
                SortDirection::Descending => result.reverse(),
            };
            result.then(a.cmp(&b))
        };
        if (old == 0 || ordered(self.order[old - 1], index) != Ordering::Greater)
            && (old + 1 == self.order.len()
                || ordered(index, self.order[old + 1]) != Ordering::Greater)
        {
            return true;
        }
        self.order.remove(old);
        let new = self
            .order
            .partition_point(|&i| ordered(i, index) == Ordering::Less);
        self.order.insert(new, index);
        self.selected_position = self.selected_position.map(|position| {
            if self.selected == Some(index) {
                new
            } else if old < position && new >= position {
                position - 1
            } else if new <= position && old > position {
                position + 1
            } else {
                position
            }
        });
        true
    }

    pub(crate) fn update_row_in_place(
        &mut self,
        index: usize,
        update: impl FnOnce(&mut T),
    ) -> bool {
        let Some(row) = self.rows.get_mut(index) else {
            return false;
        };
        update(row);
        if self.selected == Some(index) {
            self.refresh_selected_id();
        }
        if self.marked.contains(&index)
            && let Some(identity) = &self.identity
        {
            self.marked_ids
                .insert(index, (identity.key)(&self.rows[index]));
        }
        true
    }
    pub fn select(&mut self, position: usize) -> bool {
        let Some(&index) = self.order.get(position) else {
            return false;
        };
        let changed = self.selected != Some(index);
        self.selected = Some(index);
        self.selected_position = Some(position);
        self.refresh_selected_id();
        changed
    }

    /// Cycle ascending → descending → insertion order. Returns false for unsortable columns.
    pub fn toggle_sort(&mut self, column: usize) -> bool {
        if self
            .columns
            .get(column)
            .and_then(|c| c.compare.as_ref())
            .is_none()
        {
            return false;
        }
        if !self.request_sort(column) {
            return false;
        }
        self.apply_sort();
        true
    }

    /// Update sort metadata without sorting a server-provided page locally.
    pub(crate) fn request_sort(&mut self, column: usize) -> bool {
        if !self
            .columns
            .get(column)
            .is_some_and(|c| c.compare.is_some() || c.external_sortable)
        {
            return false;
        }
        self.sort = match self.sort {
            Some(Sort {
                column: c,
                direction: SortDirection::Ascending,
            }) if c == column => Some(Sort {
                column,
                direction: SortDirection::Descending,
            }),
            Some(Sort {
                column: c,
                direction: SortDirection::Descending,
            }) if c == column => None,
            _ => Some(Sort {
                column,
                direction: SortDirection::Ascending,
            }),
        };
        true
    }

    /// Replace records, preserving the active sort and selection when a unique
    /// matching ID is configured with [`Self::with_row_id`].
    pub fn replace_rows(&mut self, rows: Vec<T>) {
        self.rows = rows;
        self.restore_selection();
        self.apply_sort();
    }

    pub(crate) fn replace_rows_in_order(&mut self, rows: Vec<T>) {
        self.rows = rows;
        self.restore_selection();
        self.order = (0..self.rows.len()).collect();
        self.selected_position = self.selected;
    }

    fn apply_sort(&mut self) {
        self.order = (0..self.rows.len())
            .filter(|&index| self.matches_filter(&self.rows[index]))
            .collect();
        if let Some(sort) = self.sort {
            let compare = self.columns[sort.column]
                .compare
                .as_ref()
                .expect("sortable column");
            let rows = &self.rows;
            self.order.sort_by(|&a, &b| {
                let result = compare(&rows[a], &rows[b]);
                match sort.direction {
                    SortDirection::Ascending => result,
                    SortDirection::Descending => result.reverse(),
                }
            });
        }
        self.selected_position = self
            .selected
            .and_then(|i| self.order.iter().position(|&r| r == i));
    }
}