Skip to content

Commit 03c67b1

Browse files
committed
fix(tui): wrap the denial row and hint footer on a narrow popup
Making the scroll clamp exact meant dropping Paragraph's Wrap, which also removed wrapping from the lines that do not go through push_wrapped. At 70 columns the denial row clipped mid-value and lost the last-seen timestamp, and at 60 a pending chunk with scrollable content pushed [Esc] Close past the right edge of the single hint row. The key still worked, but that is the same controls-not-visible failure one axis over. Keep the denial row on one line while it fits and wrap it onto the label indent when it does not, so the common 80-column layout is unchanged. Pack the footer hints into as many rows as they need without splitting a hint, and derive the footer height from that: adding a hint row shrinks the body and can itself change whether the content scrolls, so the two settle together. The earlier tests were all 80x24, which is why they missed this. Cover the denial timestamps and the approve, reject and close hints at 60, 70 and 80 columns, plus the packing helper directly. Part of NVIDIA#1098. Signed-off-by: Vyncint Ng <[email protected]>
1 parent f18b11b commit 03c67b1

1 file changed

Lines changed: 193 additions & 59 deletions

File tree

‎crates/openshell-tui/src/ui/sandbox_draft.rs‎

Lines changed: 193 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -272,26 +272,47 @@ pub fn draw_detail_popup(
272272
);
273273
}
274274

275-
// Hit count (accumulated real denial count) and first/last seen.
276-
lines.push(Line::from(vec![
277-
Span::styled("Denied: ", t.muted),
278-
Span::styled(
279-
format!(
280-
"{} connection{}",
281-
chunk.hit_count,
282-
if chunk.hit_count == 1 { "" } else { "s" }
283-
),
275+
// Hit count (accumulated real denial count) and first/last seen. Kept on one
276+
// row while it fits, so a narrow terminal wraps it instead of losing the tail.
277+
let denied_label = "Denied: ";
278+
let denied_count = format!(
279+
"{} connection{}",
280+
chunk.hit_count,
281+
if chunk.hit_count == 1 { "" } else { "s" }
282+
);
283+
let denied_seen = format!(
284+
"(first {} / last {})",
285+
format_short_time(chunk.first_seen_ms),
286+
format_short_time(chunk.last_seen_ms),
287+
);
288+
let denied_width = denied_label.chars().count()
289+
+ denied_count.chars().count()
290+
+ 2
291+
+ denied_seen.chars().count();
292+
if denied_width <= text_width {
293+
lines.push(Line::from(vec![
294+
Span::styled(denied_label, t.muted),
295+
Span::styled(denied_count, t.accent),
296+
Span::styled(format!(" {denied_seen}"), t.muted),
297+
]));
298+
} else {
299+
push_wrapped(
300+
&mut lines,
301+
denied_label,
302+
t.muted,
303+
&denied_count,
284304
t.accent,
285-
),
286-
Span::styled(
287-
format!(
288-
" (first {} / last {})",
289-
format_short_time(chunk.first_seen_ms),
290-
format_short_time(chunk.last_seen_ms),
291-
),
305+
text_width,
306+
);
307+
push_wrapped(
308+
&mut lines,
309+
&" ".repeat(denied_label.chars().count()),
292310
t.muted,
293-
),
294-
]));
311+
&denied_seen,
312+
t.muted,
313+
text_width,
314+
);
315+
}
295316

296317
// Endpoints.
297318
if let Some(ref rule) = chunk.proposed_rule {
@@ -349,15 +370,36 @@ pub fn draw_detail_popup(
349370
);
350371
}
351372

