croft 0.1.0

The dev workspace you tend: one config for Docker Compose, scripts, hooks, and agents
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
//! Rendering — pure function from [`App`] state to a ratatui [`Frame`].
//!
//! Layout is three vertical bands plus an optional palette overlay:
//!
//! ```text
//! ┌─ top status bar ───────────────────────────────────────────────┐
//! │ croft · <project> · worktree:<slug> · offset:<n>              │
//! ├─ services ──────────────┬─ <selected item details / output> ────┤
//! │ ● app    ●              │                                       │
//! │ ● worker ●              │                                       │
//! ├─ watchers ──────────────┤                                       │
//! │ ◇ watch:test            │                                       │
//! ├─ recipes ───────────────┤                                       │
//! │ ▸ greet                 │                                       │
//! │ ▸ stream                │                                       │
//! ├─ scripts ───────────────┤                                       │
//! │ ▪ seed                  │                                       │
//! ├─ hotkey hint bar ───────────────────────────────────────────────┤
//! │ ↑↓ nav · enter run · s stop · / palette · q quit               │
//! └─────────────────────────────────────────────────────────────────┘
//! ```

use crate::container::ServiceStatus;
use crate::runtime::OutputStream;
use crate::tui::app::{App, Mode};
use crate::tui::runner::{CapturedLine, RunState};
use crate::tui::services::ServicePane;
use crate::tui::views::control_center::state::{Item, ItemKind};
use crate::tui::views::diff::state::{BodyMode, DiffFocus};
use crate::tui::watchers::{WatcherPane, WatcherState};
use ratatui::{
    Frame,
    layout::{Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style, Stylize},
    text::{Line, Span},
    widgets::{Block, BorderType, Borders, ListItem, Padding, Paragraph, Wrap},
};

pub(crate) const SIDEBAR_RATIO: u16 = 28;
const TOP_BAR_HEIGHT: u16 = 1;
const STATUS_BAR_HEIGHT: u16 = 1;

/// Per-view accent. Each top-level view gets its own muted hue so
/// users have a visual cue for which view they're in without
/// reading the status-bar tag — peach for the home/control center,
/// mint teal for the tmux-backed terminals view, soft sky blue for
/// the diff view. Indexed colors so terminals with non-standard
/// 16-color palettes still land somewhere reasonable.
fn view_accent(view: crate::tui::app::View) -> Color {
    match view {
        crate::tui::app::View::ControlCenter => Color::Indexed(215),
        crate::tui::app::View::Terminals => Color::Indexed(79),
        // Saturated sky blue — same hue family as the original
        // 110 so the diff view still "reads" as blue, but bright
        // enough to sit next to the peach (215) and mint teal (79)
        // accents without disappearing into the dark-grey unfocused
        // panel borders.
        crate::tui::app::View::Diff => Color::Indexed(117),
    }
}

pub(crate) fn accent_of(app: &App) -> Color {
    view_accent(app.view())
}

/// Selection highlight chrome — same across all views so the
/// "selected row" affordance reads consistently. Subtle dim grey
/// background pairs with the active accent for the row's text;
/// avoids the loud cyan-bg / black-fg contrast we used before.
pub(crate) const SELECTION_BG: Color = Color::Indexed(238);
pub(crate) const SELECTION_FG: Color = Color::Indexed(255);

pub fn render(app: &App, frame: &mut Frame) {
    let outer = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(TOP_BAR_HEIGHT),
            Constraint::Min(1),
            Constraint::Length(STATUS_BAR_HEIGHT),
        ])
        .split(frame.area());

    render_top_bar(app, frame, outer[0]);

    // Body delegated by view. Each view owns its sidebar + right
    // pane; only the chrome (top bar, status bar) is shared.
    // Clear stale diff pane rects when leaving the diff view so a
    // mouse click in another view can't accidentally hit a rect
    // populated by a previous frame.
    if app.view() != crate::tui::app::View::Diff {
        app.diff().files_rect.set(None);
        app.diff().body_rect.set(None);
    }
    match app.view() {
        crate::tui::app::View::ControlCenter => render_control_center(app, frame, outer[1]),
        crate::tui::app::View::Terminals => {
            crate::tui::views::terminals::view::render(app, frame, outer[1])
        }
        crate::tui::app::View::Diff => crate::tui::views::diff::view::render(app, frame, outer[1]),
    }

    render_status(app, frame, outer[2]);

    let accent = accent_of(app);
    if app.mode() == Mode::Palette
        && let Some(palette) = app.palette()
    {
        crate::tui::dialogs::palette_view::render(app, palette, accent, frame);
    }
    if app.mode() == Mode::Confirm
        && let Some(dialog) = app.confirm_dialog()
    {
        crate::tui::dialogs::confirm_view::render(app, dialog, accent, frame);
    }
    if app.mode() == Mode::ArgsPrompt
        && let Some(prompt) = app.args_prompt()
    {
        crate::tui::dialogs::args_prompt_view::render(prompt, accent, frame);
    }
    if app.mode() == Mode::WorktreeSwitcher
        && let Some(switcher) = app.switcher()
    {
        crate::tui::dialogs::switcher_view::render(switcher, accent, frame);
    }
}

/// Control-center body: sidebar takes the full left column; the
/// right column is split vertically into an info panel (kv pairs +
/// description) on top and an output panel underneath. The legacy
/// bottom-left "details" panel is gone — the same data now lives
/// above the output for the selected item.
fn render_control_center(app: &App, frame: &mut Frame, area: Rect) {
    let body = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Percentage(SIDEBAR_RATIO),
            Constraint::Percentage(100 - SIDEBAR_RATIO),
        ])
        .split(area);
    render_sidebar(app, frame, body[0]);
    render_right_pane(app, frame, body[1]);
}

// ───────────────────────── top bar ─────────────────────────

