rnk 0.15.31

A React-like declarative terminal UI framework for Rust, inspired by Ink
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
494
495
//! MultiSelect component for selecting multiple items
//!
//! A multi-selection component similar to Ink's ink-multi-select that handles
//! keyboard navigation and selection internally.

use crate::components::navigation::{
    NavigationConfig, calculate_visible_range, handle_list_navigation,
};
use crate::components::{Box as TinkBox, Text};
use crate::core::{Color, Element, FlexDirection};
use crate::hooks::{use_input, use_signal};

/// A selectable item in the MultiSelect
#[derive(Debug, Clone)]
pub struct MultiSelectItem<T: Clone> {
    /// Display label for the item
    pub label: String,
    /// Value associated with the item
    pub value: T,
    /// Whether this item is initially selected
    pub selected: bool,
}

impl<T: Clone> MultiSelectItem<T> {
    /// Create a new multi-select item
    pub fn new(label: impl Into<String>, value: T) -> Self {
        Self {
            label: label.into(),
            value,
            selected: false,
        }
    }

    /// Create a new item that is initially selected
    pub fn selected(label: impl Into<String>, value: T) -> Self {
        Self {
            label: label.into(),
            value,
            selected: true,
        }
    }

    /// Set whether this item is selected
    pub fn with_selected(mut self, selected: bool) -> Self {
        self.selected = selected;
        self
    }
}

/// Configuration for MultiSelect appearance
#[derive(Debug, Clone)]
pub struct MultiSelectStyle {
    /// Color for the highlighted item
    pub highlight_color: Option<Color>,
    /// Background color for the highlighted item
    pub highlight_bg: Option<Color>,
    /// Whether to show the highlighted item in bold
    pub highlight_bold: bool,
    /// Indicator shown before the highlighted item
    pub indicator: String,
    /// Indicator shown before non-highlighted items
    pub indicator_padding: String,
    /// Checkbox for selected items
    pub checkbox_selected: String,
    /// Checkbox for unselected items
    pub checkbox_unselected: String,
    /// Color for selected items
    pub selected_color: Option<Color>,
    /// Color for unselected items
    pub item_color: Option<Color>,
}

impl Default for MultiSelectStyle {
    fn default() -> Self {
        Self {
            highlight_color: Some(Color::Cyan),
            highlight_bg: None,
            highlight_bold: true,
            indicator: "".to_string(),
            indicator_padding: "  ".to_string(),
            checkbox_selected: "".to_string(),
            checkbox_unselected: "".to_string(),
            selected_color: Some(Color::Green),
            item_color: None,
        }
    }
}

impl MultiSelectStyle {
    /// Create a new style with default values
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the highlight color
    pub fn highlight_color(mut self, color: Color) -> Self {
        self.highlight_color = Some(color);
        self
    }

    /// Set the highlight background color
    pub fn highlight_bg(mut self, color: Color) -> Self {
        self.highlight_bg = Some(color);
        self
    }

    /// Set whether to bold the highlighted item
    pub fn highlight_bold(mut self, bold: bool) -> Self {
        self.highlight_bold = bold;
        self
    }

    /// Set the indicator string
    pub fn indicator(mut self, indicator: impl Into<String>) -> Self {
        let ind = indicator.into();
        self.indicator_padding = " ".repeat(ind.chars().count());
        self.indicator = ind;
        self
    }

    /// Set the checkbox characters
    pub fn checkboxes(
        mut self,
        selected: impl Into<String>,
        unselected: impl Into<String>,
    ) -> Self {
        self.checkbox_selected = selected.into();
        self.checkbox_unselected = unselected.into();
        self
    }

    /// Set the selected item color
    pub fn selected_color(mut self, color: Color) -> Self {
        self.selected_color = Some(color);
        self
    }

    /// Set the item color
    pub fn item_color(mut self, color: Color) -> Self {
        self.item_color = Some(color);
        self
    }
}

/// MultiSelect component with built-in keyboard navigation
///
/// # Example
///
/// ```ignore
/// use rnk::components::{MultiSelect, MultiSelectItem};
///
/// let items = vec![
///     MultiSelectItem::new("Option 1", 1),
///     MultiSelectItem::selected("Option 2", 2), // Initially selected
///     MultiSelectItem::new("Option 3", 3),
/// ];
///
/// MultiSelect::new(items).into_element()
/// ```
pub struct MultiSelect<T: Clone + 'static> {
    /// Items to display
    items: Vec<MultiSelectItem<T>>,
    /// Currently highlighted index
    highlighted: usize,
    /// Maximum number of visible items (None = show all)
    limit: Option<usize>,
    /// Style configuration
    style: MultiSelectStyle,
    /// Whether the component is focused (receives input)
    is_focused: bool,
    /// Whether to enable vim-style navigation (j/k)
    vim_navigation: bool,
    /// Whether to enable number key shortcuts (1-9)
    number_shortcuts: bool,
}

