rusticity-term 0.1.10

Terminal UI components for Rusticity
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 super::*;
use crate::{
    common::{format_iso_timestamp, render_dropdown, render_pagination_text, SortDirection},
    table::TableWindow,
    ui::{
        filter::{render_filter_bar, FilterConfig, FilterControl},
        table::{render_table, Column, TableConfig},
        Summary,
    },
};
use ratatui::{
    prelude::*,
    widgets::{Clear, Paragraph, Wrap},
};

pub(super) fn clean(value: &str) -> String {
    value
        .chars()
        .map(|c| if c.is_control() { ' ' } else { c })
        .collect()
}
fn value(text: &str) -> String {
    if text.is_empty() {
        "-".into()
    } else {
        clean(text)
    }
}
fn display_status(status: &str) -> String {
    match status {
        "ACTIVE" => "Active".into(),
        "COMPLETE" => "Complete".into(),
        "IN_PROGRESS" => "In progress".into(),
        "FAILED" => "Failed".into(),
        "ARCHIVED" => "Archived".into(),
        "SIGNED" => "Signed".into(),
        _ => value(status),
    }
}
fn status_color(status: &str) -> Color {
    match status {
        "ACTIVE" | "COMPLETE" | "SIGNED" => Color::Green,
        "FAILED" | "UNSUPPORTED_IMAGE" | "FINDINGS_UNAVAILABLE" => Color::Red,
        _ => Color::Yellow,
    }
}
fn artifact_type(artifact: &str, manifest: &str) -> String {
    match artifact {
        "application/vnd.docker.container.image.v1+json"
        | "application/vnd.oci.image.config.v1+json" => "Image".into(),
        "" | "-" => match manifest {
            "application/vnd.docker.distribution.manifest.v2+json"
            | "application/vnd.oci.image.manifest.v1+json" => "Image".into(),
            "application/vnd.docker.distribution.manifest.list.v2+json"
            | "application/vnd.oci.image.index.v1+json" => "Image index".into(),
            _ => "-".into(),
        },
        _ => clean(artifact),
    }
}
fn tags(view: &View) -> String {
    match &view.metadata {
        Some(details) if details.tags.is_empty() => "Untagged".into(),
        Some(details) => details
            .tags
            .iter()
            .map(|s| clean(s))
            .collect::<Vec<_>>()
            .join(", "),
        None => format!(
            "{} (snapshot; full tag list not loaded)",
            value(&view.image.tag)
        ),
    }
}
fn uri(app: &App, view: &View) -> String {
    let base = app
        .ecr_state
        .current_repository_uri
        .as_deref()
        .unwrap_or_default();
    if !base.is_empty() {
        let tag = match &view.metadata {
            Some(details) => details.tags.first().map(String::as_str),
            None => {
                Some(view.image.tag.as_str()).filter(|tag| !tag.is_empty() && *tag != "<untagged>")
            }
        };
        return clean(&match tag {
            Some(tag) => format!("{base}:{tag}"),
            None => format!("{base}@{}", view.digest),
        });
    }
    value(&view.image.uri)
}
fn general_fields(app: &App, view: &View) -> Vec<(&'static str, String)> {
    let details = view.metadata.as_ref();
    let image = &view.image;
    vec![
        (
            "Artifact type",
            artifact_type(
                details.map_or(image.artifact_type.as_str(), |d| &d.artifact_type),
                details.map_or("", |d| &d.manifest_type),
            ),
        ),
        (
            "Repository",
            value(
                app.ecr_state
                    .current_repository
                    .as_deref()
                    .unwrap_or_default(),
            ),
        ),
        (
            "Pushed at",
            format_iso_timestamp(details.map_or(image.pushed_at.as_str(), |d| &d.pushed_at)),
        ),
        (
            "Last recorded pull time",
            format_iso_timestamp(
                details.map_or(image.last_pull_time.as_str(), |d| &d.last_pull_time),
            ),
        ),
        (
            "Size (MB)",
            details
                .map_or(Some(image.size_bytes), |d| d.size_bytes)
                .map(|size| format!("{:.2}", size as f64 / 1_000_000.0))
                .unwrap_or_else(|| "-".into()),
        ),
        (
            "Image status",
            display_status(details.map_or("-", |d| &d.image_status)),
        ),
    ]
}
fn scan_fields(view: &View) -> Vec<(&'static str, String)> {
    let data = &view.tabs[DetailTab::Scanning as usize];
    let Some(scan) = &view.scan else {
        let status = match data.error.as_deref() {
            Some(error) if error.contains("ScanNotFoundException") => "No scan",
            Some(error) if error.contains("AccessDenied") => {
                "Access denied — scan availability unknown"
            }
            Some(_) => "Unavailable",
            None if data.loaded => "Unavailable",
            None => "Not checked / loading",
        };
        return vec![("Status", status.into())];
    };
    let mut fields = vec![
        ("Status", display_status(&scan.status)),
        ("Status detail", value(&scan.description)),
        (
            "Scan completed at",
            format_iso_timestamp(&scan.completed_at),
        ),
        (
            "Vulnerability source updated at",
            format_iso_timestamp(&scan.source_updated_at),
        ),
    ];
    for (label, severity) in [
        ("Critical", "CRITICAL"),
        ("High", "HIGH"),
        ("Medium", "MEDIUM"),
        ("Low", "LOW"),
        ("Info", "INFORMATIONAL"),
    ] {
        let count = scan
            .severity_counts
            .get(severity)
            .map(|n| n.to_string())
            .unwrap_or_else(|| {
                if scan.complete() && (!scan.severity_counts.is_empty() || scan.findings.is_empty())
                {
                    "0".into()
                } else {
                    "-".into()
                }
            });
        fields.push((label, count));
    }
    fields
}
fn ribbon_rows(width: u16) -> Vec<Vec<(&'static str, DetailTab)>> {
    let mut rows = vec![];
    let mut row = vec![];
    let mut used = 0;
    for tab in DetailTab::ALL {
        let len = tab.name().len() + if row.is_empty() { 0 } else { 3 };
        if !row.is_empty() && used + len > width as usize {
            rows.push(std::mem::take(&mut row));
            used = 0;
        }
        used += tab.name().len() + if row.is_empty() { 0 } else { 3 };
        row.push((tab.name(), *tab));
    }
    rows.push(row);
    rows
}

