oxi-tui 0.25.8

Terminal UI widgets and theme system for oxi, built on 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
494
495
496
497
498
499
500
501
502
503
504
505
506
# oxi-tui Guide

This guide covers the component lifecycle, rendering pipeline, and key systems in `oxi-tui`.

## Component Lifecycle

Every UI component follows this lifecycle:

```
new()  ──────►  render()  ──────►  handle_event()  ──────►  render()  ──────►  ...
              (draw to          (process input)          (re-draw with
               surface)                                   new state)
```

### Methods

```rust
pub trait Component: Send {
    /// Create and configure the component
    fn new() -> Self;
    
    /// Request a render on the next frame
    fn request_render(&mut self);
    
    /// Check if component needs re-rendering
    fn is_dirty(&self) -> bool;
    
    /// Clear dirty flag after render
    fn clear_dirty(&mut self);
    
    /// Handle user input, return true if consumed
    fn handle_event(&mut self, event: &Event) -> bool;
    
    /// Draw to the surface
    fn render(&mut self, surface: &mut Surface, area: Rect);
    
    /// Minimum size this component needs
    fn min_size(&self) -> Size;
    
    /// Desired size (optional override)
    fn desired_size(&self) -> Option<Size> { None }
    
    /// Called when component gains focus
    fn on_focus(&mut self) {}
    
    /// Called when component loses focus
    fn on_unfocus(&mut self) {}
}
```

### Lifecycle Example

```rust
struct MyComponent {
    text: String,
    dirty: bool,
}

impl Component for MyComponent {
    fn new() -> Self {
        Self { text: String::new(), dirty: true }
    }
    
    fn request_render(&mut self) {
        self.dirty = true;
    }
    
    fn is_dirty(&self) -> bool {
        self.dirty
    }
    
    fn clear_dirty(&mut self) {
        self.dirty = false;
    }
    
    fn handle_event(&mut self, event: &Event) -> bool {
        match event {
            Event::Key(KeyEvent { code: KeyCode::Char(c), .. }) => {
                self.text.push(*c);
                self.request_render();  // Re-render after change
                true
            }
            _ => false,
        }
    }
    
    fn render(&mut self, surface: &mut Surface, area: Rect) {
        surface.write_string(area.y, area.x, &self.text);
        self.clear_dirty();
    }
    
    fn min_size(&self) -> Size {
        Size { width: 10, height: 1 }
    }
}
```

## Rendering Pipeline

### Surface System

`Surface` is a 2D grid of `Cell`s representing the terminal:

```rust
pub struct Surface {
    width: u16,
    height: u16,
    cells: Vec<Vec<Cell>>,
    dirty_cells: Vec<Vec<bool>>,
    first_dirty_row: Option<u16>,
    last_dirty_row: Option<u16>,
}
```

### Rendering Flow

```
┌─────────────┐
│ Components  │
│ (dirty?)    │
└──────┬──────┘
┌─────────────────────────┐
│ Render to Surface       │
│ (clear dirty cells)    │
└──────┬──────────────────┘
┌─────────────────────────┐
│ Diff Detection          │ ──► Compute SGR (escape codes)
│ (mark changed cells)    │     Only update changed cells
└──────┬──────────────────┘
┌─────────────────────────┐
│ Flush to Terminal       │
│ (SGR + cursor moves)    │
└─────────────────────────┘
```

### Dirty Tracking

```rust
// Mark cells as dirty when changing
surface.set(row, col, cell);  // Marks cell as dirty

// Query dirty range for optimal updates
if let Some(first) = surface.first_dirty() {
    let last = surface.last_dirty().unwrap();
    // Only redraw rows first..=last
}

// Clear after rendering
surface.clear_dirty();
```

### Cell Structure

```rust
pub struct Cell {
    pub char: char,
    pub fg: Color,
    pub bg: Color,
    pub attrs: Attributes,
}

pub struct Attributes {
    pub bold: bool,
    pub italic: bool,
    pub underline: bool,
    pub strikethrough: bool,
    pub reverse: bool,
}
```

### SGR Escape Codes

Only changed cells are sent to the terminal:

```rust
// SGR sequence builder
fn cell_to_sgr(cell: &Cell) -> String {
    let mut parts = vec![];
    parts.push("38;2;{};{};{}".format(r, g, b)); // fg
    parts.push("48;2;{};{};{}".format(r, g, b)); // bg
    if attrs.bold { parts.push("1"); }
    if attrs.italic { parts.push("3"); }
    format!("\x1b[{}m", parts.join(";"))
}
```

### Wide Character Support

CJK characters occupy 2 columns:

```rust
surface.write_string(0, 0, "Hello 世界");

// Cells:
// [H][e][l][l][o][ ][W][o]        │           └── wide continuation
//        └─────────────── wide continuation
```

### Flush Pattern

```rust
fn flush_surface(surface: &Surface) {
    let first_dirty = surface.first_dirty().unwrap_or(0);
    let last_dirty = surface.last_dirty().unwrap_or(surface.height - 1);
    
    for row in first_dirty..=last_dirty {
        // Move cursor
        print!("\x1b[{};{}H", row + 1, 1);
        
        // Build row content with SGR codes
        for col in 0..surface.width() {
            let cell = surface.get(row, col).unwrap();
            // Emit SGR + char
        }
        
        // Clear to end of line
        print!("\x1b[0m\x1b[K");
    }
}
```

