collet 0.1.1

Relentless agentic coding orchestrator with zero-drop agent loops
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
use ratatui::prelude::*;
use ratatui::widgets::{Block, Paragraph};

use crate::tui::theme::Theme;

use super::{PopupListCtx, render_search_bar, truncate_str};

pub(super) fn render_mcp_toggle(
    ctx: &PopupListCtx<'_>,
    buf: &mut Buffer,
    block: Block,
    items: &[crate::tui::state::McpToggleItem],
) {
    let inner = block.inner(ctx.popup_area);
    block.render(ctx.popup_area, buf);

    // Filter by search
    let q = ctx.search.to_lowercase();
    let filtered: Vec<&crate::tui::state::McpToggleItem> = items
        .iter()
        .filter(|it| {
            it.name.to_lowercase().contains(&q) || it.description.to_lowercase().contains(&q)
        })
        .collect();

    // Search bar (always visible)
    render_search_bar(
        inner,
        buf,
        ctx.search,
        filtered.len(),
        items.len(),
        ctx.theme,
    );

    let list_inner = Rect::new(
        inner.x,
        inner.y + 1,
        inner.width,
        inner.height.saturating_sub(1),
    );

    if filtered.is_empty() {
        Paragraph::new(Span::styled(
            "  no matches",
            Style::default().fg(ctx.theme.text_muted),
        ))
        .render(
            Rect::new(list_inner.x, list_inner.y, list_inner.width, 1),
            buf,
        );
        return;
    }

    let skip = ctx.scroll as usize;
    let mut row_y = list_inner.y;

    for (i, item) in filtered.iter().enumerate() {
        if i < skip {
            continue;
        }
        if row_y >= list_inner.y + list_inner.height {
            break;
        }

        let is_sel = i == ctx.selected;

        let check = if item.enabled { "" } else { "" };
        let check_color = if item.enabled {
            ctx.theme.success
        } else {
            ctx.theme.error
        };

        let tool_info = if item.tool_count > 0 {
            format!("  {} tools", item.tool_count)
        } else {
            String::new()
        };

        let name_width = 24usize;
        let padded_name = format!("{:<width$}", item.name, width = name_width);

        let mut spans = vec![
            Span::styled(format!("  [{}] ", check), Style::default().fg(check_color)),
            Span::styled(
                padded_name,
                Style::default()
                    .fg(if is_sel {
                        ctx.theme.accent
                    } else {
                        ctx.theme.text
                    })
                    .add_modifier(if is_sel {
                        Modifier::BOLD
                    } else {
                        Modifier::empty()
                    }),
            ),
            Span::styled(
                format!("{:<14}", item.status),
                Style::default().fg(ctx.theme.text_dim),
            ),
        ];

        if !tool_info.is_empty() {
            spans.push(Span::styled(
                tool_info,
                Style::default().fg(ctx.theme.text_muted),
            ));
        }

        let line = Line::from(spans);
        let row_area = Rect::new(list_inner.x, row_y, list_inner.width, 1);

        if is_sel {
            let bg_span = Span::styled(
                " ".repeat(list_inner.width as usize),
                Style::default().bg(ctx.theme.accent_dim),
            );
            Paragraph::new(Line::from(bg_span)).render(row_area, buf);
        }

        Paragraph::new(line).render(row_area, buf);
        row_y += 1;
    }
}