fn render_top_bar(app: &App, frame: &mut Frame, area: Rect) {
    let project = app
        .config()
        .project
        .name
        .clone()
        .unwrap_or_else(|| "croft".into());

    // Top bar's "croft" wordmark stays bold white regardless of
    // view — pinning it to the active accent made it visually
    // collide with the first sidebar group's accent-colored title
    // immediately below. The view accent already has plenty of
    // representation (panel titles, status-bar tag, group headers).
    let mut spans: Vec<Span<'static>> = vec![
        Span::styled("croft ", Style::default().add_modifier(Modifier::BOLD)),
        Span::styled("│ ", Style::default().fg(Color::DarkGray)),
        Span::styled(project, Style::default().add_modifier(Modifier::BOLD)),
    ];

    // Devcontainer indicator. Surfaces *that* the project is opted
    // in (so users aren't surprised when `n` drops them into a
    // container) and *which* container — the deterministic name is
    // a useful breadcrumb when multiple worktrees / projects are
    // running side by side.
    if let Some(dc) = app.devcontainer() {
        spans.push(Span::styled(" │ ", Style::default().fg(Color::DarkGray)));
        spans.push(Span::styled(
            "▣ devcontainer ",
            Style::default().fg(Color::Cyan),
        ));
        spans.push(Span::styled(
            dc.container_name().to_string(),
            Style::default().fg(Color::Gray),
        ));
    }

    // Branch + dirty marker — globally relevant across every view.
    // The branch is set by the CLI from the detected Identity at
    // startup (and on every worktree hot-reload). Dirty count comes
    // from the same `git status --porcelain` we already shell out
    // for the diff view; preloaded on startup so this header is
    // populated even before the user opens the diff view.
    if let Some(branch) = app.branch() {
        spans.push(Span::styled(" │ ", Style::default().fg(Color::DarkGray)));
        spans.push(Span::styled(
            branch.to_string(),
            Style::default().fg(Color::Gray),
        ));
        // "vs <trunk>" — surfaces what the diff view is anchored
        // against so users know whether `●3` means "3 working-tree
        // changes" or "3 files differ from main." Hidden when the
        // current branch IS the trunk (showing "main vs main" reads
        // odd) and when no trunk could be detected.
        if let Some(trunk) = app.diff().trunk.as_deref()
            && trunk != branch
        {
            spans.push(Span::styled(" vs ", Style::default().fg(Color::DarkGray)));
            spans.push(Span::styled(
                trunk.to_string(),
                Style::default().fg(Color::Gray),
            ));
        }
        let dirty = app.diff().files.len();
        if app.diff().loaded && dirty > 0 {
            spans.push(Span::raw(" "));
            spans.push(Span::styled(
                format!("●{dirty}"),
                Style::default().fg(Color::Yellow),
            ));
        } else if app.diff().loaded {
            spans.push(Span::styled(" clean", Style::default().fg(Color::DarkGray)));
        }
    }

    let p = Paragraph::new(Line::from(spans));
    frame.render_widget(p, area);
}

// ─────────────────────── sidebar ───────────────────────

fn render_sidebar(app: &App, frame: &mut Frame, area: Rect) {
    use crate::tui::shared::sidebar_layout::{JoinPosition, SidebarGroup, render_grouped_sidebar};

    // Bucket items by kind into the four named groups. Recipes and
    // scripts share a single "commands" group — to most users they're
    // the same thing (a runnable command), and the distinction is
    // still visible where it matters (kind tag in the palette and
    // detail pane). Sidebar order preserves the iteration order of
    // `app.items()` within the commands group, which is recipes
    // (alphabetical) then scripts (alphabetical).
    let mut runtime: Vec<&Item> = Vec::new();
    let mut services: Vec<&Item> = Vec::new();
    let mut watchers: Vec<&Item> = Vec::new();
    let mut commands: Vec<&Item> = Vec::new();
    for item in app.items() {
        match item.kind {
            ItemKind::Runtime => runtime.push(item),
            ItemKind::Service => services.push(item),
            ItemKind::Watcher => watchers.push(item),
            ItemKind::Recipe | ItemKind::Script => commands.push(item),
        }
    }
    let mut buckets: Vec<(&'static str, Vec<&Item>)> = Vec::new();
    if !runtime.is_empty() {
        buckets.push(("runtime", runtime));
    }
    if !services.is_empty() {
        buckets.push(("services", services));
    }
    if !watchers.is_empty() {
        buckets.push(("watchers", watchers));
    }
    if !commands.is_empty() {
        buckets.push(("commands", commands));
    }

    if buckets.is_empty() {
        let block = panel_block(" commands ", accent_of(app));
        let body = Paragraph::new(Line::from(Span::styled(
            "  (no items)",
            Style::default().fg(Color::DarkGray),
        )))
        .block(block);
        frame.render_widget(body, area);
        return;
    }

    // Build shared SidebarGroups with pre-rendered list items + the
    // local-selected index within each bucket. The flat-index order
    // matches `app.items()` (build_items is the canonical order), so
    // the global selection maps to one bucket's local index.
    let global_idx = app.selected_index();
    let mut groups: Vec<SidebarGroup<'_>> = Vec::with_capacity(buckets.len());
    let mut cursor = 0usize;
    for (label, items) in &buckets {
        let local_selected = if global_idx >= cursor && global_idx < cursor + items.len() {
            Some(global_idx - cursor)
        } else {
            None
        };
        let list_items: Vec<ListItem> = items
            .iter()
            .map(|item| ListItem::new(sidebar_item_line(app, item)))
            .collect();
        groups.push(SidebarGroup {
            label,
            items: list_items,
            selected_local: local_selected,
        });
        cursor += items.len();
    }

    // Mark adjacent runtime + services groups so they render as one
    // joined block (shared horizontal seam) — they're related enough
    // (lifecycle of the workspace's runtime processes) to read as one
    // grouped panel rather than two separate ones.
    let mut joins = vec![JoinPosition::Standalone; groups.len()];
    for i in 0..groups.len().saturating_sub(1) {
        if groups[i].label == "runtime" && groups[i + 1].label == "services" {
            joins[i] = JoinPosition::Top;
            joins[i + 1] = JoinPosition::Bottom;
        }
    }

    let mut rects = app.sidebar_item_rects.borrow_mut();
    render_grouped_sidebar(
        frame,
        area,
        &groups,
        &joins,
        accent_of(app),
        &mut rects,
        Some("▶ "),
    );
}

/// One control-center sidebar row: kind glyph + name + optional
/// backend badge for non-compose services.
fn sidebar_item_line<'a>(app: &App, item: &'a Item) -> Line<'a> {
    let glyph = item.kind.glyph();
    let glyph_style = item_indicator_style(app, item);
    let mut spans = vec![
        Span::styled(format!("{glyph} "), glyph_style),
        Span::raw(item.name.clone()),
    ];
    if let Some(badge) = service_backend_badge(app, item) {
        spans.push(Span::raw("  "));
        spans.push(Span::styled(
            badge,
            Style::default().fg(Color::DarkGray).dim(),
        ));
    }
    Line::from(spans)
}

/// Returns a tiny tag for service rows whose backend isn't compose,
/// so the user can see at a glance which lifecycle each service
/// belongs to. Compose services get no badge — they're the default
/// and visual noise on the most common case isn't worth it.
fn service_backend_badge(app: &App, item: &Item) -> Option<&'static str> {
    if item.kind != ItemKind::Service {
        return None;
    }
    let cfg = app.config();
    if cfg.services.systemd.iter().any(|s| s.name == item.name) {
        return Some("systemd");
    }
    if cfg.services.custom.iter().any(|s| s.name == item.name) {
        return Some("custom");
    }
    None
}