## Event System

### Event Types

```rust
pub enum Event {
    /// Keyboard input
    Key(KeyEvent),
    
    /// Mouse input
    Mouse(MouseEvent),
    
    /// Terminal resize
    Resize(ResizeEvent),
    
    /// Focus gained/lost
    Focus(FocusEvent),
}
```

### KeyEvent Structure

```rust
pub struct KeyEvent {
    pub code: KeyCode,
    pub modifiers: KeyModifiers,
}

pub enum KeyCode {
    Char(char),
    Enter,
    Escape,
    Left,
    Right,
    Up,
    Down,
    Tab,
    Backspace,
    Home,
    End,
    PageUp,
    PageDown,
    F(u8),
}

pub struct KeyModifiers {
    pub control: bool,
    pub shift: bool,
    pub alt: bool,
}
```

### Key Input Handling

#### Kitty Keyboard Protocol

Modern terminals use the Kitty keyboard protocol for extended keys:

```
\x1b[...;A u  (modifier + key with unicode codepoint)
```

The parser handles:
- Modifier flags in escape sequence
- Unicode codepoint for special keys

#### xterm Extensions

xterm provides additional sequences:
- F1-F4 via application mode
- Bracketed paste mode
- Focus in/out events

### Mouse Input

```rust
pub struct MouseEvent {
    pub kind: MouseEventKind,
    pub button: MouseButton,
    pub modifiers: KeyModifiers,
    pub position: Position,
}

pub enum MouseEventKind {
    Press,
    Release,
    Drag,
    ScrollUp,
    ScrollDown,
}
```

## Theme System

### Theme Structure

```rust
pub struct Theme {
    pub name: String,
    pub colors: ColorScheme,
    pub fonts: FontScheme,
    pub spacing: Spacing,
}

pub struct ColorScheme {
    pub fg: Color,
    pub bg: Color,
    pub cursor: Color,
    pub selection: Color,
    pub scrollbar: Color,
    pub border: Color,
    // ... component-specific colors
}

pub struct FontScheme {
    pub regular: String,
    pub bold: String,
    pub italic: String,
}
```

### Theme Files

Themes are TOML files:

```toml
[colors]
fg = "#f8f8f2"
bg = "#282a36"
cursor = "#f8f8f0"
selection = "#44475a"

[colors.chat]
user_fg = "#8be9fd"
assistant_fg = "#50fa7b"
system_fg = "#ffb86c"

[fonts]
regular = "JetBrains Mono"
size = 14
line_height = 1.4
```

### Loading Themes

```rust
let manager = ThemeManager::new();
manager.load_theme("nord")?;
manager.set_active("nord");

// Apply to surface
surface.fill(Cell::default().with_bg(theme.colors.bg));
```

## Built-in Components

### Text

Simple text display:

```rust
let text = Text::new("Hello, world!".to_string());
```

### Spacer

Flexible spacing:

```rust
// Vertical spacer
let spacer = Spacer::vertical(10);

// Horizontal spacer
let spacer = Spacer::horizontal(20);

// Flexible spacer (takes remaining space)
let spacer = Spacer::flexible();
```

### SelectList

Selectable list items:

```rust
let list = SelectList::new(vec![
    SelectItem::new("Option 1", Some("description")),
    SelectItem::new("Option 2", None),
]);
list.on_select(|item| { /* ... */ });
```

### CommandPalette

Fuzzy-filtered command list:

```rust
let palette = CommandPalette::new();
palette.add_command(Command::new("save", "Save file", |_| {}));
palette.add_command(Command::new("open", "Open file", |_| {}));
```

## Layout System

### Container

```rust
use oxi_tui::{Container, Direction, Constraint};

let container = Container::new(Direction::Vertical)
    .add(Box::new(header).height(Constraint::Length(1)))
    .add(Box::new(content).height(Constraint::Fill))
    .add(Box::new(footer).height(Constraint::Length(1)));
```

### Constraints

```rust
pub enum Constraint {
    /// Fixed pixel size
    Length(u16),
    /// Percentage of parent
    Percent(u16),
    /// Fill remaining space
    Fill,
}
```

## Overlay System

### Creating Overlays

```rust
let overlay = OverlayContent::new()
    .title("Settings")
    .component(Box::new(settings_panel))
    .on_close(|| { /* cleanup */ });

tui.push_overlay(overlay);
```

### Modal Pattern

```rust
let modal = OverlayBox::new(area, |surface, rect| {
    // Draw modal box with border
    surface.fill_row(rect.y, rect.x, ' ', Color::Blue, Color::DarkBlue);
    // ...
});
```

## Keyboard Shortcuts

### KeyBindings

```rust
use oxi_tui::KeyBindings;

let bindings = KeyBindings::new()
    .bind("C-s", Action::Save)
    .bind("C-o", Action::Open)
    .bind("C-c", Action::Cancel);
```

### Action Handling

```rust
fn handle_key(&mut self, event: &Event) -> bool {
    if let Some(action) = self.bindings.resolve(event) {
        match action {
            Action::Save => self.save(),
            Action::Open => self.open(),
            Action::Cancel => return false,  // Pass through
        }
        true
    } else {
        false
    }
}
```