Skip to content

Commit 914c79e

Browse files
committed
Guard shortcut chrome against inherited terminal colors
1 parent ebc12c9 commit 914c79e

3 files changed

Lines changed: 161 additions & 29 deletions

File tree

‎crates/jcode-tui/src/tui/app/tests/scroll_copy_02/part_02.rs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,31 @@ fn test_expand_badge_rendered_shortcut_expands_with_alt_lowercase_event() {
382382
);
383383
}
384384

385+
#[test]
386+
fn test_expand_badge_brackets_match_dim_shortcut_color() {
387+
let _render_lock = scroll_render_test_lock();
388+
let (app, mut terminal) = make_edit_badge_test_app(20);
389+
render_and_snap(&app, &mut terminal);
390+
let buf = terminal.backend().buffer();
391+
let area = *buf.area();
392+
for row in 0..area.height {
393+
let line = (0..area.width)
394+
.map(|col| buf[(col, row)].symbol())
395+
.collect::<String>();
396+
if let Some(end) = line.find("⇧E] expand") {
397+
let start = line[..end].rfind('[').expect("opening shortcut bracket");
398+
let opening = unicode_width::UnicodeWidthStr::width(&line[..start]) as u16;
399+
let key = unicode_width::UnicodeWidthStr::width(&line[..end]) as u16 + 1;
400+
let closing = key + 1;
401+
assert_ne!(buf[(key, row)].fg, ratatui::style::Color::Reset);
402+
assert_eq!(buf[(opening, row)].fg, buf[(key, row)].fg);
403+
assert_eq!(buf[(closing, row)].fg, buf[(key, row)].fg);
404+
return;
405+
}
406+
}
407+
panic!("expand shortcut must be visible");
408+
}
409+
385410
#[test]
386411
fn test_clicking_expand_edit_badge_expands_to_full_diff() {
387412
let _render_lock = scroll_render_test_lock();

‎crates/jcode-tui/src/tui/ui_viewport.rs‎

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,42 @@ pub(crate) fn expand_badge_reserved_width(badge_text: &str) -> usize {
5858
UnicodeWidthStr::width(format!(" [{}⇧E]{badge_text}", copy_badge_alt_label()).as_str())
5959
}
6060

61+
fn expand_badge_spans(
62+
alt_label: &str,
63+
alt_active: bool,
64+
shift_active: bool,
65+
key_active: bool,
66+
expanded: bool,
67+
) -> [Span<'static>; 7] {
68+
let dim = Style::default().fg(dim_color());
69+
let modifier = |active| {
70+
if active {
71+
Style::default().fg(queued_color()).bold()
72+
} else {
73+
dim
74+
}
75+
};
76+
let key_style = if key_active {
77+
Style::default().fg(accent_color()).bold()
78+
} else {
79+
dim
80+
};
81+
let (label, label_style) = if expanded {
82+
(" ✓ Expanded", Style::default().fg(ai_color()).bold())
83+
} else {
84+
(" expand", dim)
85+
};
86+
[
87+
Span::styled(" ", dim),
88+
Span::styled("[", dim),
89+
Span::styled(alt_label.to_owned(), modifier(alt_active)),
90+
Span::styled("⇧", modifier(shift_active)),
91+
Span::styled("E", key_style),
92+
Span::styled("]", dim),
93+
Span::styled(label, label_style),
94+
]
95+
}
96+
6197
fn expand_badge_target_line(active: &ActiveFileDiffContext, inline: bool) -> usize {
6298
if inline {
6399
active.end_line.saturating_sub(1).max(active.start_line)
@@ -809,36 +845,14 @@ pub(super) fn draw_messages(
809845
*width = (*width).saturating_sub(reserved as u16);
810846
}
811847

812-
let alt_style = if copy_badge_ui.alt_is_active(copy_badge_now) {
813-
Style::default().fg(queued_color()).bold()
814-
} else {
815-
Style::default().fg(dim_color())
816-
};
817-
let shift_style = if copy_badge_ui.shift_is_active(copy_badge_now) {
818-
Style::default().fg(queued_color()).bold()
819-
} else {
820-
Style::default().fg(dim_color())
821-
};
822-
let key_style = if copy_badge_ui.key_is_active('e', copy_badge_now) {
823-
Style::default().fg(accent_color()).bold()
824-
} else {
825-
Style::default().fg(dim_color())
826-
};
827-
828848
let badge_start = line.width().saturating_add(1);
829-
line.spans.push(Span::raw(" "));
830-
line.spans.push(Span::raw("["));
831-
line.spans
832-
.push(Span::styled(copy_badge_alt_label(), alt_style));
833-
line.spans.push(Span::styled("⇧", shift_style));
834-
line.spans.push(Span::styled("E", key_style));
835-
line.spans.push(Span::raw("]"));
836-
let badge_text_style = if expand_feedback_active {
837-
Style::default().fg(ai_color()).bold()
838-
} else {
839-
Style::default().fg(dim_color())
840-
};
841-
line.spans.push(Span::styled(badge_text, badge_text_style));
849+
line.spans.extend(expand_badge_spans(
850+
&copy_badge_alt_label(),
851+
copy_badge_ui.alt_is_active(copy_badge_now),
852+
copy_badge_ui.shift_is_active(copy_badge_now),
853+
copy_badge_ui.key_is_active('e', copy_badge_now),
854+
expand_feedback_active,
855+
));
842856

843857
let final_width = line.width();
844858
let aligned_x = match line.alignment.unwrap_or(Alignment::Left) {

‎crates/jcode-tui/src/tui/ui_viewport_tests.rs‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,96 @@ fn exact_width_expand_badge_leaves_no_content_or_ellipsis() {
174174
assert_eq!(line.width(), 0);
175175
assert!(line.spans.is_empty());
176176
}
177+
178+
// Chrome must own its visible colors instead of relying on a terminal default.
179+
// Whitespace can inherit because it does not draw a foreground glyph.
180+
fn assert_chrome_has_explicit_foregrounds(spans: &[ratatui::text::Span<'_>]) {
181+
for span in spans.iter().filter(|span| !span.content.trim().is_empty()) {
182+
assert!(
183+
span.style
184+
.fg
185+
.is_some_and(|fg| fg != ratatui::style::Color::Reset),
186+
"unstyled UI chrome: {:?}",
187+
span.content,
188+
);
189+
}
190+
}
191+
192+
#[test]
193+
#[should_panic(expected = "unstyled UI chrome")]
194+
fn chrome_style_guard_rejects_raw_punctuation() {
195+
assert_chrome_has_explicit_foregrounds(&[ratatui::text::Span::raw("[")]);
196+
}
197+
198+
#[test]
199+
#[should_panic(expected = "unstyled UI chrome")]
200+
fn chrome_style_guard_rejects_terminal_default_foreground() {
201+
assert_chrome_has_explicit_foregrounds(&[ratatui::text::Span::styled(
202+
"]",
203+
ratatui::style::Style::default().fg(ratatui::style::Color::Reset),
204+
)]);
205+
}
206+
207+
#[test]
208+
fn expand_badge_all_states_have_explicit_chrome_colors() {
209+
for alt_label in ["Alt", "⌥", "Option"] {
210+
for state in 0..16 {
211+
let alt = state & 1 != 0;
212+
let shift = state & 2 != 0;
213+
let key = state & 4 != 0;
214+
let expanded = state & 8 != 0;
215+
let spans = super::expand_badge_spans(alt_label, alt, shift, key, expanded);
216+
assert_chrome_has_explicit_foregrounds(&spans);
217+
for span in spans
218+
.iter()
219+
.filter(|span| matches!(span.content.as_ref(), "[" | "]"))
220+
{
221+
assert_eq!(span.style.fg, Some(super::dim_color()));
222+
assert!(
223+
!span
224+
.style
225+
.add_modifier
226+
.contains(ratatui::style::Modifier::BOLD)
227+
);
228+
}
229+
assert_eq!(
230+
spans[2].style.fg,
231+
Some(if alt {
232+
super::queued_color()
233+
} else {
234+
super::dim_color()
235+
})
236+
);
237+
assert_eq!(
238+
spans[3].style.fg,
239+
Some(if shift {
240+
super::queued_color()
241+
} else {
242+
super::dim_color()
243+
})
244+
);
245+
assert_eq!(
246+
spans[4].style.fg,
247+
Some(if key {
248+
super::accent_color()
249+
} else {
250+
super::dim_color()
251+
})
252+
);
253+
assert_eq!(
254+
spans[6].style.fg,
255+
Some(if expanded {
256+
super::ai_color()
257+
} else {
258+
super::dim_color()
259+
})
260+
);
261+
let rendered = spans
262+
.iter()
263+
.map(|span| span.content.as_ref())
264+
.collect::<String>();
265+
let suffix = if expanded { " ✓ Expanded" } else { " expand" };
266+
assert_eq!(rendered, format!(" [{alt_label}⇧E]{suffix}"));
267+
}
268+
}
269+
}

0 commit comments

Comments
 (0)