fn item_indicator_style(app: &App, item: &Item) -> Style {
    crate::tui::views::control_center::dispatch::for_kind(item.kind)
        .indicator_style(&item.name, app)
}

/// Standard run-state palette for sidebar glyphs:
///   never run         → dark grey
///   running           → yellow
///   exit 0            → green
///   non-zero / aborted → red
///
/// Used for the container row, recipe rows, and script rows — any
/// sidebar entry whose state is a `RunState`. Services and watchers
/// have their own indicator paths because their lifecycle isn't a
/// `RunState` (services tail logs; watchers debounce).
pub(crate) fn run_indicator_style(run: Option<&RunState>) -> Style {
    match run {
        None => Style::default().fg(Color::DarkGray),
        Some(r) if !r.is_done() => Style::default().fg(Color::Yellow),
        Some(r) => match r.exit_code {
            Some(0) => Style::default().fg(Color::Green),
            _ => Style::default().fg(Color::Red),
        },
    }
}

// ─────────────────────── right pane ───────────────────────
//
// Two stacked panels: a small "info" panel up top with the kv
// pairs + description that used to live in the bottom-left
// details panel, and an "output" panel underneath that hosts the
// run / service / watcher buffers. Padding is horizontal-only —
// vertical breathing room comes from blank rows in the body, not
// from block padding, so we don't waste rows we could be filling.

fn render_right_pane(app: &App, frame: &mut Frame, area: Rect) {
    let accent = accent_of(app);
    let Some(item) = app.selected_item() else {
        // Selection-less state (e.g. an empty config). One panel
        // spanning the whole right column reads cleaner than an
        // empty info-over-output split.
        let block = panel_block(" output ", accent);
        frame.render_widget(block, area);
        return;
    };

    let info_inner_width = info_inner_width(area);
    let info_body = build_info_body(app, item, info_inner_width);
    let [info_area, output_area] = split_info_output(area, info_body.len());
    if info_body.is_empty() {
        // No info to show (e.g. service / container with no
        // metadata) — give the output panel the whole column so we
        // don't render an empty bordered slot.
        render_output_for_item(app, item, accent, frame, area);
        return;
    }
    render_info_panel(item, accent, frame, info_area, info_body);
    render_output_for_item(app, item, accent, frame, output_area);
}

/// Stack two panels vertically: info up top, output below. The info
/// panel sizes to its content (borders + rows) but is capped so the
/// output always gets at least 5 rows — a recipe with twenty env
/// vars shouldn't push the buffer off-screen.
pub(crate) fn split_info_output(area: Rect, info_rows: usize) -> [Rect; 2] {
    let max_info_height = area.height.saturating_sub(5).max(3);
    let info_h = ((info_rows as u16) + 2).min(max_info_height);
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Length(info_h), Constraint::Min(1)])
        .split(area);
    [chunks[0], chunks[1]]
}

fn info_inner_width(area: Rect) -> usize {
    // Borders eat 2 cols, horizontal padding eats 4 (2 each side).
    (area.width as usize).saturating_sub(2 + 4)
}

fn render_info_panel(
    item: &Item,
    accent: Color,
    frame: &mut Frame,
    area: Rect,
    body: Vec<Line<'static>>,
) {
    let title = info_panel_title(item, accent);
    let block = panel_block_titled(title).padding(Padding::horizontal(2));
    frame.render_widget(Paragraph::new(body).block(block), area);
}

fn info_panel_title(item: &Item, accent: Color) -> Line<'static> {
    Line::from(vec![
        Span::raw(" "),
        Span::styled(
            item.name.clone(),
            Style::default().fg(accent).add_modifier(Modifier::BOLD),
        ),
        Span::raw("  "),
        Span::styled(
            item.kind.label(),
            Style::default()
                .fg(Color::DarkGray)
                .add_modifier(Modifier::ITALIC),
        ),
        Span::raw(" "),
    ])
}

fn render_output_for_item(app: &App, item: &Item, accent: Color, frame: &mut Frame, area: Rect) {
    crate::tui::views::control_center::dispatch::for_kind(item.kind)
        .render_output(&item.name, app, accent, frame, area);
}

/// Idle output panel — used by recipes / scripts / lifecycle row
/// when nothing has been run yet this session. Title carries an
/// "idle" badge instead of an exit code or duration.
pub(crate) fn render_idle_output(
    accent: Color,
    frame: &mut Frame,
    area: Rect,
    body_text: &str,
    hint: Option<&str>,
) {
    let title = output_pane_title(OutputStatus::Idle, accent);
    let block = panel_block_titled(title).padding(Padding::horizontal(2));
    let mut lines = vec![Line::from(Span::styled(
        body_text.to_string(),
        Style::default().fg(Color::DarkGray),
    ))];
    if let Some(h) = hint {
        lines.push(Line::from(""));
        lines.push(Line::from(Span::styled(
            h.to_string(),
            Style::default().fg(Color::DarkGray),
        )));
    }
    frame.render_widget(Paragraph::new(lines).block(block), area);
}

pub(crate) fn render_run_buffer(run: &RunState, accent: Color, frame: &mut Frame, area: Rect) {
    let title = output_pane_title(OutputStatus::Run(run), accent);
    let block = panel_block_titled(title).padding(Padding::horizontal(2));
    let max_lines = area.height.saturating_sub(2) as usize;
    let total = run.buffer.len();
    let start = total.saturating_sub(max_lines);
    let lines: Vec<Line<'static>> = run
        .buffer
        .iter()
        .skip(start)
        .map(render_captured_line)
        .collect();
    let paragraph = Paragraph::new(lines)
        .block(block)
        .wrap(Wrap { trim: false });
    frame.render_widget(paragraph, area);
}

// ─────────────────────── info panel body ───────────────────────

