Skip to content

Commit a4d7804

Browse files
authored
fix: Make Table Cells set the style of their text (#13776)
1 parent ea2541f commit a4d7804

File tree

4 files changed

+169
-4
lines changed

4 files changed

+169
-4
lines changed

helix-term/src/ui/picker.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,10 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
309309
F: Fn(&mut Context, &T, Action) + 'static,
310310
{
311311
let columns: Arc<[_]> = columns.into_iter().collect();
312-
let matcher_columns = columns.iter().filter(|col| col.filter).count() as u32;
312+
let matcher_columns = columns
313+
.iter()
314+
.filter(|col: &&Column<T, D>| col.filter)
315+
.count() as u32;
313316
assert!(matcher_columns > 0);
314317
let matcher = Nucleo::new(
315318
Config::DEFAULT,

helix-tui/src/text.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,30 @@ impl<'a> Text<'a> {
374374
self.lines.len()
375375
}
376376

377+
/// Patch text with a new style. Only updates fields that are in the new style.
378+
///
379+
/// # Examples
380+
///
381+
/// ```rust
382+
/// # use helix_tui::text::Text;
383+
/// # use helix_view::graphics::{Color, Style};
384+
/// let style1 = Style::default().fg(Color::Yellow);
385+
/// let style2 = Style::default().fg(Color::Yellow).bg(Color::Black);
386+
/// let mut half_styled_text = Text::styled(String::from("The first line\nThe second line"), style1);
387+
/// let full_styled_text = Text::styled(String::from("The first line\nThe second line"), style2);
388+
/// assert_ne!(half_styled_text, full_styled_text);
389+
///
390+
/// half_styled_text.patch_style(Style::default().bg(Color::Black));
391+
/// assert_eq!(half_styled_text, full_styled_text);
392+
/// ```
393+
pub fn patch_style(&mut self, style: Style) {
394+
for line in &mut self.lines {
395+
for span in &mut line.0 {
396+
span.style = span.style.patch(style);
397+
}
398+
}
399+
}
400+
377401
/// Apply a new style to existing text.
378402
///
379403
/// # Examples
@@ -386,13 +410,13 @@ impl<'a> Text<'a> {
386410
/// let styled_text = Text::styled(String::from("The first line\nThe second line"), style);
387411
/// assert_ne!(raw_text, styled_text);
388412
///
389-
/// raw_text.patch_style(style);
413+
/// raw_text.set_style(style);
390414
/// assert_eq!(raw_text, styled_text);
391415
/// ```
392-
pub fn patch_style(&mut self, style: Style) {
416+
pub fn set_style(&mut self, style: Style) {
393417
for line in &mut self.lines {
394418
for span in &mut line.0 {
395-
span.style = span.style.patch(style);
419+
span.style = style;
396420
}
397421
}
398422
}

helix-tui/src/widgets/table.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,15 @@ impl Cell<'_> {
3838
/// Set the `Style` of this cell.
3939
pub fn style(mut self, style: Style) -> Self {
4040
self.style = style;
41+
self.content.set_style(style);
4142
self
4243
}
44+
45+
/// Set the `Style` of this cell.
46+
pub fn set_style(&mut self, style: Style) {
47+
self.style = style;
48+
self.content.set_style(style);
49+
}
4350
}
4451

4552
impl<'a, T> From<T> for Cell<'a>
@@ -453,6 +460,9 @@ impl Table<'_> {
453460
};
454461
if is_selected {
455462
buf.set_style(table_row_area, self.highlight_style);
463+
for cell in &mut table_row.cells {
464+
cell.set_style(self.highlight_style);
465+
}
456466
}
457467
let mut col = table_row_start_col;
458468
for (width, cell) in columns_widths.iter().zip(table_row.cells.iter()) {

helix-tui/tests/text.rs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
use helix_tui::text::{Span, Spans, StyledGrapheme, Text};
2+
use helix_view::graphics::{Color, Modifier, Style};
3+
4+
// Text
5+
#[test]
6+
fn text_width() {
7+
let text = Text::from("The first line\nThe second line");
8+
assert_eq!(15, text.width());
9+
}
10+
11+
#[test]
12+
fn text_height() {
13+
let text = Text::from("The first line\nThe second line");
14+
assert_eq!(2, text.height());
15+
}
16+
17+
#[test]
18+
fn patch_style() {
19+
let style1 = Style::default().fg(Color::Yellow);
20+
let style2 = Style::default().fg(Color::Yellow).bg(Color::Black);
21+
let mut half_styled_text =
22+
Text::styled(String::from("The first line\nThe second line"), style1);
23+
let full_styled_text = Text::styled(String::from("The first line\nThe second line"), style2);
24+
assert_ne!(half_styled_text, full_styled_text);
25+
26+
half_styled_text.patch_style(Style::default().bg(Color::Black));
27+
assert_eq!(half_styled_text, full_styled_text);
28+
}
29+
30+
#[test]
31+
fn set_style() {
32+
let style = Style::default()
33+
.fg(Color::Yellow)
34+
.add_modifier(Modifier::ITALIC);
35+
let mut raw_text = Text::raw("The first line\nThe second line");
36+
let styled_text = Text::styled(String::from("The first line\nThe second line"), style);
37+
assert_ne!(raw_text, styled_text);
38+
39+
raw_text.set_style(style);
40+
assert_eq!(raw_text, styled_text);
41+
}
42+
43+
#[test]
44+
fn text_extend() {
45+
let style = Style::default()
46+
.fg(Color::Yellow)
47+
.add_modifier(Modifier::ITALIC);
48+
let mut text = Text::from("The first line\nThe second line");
49+
assert_eq!(2, text.height());
50+
51+
// Adding two more unstyled lines
52+
text.extend(Text::raw("These are two\nmore lines!"));
53+
assert_eq!(4, text.height());
54+
55+
// Adding a final two styled lines
56+
text.extend(Text::styled("Some more lines\nnow with more style!", style));
57+
assert_eq!(6, text.height());
58+
}
59+
60+
// Span
61+
62+
#[test]
63+
fn styled_graphemes() {
64+
let style = Style::default().fg(Color::Yellow);
65+
let span = Span::styled("Text", style);
66+
let style = Style::default().fg(Color::Green).bg(Color::Black);
67+
let styled_graphemes = span.styled_graphemes(style);
68+
assert_eq!(
69+
vec![
70+
StyledGrapheme {
71+
symbol: "T",
72+
style: Style {
73+
fg: Some(Color::Yellow),
74+
bg: Some(Color::Black),
75+
underline_color: None,
76+
underline_style: None,
77+
add_modifier: Modifier::empty(),
78+
sub_modifier: Modifier::empty(),
79+
},
80+
},
81+
StyledGrapheme {
82+
symbol: "e",
83+
style: Style {
84+
fg: Some(Color::Yellow),
85+
bg: Some(Color::Black),
86+
underline_color: None,
87+
underline_style: None,
88+
add_modifier: Modifier::empty(),
89+
sub_modifier: Modifier::empty(),
90+
},
91+
},
92+
StyledGrapheme {
93+
symbol: "x",
94+
style: Style {
95+
fg: Some(Color::Yellow),
96+
bg: Some(Color::Black),
97+
underline_color: None,
98+
underline_style: None,
99+
add_modifier: Modifier::empty(),
100+
sub_modifier: Modifier::empty(),
101+
},
102+
},
103+
StyledGrapheme {
104+
symbol: "t",
105+
style: Style {
106+
fg: Some(Color::Yellow),
107+
bg: Some(Color::Black),
108+
underline_color: None,
109+
underline_style: None,
110+
add_modifier: Modifier::empty(),
111+
sub_modifier: Modifier::empty(),
112+
},
113+
},
114+
],
115+
styled_graphemes.collect::<Vec<StyledGrapheme>>()
116+
);
117+
}
118+
119+
// Spans
120+
121+
#[test]
122+
fn spans_width() {
123+
let spans = Spans::from(vec![
124+
Span::styled("My", Style::default().fg(Color::Yellow)),
125+
Span::raw(" text"),
126+
]);
127+
assert_eq!(7, spans.width());
128+
}

0 commit comments

Comments
 (0)