cargo-port 0.0.3

A TUI for inspecting and managing Rust projects
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
use std::borrow::Cow;

use ratatui::style::Modifier;
use ratatui::style::Style;
use ratatui::widgets::Block;
use ratatui::widgets::Borders;

mod ci;
mod git;
mod lang;
mod lints;
mod package;

#[cfg(test)]
pub(super) use ci::CI_COMPACT_DURATION_WIDTH;
#[cfg(test)]
pub(super) use ci::ci_table_shows_durations;
#[cfg(test)]
pub(super) use ci::ci_total_width;
pub(super) use ci::render_ci_panel;
#[cfg(test)]
pub(super) use git::git_label_width;
pub(super) use git::render_git_panel;
pub(super) use lang::render_lang_panel_standalone;
pub(super) use lints::render_lints_panel;
pub(super) use package::RenderStyles;
#[cfg(test)]
pub(super) use package::description_lines;
#[cfg(test)]
pub(super) use package::detail_column_scroll_offset;
#[cfg(test)]
pub(super) use package::package_label_width;
pub(super) use package::render_empty_targets_panel;
pub(super) use package::render_package_panel;
pub(super) use package::render_targets_panel;
#[cfg(test)]
pub(super) use package::stats_column_width;

use super::constants::ACTIVE_BORDER_COLOR;
use super::constants::INACTIVE_BORDER_COLOR;
use super::constants::INACTIVE_TITLE_COLOR;
use super::constants::TITLE_COLOR;
use super::detail::CiData;
use super::detail::GitData;
use super::detail::LintsData;
use super::detail::PackageData;
use super::detail::TargetsData;
use super::types::Pane;
use super::types::PaneId;

#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) struct PaneTitleGroup<'a> {
    pub label:  Cow<'a, str>,
    pub len:    usize,
    pub cursor: Option<usize>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) enum PaneTitleCount<'a> {
    None,
    Single {
        len:    usize,
        cursor: Option<usize>,
    },
    Grouped(Vec<PaneTitleGroup<'a>>),
}

impl PaneTitleCount<'_> {
    fn count_text(len: usize, cursor: Option<usize>) -> String {
        if let Some(pos) = cursor
            && pos < len
        {
            crate::tui::types::scroll_indicator(pos, len)
        } else {
            len.to_string()
        }
    }

    pub(super) fn body(&self) -> String {
        match self {
            Self::None => String::new(),
            Self::Single { len, cursor } => format!("({})", Self::count_text(*len, *cursor)),
            Self::Grouped(groups) => groups
                .iter()
                .map(|group| {
                    format!(
                        "{} ({})",
                        group.label,
                        Self::count_text(group.len, group.cursor)
                    )
                })
                .collect::<Vec<_>>()
                .join(", "),
        }
    }
}

pub(super) fn pane_title(title: &str, count: &PaneTitleCount<'_>) -> String {
    let body = count.body();
    if body.is_empty() {
        format!(" {title} ")
    } else {
        format!(" {title} {body} ")
    }
}

pub(super) fn prefixed_pane_title(title: &str, count: &PaneTitleCount<'_>) -> String {
    let body = count.body();
    if body.is_empty() {
        format!(" {title} ")
    } else {
        format!(" {title}: {body} ")
    }
}

#[derive(Clone, Copy)]
pub(super) struct PaneChrome {
    pub active_border:   Style,
    pub inactive_border: Style,
    pub active_title:    Style,
    pub inactive_title:  Style,
}

impl PaneChrome {
    pub(super) fn block(self, title: String, focused: bool) -> Block<'static> {
        Block::default()
            .borders(Borders::ALL)
            .title(title)
            .title_style(if focused {
                self.active_title
            } else {
                self.inactive_title
            })
            .border_style(if focused {
                self.active_border
            } else {
                self.inactive_border
            })
    }

    pub(super) const fn with_inactive_border(self, inactive_border: Style) -> Self {
        Self {
            inactive_border,
            ..self
        }
    }
}

pub(super) fn default_pane_chrome() -> PaneChrome {
    let title_style = Style::default().add_modifier(Modifier::BOLD);
    PaneChrome {
        active_border:   Style::default().fg(ACTIVE_BORDER_COLOR),
        inactive_border: Style::default(),
        active_title:    title_style.fg(TITLE_COLOR),
        inactive_title:  title_style.fg(INACTIVE_TITLE_COLOR),
    }
}