/// Body for the info panel that sits above the output pane. The
/// panel TITLE carries the item name + kind; the body is the
/// definition list — `desc` (when present) leads as the first row,
/// followed by the static kv pairs (in / path / forward_args / …).
/// Lines render under `Padding::horizontal(2)`, so neither the kv
/// builder nor the desc wrapper add a left gutter themselves.
fn build_info_body(app: &App, item: &Item, wrap_width: usize) -> Vec<Line<'static>> {
    let (desc, kv_lines) = match item.kind {
        ItemKind::Recipe => app
            .selected_recipe()
            .map(|r| (r.desc.clone(), recipe_kv_lines(r)))
            .unwrap_or_default(),
        ItemKind::Script => app
            .selected_script()
            .map(|s| (s.desc.clone(), script_kv_lines(s)))
            .unwrap_or_default(),
        ItemKind::Watcher => app
            .selected_watcher()
            .map(|w| (None, watcher_kv_lines(w)))
            .unwrap_or_default(),
        // Services and containers don't have config-level
        // descriptions or static kv data worth showing here. Title
        // already carries the name + kind; status moves into the
        // output panel header.
        _ => (None, Vec::new()),
    };

    let mut lines: Vec<Line<'static>> = Vec::new();
    if let Some(text) = desc {
        lines.extend(kv_wrapped("desc", &text, wrap_width));
    }
    lines.extend(kv_lines);
    lines
}

fn recipe_kv_lines(recipe: &crate::config::Recipe) -> Vec<Line<'static>> {
    let mut out = Vec::new();
    out.push(kv("in", recipe.service.as_deref().unwrap_or("host")));
    if recipe.tty {
        out.push(kv("tty", "true"));
    }
    if recipe.forward_args {
        out.push(kv("forward_args", "true"));
    }
    if recipe.parallel {
        out.push(kv("parallel", "true"));
    }
    if !recipe.needs.is_empty() {
        out.push(kv("needs", &recipe.needs.join(", ")));
    }
    for (k, v) in &recipe.env {
        out.push(kv(&format!("env.{k}"), v));
    }
    out
}

fn watcher_kv_lines(watcher: &WatcherPane) -> Vec<Line<'static>> {
    let mut out = Vec::new();
    out.push(kv("recipe", &watcher.recipe));
    out.push(kv("globs", &watcher.globs.join(", ")));
    out.push(kv(
        "debounce",
        &format!("{} ms", watcher.debounce.as_millis()),
    ));
    out
}

fn script_kv_lines(script: &crate::config::ScriptCommand) -> Vec<Line<'static>> {
    let mut out = Vec::new();
    out.push(kv("in", script.service.as_deref().unwrap_or("host")));
    let path_display = script
        .path
        .file_name()
        .map(|f| format!(".croft/commands/{}", f.to_string_lossy()))
        .unwrap_or_else(|| script.path.display().to_string());
    out.push(kv("path", &path_display));
    if script.tty {
        out.push(kv("tty", "true"));
    }
    if script.forward_args {
        out.push(kv("forward_args", "true"));
    }
    if !script.needs.is_empty() {
        out.push(kv("needs", &script.needs.join(", ")));
    }
    for (k, v) in &script.env {
        out.push(kv(&format!("env.{k}"), v));
    }
    out
}

/// Word-wrap `text` to lines no wider than `width`. Single words
/// longer than `width` (rare in human-written descriptions) get
/// their own line and overflow the bound — we'd rather show them
/// truncated by ratatui at the panel border than break inside a
/// word. Empty input → empty output.
fn wrap_words(text: &str, width: usize) -> Vec<String> {
    if width == 0 {
        return vec![text.to_string()];
    }
    let mut out: Vec<String> = Vec::new();
    let mut current = String::new();
    for word in text.split_whitespace() {
        if current.is_empty() {
            current.push_str(word);
        } else if current.len() + 1 + word.len() <= width {
            current.push(' ');
            current.push_str(word);
        } else {
            out.push(std::mem::take(&mut current));
            current.push_str(word);
        }
    }
    if !current.is_empty() {
        out.push(current);
    }
    out
}

pub(crate) fn kv(key: &str, value: &str) -> Line<'static> {
    Line::from(vec![
        Span::styled(format!("{key:<14}"), Style::default().fg(Color::DarkGray)),
        Span::raw(value.to_string()),
    ])
}

/// Same as [`kv`] but wraps long values across multiple rows with a
/// hanging indent so continuation lines align under the value
/// column. Used for `desc`, which is the only kv whose value can
/// realistically run wider than the panel.
fn kv_wrapped(key: &str, value: &str, wrap_width: usize) -> Vec<Line<'static>> {
    const KEY_COL: usize = 14;
    let value_width = wrap_width.saturating_sub(KEY_COL).max(1);
    let chunks = wrap_words(value, value_width);
    if chunks.is_empty() {
        return vec![kv(key, "")];
    }
    chunks
        .into_iter()
        .enumerate()
        .map(|(i, chunk)| {
            let label = if i == 0 {
                format!("{key:<KEY_COL$}")
            } else {
                " ".repeat(KEY_COL)
            };
            Line::from(vec![
                Span::styled(label, Style::default().fg(Color::DarkGray)),
                Span::raw(chunk),
            ])
        })
        .collect()
}

// ─────────────────────── output pane title ───────────────────────

/// Variants the output panel's title bar surfaces. The item name +
/// kind already live in the info panel above; the output title only
/// carries dynamic state — exit code, duration, running indicator,
/// idle/missing/error badges.
enum OutputStatus<'a> {
    Idle,
    Run(&'a RunState),
    Service(&'a ServicePane),
    Watcher(&'a WatcherPane),
}

fn output_pane_title(status: OutputStatus<'_>, accent: Color) -> Line<'static> {
    let mut spans: Vec<Span<'static>> = vec![
        Span::raw(" "),
        Span::styled(
            "output",
            Style::default().fg(accent).add_modifier(Modifier::BOLD),
        ),
    ];
    let suffix: Vec<Span<'static>> = match status {
        OutputStatus::Idle => vec![Span::styled(
            "  idle ".to_string(),
            Style::default().fg(Color::DarkGray),
        )],
        OutputStatus::Run(run) => run_status_spans(run),
        OutputStatus::Service(svc) => service_status_spans(svc),
        OutputStatus::Watcher(w) => watcher_status_spans(w, accent),
    };
    spans.extend(suffix);
    Line::from(spans)
}

fn run_status_spans(run: &RunState) -> Vec<Span<'static>> {
    let duration = format_duration(run.duration());
    if let Some(code) = run.exit_code {
        if code == 0 {
            return vec![Span::styled(
                format!("  ✓ exit 0 · {duration} "),
                Style::default()
                    .fg(Color::Green)
                    .add_modifier(Modifier::BOLD),
            )];
        }
        return vec![Span::styled(
            format!("  ✗ exit {code} · {duration} "),
            Style::default().fg(Color::Red).add_modifier(Modifier::BOLD),
        )];
    }
    if let Some(err) = &run.error {
        return vec![Span::styled(
            format!("  ! {err} · {duration} "),
            Style::default().fg(Color::Red),
        )];
    }
    vec![Span::styled(
        format!("  ● running · {duration} "),
        Style::default()
            .fg(Color::Yellow)
            .add_modifier(Modifier::BOLD),
    )]
}