352-
// Split the inner area into a scrollable body and a pinned hint row, so the
353-
// action and close controls stay on screen however long the content is.
373+
// Split the inner area into a scrollable body and pinned hint rows, so the
374+
// action and close controls stay on screen however long the content is. The
375+
// hints need a second row on a narrow terminal and that shrinks the body, so
376+
// settle the two together.
354377
let inner = block.inner(popup_area);
378+
let inner_height = usize::from(inner.height);
379+
let total_rows = lines.len();
380+
let max_footer = inner_height.saturating_sub(1).max(1);
381+
382+
let mut footer_rows = 1usize;
383+
let mut hint_rows = Vec::new();
384+
for _ in 0..2 {
385+
let body = inner_height.saturating_sub(footer_rows);
386+
let scrollable = total_rows.saturating_sub(body) > 0;
387+
hint_rows = pack_hints(&hint_units(chunk, t, scrollable), text_width);
388+
let needed = hint_rows.len().clamp(1, max_footer);
389+
if needed == footer_rows {
390+
break;
391+
}
392+
footer_rows = needed;
393+
}
394+
355395
let parts = Layout::default()
356396
.direction(Direction::Vertical)
357-
.constraints([Constraint::Min(0), Constraint::Length(1)])
397+
.constraints([
398+
Constraint::Min(0),
399+
Constraint::Length(u16::try_from(footer_rows).unwrap_or(1)),
400+
])
358401
.split(inner);
359402
let body_height = usize::from(parts[0].height);
360-
let total_rows = lines.len();
361403
let max_scroll = total_rows.saturating_sub(body_height);
362404
let scroll = scroll.min(max_scroll);
363405

@@ -378,43 +420,7 @@ pub fn draw_detail_popup(
378420
Paragraph::new(lines).scroll((u16::try_from(scroll).unwrap_or(u16::MAX), 0)),
379421
parts[0],
380422
);
381-
382-
// Action hints — state-aware toggle keys, pinned below the scrolling body.
383-
let mut hint_spans: Vec<Span<'_>> = Vec::new();
384-
match chunk.status.as_str() {
385-
"pending" => {
386-
hint_spans.extend([
387-
Span::styled("[a]", t.key_hint),
388-
Span::styled(" Approve ", t.text),
389-
Span::styled("[x]", t.key_hint),
390-
Span::styled(" Reject ", t.text),
391-
]);
392-
}
393-
"approved" => {
394-
hint_spans.extend([
395-
Span::styled("[x]", t.key_hint),
396-
Span::styled(" Revoke ", t.text),
397-
]);
398-
}
399-
"rejected" => {
400-
hint_spans.extend([
401-
Span::styled("[a]", t.key_hint),
402-
Span::styled(" Approve ", t.text),
403-
]);
404-
}
405-
_ => {}
406-
}
407-
if max_scroll > 0 {
408-
hint_spans.extend([
409-
Span::styled("[j/k]", t.key_hint),
410-
Span::styled(" Scroll ", t.text),
411-
]);
412-
}
413-
hint_spans.extend([
414-
Span::styled("[Esc]", t.muted),
415-
Span::styled(" Close", t.muted),
416-
]);
417-
frame.render_widget(Paragraph::new(Line::from(hint_spans)), parts[1]);
423+
frame.render_widget(Paragraph::new(hint_rows), parts[1]);
418424

419425
DetailMetrics {
420426
total_rows,
@@ -614,6 +620,64 @@ fn push_wrapped(
614620
}
615621
}
616622