impl<T: Clone + 'static> MultiSelect<T> {
    /// Create a new MultiSelect with items
    pub fn new(items: Vec<MultiSelectItem<T>>) -> Self {
        Self {
            items,
            highlighted: 0,
            limit: None,
            style: MultiSelectStyle::default(),
            is_focused: true,
            vim_navigation: true,
            number_shortcuts: false,
        }
    }

    /// Create from an iterator of items
    pub fn from_items<I>(iter: I) -> Self
    where
        I: IntoIterator<Item = MultiSelectItem<T>>,
    {
        Self::new(iter.into_iter().collect())
    }

    /// Set the initially highlighted index
    pub fn highlighted(mut self, index: usize) -> Self {
        self.highlighted = index.min(self.items.len().saturating_sub(1));
        self
    }

    /// Set the maximum number of visible items
    pub fn limit(mut self, limit: usize) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Set the style configuration
    pub fn style(mut self, style: MultiSelectStyle) -> Self {
        self.style = style;
        self
    }

    /// Set whether the component is focused
    pub fn focused(mut self, focused: bool) -> Self {
        self.is_focused = focused;
        self
    }

    /// Enable or disable vim-style navigation (j/k keys)
    pub fn vim_navigation(mut self, enabled: bool) -> Self {
        self.vim_navigation = enabled;
        self
    }

    /// Enable or disable number key shortcuts (1-9)
    pub fn number_shortcuts(mut self, enabled: bool) -> Self {
        self.number_shortcuts = enabled;
        self
    }

    /// Set highlight color
    pub fn highlight_color(mut self, color: Color) -> Self {
        self.style.highlight_color = Some(color);
        self
    }

    /// Set indicator string
    pub fn indicator(mut self, indicator: impl Into<String>) -> Self {
        self.style = self.style.indicator(indicator);
        self
    }

    /// Get the number of items
    pub fn len(&self) -> usize {
        self.items.len()
    }

    /// Check if the list is empty
    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }

    /// Get the currently selected items
    pub fn selected_items(&self) -> Vec<&MultiSelectItem<T>> {
        self.items.iter().filter(|item| item.selected).collect()
    }

    /// Get the currently selected values
    pub fn selected_values(&self) -> Vec<&T> {
        self.items
            .iter()
            .filter(|item| item.selected)
            .map(|item| &item.value)
            .collect()
    }

    /// Convert to element with internal state management
    pub fn into_element(self) -> Element {
        if self.items.is_empty() {
            return TinkBox::new().into_element();
        }

        let initial_highlighted = self.highlighted;
        let initial_selections: Vec<bool> = self.items.iter().map(|i| i.selected).collect();
        let items = self.items.clone();
        let limit = self.limit;
        let style = self.style.clone();
        let is_focused = self.is_focused;
        let vim_navigation = self.vim_navigation;
        let number_shortcuts = self.number_shortcuts;

        // Create signals for state
        let highlighted_signal = use_signal(|| initial_highlighted);
        let selections_signal = use_signal(|| initial_selections);

        // Set up input handling if focused
        if is_focused {
            let items_len = items.len();
            let highlighted_for_input = highlighted_signal.clone();
            let selections_for_input = selections_signal.clone();

            use_input(move |input, key| {
                let current = highlighted_for_input.get();

                // Handle navigation
                let config = NavigationConfig::new()
                    .vim_navigation(vim_navigation)
                    .number_shortcuts(number_shortcuts);
                let result = handle_list_navigation(current, items_len, input, *key, &config);
                if result.is_moved() {
                    let new_pos = result.unwrap_or(current);
                    if new_pos != current {
                        highlighted_for_input.set(new_pos);
                    }
                }

                // Toggle selection with Space
                if key.space {
                    selections_for_input.update(|selections| {
                        if let Some(selected) = selections.get_mut(current) {
                            *selected = !*selected;
                        }
                    });
                }

                // Select all with 'a'
                if input == "a" && key.ctrl {
                    selections_for_input.update(|selections| {
                        for selected in selections.iter_mut() {
                            *selected = true;
                        }
                    });
                }

                // Deselect all with 'd' (Ctrl+D)
                if input == "d" && key.ctrl {
                    selections_for_input.update(|selections| {
                        for selected in selections.iter_mut() {
                            *selected = false;
                        }
                    });
                }
            });
        }

        // Render the list
        render_multi_select_list(&items, highlighted_signal, selections_signal, limit, &style)
    }
}

