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
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
use crate::core::buffer::Buffer;
use crate::core::rect::Rect;
use crate::core::style::Style;
use crate::core::text::Span;
use crate::ontology::{
    AgentAction, AgentCapability, Discoverable, PropertyConstraint, PropertySchema, PropertyType,
    SemanticRole, WidgetSchema,
};
use crate::widget::block::Block;
use crate::widget::Widget;
use unicode_width::UnicodeWidthStr;

/// A single bar in a bar chart.
#[derive(Debug, Clone)]
pub struct Bar {
    /// The numeric value of this bar.
    pub value: u64,
    /// Optional label displayed below the bar.
    pub label: Option<Span>,
    /// Style applied to the bar body.
    pub style: Style,
    /// Optional style for the value text above the bar.
    pub value_style: Style,
}

impl Bar {
    pub fn new(value: u64) -> Self {
        Self {
            value,
            label: None,
            style: Style::default(),
            value_style: Style::default(),
        }
    }

    pub fn label(mut self, label: impl Into<Span>) -> Self {
        self.label = Some(label.into());
        self
    }

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

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

/// A group of bars rendered side-by-side with an optional group label.
#[derive(Debug, Clone)]
pub struct BarGroup {
    /// The bars in this group.
    pub bars: Vec<Bar>,
    /// Optional label for the group.
    pub label: Option<Span>,
}

impl BarGroup {
    pub fn new(bars: impl Into<Vec<Bar>>) -> Self {
        Self {
            bars: bars.into(),
            label: None,
        }
    }

    pub fn label(mut self, label: impl Into<Span>) -> Self {
        self.label = Some(label.into());
        self
    }
}

/// Direction for bar rendering.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BarDirection {
    Vertical,
    Horizontal,
}

/// Bar chart widget for visualizing categorical data.
///
/// Supports grouped bars, per-bar styling, value labels, and both
/// vertical and horizontal orientations.
///
/// # Example
///
/// ```ignore
/// use louie::widget::barchart::{BarChart, Bar, BarGroup};
///
/// let chart = BarChart::new(vec![
///     BarGroup::new(vec![Bar::new(80), Bar::new(60)]).label("Q1"),
///     BarGroup::new(vec![Bar::new(90), Bar::new(70)]).label("Q2"),
/// ])
/// .bar_width(5)
/// .bar_gap(1)
/// .group_gap(2);
/// ```
#[derive(Debug, Clone)]
pub struct BarChart {
    groups: Vec<BarGroup>,
    block: Option<Block>,
    style: Style,
    bar_width: u16,
    bar_gap: u16,
    group_gap: u16,
    max_value: Option<u64>,
    direction: BarDirection,
    value_style: Style,
    label_style: Style,
}

/// Bar symbols for sub-cell resolution (eighths).
const BAR_EIGHTHS: [&str; 9] = [" ", "", "", "", "", "", "", "", ""];

/// Horizontal bar symbols (eighths, left-to-right).
const HBAR_EIGHTHS: [&str; 9] = [" ", "", "", "", "", "", "", "", ""];

impl BarChart {
    pub fn new(groups: impl Into<Vec<BarGroup>>) -> Self {
        Self {
            groups: groups.into(),
            block: None,
            style: Style::default(),
            bar_width: 3,
            bar_gap: 1,
            group_gap: 2,
            max_value: None,
            direction: BarDirection::Vertical,
            value_style: Style::default(),
            label_style: Style::default(),
        }
    }

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

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

    /// Width of each bar in terminal columns.
    pub fn bar_width(mut self, width: u16) -> Self {
        self.bar_width = width.max(1);
        self
    }

    /// Gap between bars within a group.
    pub fn bar_gap(mut self, gap: u16) -> Self {
        self.bar_gap = gap;
        self
    }

    /// Gap between groups.
    pub fn group_gap(mut self, gap: u16) -> Self {
        self.group_gap = gap;
        self
    }

    /// Manually set the maximum value. If `None`, auto-detects from data.
    pub fn max_value(mut self, max: u64) -> Self {
        self.max_value = Some(max);
        self
    }

    /// Set bar direction (vertical or horizontal).
    pub fn direction(mut self, direction: BarDirection) -> Self {
        self.direction = direction;
        self
    }

    /// Style for value labels displayed alongside bars.
    pub fn value_style(mut self, style: Style) -> Self {
        self.value_style = style;
        self
    }