pub(super) fn empty_pane_block(title: impl Into<String>) -> Block<'static> {
    Block::default()
        .borders(Borders::ALL)
        .title(title.into())
        .title_style(Style::default().fg(INACTIVE_BORDER_COLOR))
        .border_style(Style::default().fg(INACTIVE_BORDER_COLOR))
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct PanePlacement {
    pub pane:     PaneId,
    pub row:      usize,
    pub col:      usize,
    pub row_span: usize,
    pub col_span: usize,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct PaneGridLayout {
    pub placements: &'static [PanePlacement],
}

const TILED_LAYOUT: PaneGridLayout = PaneGridLayout {
    placements: &[
        PanePlacement {
            pane:     PaneId::ProjectList,
            row:      0,
            col:      0,
            row_span: 2,
            col_span: 1,
        },
        PanePlacement {
            pane:     PaneId::Package,
            row:      0,
            col:      1,
            row_span: 1,
            col_span: 1,
        },
        PanePlacement {
            pane:     PaneId::Git,
            row:      0,
            col:      2,
            row_span: 1,
            col_span: 1,
        },
        PanePlacement {
            pane:     PaneId::Lang,
            row:      1,
            col:      1,
            row_span: 1,
            col_span: 1,
        },
        PanePlacement {
            pane:     PaneId::Targets,
            row:      1,
            col:      2,
            row_span: 1,
            col_span: 1,
        },
        PanePlacement {
            pane:     PaneId::Lints,
            row:      2,
            col:      0,
            row_span: 1,
            col_span: 1,
        },
        PanePlacement {
            pane:     PaneId::CiRuns,
            row:      2,
            col:      1,
            row_span: 1,
            col_span: 2,
        },
    ],
};

const OUTPUT_LAYOUT: PaneGridLayout = PaneGridLayout {
    placements: &[
        PanePlacement {
            pane:     PaneId::ProjectList,
            row:      0,
            col:      0,
            row_span: 2,
            col_span: 1,
        },
        PanePlacement {
            pane:     PaneId::Package,
            row:      0,
            col:      1,
            row_span: 1,
            col_span: 1,
        },
        PanePlacement {
            pane:     PaneId::Git,
            row:      0,
            col:      2,
            row_span: 1,
            col_span: 1,
        },
        PanePlacement {
            pane:     PaneId::Lang,
            row:      1,
            col:      1,
            row_span: 1,
            col_span: 1,
        },
        PanePlacement {
            pane:     PaneId::Targets,
            row:      1,
            col:      2,
            row_span: 1,
            col_span: 1,
        },
        PanePlacement {
            pane:     PaneId::Output,
            row:      2,
            col:      0,
            row_span: 1,
            col_span: 3,
        },
    ],
};

impl PaneGridLayout {
    pub(super) fn tab_order(self) -> Vec<PaneId> {
        let mut placements = self.placements.to_vec();
        placements.sort_by_key(|placement| (placement.row, placement.col));
        placements
            .into_iter()
            .map(|placement| placement.pane)
            .collect()
    }
}

// ── PaneManager ────────────────────────────────────────────────────

/// Owns all pane navigation state and per-pane data models.
///
/// Extracted from `App` so render functions can borrow `PaneManager`
/// mutably while borrowing `App` immutably for project data. Each pane
/// owns its display data — no shared monolithic struct.
pub(super) struct PaneManager {
    panes:            Vec<Pane>,
    // Per-pane data models — populated when the selected project changes.
    pub package_data: Option<PackageData>,
    pub git_data:     Option<GitData>,
    pub targets_data: Option<TargetsData>,
    pub ci_data:      Option<CiData>,
    pub lints_data:   Option<LintsData>,
}

impl PaneManager {
    pub fn pane(&self, id: PaneId) -> &Pane { &self.panes[id.index()] }

    pub fn pane_mut(&mut self, id: PaneId) -> &mut Pane { &mut self.panes[id.index()] }

    pub fn new() -> Self {
        Self {
            panes:        vec![Pane::new(); PaneId::pane_count()],
            package_data: None,
            git_data:     None,
            targets_data: None,
            ci_data:      None,
            lints_data:   None,
        }
    }

    pub fn clear_hover(&mut self) {
        for pane in &mut self.panes {
            pane.set_hovered(None);
        }
    }

    pub(super) const fn layout(output_visible: bool) -> PaneGridLayout {
        if output_visible {
            OUTPUT_LAYOUT
        } else {
            TILED_LAYOUT
        }
    }

    pub(super) fn tab_order(output_visible: bool) -> Vec<PaneId> {
        Self::layout(output_visible).tab_order()
    }

    /// Populate per-pane data for the selected row. Called when the
    /// selected project changes or detail cache is rebuilt.
    pub fn set_detail_data(
        &mut self,
        package_data: PackageData,
        git_data: GitData,
        targets_data: TargetsData,
        ci_data: CiData,
        lints_data: LintsData,
    ) {
        self.package_data = Some(package_data);
        self.git_data = Some(git_data);
        self.targets_data = Some(targets_data);
        self.ci_data = Some(ci_data);
        self.lints_data = Some(lints_data);
    }

    /// Clear per-pane data (e.g., when no project is selected).
    pub fn clear_detail_data(&mut self) {
        self.package_data = None;
        self.git_data = None;
        self.targets_data = None;
        self.ci_data = None;
        self.lints_data = None;
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashSet;

    use super::OUTPUT_LAYOUT;
    use super::PaneGridLayout;
    use super::PaneId;
    use super::PanePlacement;
    use super::PaneTitleCount;
    use super::PaneTitleGroup;
    use super::TILED_LAYOUT;
    use super::pane_title;
    use super::prefixed_pane_title;

    #[test]
    fn tiled_layout_has_no_overlapping_cells() { assert_layout_has_no_overlaps(TILED_LAYOUT); }

    #[test]
    fn output_layout_has_no_overlapping_cells() { assert_layout_has_no_overlaps(OUTPUT_LAYOUT); }

    #[test]
    fn tab_order_is_derived_from_grid_position() {
        let layout = PaneGridLayout {
            placements: &[
                PanePlacement {
                    pane:     PaneId::Targets,
                    row:      1,
                    col:      2,
                    row_span: 1,
                    col_span: 1,
                },
                PanePlacement {
                    pane:     PaneId::ProjectList,
                    row:      0,
                    col:      0,
                    row_span: 2,
                    col_span: 1,
                },
                PanePlacement {
                    pane:     PaneId::Git,
                    row:      0,
                    col:      2,
                    row_span: 1,
                    col_span: 1,
                },
                PanePlacement {
                    pane:     PaneId::Package,
                    row:      0,
                    col:      1,
                    row_span: 1,
                    col_span: 1,
                },
            ],
        };

        assert_eq!(
            layout.tab_order(),
            vec![
                super::PaneId::ProjectList,
                super::PaneId::Package,
                super::PaneId::Git,
                super::PaneId::Targets,
            ]
        );
    }

    #[test]
    fn single_title_count_formats_cursor_position() {
        assert_eq!(
            pane_title(
                "Languages",
                &PaneTitleCount::Single {
                    len:    4,
                    cursor: Some(1),
                }
            ),
            " Languages (2 of 4) "
        );
    }

    #[test]
    fn single_title_count_ignores_out_of_range_cursor() {
        assert_eq!(
            pane_title(
                "Lint Runs",
                &PaneTitleCount::Single {
                    len:    3,
                    cursor: Some(9),
                }
            ),
            " Lint Runs (3) "
        );
    }

    #[test]
    fn grouped_title_count_formats_each_group() {
        assert_eq!(
            prefixed_pane_title(
                "Targets",
                &PaneTitleCount::Grouped(vec![
                    PaneTitleGroup {
                        label:  "Binary".into(),
                        len:    1,
                        cursor: Some(0),
                    },
                    PaneTitleGroup {
                        label:  "Examples".into(),
                        len:    3,
                        cursor: None,
                    },
                ])
            ),
            " Targets: Binary (1 of 1), Examples (3) "
        );
    }

    fn assert_layout_has_no_overlaps(layout: PaneGridLayout) {
        let mut occupied = HashSet::new();
        for placement in layout.placements {
            for row in placement.row..placement.row + placement.row_span {
                for col in placement.col..placement.col + placement.col_span {
                    assert!(
                        occupied.insert((row, col)),
                        "pane {:?} overlaps cell ({row}, {col})",
                        placement.pane
                    );
                }
            }
        }
    }
}