623+
/// One footer hint: the spans that render it, and its display width.
624+
type HintUnit = (Vec<Span<'static>>, usize);
625+
626+
/// State-aware hints for the detail popup footer.
627+
fn hint_units(chunk: &PolicyChunk, t: &crate::theme::Theme, scrollable: bool) -> Vec<HintUnit> {
628+
let mut units: Vec<HintUnit> = Vec::new();
629+
{
630+
let mut add = |key: &str, label: &str, key_style: Style, label_style: Style| {
631+
units.push((
632+
vec![
633+
Span::styled(key.to_string(), key_style),
634+
Span::styled(label.to_string(), label_style),
635+
],
636+
key.chars().count() + label.chars().count(),
637+
));
638+
};
639+
match chunk.status.as_str() {
640+
"pending" => {
641+
add("[a]", " Approve ", t.key_hint, t.text);
642+
add("[x]", " Reject ", t.key_hint, t.text);
643+
}
644+
"approved" => add("[x]", " Revoke ", t.key_hint, t.text),
645+
"rejected" => add("[a]", " Approve ", t.key_hint, t.text),
646+
_ => {}
647+
}
648+
if scrollable {
649+
add("[j/k]", " Scroll ", t.key_hint, t.text);
650+
}
651+
add("[Esc]", " Close", t.muted, t.muted);
652+
}
653+
units
654+
}
655+
656+
/// Pack hint units into rows no wider than `width`, never splitting a unit.
657+
///
658+
/// A narrow terminal would otherwise clip the trailing hints, and `[Esc] Close`
659+
/// is the last one.
660+
fn pack_hints(units: &[HintUnit], width: usize) -> Vec<Line<'static>> {
661+
let mut rows: Vec<Line<'static>> = Vec::new();
662+
let mut current: Vec<Span<'static>> = Vec::new();
663+
let mut current_width = 0usize;
664+
for (spans, unit_width) in units {
665+
if current_width + unit_width > width && !current.is_empty() {
666+
rows.push(Line::from(std::mem::take(&mut current)));
667+
current_width = 0;
668+
}
669+
current.extend(spans.iter().cloned());
670+
current_width += unit_width;
671+
}
672+
if !current.is_empty() {
673+
rows.push(Line::from(current));
674+
}
675+
if rows.is_empty() {
676+
rows.push(Line::from(String::new()));
677+
}
678+
rows
679+
}
680+
617681
/// The reviewer's persisted note for a rejected chunk.
618682
///
619683
/// Gated on status rather than on the field alone: the gateway's
@@ -1096,4 +1160,74 @@ mod tests {
10961160
"scroll hint should be hidden when everything fits"
10971161
);
10981162
}
1163+
1164+
fn denied_chunk(status: &str, reason: &str) -> PolicyChunk {
1165+
PolicyChunk {
1166+
status: status.to_string(),
1167+
rejection_reason: reason.to_string(),
1168+
rule_name: "allow-github".to_string(),
1169+
confidence: 0.82,
1170+
hit_count: 3,
1171+
first_seen_ms: 1_700_000_000_000,
1172+
last_seen_ms: 1_700_000_100_000,
1173+
..Default::default()
1174+
}
1175+
}
1176+
1177+
/// Dropping `Wrap` means anything not routed through `push_wrapped` clips
1178+
/// horizontally, so the denial timestamps have to wrap on a narrow popup.
1179+
#[test]
1180+
fn denied_timestamps_survive_a_narrow_popup() {
1181+
for width in [60u16, 70u16, 80u16] {
1182+
let chunk = denied_chunk("rejected", "scope this to docs/ paths only");
1183+
let (screen, _) = render(&chunk, width, 24, 0);
1184+
assert!(
1185+
screen.contains("22:13:20"),
1186+
"first-seen lost at width {width}"
1187+
);
1188+
assert!(
1189+
screen.contains("22:15:00"),
1190+
"last-seen lost at width {width}"
1191+
);
1192+
}
1193+
}
1194+
1195+
/// The close hint is the last one, so a narrow footer would drop it first.
1196+
#[test]
1197+
fn close_hint_survives_a_narrow_popup_with_scrollable_content() {
1198+
for width in [60u16, 70u16, 80u16] {
1199+
let chunk = denied_chunk("pending", "");
1200+
let chunk = PolicyChunk {
1201+
rationale: long_reason(),
1202+
..chunk
1203+
};
1204+
let (screen, max_scroll) = render(&chunk, width, 24, 0);
1205+
assert!(max_scroll > 0, "content should overflow at width {width}");
1206+
assert!(screen.contains("Close"), "close hint lost at width {width}");
1207+
assert!(
1208+
screen.contains("Approve"),
1209+
"approve hint lost at width {width}"
1210+
);
1211+
assert!(
1212+
screen.contains("Reject"),
1213+
"reject hint lost at width {width}"
1214+
);
1215+
}
1216+
}
1217+
1218+
#[test]
1219+
fn hints_pack_onto_one_row_when_they_fit() {
1220+
let theme = Theme::dark();
1221+
let chunk = denied_chunk("pending", "");
1222+
let rows = pack_hints(&hint_units(&chunk, &theme, true), 200);
1223+
assert_eq!(rows.len(), 1);
1224+
}
1225+
1226+
#[test]
1227+
fn hints_spill_onto_a_second_row_when_they_do_not_fit() {
1228+
let theme = Theme::dark();
1229+
let chunk = denied_chunk("pending", "");
1230+
let rows = pack_hints(&hint_units(&chunk, &theme, true), 30);
1231+
assert!(rows.len() > 1, "hints should wrap at 30 columns");
1232+
}
10991233
}

0 commit comments

Comments
 (0)