    /// Style for bar/group labels.
    pub fn label_style(mut self, style: Style) -> Self {
        self.label_style = style;
        self
    }

    fn effective_max(&self) -> u64 {
        self.max_value
            .unwrap_or_else(|| {
                self.groups
                    .iter()
                    .flat_map(|g| g.bars.iter())
                    .map(|b| b.value)
                    .max()
                    .unwrap_or(1)
            })
            .max(1)
    }

    fn render_vertical(self, inner: Rect, buf: &mut Buffer) {
        if inner.is_empty() {
            return;
        }

        let max = self.effective_max();
        // Reserve 1 row for bar labels and 1 for group labels if any groups have labels
        let has_bar_labels = self
            .groups
            .iter()
            .any(|g| g.bars.iter().any(|b| b.label.is_some()));
        let has_group_labels = self.groups.iter().any(|g| g.label.is_some());
        let label_rows = has_bar_labels as u16 + has_group_labels as u16;
        let chart_height = inner.height.saturating_sub(label_rows);
        if chart_height == 0 {
            return;
        }

        let mut x = inner.x;
        for (gi, group) in self.groups.iter().enumerate() {
            if gi > 0 {
                x += self.group_gap;
            }
            let group_start_x = x;

            for (bi, bar) in group.bars.iter().enumerate() {
                if bi > 0 {
                    x += self.bar_gap;
                }
                if x >= inner.right() {
                    break;
                }

                let bar_w = self.bar_width.min(inner.right().saturating_sub(x));
                if bar_w == 0 {
                    break;
                }

                // Calculate bar height in eighths
                let scaled =
                    (bar.value.min(max) as f64 / max as f64 * (chart_height as f64 * 8.0)) as u64;
                let full_rows = (scaled / 8) as u16;
                let remainder = (scaled % 8) as usize;

                let bar_style = if bar.style != Style::default() {
                    bar.style
                } else {
                    self.style
                };

                // Draw bar from bottom up
                let bar_bottom = inner.y + chart_height;
                for row in 0..chart_height {
                    let y = bar_bottom - 1 - row;
                    let symbol = if row < full_rows {
                        BAR_EIGHTHS[8]
                    } else if row == full_rows && remainder > 0 {
                        BAR_EIGHTHS[remainder]
                    } else {
                        break;
                    };
                    for bx in x..x + bar_w {
                        if bx < inner.right() {
                            buf[(bx, y)].set_symbol(symbol);
                            buf[(bx, y)].set_style(bar_style);
                        }
                    }
                }

                // Draw bar label
                if has_bar_labels {
                    let label_y = inner.y + chart_height;
                    if let Some(ref label) = bar.label {
                        let text = &label.content;
                        let tw = text.width() as u16;
                        let lx = x + bar_w.saturating_sub(tw) / 2;
                        buf.set_string(lx, label_y, text, self.label_style);
                    }
                }

                x += bar_w;
            }

            // Draw group label centered under the group
            if has_group_labels {
                if let Some(ref label) = group.label {
                    let group_end_x = x;
                    let group_w = group_end_x.saturating_sub(group_start_x);
                    let label_y = inner.y + chart_height + has_bar_labels as u16;
                    let text = &label.content;
                    let tw = text.width() as u16;
                    let lx = group_start_x + group_w.saturating_sub(tw) / 2;
                    if label_y < inner.bottom() {
                        buf.set_string(lx, label_y, text, self.label_style);
                    }
                }
            }
        }
    }