/// Compact duration formatter for run-pane titles. Sub-second times
/// land in `0.4s` form; ≥10s uses one decimal less; ≥1m switches to
/// `m m s s` form so titles don't sprawl on long runs.
fn format_duration(d: std::time::Duration) -> String {
    let secs = d.as_secs_f64();
    if secs < 10.0 {
        format!("{secs:.1}s")
    } else if secs < 60.0 {
        format!("{secs:.0}s")
    } else {
        let total = secs as u64;
        let m = total / 60;
        let s = total % 60;
        format!("{m}m{s:02}s")
    }
}

fn render_captured_line(line: &CapturedLine) -> Line<'static> {
    // Stream tint is the *base* style: stderr stays red when the
    // line carries no ANSI of its own (most of our captures don't,
    // since piping disables color in most CLIs), and SGR resets
    // (`ESC[0m`) inside a line drop back to that base rather than
    // ratatui's default. Programs that do emit color (e.g. invoked
    // with FORCE_COLOR=1, or anything routed through the tmux
    // preview path) get rendered with their own palette on top.
    let base = match line.stream {
        OutputStream::Stdout => Style::default(),
        OutputStream::Stderr => Style::default().fg(Color::Red),
    };
    crate::tui::ansi::ansi_to_line(&line.text, base)
}

pub(crate) fn render_service_logs(
    service: &ServicePane,
    accent: Color,
    frame: &mut Frame,
    area: Rect,
) {
    let title = output_pane_title(OutputStatus::Service(service), accent);

    // Horizontal-only padding — every output pane sits in the same
    // visual frame so all three (run / service / lifecycle) read
    // consistently. Compose's own `<svc-N> |` log prefix lives
    // inside the body.
    let block = panel_block_titled(title).padding(Padding::horizontal(2));

    let max_lines = area.height.saturating_sub(2) as usize;
    let total = service.buffer.len();
    let start = total.saturating_sub(max_lines);
    let lines: Vec<Line<'static>> = service
        .buffer
        .iter()
        .skip(start)
        .map(render_captured_line)
        .collect();

    let body = if service.buffer.is_empty() && service.tail_error.is_none() {
        // Block padding now provides the left gutter; no leading "  "
        // needed in the placeholder.
        vec![Line::from(Span::styled(
            "waiting for output…",
            Style::default().fg(Color::DarkGray),
        ))]
    } else if let Some(err) = &service.tail_error {
        // No leading "  " — the block padding handles it, including
        // for wrapped continuation lines.
        vec![
            Line::from(Span::styled(
                format!("log tail unavailable: {err}"),
                Style::default().fg(Color::Red),
            )),
            Line::from(""),
            Line::from(Span::styled(
                "set runtime.backend = \"compose\" in croft.toml",
                Style::default().fg(Color::DarkGray),
            )),
            Line::from(Span::styled(
                "and ensure docker compose is on PATH.",
                Style::default().fg(Color::DarkGray),
            )),
        ]
    } else {
        lines
    };

    let paragraph = Paragraph::new(body).block(block).wrap(Wrap { trim: false });
    frame.render_widget(paragraph, area);
}

fn service_status_spans(service: &ServicePane) -> Vec<Span<'static>> {
    if service.tail_error.is_some() {
        return vec![Span::styled(
            "  ✗ unavailable ".to_string(),
            Style::default().fg(Color::Red).add_modifier(Modifier::BOLD),
        )];
    }
    match service.status {
        Some(ServiceStatus::Running) => vec![Span::styled(
            "  ● running ".to_string(),
            Style::default()
                .fg(Color::Green)
                .add_modifier(Modifier::BOLD),
        )],
        Some(ServiceStatus::Stopped) => vec![Span::styled(
            "  ○ stopped ".to_string(),
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD),
        )],
        Some(ServiceStatus::Missing) => vec![Span::styled(
            "  ✗ missing ".to_string(),
            Style::default().fg(Color::Red).add_modifier(Modifier::BOLD),
        )],
        None => vec![Span::styled(
            "  ? unknown ".to_string(),
            Style::default().fg(Color::DarkGray),
        )],
    }
}

pub(crate) fn render_watcher(watcher: &WatcherPane, accent: Color, frame: &mut Frame, area: Rect) {
    // Watcher meta (recipe / globs / debounce) lives in the info
    // panel above. The output pane is purely the buffer from the
    // most recent run, with a placeholder when nothing has run yet —
    // matching the recipe / script / lifecycle pane shape.
    let title = output_pane_title(OutputStatus::Watcher(watcher), accent);
    let block = panel_block_titled(title).padding(Padding::horizontal(2));

    let max_lines = area.height.saturating_sub(2) as usize;
    let total = watcher.buffer.len();
    let start = total.saturating_sub(max_lines);
    let captured: Vec<Line<'static>> = watcher
        .buffer
        .iter()
        .skip(start)
        .map(render_captured_line)
        .collect();

    let body = if captured.is_empty() {
        let placeholder = match watcher.state {
            WatcherState::Idle if watcher.last_exit_code.is_none() => {
                "no run yet — edit a watched file"
            }
            WatcherState::Idle => "idle — buffer cleared on next run",
            WatcherState::Debouncing => "cooldown…",
            WatcherState::Running => "starting…",
        };
        vec![Line::from(Span::styled(
            placeholder.to_string(),
            Style::default().fg(Color::DarkGray),
        ))]
    } else {
        captured
    };

    let paragraph = Paragraph::new(body).block(block).wrap(Wrap { trim: false });
    frame.render_widget(paragraph, area);
}

fn watcher_status_spans(watcher: &WatcherPane, accent: Color) -> Vec<Span<'static>> {
    match watcher.state {
        WatcherState::Idle => match watcher.last_exit_code {
            None => vec![Span::styled(
                "  ○ idle ".to_string(),
                Style::default().fg(Color::DarkGray),
            )],
            Some(0) => vec![Span::styled(
                "  ✓ idle (last 0) ".to_string(),
                Style::default()
                    .fg(Color::Green)
                    .add_modifier(Modifier::BOLD),
            )],
            Some(c) => vec![Span::styled(
                format!("  ✗ idle (last {c}) "),
                Style::default().fg(Color::Red).add_modifier(Modifier::BOLD),
            )],
        },
        WatcherState::Debouncing => {
            let remaining = watcher.pending_remaining_ms().unwrap_or(0);
            vec![Span::styled(
                format!("  ◐ cooldown {remaining} ms "),
                Style::default()
                    .fg(Color::Yellow)
                    .add_modifier(Modifier::BOLD),
            )]
        }
        WatcherState::Running => {
            let elapsed = watcher
                .last_run_started_at
                .map(|t| t.elapsed().as_secs_f32())
                .unwrap_or(0.0);
            vec![Span::styled(
                format!("  ● running {elapsed:.1}s "),
                Style::default().fg(accent).add_modifier(Modifier::BOLD),
            )]
        }
    }
}

