louietui 1.0.0

An agentic-first TUI framework with complete ontology for agent discoverability
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
//! Interactive select list widget.
//!
//! A scrollable list with single or multi-select, keyboard navigation,
//! and optional filtering. Fully discoverable by agent systems.

use crate::core::buffer::Buffer;
use crate::core::rect::Rect;
use crate::core::style::Style;
use crate::ontology::{
    ActionParam, ActionParamType, AgentAction, AgentCapability, Discoverable, PropertySchema,
    PropertyType, SemanticRole, WidgetSchema,
};
use crate::widget::block::Block;
use crate::widget::{StatefulWidget, Widget};

/// Selection mode for the list.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SelectMode {
    Single,
    Multi,
}

/// An item in a [`SelectList`].
#[derive(Debug, Clone)]
pub struct SelectItem {
    pub label: String,
    pub value: String,
}

impl SelectItem {
    pub fn new(label: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            label: label.into(),
            value: value.into(),
        }
    }

    /// Convenience: label and value are the same.
    pub fn simple(s: impl Into<String>) -> Self {
        let s = s.into();
        Self {
            label: s.clone(),
            value: s,
        }
    }
}

/// An interactive select list widget.
#[derive(Debug, Clone)]
pub struct SelectList {
    block: Option<Block>,
    items: Vec<SelectItem>,
    mode: SelectMode,
    style: Style,
    highlight_style: Style,
    selected_marker: &'static str,
    unselected_marker: &'static str,
}

impl SelectList {
    pub fn new(items: Vec<SelectItem>) -> Self {
        Self {
            block: None,
            items,
            mode: SelectMode::Single,
            style: Style::default(),
            highlight_style: Style::default().reversed(),
            selected_marker: "[x] ",
            unselected_marker: "[ ] ",
        }
    }

    pub fn block(mut self, block: Block) -> Self {
        self.block = Some(block);
        self
    }

    pub fn mode(mut self, mode: SelectMode) -> Self {
        self.mode = mode;
        self
    }

    pub fn style(mut self, style: Style) -> Self {
        self.style = style;
        self
    }

    pub fn highlight_style(mut self, style: Style) -> Self {
        self.highlight_style = style;
        self
    }

    pub fn markers(mut self, selected: &'static str, unselected: &'static str) -> Self {
        self.selected_marker = selected;
        self.unselected_marker = unselected;
        self
    }
}

/// State for a [`SelectList`].
#[derive(Debug, Clone, Default)]
pub struct SelectListState {
    /// Currently highlighted index.
    pub cursor: usize,
    /// Set of selected indices.
    pub selected: Vec<usize>,
    /// Vertical scroll offset.
    pub scroll: usize,
    /// Optional filter text. Only items containing this substring are shown.
    pub filter: String,
}

impl SelectListState {
    pub fn new() -> Self {
        Self::default()
    }

    /// Move cursor up within the visible item count.
    pub fn move_up(&mut self, visible_count: usize) {
        if visible_count == 0 {
            return;
        }
        if self.cursor > 0 {
            self.cursor -= 1;
        } else {
            self.cursor = visible_count.saturating_sub(1);
        }
    }

    /// Move cursor down within the visible item count.
    pub fn move_down(&mut self, visible_count: usize) {
        if visible_count == 0 {
            return;
        }
        if self.cursor + 1 < visible_count {
            self.cursor += 1;
        } else {
            self.cursor = 0;
        }
    }

    /// Toggle selection of the item at the given original index.
    pub fn toggle_select(&mut self, original_idx: usize, mode: SelectMode) {
        match mode {
            SelectMode::Single => {
                self.selected.clear();
                self.selected.push(original_idx);
            }
            SelectMode::Multi => {
                if let Some(pos) = self.selected.iter().position(|&i| i == original_idx) {
                    self.selected.remove(pos);
                } else {
                    self.selected.push(original_idx);
                }
            }
        }
    }

    /// Returns the selected indices.
    pub fn selected_indices(&self) -> &[usize] {
        &self.selected
    }
}

impl StatefulWidget for SelectList {
    type State = SelectListState;

    fn render(self, area: Rect, buf: &mut Buffer, state: &mut SelectListState) {
        if area.is_empty() {
            return;
        }

        buf.set_style(area, self.style);

        let inner = if let Some(block) = self.block {
            let inner = block.inner(area);
            block.render(area, buf);
            inner
        } else {
            area
        };

        if inner.is_empty() {
            return;
        }

        // Filter items
        let filter_lower = state.filter.to_lowercase();
        let visible_items: Vec<(usize, &SelectItem)> = self
            .items
            .iter()
            .enumerate()
            .filter(|(_, item)| {
                if state.filter.is_empty() {
                    true
                } else {
                    item.label.to_lowercase().contains(&filter_lower)
                }
            })
            .collect();

        let visible_count = visible_items.len();

        // Clamp cursor
        if visible_count > 0 {
            state.cursor = state.cursor.min(visible_count - 1);
        }

        // Adjust scroll to keep cursor visible
        let rows = inner.height as usize;
        if state.cursor < state.scroll {
            state.scroll = state.cursor;
        } else if state.cursor >= state.scroll + rows {
            state.scroll = state.cursor - rows + 1;
        }

        for row_offset in 0..rows {
            let vi = state.scroll + row_offset;
            if vi >= visible_count {
                break;
            }

            let (original_idx, item) = visible_items[vi];
            let y = inner.y + row_offset as u16;
            let is_highlighted = vi == state.cursor;
            let is_selected = state.selected.contains(&original_idx);

            let marker = if self.mode == SelectMode::Multi {
                if is_selected {
                    self.selected_marker
                } else {
                    self.unselected_marker
                }
            } else if is_selected {
                "> "
            } else {
                "  "
            };

            let max_w = inner.width as usize;
            let text = format!("{marker}{}", item.label);
            let truncated: String = text.chars().take(max_w).collect();

            let row_style = if is_highlighted {
                self.highlight_style
            } else {
                self.style
            };

            buf.set_string(inner.x, y, &truncated, row_style);
        }
    }
}