/// Render the session resume picker (4-column: id, status, time, task preview).
pub(super) fn render_session_resume(
    ctx: &PopupListCtx<'_>,
    buf: &mut Buffer,
    block: Block,
    items: &[(String, String, String, String)],
) {
    let inner = block.inner(ctx.popup_area);
    block.render(ctx.popup_area, buf);

    // Filter by search query
    let q = ctx.search.to_lowercase();
    let filtered: Vec<&(String, String, String, String)> = items
        .iter()
        .filter(|(id, ts, st, task)| {
            id.to_lowercase().contains(&q)
                || ts.to_lowercase().contains(&q)
                || st.to_lowercase().contains(&q)
                || task.to_lowercase().contains(&q)
        })
        .collect();

    // Search bar
    render_search_bar(
        inner,
        buf,
        ctx.search,
        filtered.len(),
        items.len(),
        ctx.theme,
    );

    let list_inner = Rect::new(
        inner.x,
        inner.y + 1,
        inner.width,
        inner.height.saturating_sub(1),
    );

    let id_w: u16 = 9;
    let status_w: u16 = 8;
    let time_w: u16 = 17;
    let task_w = list_inner
        .width
        .saturating_sub(id_w + status_w + time_w + 3);

    if filtered.is_empty() {
        Paragraph::new(Span::styled(
            "  no matches",
            Style::default().fg(ctx.theme.text_muted),
        ))
        .render(
            Rect::new(list_inner.x, list_inner.y, list_inner.width, 1),
            buf,
        );
        return;
    }

    let skip = ctx.scroll as usize;
    let mut row_y = list_inner.y;

    for (i, (id, ts, status, task)) in filtered.iter().enumerate() {
        if i < skip {
            continue;
        }
        if row_y >= list_inner.y + list_inner.height {
            break;
        }

        let is_selected = i == ctx.selected;

        if is_selected {
            let full = Rect::new(list_inner.x, row_y, list_inner.width, 1);
            Paragraph::new(Span::styled(
                " ".repeat(list_inner.width as usize),
                Style::default().bg(ctx.theme.accent),
            ))
            .render(full, buf);
        }

        let (fg_main, fg_dim, fg_status) = if is_selected {
            (ctx.theme.bg, ctx.theme.bg, ctx.theme.bg)
        } else {
            (ctx.theme.text, ctx.theme.text_muted, ctx.theme.accent)
        };
        let bg = if is_selected {
            ctx.theme.accent
        } else {
            ctx.theme.bg_surface
        };

        // id column
        Paragraph::new(Span::styled(
            format!(" {}", truncate_str(id, (id_w - 1) as usize)),
            Style::default()
                .fg(fg_main)
                .bg(bg)
                .add_modifier(if is_selected {
                    Modifier::BOLD
                } else {
                    Modifier::empty()
                }),
        ))
        .render(Rect::new(list_inner.x, row_y, id_w, 1), buf);

        // status column
        Paragraph::new(Span::styled(
            truncate_str(status, status_w as usize),
            Style::default().fg(fg_status).bg(bg),
        ))
        .render(Rect::new(list_inner.x + id_w, row_y, status_w, 1), buf);

        // timestamp column
        Paragraph::new(Span::styled(
            truncate_str(ts, time_w as usize),
            Style::default().fg(fg_dim).bg(bg),
        ))
        .render(
            Rect::new(list_inner.x + id_w + status_w, row_y, time_w, 1),
            buf,
        );

        // task preview column
        if task_w > 0 {
            Paragraph::new(Span::styled(
                truncate_str(task, task_w as usize),
                Style::default().fg(fg_dim).bg(bg),
            ))
            .render(
                Rect::new(list_inner.x + id_w + status_w + time_w, row_y, task_w, 1),
                buf,
            );
        }

        row_y += 1;
    }
}