pub(crate) fn watcher_indicator_style(app: &App, name: &str) -> Style {
    let Some(w) = app.watchers().get(name) else {
        return Style::default().fg(Color::DarkGray);
    };
    match w.state {
        WatcherState::Idle => match w.last_exit_code {
            None => Style::default().fg(Color::DarkGray),
            Some(0) => Style::default().fg(Color::Green),
            Some(_) => Style::default().fg(Color::Red),
        },
        WatcherState::Debouncing => Style::default().fg(Color::Yellow),
        WatcherState::Running => Style::default().fg(accent_of(app)),
    }
}

pub(crate) fn service_indicator_style(app: &App, service: &str) -> Style {
    if let Some(pane) = app.services().get(service)
        && pane.tail_error.is_some()
    {
        return Style::default().fg(Color::Red);
    }
    match app.services().get(service).and_then(|p| p.status) {
        Some(ServiceStatus::Running) => Style::default().fg(Color::Green),
        Some(ServiceStatus::Stopped) => Style::default().fg(Color::Yellow),
        Some(ServiceStatus::Missing) => Style::default().fg(Color::Red),
        None => Style::default().fg(Color::DarkGray),
    }
}

pub(crate) fn centered_rect(area: Rect, percent_x: u16, height: u16) -> Rect {
    let h = height.min(area.height);
    let v = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(area.height.saturating_sub(h) / 2),
            Constraint::Length(h),
            Constraint::Min(0),
        ])
        .split(area);
    let h_layout = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Percentage((100 - percent_x) / 2),
            Constraint::Percentage(percent_x),
            Constraint::Percentage((100 - percent_x) / 2),
        ])
        .split(v[1]);
    h_layout[1]
}

pub(crate) fn window(selected: usize, visible: usize, total: usize) -> (usize, usize) {
    if total <= visible {
        return (0, total);
    }
    let half = visible / 2;
    let start = selected.saturating_sub(half).min(total - visible);
    let end = (start + visible).min(total);
    (start, end)
}

// ─────────────────────── status / hint bar ───────────────────────

/// Hint set for the active view. Phases 3 and 4 fill in their own
/// view-specific hints; for now the placeholder views just show
/// the global view-switch keys.
fn view_hints(app: &App) -> Vec<(&'static str, &'static str)> {
    match app.view() {
        crate::tui::app::View::ControlCenter => control_center_hints(app),
        crate::tui::app::View::Terminals => {
            let mut h: Vec<(&'static str, &'static str)> = vec![
                ("↑↓", "nav"),
                ("enter", "attach"),
                ("n", "new"),
                ("d", "delete"),
                ("C", "control"),
                ("G", "diff"),
                ("W", "worktree"),
            ];
            if app.editor().opens_directory {
                h.push(("E", "open project"));
            }
            h.push(("q", "quit"));
            h
        }
        crate::tui::app::View::Diff => diff_hints(app),
    }
}

fn diff_hints(app: &App) -> Vec<(&'static str, &'static str)> {
    let in_read = app.diff().body_mode() == BodyMode::Read;
    let mut hints: Vec<(&'static str, &'static str)> = match app.diff().focus() {
        DiffFocus::Files => {
            let mut h: Vec<(&'static str, &'static str)> = vec![("↑↓", "file"), ("tab", "body")];
            if !in_read {
                h.push(("]/[", "hunk"));
            }
            // `e` opens a single file — works for any editor (terminal
            // or GUI), so no mode gate here.
            h.push(("e", "edit"));
            h
        }
        DiffFocus::Body => {
            let mut h: Vec<(&'static str, &'static str)> = vec![("↑↓", "scroll"), ("tab", "files")];
            if !in_read {
                h.push(("]/[", "hunk"));
            }
            h.push(("gg/G", "top/bot"));
            h
        }
    };
    hints.push(match app.diff().body_mode() {
        BodyMode::Diff => ("v", "read"),
        BodyMode::Read => ("v", "diff"),
    });
    hints.push(("w", "wrap"));
    if app.diff().lazygit_available {
        hints.push(("L", "lazygit"));
    }
    hints.push(("r", "refresh"));
    hints.push(("C", "control"));
    hints.push(("T", "terminals"));
    hints.push(("W", "worktree"));
    if app.editor().opens_directory {
        hints.push(("E", "open project"));
    }
    hints.push(("q", "quit"));
    hints
}

fn control_center_hints(app: &App) -> Vec<(&'static str, &'static str)> {
    let mode_palette = app.mode() == Mode::Palette;

    let mut hints: Vec<(&str, &str)> = vec![("↑↓", "nav")];
    if !mode_palette {
        // Enter label adapts to the row kind so users see what
        // pressing it will do without consulting docs.
        let enter_label = match app.selected_item().map(|i| i.kind) {
            Some(ItemKind::Runtime) => "up all",
            Some(ItemKind::Service) => "attach",
            Some(ItemKind::Recipe | ItemKind::Script) => "run",
            _ => "select",
        };
        hints.push(("enter", enter_label));
        // Combined single/all keybinds — lowercase acts on the
        // selected service, uppercase on every service. Pairing
        // them in one hint keeps the legend short.
        hints.push(("u/U", "up"));
        hints.push(("r/R", "restart"));
        hints.push(("s/S", "stop"));
        hints.push(("D", "down all"));
        // View / worktree switches at the end so they never compete
        // with the action keys above for short-list real estate.
        hints.push(("T", "terminals"));
        hints.push(("G", "diff"));
        hints.push(("W", "worktree"));
        if app.editor().opens_directory {
            hints.push(("E", "open project"));
        }
    }
    hints.push(("/", "palette"));
    hints.push(("q", "quit"));
    hints
}