/// Render the multi-select list as an Element
fn render_multi_select_list<T: Clone + 'static>(
    items: &[MultiSelectItem<T>],
    highlighted_signal: crate::hooks::Signal<usize>,
    selections_signal: crate::hooks::Signal<Vec<bool>>,
    limit: Option<usize>,
    style: &MultiSelectStyle,
) -> Element {
    let highlighted = highlighted_signal.get();
    let selections = selections_signal.get();
    let total_items = items.len();

    // Calculate visible range
    let (start, end) = calculate_visible_range(highlighted, total_items, limit);

    let mut container = TinkBox::new().flex_direction(FlexDirection::Column);

    for (idx, item) in items.iter().enumerate().skip(start).take(end - start) {
        let is_highlighted = idx == highlighted;
        let is_selected = selections.get(idx).copied().unwrap_or(item.selected);

        let prefix = if is_highlighted {
            &style.indicator
        } else {
            &style.indicator_padding
        };

        let checkbox = if is_selected {
            &style.checkbox_selected
        } else {
            &style.checkbox_unselected
        };

        let label = format!("{}{}{}", prefix, checkbox, item.label);
        let mut text = Text::new(&label);

        if is_highlighted {
            if let Some(color) = style.highlight_color {
                text = text.color(color);
            }
            if let Some(bg) = style.highlight_bg {
                text = text.background(bg);
            }
            if style.highlight_bold {
                text = text.bold();
            }
        } else if is_selected {
            if let Some(color) = style.selected_color {
                text = text.color(color);
            }
        } else if let Some(color) = style.item_color {
            text = text.color(color);
        }

        container = container.child(text.into_element());
    }

    container.into_element()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_multi_select_item_creation() {
        let item = MultiSelectItem::new("Test", 42);
        assert_eq!(item.label, "Test");
        assert_eq!(item.value, 42);
        assert!(!item.selected);
    }

    #[test]
    fn test_multi_select_item_selected() {
        let item = MultiSelectItem::selected("Test", 42);
        assert!(item.selected);
    }

    #[test]
    fn test_multi_select_creation() {
        let items = vec![
            MultiSelectItem::new("One", 1),
            MultiSelectItem::selected("Two", 2),
            MultiSelectItem::new("Three", 3),
        ];
        let select = MultiSelect::new(items);
        assert_eq!(select.len(), 3);
        assert!(!select.is_empty());
    }

    #[test]
    fn test_multi_select_empty() {
        let select: MultiSelect<i32> = MultiSelect::new(vec![]);
        assert!(select.is_empty());
        assert_eq!(select.len(), 0);
    }

    #[test]
    fn test_multi_select_selected_values() {
        let items = vec![
            MultiSelectItem::new("One", 1),
            MultiSelectItem::selected("Two", 2),
            MultiSelectItem::selected("Three", 3),
        ];
        let select = MultiSelect::new(items);
        let selected = select.selected_values();
        assert_eq!(selected.len(), 2);
        assert!(selected.contains(&&2));
        assert!(selected.contains(&&3));
    }

    #[test]
    fn test_multi_select_style() {
        let style = MultiSelectStyle::new()
            .highlight_color(Color::Green)
            .indicator("> ")
            .checkboxes("[x]", "[ ]");

        assert_eq!(style.highlight_color, Some(Color::Green));
        assert_eq!(style.indicator, "> ");
        assert_eq!(style.checkbox_selected, "[x]");
        assert_eq!(style.checkbox_unselected, "[ ]");
    }

    #[test]
    fn test_multi_select_builder_chain() {
        let items = vec![MultiSelectItem::new("Test", 1)];
        let select = MultiSelect::new(items)
            .highlighted(0)
            .limit(5)
            .focused(true)
            .vim_navigation(true)
            .highlight_color(Color::Yellow)
            .indicator("");

        assert_eq!(select.highlighted, 0);
        assert_eq!(select.limit, Some(5));
        assert!(select.is_focused);
        assert!(select.vim_navigation);
    }

    #[test]
    fn test_multi_select_from_items() {
        let items = vec![
            MultiSelectItem::new("A", 'a'),
            MultiSelectItem::new("B", 'b'),
        ];
        let select = MultiSelect::from_items(items);
        assert_eq!(select.len(), 2);
    }
}