/// Render a two-column selectable table inside the popup block.
pub(super) fn render_table_select(
    ctx: &PopupListCtx<'_>,
    buf: &mut Buffer,
    block: Block,
    items: &[(String, String)],
) {
    let inner = block.inner(ctx.popup_area);
    block.render(ctx.popup_area, buf);

    // Filter items by search query
    let q = ctx.search.to_lowercase();
    let filtered: Vec<&(String, String)> = items
        .iter()
        .filter(|(n, d)| n.to_lowercase().contains(&q) || d.to_lowercase().contains(&q))
        .collect();

    // Search bar (1 row)
    render_search_bar(
        inner,
        buf,
        ctx.search,
        filtered.len(),
        items.len(),
        ctx.theme,
    );

    // List area below search bar
    let list_inner = Rect::new(
        inner.x,
        inner.y + 1,
        inner.width,
        inner.height.saturating_sub(1),
    );

    // Column split: left = 35% of inner width, right = remainder
    let left_w = (list_inner.width as f32 * 0.35) as u16;
    let right_w = list_inner.width.saturating_sub(left_w + 2); // 2 = gap

    let skip = ctx.scroll as usize;
    let mut row_y = list_inner.y;

    if filtered.is_empty() {
        Paragraph::new(Span::styled(
            "  no matches",
            Style::default().fg(ctx.theme.text_muted),
        ))
        .render(
            Rect::new(list_inner.x, list_inner.y, list_inner.width, 1),
            buf,
        );
        return;
    }

    for (i, (name, desc)) in filtered.iter().enumerate() {
        if i < skip {
            continue;
        }
        if row_y >= list_inner.y + list_inner.height {
            break;
        }

        let is_selected = i == ctx.selected;

        // Left column (name)
        let name_display = truncate_str(name, left_w as usize);
        let left_area = Rect::new(list_inner.x, row_y, left_w, 1);
        let name_style = if is_selected {
            Style::default()
                .fg(ctx.theme.bg)
                .bg(ctx.theme.accent)
                .add_modifier(Modifier::BOLD)
        } else {
            Style::default().fg(ctx.theme.accent)
        };

        // Fill the entire row background when selected
        if is_selected {
            let full_row = Rect::new(list_inner.x, row_y, list_inner.width, 1);
            Paragraph::new(Span::styled(
                " ".repeat(list_inner.width as usize),
                Style::default().bg(ctx.theme.accent),
            ))
            .render(full_row, buf);
        }

        Paragraph::new(Span::styled(format!(" {name_display}"), name_style)).render(left_area, buf);

        // Gap
        let gap_x = list_inner.x + left_w;
        if gap_x < list_inner.x + list_inner.width {
            let gap_area = Rect::new(gap_x, row_y, 2, 1);
            let gap_style = if is_selected {
                Style::default().bg(ctx.theme.accent)
            } else {
                Style::default()
            };
            Paragraph::new(Span::styled("  ", gap_style)).render(gap_area, buf);
        }

        // Right column (description)
        let desc_display = truncate_str(desc, right_w as usize);
        let right_x = list_inner.x + left_w + 2;
        if right_x < list_inner.x + list_inner.width {
            let right_area = Rect::new(right_x, row_y, right_w, 1);
            let desc_style = if is_selected {
                Style::default().fg(ctx.theme.bg).bg(ctx.theme.accent)
            } else {
                Style::default().fg(ctx.theme.text_muted)
            };
            Paragraph::new(Span::styled(desc_display, desc_style)).render(right_area, buf);
        }

        row_y += 1;
    }
}

/// Render the interactive config editor (two-column: label + value).
pub(super) fn render_config(
    popup_area: Rect,
    buf: &mut Buffer,
    block: Block,
    items: &[crate::tui::state::ConfigItem],
    selected: usize,
    scroll: u16,
    theme: &crate::tui::theme::Theme,
) {
    let inner = block.inner(popup_area);
    block.render(popup_area, buf);

    // Left column 60%, right 40%
    let left_w = (inner.width as f32 * 0.60) as u16;
    let right_w = inner.width.saturating_sub(left_w);

    let skip = scroll as usize;
    let mut row_y = inner.y;

    for (i, item) in items.iter().enumerate() {
        if i < skip {
            continue;
        }
        if row_y >= inner.y + inner.height {
            break;
        }

        let is_selected = i == selected;
        let value_str = item.value.display();

        // Check if value exceeds warning threshold
        let is_warned = item.warn_above.as_ref().is_some_and(|threshold| {
            value_str.parse::<f64>().unwrap_or(0.0) > threshold.parse::<f64>().unwrap_or(f64::MAX)
        });

        // Value color: warned=warning, bool true=green, false=dim, choice=accent
        let value_color = if is_warned {
            theme.warning
        } else {
            match &item.value {
                crate::tui::state::ConfigValue::Bool(true) => theme.success,
                crate::tui::state::ConfigValue::Bool(false) => theme.text_muted,
                crate::tui::state::ConfigValue::Choice { .. } => theme.accent,
                crate::tui::state::ConfigValue::Text(_) => theme.text_muted,
            }
        };

        // Warning suffix
        let warn_suffix = if is_warned { "" } else { "" };

        if is_selected {
            // Fill full row background
            let full_row = Rect::new(inner.x, row_y, inner.width, 1);
            Paragraph::new(Span::styled(
                " ".repeat(inner.width as usize),
                Style::default().bg(theme.accent),
            ))
            .render(full_row, buf);

            // Selected: label in bg color on accent bg, value in bg color
            let label_area = Rect::new(inner.x, row_y, left_w, 1);
            let cursor = "";
            Paragraph::new(Span::styled(
                format!(
                    " {cursor}{}",
                    truncate_str(&item.label, (left_w as usize).saturating_sub(3))
                ),
                Style::default()
                    .fg(theme.bg)
                    .bg(theme.accent)
                    .add_modifier(Modifier::BOLD),
            ))
            .render(label_area, buf);

            let right_x = inner.x + left_w;
            if right_x < inner.x + inner.width {
                let val_area = Rect::new(right_x, row_y, right_w, 1);
                let val_display = format!("{value_str}{warn_suffix}");
                let val_fg = if is_warned { theme.warning } else { theme.bg };
                Paragraph::new(Span::styled(
                    truncate_str(&val_display, right_w as usize),
                    Style::default()
                        .fg(val_fg)
                        .bg(theme.accent)
                        .add_modifier(Modifier::BOLD),
                ))
                .render(val_area, buf);
            }
        } else {
            let label_area = Rect::new(inner.x, row_y, left_w, 1);
            Paragraph::new(Span::styled(
                format!(
                    "   {}",
                    truncate_str(&item.label, (left_w as usize).saturating_sub(3))
                ),
                Style::default().fg(theme.text),
            ))
            .render(label_area, buf);

            let right_x = inner.x + left_w;
            if right_x < inner.x + inner.width {
                let val_area = Rect::new(right_x, row_y, right_w, 1);
                let val_display = format!("{value_str}{warn_suffix}");
                Paragraph::new(Span::styled(
                    truncate_str(&val_display, right_w as usize),
                    Style::default().fg(value_color),
                ))
                .render(val_area, buf);
            }
        }

        row_y += 1;
    }
}