fn render_status(app: &App, frame: &mut Frame, area: Rect) {
    if let Some(flash) = app.flash_message() {
        let p = Paragraph::new(Line::from(vec![
            Span::styled(" ! ", Style::default().fg(Color::Black).bg(Color::Yellow)),
            Span::raw(" "),
            Span::styled(flash.to_string(), Style::default().fg(Color::Yellow)),
        ]));
        frame.render_widget(p, area);
        return;
    }

    let hints = view_hints(app);
    let accent = accent_of(app);

    let mut spans: Vec<Span<'static>> = Vec::with_capacity(hints.len() * 4 + 4);
    // Leading view tag — `[control]` / `[terminals]` / `[diff]`.
    // Anchors the user to which view their hints apply to.
    spans.push(Span::raw(" "));
    spans.push(Span::styled(
        format!("[{}]", app.view().tag()),
        Style::default().fg(accent).add_modifier(Modifier::BOLD),
    ));
    spans.push(Span::raw(" "));
    spans.push(Span::styled("·", Style::default().fg(Color::DarkGray)));
    spans.push(Span::raw(" "));
    for (i, (key, label)) in hints.iter().enumerate() {
        if i > 0 {
            spans.push(Span::raw(" "));
            spans.push(Span::styled("·", Style::default().fg(Color::DarkGray)));
            spans.push(Span::raw(" "));
        }
        spans.push(Span::styled(
            (*key).to_string(),
            Style::default().fg(accent).add_modifier(Modifier::BOLD),
        ));
        spans.push(Span::raw(" "));
        spans.push(Span::styled(
            (*label).to_string(),
            Style::default().fg(Color::White).dim(),
        ));
    }
    frame.render_widget(Paragraph::new(Line::from(spans)), area);
}

// ─────────────────────── helpers ───────────────────────