impl Discoverable for SelectList {
    fn schema() -> WidgetSchema {
        WidgetSchema {
            name: "SelectList".into(),
            description:
                "An interactive select list supporting single or multi-select with filtering."
                    .into(),
            default_role: SemanticRole::Input,
            properties: vec![
                PropertySchema {
                    name: "mode".into(),
                    description: "Selection mode: Single or Multi.".into(),
                    property_type: PropertyType::String,
                    required: false,
                    default_value: Some(serde_json::json!("Single")),
                    constraints: vec![],
                },
                PropertySchema {
                    name: "items".into(),
                    description: "List of selectable items.".into(),
                    property_type: PropertyType::Array(Box::new(PropertyType::String)),
                    required: true,
                    default_value: None,
                    constraints: vec![],
                },
            ],
            actions: vec![
                AgentAction {
                    name: "select_index".into(),
                    description: "Select an item by index.".into(),
                    params: vec![ActionParam {
                        name: "index".into(),
                        description: "Zero-based index of the item to select.".into(),
                        param_type: ActionParamType::Integer,
                        required: true,
                        default_value: None,
                    }],
                    returns: None,
                    mutates: true,
                    idempotent: true,
                    shortcut: None,
                },
                AgentAction {
                    name: "set_filter".into(),
                    description: "Set the filter string for fuzzy matching.".into(),
                    params: vec![ActionParam {
                        name: "filter".into(),
                        description: "Substring to filter items by.".into(),
                        param_type: ActionParamType::String,
                        required: true,
                        default_value: None,
                    }],
                    returns: None,
                    mutates: true,
                    idempotent: true,
                    shortcut: None,
                },
                AgentAction {
                    name: "get_selected".into(),
                    description: "Get the currently selected item values.".into(),
                    params: vec![],
                    returns: Some("Array of selected item values.".into()),
                    mutates: false,
                    idempotent: true,
                    shortcut: None,
                },
            ],

            usage_hint: Some("SelectList::new(items).mode(SelectMode::Multi)".into()),
            tags: vec![
                "select".into(),
                "list".into(),
                "input".into(),
                "menu".into(),
            ],
        }
    }

    fn capabilities(&self) -> Vec<AgentCapability> {
        vec![
            AgentCapability::Focusable,
            AgentCapability::Selectable {
                multi_select: self.mode == SelectMode::Multi,
                item_count: self.items.len(),
            },
            AgentCapability::Scrollable {
                vertical: true,
                horizontal: false,
            },
            AgentCapability::Searchable,
            AgentCapability::HasKeyBindings {
                bindings: vec![
                    ("Up/k".into(), "Move cursor up".into()),
                    ("Down/j".into(), "Move cursor down".into()),
                    ("Space/Enter".into(), "Toggle/select item".into()),
                    ("Home".into(), "Jump to first item".into()),
                    ("End".into(), "Jump to last item".into()),
                ],
            },
        ]
    }

    fn actions(&self) -> Vec<AgentAction> {
        vec![
            AgentAction {
                name: "select_index".into(),
                description: "Select an item by index.".into(),
                params: vec![ActionParam {
                    name: "index".into(),
                    description: "Zero-based index of the item to select.".into(),
                    param_type: ActionParamType::Integer,
                    required: true,
                    default_value: None,
                }],
                returns: None,
                mutates: true,
                idempotent: true,
                shortcut: None,
            },
            AgentAction {
                name: "set_filter".into(),
                description: "Set the filter string for fuzzy matching.".into(),
                params: vec![ActionParam {
                    name: "filter".into(),
                    description: "Substring to filter items by.".into(),
                    param_type: ActionParamType::String,
                    required: true,
                    default_value: None,
                }],
                returns: None,
                mutates: true,
                idempotent: true,
                shortcut: None,
            },
            AgentAction {
                name: "get_selected".into(),
                description: "Get the currently selected item values.".into(),
                params: vec![],
                returns: Some("Array of selected item values.".into()),
                mutates: false,
                idempotent: true,
                shortcut: None,
            },
        ]
    }

    fn semantic_role(&self) -> SemanticRole {
        SemanticRole::Input
    }

    fn agent_state(&self) -> serde_json::Value {
        let labels: Vec<&str> = self.items.iter().map(|i| i.label.as_str()).collect();
        serde_json::json!({
            "widget": "SelectList",
            "item_count": self.items.len(),
            "items": labels,
            "mode": format!("{:?}", self.mode),
        })
    }

    fn execute_action(
        &mut self,
        action: &str,
        _params: &serde_json::Value,
    ) -> Result<serde_json::Value, String> {
        Err(format!(
            "SelectList actions require SelectListState; use stateful dispatch. Action: {action}"
        ))
    }
}