pub fn render(frame: &mut Frame, app: &App, area: Rect) -> bool {
    let Some(view) = &app.ecr_state.scan.view else {
        return false;
    };
    // Use the same content-sized, max-three-column labeled pane as other services.
    let summary = Summary::new(
        "Details",
        vec![
            ui::labeled_field("Image tags", tags(view)),
            ui::labeled_field("URI", uri(app, view)),
            ui::labeled_field("Digest", value(&view.digest)),
        ],
    );
    let ribbons = ribbon_rows(area.width);
    let chunks = Layout::vertical([
        Constraint::Length(summary.height(area.width)),
        Constraint::Length(ribbons.len() as u16),
        Constraint::Min(0),
    ])
    .split(area);
    summary.render_fill(frame, chunks[0]);
    for (i, row) in ribbons.iter().enumerate() {
        if i < chunks[1].height as usize {
            ui::render_tabs(
                frame,
                Rect {
                    y: chunks[1].y + i as u16,
                    height: 1,
                    ..chunks[1]
                },
                row,
                &view.tab,
            );
        }
    }
    let mut body = chunks[2];
    if view.tab == DetailTab::General {
        let lines = general_fields(app, view)
            .iter()
            .map(|(label, value)| {
                if *label == "Image status" {
                    ui::labeled_field_colored(
                        label,
                        value,
                        status_color(view.metadata.as_ref().map_or("", |d| &d.image_status)),
                    )
                } else {
                    ui::labeled_field(label, value)
                }
            })
            .collect();
        let used = Summary::new("General information", lines).render(frame, body);
        body.y += used.height;
        body.height = body.height.saturating_sub(used.height);
        let notice = if let Some(error) = &view.data().error {
            Some(format!("Metadata unavailable: {}", clean(error)))
        } else if app.ecr_state.scan.busy() {
            Some("Loading image metadata… (showing last known values)".into())
        } else {
            None
        };
        if let Some(notice) = notice {
            frame.render_widget(Paragraph::new(notice).wrap(Wrap { trim: false }), body);
        }
    } else {
        if view.tab == DetailTab::Scanning {
            let lines = scan_fields(view)
                .iter()
                .map(|(label, text)| {
                    if *label == "Status" {
                        ui::labeled_field_colored(
                            label,
                            text,
                            status_color(view.scan.as_ref().map_or("", |s| &s.status)),
                        )
                    } else {
                        ui::labeled_field(label, text)
                    }
                })
                .collect();
            let used = Summary::new("Scanning and vulnerabilities", lines).render(frame, body);
            body.y += used.height;
            body.height = body.height.saturating_sub(used.height);
        }
        render_results(frame, app, view, body);
    }
    true
}
struct FieldColumn(&'static str, u16);
impl Column<Row> for FieldColumn {
    fn name(&self) -> &str {
        self.0
    }
    fn width(&self) -> u16 {
        self.1
    }
    fn render(&self, row: &Row) -> (String, Style) {
        let text = row.field(self.0);
        (
            value(text),
            if self.0 == "Status" {
                Style::default().fg(status_color(text))
            } else {
                Style::default()
            },
        )
    }
}
fn columns(view: &View) -> Vec<Box<dyn Column<Row>>> {
    view.tab
        .columns()
        .iter()
        .filter(|(id, _)| view.data().visible_column_ids.contains(id))
        .map(|(name, width)| Box::new(FieldColumn(name, *width)) as Box<dyn Column<Row>>)
        .collect()
}
fn empty_message(view: &View, busy: bool) -> &'static str {
    if busy {
        return "Loading…";
    }
    let data = view.data();
    if data.error.is_some() {
        return "Results unavailable or incomplete — see warning above; Ctrl+R retries.";
    }
    if !data.loaded {
        return "Not loaded";
    }
    if !data.table.filter.is_empty() && !data.table.items.is_empty() {
        return "No matching results";
    }
    match view.tab {
        DetailTab::Referrers => "No referrers — this artifact does not have any referrers.",
        DetailTab::Replication => "No replication statuses reported by AWS for this source.",
        DetailTab::Signing => "No signing statuses reported by AWS.",
        DetailTab::Scanning => {
            if !view.scan.as_ref().is_some_and(|s| s.complete()) {
                "Scan results unavailable or incomplete; an empty list does not mean zero vulnerabilities."
            } else if view
                .scan
                .as_ref()
                .is_some_and(|s| s.severity_counts.values().any(|n| *n > 0))
            {
                "AWS reports findings but returned no details; Ctrl+R retries."
            } else {
                "No findings reported by this scan. This is not a guarantee the image is vulnerability-free."
            }
        }
        DetailTab::General => "",
    }
}
fn source_label(view: &View) -> String {
    view.source_tag
        .as_ref()
        .map(|tag| format!("tag: {} (mutable)", clean(tag)))
        .unwrap_or_else(|| "digest".into())
}
fn short_label(text: &str, width: usize) -> String {
    if text.chars().count() <= width {
        text.into()
    } else {
        format!(
            "{}…",
            text.chars()
                .take(width.saturating_sub(1))
                .collect::<String>()
        )
    }
}
fn render_results(frame: &mut Frame, app: &App, view: &View, mut area: Rect) {
    let data = view.data();
    if let Some(error) = &data.error {
        let parts = Layout::vertical([Constraint::Length(2), Constraint::Min(0)]).split(area);
        frame.render_widget(
            Paragraph::new(format!("Warning: {}", clean(error)))
                .style(Style::default().fg(Color::Yellow))
                .wrap(Wrap { trim: false }),
            parts[0],
        );
        area = parts[1];
    }
    let parts = Layout::vertical([Constraint::Length(3), Constraint::Min(0)]).split(area);
    let prepared = data
        .table
        .prepare_view(data.filtered(view.tab), TableWindow::SelectedPage);
    let pagination = render_pagination_text(prepared.current_page, prepared.total_pages);
    let mut controls = vec![];
    if view.tab == DetailTab::Replication {
        controls.push(FilterControl {
            text: short_label(
                &format!("Select status source: {} ▾", source_label(view)),
                area.width.saturating_sub(24) as usize,
            ),
            is_focused: view.selecting_source(),
        });
    }
    controls.push(FilterControl {
        text: pagination.clone(),
        is_focused: data.focus == crate::common::InputFocus::Pagination,
    });
    render_filter_bar(
        frame,
        FilterConfig {
            filter_text: &data.table.filter,
            placeholder: if view.tab.status_filter() {
                "Filter by status"
            } else {
                "Filter results"
            },
            mode: app.mode,
            is_input_focused: data.focus == crate::common::InputFocus::Filter,
            controls,
            area: parts[0],
        },
    );
    let empty = prepared.items.is_empty();
    let table_columns = columns(view);
    let name = if view.tab == DetailTab::Scanning {
        "Vulnerabilities"
    } else {
        view.tab.name()
    };
    render_table(
        frame,
        TableConfig {
            items: prepared.items,
            selected_index: prepared.selected_index,
            expanded_index: prepared.expanded_index,
            columns: &table_columns,
            sort_column: "",
            sort_direction: SortDirection::Asc,
            title: ui::format_title(&format!("{name} ({})", prepared.total_items)),
            area: parts[1],
            get_expanded_content: Some(Box::new(|row: &Row| {
                row.0
                    .iter()
                    .map(|(label, text)| {
                        (
                            format!("{}: {}", clean(label), value(text)),
                            Style::default(),
                        )
                    })
                    .collect()
            })),
            is_active: app.mode == Mode::Normal,
        },
    );
    if empty && parts[1].height > 3 && parts[1].width > 2 {
        let inner = Rect {
            x: parts[1].x + 1,
            y: parts[1].y + 2,
            width: parts[1].width - 2,
            height: parts[1].height - 3,
        };
        frame.render_widget(Clear, inner);
        frame.render_widget(
            Paragraph::new(empty_message(view, app.ecr_state.scan.busy()))
                .wrap(Wrap { trim: false }),
            inner,
        );
    }
    // Same inline control/dropdown renderer as DynamoDB, IAM, EC2 and SQS.
    if let Some(selected) = view.source_picker {
        let sources = view.sources();
        let visible = frame
            .area()
            .bottom()
            .saturating_sub(parts[0].bottom())
            .saturating_sub(2)
            .max(1) as usize;
        let start = selected.saturating_sub(visible.saturating_sub(1));
        let names: Vec<String> = sources
            .iter()
            .skip(start)
            .take(visible)
            .map(|source| {
                short_label(
                    &source
                        .as_ref()
                        .map(|tag| format!("tag: {}", clean(tag)))
                        .unwrap_or_else(|| "digest (immutable)".into()),
                    area.width.saturating_sub(pagination.len() as u16 + 7) as usize,
                )
            })
            .collect();
        render_dropdown(
            frame,
            &names,
            selected.saturating_sub(start),
            parts[0],
            pagination.len() as u16 + 3,
        );
    }
}
pub(super) fn copy_uri(app: &App) -> String {
    let view = app.ecr_state.scan.view.as_ref().unwrap();
    uri(app, view)
}