    fn render_horizontal(self, inner: Rect, buf: &mut Buffer) {
        if inner.is_empty() {
            return;
        }

        let max = self.effective_max();
        // Reserve columns for value labels
        let value_col_width = {
            let max_digits = format!("{}", max).len() as u16;
            max_digits + 1
        };
        let chart_width = inner.width.saturating_sub(value_col_width);
        if chart_width == 0 {
            return;
        }

        let mut y = inner.y;
        for (gi, group) in self.groups.iter().enumerate() {
            if gi > 0 {
                y += self.group_gap;
            }

            // Group label
            if let Some(ref label) = group.label {
                if y < inner.bottom() {
                    buf.set_string(inner.x, y, &label.content, self.label_style);
                    y += 1;
                }
            }

            for (bi, bar) in group.bars.iter().enumerate() {
                if bi > 0 {
                    y += self.bar_gap;
                }
                if y >= inner.bottom() {
                    break;
                }

                let bar_style = if bar.style != Style::default() {
                    bar.style
                } else {
                    self.style
                };

                // Value label at left
                let value_text = format!("{}", bar.value);
                let vw = value_text.len() as u16;
                let vx = inner.x + value_col_width.saturating_sub(vw + 1);
                buf.set_string(vx, y, &value_text, self.value_style);

                // Calculate bar width in eighths
                let bar_start = inner.x + value_col_width;
                let scaled =
                    (bar.value.min(max) as f64 / max as f64 * (chart_width as f64 * 8.0)) as u64;
                let full_cols = (scaled / 8) as u16;
                let remainder = (scaled % 8) as usize;

                for col in 0..chart_width {
                    let bx = bar_start + col;
                    if bx >= inner.right() {
                        break;
                    }
                    let symbol = if col < full_cols {
                        HBAR_EIGHTHS[8]
                    } else if col == full_cols && remainder > 0 {
                        HBAR_EIGHTHS[remainder]
                    } else {
                        break;
                    };
                    buf[(bx, y)].set_symbol(symbol);
                    buf[(bx, y)].set_style(bar_style);
                }

                // Bar label at end
                if let Some(ref label) = bar.label {
                    let lx = bar_start + full_cols + (remainder > 0) as u16 + 1;
                    if lx < inner.right() {
                        buf.set_string(lx, y, &label.content, self.label_style);
                    }
                }

                y += 1;
            }
        }
    }
}

impl Default for BarChart {
    fn default() -> Self {
        Self::new(Vec::<BarGroup>::new())
    }
}

impl Widget for BarChart {
    fn render(self, area: Rect, buf: &mut Buffer) {
        if area.is_empty() {
            return;
        }

        buf.set_style(area, self.style);

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

        if inner.is_empty() {
            return;
        }

        match self.direction {
            BarDirection::Vertical => self.render_vertical(inner, buf),
            BarDirection::Horizontal => self.render_horizontal(inner, buf),
        }
    }
}

impl Discoverable for BarChart {
    fn schema() -> WidgetSchema {
        WidgetSchema {
            name: "BarChart".into(),
            description: "A bar chart for visualizing categorical data with grouped bars.".into(),
            default_role: SemanticRole::DataVisualization,
            properties: vec![
                PropertySchema {
                    name: "groups".into(),
                    description: "Bar groups, each containing one or more bars.".into(),
                    property_type: PropertyType::Array(Box::new(PropertyType::Object(vec![]))),
                    required: true,
                    default_value: None,
                    constraints: vec![],
                },
                PropertySchema {
                    name: "bar_width".into(),
                    description: "Width of each bar in terminal columns.".into(),
                    property_type: PropertyType::Integer,
                    required: false,
                    default_value: Some(serde_json::json!(3)),
                    constraints: vec![PropertyConstraint::Min(1.0)],
                },
                PropertySchema {
                    name: "direction".into(),
                    description: "Rendering direction: Vertical or Horizontal.".into(),
                    property_type: PropertyType::Enum(vec!["Vertical".into(), "Horizontal".into()]),
                    required: false,
                    default_value: Some(serde_json::json!("Vertical")),
                    constraints: vec![],
                },
            ],
            actions: vec![],

            usage_hint: Some("Use for categorical comparisons. Each group can have multiple bars for multi-series data.".into()),
            tags: vec!["chart".into(), "bar".into(), "data".into(), "visualization".into()],
        }
    }

    fn capabilities(&self) -> Vec<AgentCapability> {
        vec![]
    }

    fn actions(&self) -> Vec<AgentAction> {
        vec![]
    }

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

    fn agent_state(&self) -> serde_json::Value {
        let bars: Vec<serde_json::Value> = self
            .groups
            .iter()
            .map(|g| {
                serde_json::json!({
                    "label": g.label.as_ref().map(|l| l.content.as_ref()),
                    "bars": g.bars.iter().map(|b| {
                        serde_json::json!({
                            "value": b.value,
                            "label": b.label.as_ref().map(|l| l.content.as_ref()),
                        })
                    }).collect::<Vec<_>>(),
                })
            })
            .collect();
        serde_json::json!({
            "groups": bars,
            "max_value": self.effective_max(),
            "direction": format!("{:?}", self.direction),
        })
    }

    fn execute_action(
        &mut self,
        _action: &str,
        _params: &serde_json::Value,
    ) -> Result<serde_json::Value, String> {
        Err("BarChart has no executable actions".into())
    }
}