pub(super) fn render_theme_select(
    popup_area: Rect,
    buf: &mut Buffer,
    block: Block,
    selected: usize,
    dark_mode: bool,
    scroll: u16,
    theme: &crate::tui::theme::Theme,
) {
    let inner = block.inner(popup_area);
    block.render(popup_area, buf);

    let families = Theme::families();
    let skip = scroll as usize;
    let mut row_y = inner.y;

    for (i, family) in families.iter().enumerate() {
        if i < skip {
            continue;
        }
        if row_y >= inner.y + inner.height {
            break;
        }

        let is_selected = i == selected;

        if is_selected {
            // Highlight full row
            let full_row = Rect::new(inner.x, row_y, inner.width, 1);
            Paragraph::new(Span::styled(
                " ".repeat(inner.width as usize),
                Style::default().bg(theme.accent),
            ))
            .render(full_row, buf);

            // Build the badge: show both toggles, highlight active one
            let dark_badge = if dark_mode {
                " ◐ Dark "
            } else {
                " ◑ Dark "
            };
            let light_badge = if !dark_mode {
                " ◑ Light "
            } else {
                " ◐ Light "
            };
            let has_light = family.light_id.is_some();

            let name_max = inner.width.saturating_sub(20) as usize;
            let name_display = truncate_str(family.name, name_max);

            // Name span
            let name_span = Span::styled(
                format!("{}", name_display),
                Style::default()
                    .fg(theme.bg)
                    .bg(theme.accent)
                    .add_modifier(Modifier::BOLD),
            );

            // Dark badge span
            let dark_style = if dark_mode {
                Style::default()
                    .fg(theme.accent)
                    .bg(theme.bg_surface)
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(theme.bg).bg(theme.accent)
            };
            let dark_span = Span::styled(dark_badge, dark_style);

            // Light badge span (dimmed if no light variant)
            let light_style = if !has_light {
                Style::default().fg(theme.text_muted).bg(theme.accent)
            } else if !dark_mode {
                Style::default()
                    .fg(theme.accent)
                    .bg(theme.bg_surface)
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(theme.bg).bg(theme.accent)
            };
            let light_span = Span::styled(light_badge, light_style);

            let name_area = Rect::new(inner.x, row_y, inner.width.saturating_sub(20), 1);
            let badge_x = inner.x + inner.width.saturating_sub(19);
            let badge_area = Rect::new(badge_x, row_y, 19, 1);

            Paragraph::new(Line::from(vec![name_span])).render(name_area, buf);
            Paragraph::new(Line::from(vec![dark_span, light_span])).render(badge_area, buf);
        } else {
            // Dim unselected rows
            let name_max = inner.width.saturating_sub(2) as usize;
            let name_display = truncate_str(family.name, name_max);
            Paragraph::new(Span::styled(
                format!("   {}", name_display),
                Style::default().fg(theme.text_muted),
            ))
            .render(Rect::new(inner.x, row_y, inner.width, 1), buf);
        }

        row_y += 1;
    }
}