pub(crate) fn panel_block(title: &'static str, accent: Color) -> Block<'static> {
    Block::default()
        .title(Span::styled(
            title,
            Style::default().fg(accent).add_modifier(Modifier::BOLD),
        ))
        .borders(Borders::ALL)
        .border_type(BorderType::Rounded)
        .border_style(Style::default().fg(Color::DarkGray))
}

pub(crate) fn panel_block_titled(title: Line<'static>) -> Block<'static> {
    Block::default()
        .title(title)
        .borders(Borders::ALL)
        .border_type(BorderType::Rounded)
        .border_style(Style::default().fg(Color::DarkGray))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tui::views::diff::state::{DiffFile, DiffStatus};
    use crate::tui::views::terminals::state::TmuxWindow;
    use ratatui::{Terminal, backend::TestBackend};
    use std::sync::Arc;

    fn cfg() -> Arc<crate::config::Config> {
        Arc::new(
            crate::config::parse_str(
                r#"
                [project]
                name = "tuitest"

                [command.up]
                desc = "Start"
                run = "docker compose up"

                [command.test]
                run = "composer test"
                in = "app"
                forward_args = true

                [[ui.pane]]
                type = "service"
                service = "app"

                [[ui.pane]]
                type = "watcher"
                glob = ["*.rs"]
                on_change = "test"
            "#,
            )
            .unwrap(),
        )
    }

    #[test]
    fn renders_without_panic() {
        let backend = TestBackend::new(80, 24);
        let mut terminal = Terminal::new(backend).unwrap();
        let app = App::new(cfg());
        terminal.draw(|f| render(&app, f)).unwrap();
    }

    #[test]
    fn wrap_words_breaks_at_word_boundary() {
        let out = wrap_words("the quick brown fox jumps", 10);
        assert_eq!(out, vec!["the quick", "brown fox", "jumps"]);
    }

    #[test]
    fn wrap_words_oversize_word_keeps_alone() {
        // A single word larger than the width gets its own line; we
        // don't break inside a word.
        let out = wrap_words("foo supercalifragilistic bar", 8);
        assert_eq!(out, vec!["foo", "supercalifragilistic", "bar"]);
    }

    #[test]
    fn wrap_words_collapses_whitespace() {
        // Multiple internal spaces become single spaces (split_whitespace).
        let out = wrap_words("a   b\tc", 10);
        assert_eq!(out, vec!["a b c"]);
    }

    #[test]
    fn wrap_words_empty_input() {
        assert_eq!(wrap_words("", 10), Vec::<String>::new());
    }

    /// Smoke test: open the switcher → switcher_confirm on the
    /// "+ new worktree" sentinel → open_create_form. Render must
    /// not panic, and the rendered buffer must contain the form's
    /// title chrome plus at least one branch row.
    #[test]
    fn renders_create_worktree_form_with_branches() {
        use crate::tui::dialogs::switcher::WorktreeRow;
        let backend = TestBackend::new(120, 30);
        let mut terminal = Terminal::new(backend).unwrap();
        let mut app = App::new(cfg());
        let entries = vec![WorktreeRow {
            path: std::path::PathBuf::from("/repo"),
            branch: Some("main".into()),
            slug: "main".into(),
            is_current: true,
        }];
        app.open_worktree_switcher(entries);
        // Move to "+ new worktree" sentinel and confirm.
        if let Some(s) = app.switcher_mut() {
            s.select_next();
        };
        app.switcher_confirm();
        // Provide a couple of fake branches.
        app.open_create_form(
            vec![
                crate::runtime::BranchEntry {
                    name: "main".into(),
                    remote_only: false,
                },
                crate::runtime::BranchEntry {
                    name: "feat-x".into(),
                    remote_only: false,
                },
            ],
            None,
        );
        terminal.draw(|f| render(&app, f)).unwrap();
        let buffer = terminal.backend().buffer();
        let mut text = String::new();
        for y in 0..buffer.area.height {
            for x in 0..buffer.area.width {
                text.push_str(buffer[(x, y)].symbol());
            }
            text.push('\n');
        }
        assert!(text.contains("new worktree"), "title missing:\n{text}");
        assert!(text.contains("main"), "branch row missing:\n{text}");
        assert!(text.contains("feat-x"), "second branch missing:\n{text}");
    }

    #[test]
    fn renders_empty_config() {
        let backend = TestBackend::new(40, 10);
        let mut terminal = Terminal::new(backend).unwrap();
        let app = App::new(Arc::new(crate::config::Config::default()));
        terminal.draw(|f| render(&app, f)).unwrap();
    }

    #[test]
    fn top_bar_shows_branch_when_set() {
        let backend = TestBackend::new(120, 6);
        let mut terminal = Terminal::new(backend).unwrap();
        let app = App::new(cfg()).with_branch(Some("feature-x".into()));
        terminal.draw(|f| render(&app, f)).unwrap();
        let buffer = terminal.backend().buffer();
        let mut text = String::new();
        for x in 0..buffer.area.width {
            text.push_str(buffer[(x, 0)].symbol());
        }
        assert!(
            text.contains("feature-x"),
            "expected branch in top bar, got:\n{text}"
        );
    }

    #[test]
    fn top_bar_shows_dirty_count_when_files_present() {
        let backend = TestBackend::new(120, 6);
        let mut terminal = Terminal::new(backend).unwrap();
        let mut app = App::new(cfg()).with_branch(Some("main".into()));
        app.diff_mut().set_files(vec![
            DiffFile {
                path: "src/a.rs".into(),
                status: DiffStatus::Modified,
                additions: 0,
                deletions: 0,
                binary: false,
                old_path: None,
            },
            DiffFile {
                path: "src/b.rs".into(),
                status: DiffStatus::Added,
                additions: 0,
                deletions: 0,
                binary: false,
                old_path: None,
            },
        ]);
        terminal.draw(|f| render(&app, f)).unwrap();
        let buffer = terminal.backend().buffer();
        let mut text = String::new();
        for x in 0..buffer.area.width {
            text.push_str(buffer[(x, 0)].symbol());
        }
        assert!(
            text.contains("●2"),
            "expected dirty marker '●2' in top bar, got:\n{text}"
        );
    }

    #[test]
    fn top_bar_omits_branch_when_unset() {
        // No branch (e.g. not in a git repo) → header skips the slot
        // entirely rather than showing an empty separator.
        let backend = TestBackend::new(120, 6);
        let mut terminal = Terminal::new(backend).unwrap();
        let app = App::new(cfg());
        terminal.draw(|f| render(&app, f)).unwrap();
        let buffer = terminal.backend().buffer();
        let mut text = String::new();
        for x in 0..buffer.area.width {
            text.push_str(buffer[(x, 0)].symbol());
        }
        // Project name renders, but no second separator after it.
        assert!(text.contains("tuitest"), "expected project name");
        assert!(
            !text.contains("●"),
            "should not show dirty marker without a branch context"
        );
    }

    #[test]
    fn renders_with_palette_open() {
        let backend = TestBackend::new(120, 30);
        let mut terminal = Terminal::new(backend).unwrap();
        let mut app = App::new(cfg());
        app.open_palette();
        terminal.draw(|f| render(&app, f)).unwrap();
    }

    /// Reproduces the user's report: enter terminals view, create a
    /// new shell, detach, refresh shows 1 window. The sidebar must
    /// render that window — if the row is missing the test asserts
    /// here. Buffer dump on failure tells us where it actually
    /// landed.
    #[test]
    fn terminals_view_renders_a_window_row() {
        let backend = TestBackend::new(120, 30);
        let mut terminal = Terminal::new(backend).unwrap();
        let mut app = App::new(cfg());
        app.terminals_mut().set_tmux_available(true);
        app.switch_view(crate::tui::app::View::Terminals);
        app.terminals_set_windows(vec![TmuxWindow {
            index: 0,
            name: "zsh".into(),
            cwd: None,
            has_bell: false,
            workspace: None,
        }]);
        terminal.draw(|f| render(&app, f)).unwrap();

        // Dump the rendered buffer into a string so we can search
        // it for the window's name. The test fixture has no
        // services, so the row should land in the only sidebar
        // group.
        let buffer = terminal.backend().buffer();
        let mut text = String::new();
        for y in 0..buffer.area.height {
            for x in 0..buffer.area.width {
                text.push_str(buffer[(x, y)].symbol());
            }
            text.push('\n');
        }
        assert!(
            text.contains("zsh"),
            "expected 'zsh' in rendered sidebar, got:\n{text}"
        );
        assert!(
            text.contains("new shell"),
            "expected '+ new shell' sentinel, got:\n{text}"
        );
        // Baseline: no bell → diamond glyph, no filled dot in the
        // sidebar (the dot only appears for service status, which
        // lives elsewhere in the layout).
        assert!(
            text.contains('◇'),
            "expected diamond glyph for bell-less window, got:\n{text}"
        );
    }

    /// When a window has `has_bell` set, the sidebar swaps the
    /// diamond glyph for a filled dot — the visual cue that the
    /// terminal is waiting on the user (e.g. a coding agent rang
    /// the bell).
    #[test]
    fn terminals_view_marks_window_with_pending_bell() {
        let backend = TestBackend::new(120, 30);
        let mut terminal = Terminal::new(backend).unwrap();
        let mut app = App::new(cfg());
        app.terminals_mut().set_tmux_available(true);
        app.switch_view(crate::tui::app::View::Terminals);
        app.terminals_set_windows(vec![TmuxWindow {
            index: 0,
            name: "claude".into(),
            cwd: None,
            has_bell: true,
            workspace: None,
        }]);
        terminal.draw(|f| render(&app, f)).unwrap();

        let buffer = terminal.backend().buffer();
        let mut text = String::new();
        for y in 0..buffer.area.height {
            for x in 0..buffer.area.width {
                text.push_str(buffer[(x, y)].symbol());
            }
            text.push('\n');
        }
        assert!(
            text.contains('●'),
            "expected filled dot for bell-set window, got:\n{text}"
        );
        assert!(
            !text.contains('◇'),
            "diamond should be replaced by dot when bell is set, got:\n{text}"
        );
    }

    /// Same as the previous test but with services in the sidebar
    /// (matching the user's tmp/test fixture which auto-discovers
    /// `app` and `worker` from docker-compose). The terminals
    /// group has to share vertical real estate with services here
    /// — if the constraint math is wrong, the window row gets
    /// pushed out of the visible area.
    #[test]
    fn terminals_view_renders_window_alongside_services() {
        let backend = TestBackend::new(120, 30);
        let mut terminal = Terminal::new(backend).unwrap();
        let cfg = Arc::new(
            crate::config::parse_str(
                r#"
                [project]
                name = "tuitest"

                [runtime]
                backend = "none"

                [[ui.pane]]
                type    = "service"
                service = "app"

                [[ui.pane]]
                type    = "service"
                service = "worker"
            "#,
            )
            .unwrap(),
        );
        let mut app = App::new(cfg);
        app.terminals_mut().set_tmux_available(true);
        app.switch_view(crate::tui::app::View::Terminals);
        app.terminals_set_windows(vec![TmuxWindow {
            index: 0,
            name: "zsh".into(),
            cwd: None,
            has_bell: false,
            workspace: None,
        }]);
        terminal.draw(|f| render(&app, f)).unwrap();

        let buffer = terminal.backend().buffer();
        let mut text = String::new();
        for y in 0..buffer.area.height {
            for x in 0..buffer.area.width {
                text.push_str(buffer[(x, y)].symbol());
            }
            text.push('\n');
        }
        assert!(
            text.contains("zsh"),
            "expected 'zsh' alongside services, got:\n{text}"
        );
        // Always print so we can eyeball the actual layout — passes
        // either way; failure already prints text.
        println!("---rendered buffer---\n{text}---end---");
    }
}