Skip to content

Commit b2f0584

Browse files
committed
Move LineSegment styles to bitflags Struct
1 parent ea756cf commit b2f0584

File tree

10 files changed

+92
-87
lines changed

10 files changed

+92
-87
lines changed

src/components/edit.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
components::shared::EditableLine,
88
display::DisplayColor,
99
input::{Event, InputOptions, KeyCode, KeyEvent, KeyModifiers},
10-
view::{LineSegment, ViewData, ViewDataUpdater, ViewLine},
10+
view::{LineSegment, LineSegmentOptions, ViewData, ViewDataUpdater, ViewLine},
1111
};
1212

1313
lazy_static! {
@@ -74,9 +74,7 @@ impl Edit {
7474
self.editable_line.set_label(LineSegment::new_with_color_and_style(
7575
label,
7676
DisplayColor::Normal,
77-
true,
78-
false,
79-
false,
77+
LineSegmentOptions::DIMMED,
8078
));
8179
}
8280

src/components/help.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
process::Results,
1010
select,
1111
util::handle_view_data_scroll,
12-
view::{LineSegment, ViewData, ViewLine},
12+
view::{LineSegment, LineSegmentOptions, ViewData, ViewLine},
1313
};
1414

1515
const INPUT_OPTIONS: InputOptions = InputOptions::RESIZE
@@ -44,11 +44,9 @@ impl Help {
4444
ViewLine::new_pinned(vec![LineSegment::new_with_color_and_style(
4545
format!(" {0:width$} Action", "Key", width = max_key_length).as_str(),
4646
DisplayColor::Normal,
47-
false,
48-
true,
49-
false,
47+
LineSegmentOptions::UNDERLINED,
5048
)])
51-
.set_padding_with_color_and_style(' ', DisplayColor::Normal, false, true, false),
49+
.set_padding_with_color_and_style(' ', DisplayColor::Normal, LineSegmentOptions::UNDERLINED),
5250
);
5351

5452
for line in keybindings {
@@ -58,7 +56,7 @@ impl Help {
5856
format!(" {0:width$}", line.0.join(", "), width = max_key_length).as_str(),
5957
DisplayColor::IndicatorColor,
6058
),
61-
LineSegment::new_with_color_and_style("|", DisplayColor::Normal, true, false, false),
59+
LineSegment::new_with_color_and_style("|", DisplayColor::Normal, LineSegmentOptions::DIMMED),
6260
LineSegment::new(line.1.as_str()),
6361
],
6462
2,

src/components/shared/editable_line.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use unicode_segmentation::UnicodeSegmentation;
33
use crate::{
44
display::DisplayColor,
55
input::{Event, KeyCode, KeyEvent, KeyModifiers},
6-
view::LineSegment,
6+
view::{LineSegment, LineSegmentOptions},
77
};
88

99
#[derive(Debug, PartialEq, Eq)]
@@ -79,10 +79,14 @@ impl EditableLine {
7979
}
8080
segments.push(
8181
if indicator.is_empty() {
82-
LineSegment::new_with_color_and_style(" ", DisplayColor::Normal, false, true, false)
82+
LineSegment::new_with_color_and_style(" ", DisplayColor::Normal, LineSegmentOptions::UNDERLINED)
8383
}
8484
else {
85-
LineSegment::new_with_color_and_style(indicator.as_str(), DisplayColor::Normal, false, true, false)
85+
LineSegment::new_with_color_and_style(
86+
indicator.as_str(),
87+
DisplayColor::Normal,
88+
LineSegmentOptions::UNDERLINED,
89+
)
8690
},
8791
);
8892
if !end.is_empty() {

src/modules/list/utils.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
display::DisplayColor,
99
modules::list::search::LineMatch,
1010
todo_file::{Action, Line, TodoFile},
11-
view::LineSegment,
11+
view::{LineSegment, LineSegmentOptions},
1212
};
1313

1414
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -269,9 +269,7 @@ pub(super) fn get_todo_line_segments(
269269
segments.push(LineSegment::new_with_color_and_style(
270270
indicator,
271271
DisplayColor::Normal,
272-
!is_cursor_line && selected,
273-
false,
274-
false,
272+
LineSegmentOptions::conditional(!is_cursor_line && selected, LineSegmentOptions::DIMMED),
275273
));
276274

277275
let action_padding = cmp::max(maximum_action_width, 6);
@@ -317,9 +315,7 @@ pub(super) fn get_todo_line_segments(
317315
else {
318316
DisplayColor::Normal
319317
},
320-
false,
321-
search_hash_match && is_search_index,
322-
false,
318+
LineSegmentOptions::conditional(search_hash_match && is_search_index, LineSegmentOptions::UNDERLINED),
323319
));
324320
segments.push(LineSegment::new(" "));
325321
},
@@ -345,9 +341,7 @@ pub(super) fn get_todo_line_segments(
345341
segments.push(LineSegment::new_with_color_and_style(
346342
term,
347343
DisplayColor::IndicatorColor,
348-
false,
349-
is_search_index,
350-
false,
344+
LineSegmentOptions::conditional(is_search_index, LineSegmentOptions::UNDERLINED),
351345
));
352346
if !split.is_empty() {
353347
segments.push(LineSegment::new(split));

src/modules/show_commit/view_builder.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
get_partition_index_on_whitespace_for_line,
77
get_stat_item_segments,
88
},
9-
view::{LineSegment, ViewDataUpdater, ViewLine},
9+
view::{LineSegment, LineSegmentOptions, ViewDataUpdater, ViewLine},
1010
};
1111

1212
const PADDING_CHARACTER: char = '\u{2015}'; // '―'
@@ -253,7 +253,7 @@ impl ViewBuilder {
253253
for delta in status.deltas() {
254254
updater.push_line(ViewLine::new_empty_line());
255255
updater.push_line(ViewLine::from(vec![
256-
LineSegment::new_with_color_and_style("@@", DisplayColor::Normal, true, false, false),
256+
LineSegment::new_with_color_and_style("@@", DisplayColor::Normal, LineSegmentOptions::DIMMED),
257257
LineSegment::new_with_color(
258258
format!(
259259
" -{},{} +{},{} ",
@@ -265,7 +265,7 @@ impl ViewBuilder {
265265
.as_str(),
266266
DisplayColor::DiffContextColor,
267267
),
268-
LineSegment::new_with_color_and_style("@@", DisplayColor::Normal, true, false, false),
268+
LineSegment::new_with_color_and_style("@@", DisplayColor::Normal, LineSegmentOptions::DIMMED),
269269
LineSegment::new_with_color(
270270
format!(" {}", delta.context()).as_str(),
271271
DisplayColor::DiffContextColor,
@@ -274,9 +274,7 @@ impl ViewBuilder {
274274
updater.push_line(ViewLine::new_pinned(vec![]).set_padding_with_color_and_style(
275275
PADDING_CHARACTER,
276276
DisplayColor::Normal,
277-
true,
278-
false,
279-
false,
277+
LineSegmentOptions::DIMMED,
280278
));
281279

282280
for line in delta.lines() {

src/view.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use anyhow::{Error, Result};
2727
#[cfg(test)]
2828
pub(crate) use self::render_slice::RenderAction;
2929
pub(crate) use self::{
30-
line_segment::LineSegment,
30+
line_segment::{LineSegment, LineSegmentOptions},
3131
render_context::RenderContext,
3232
render_slice::RenderSlice,
3333
scroll_position::ScrollPosition,

src/view/line_segment.rs

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::cell::RefCell;
22

3+
use bitflags::bitflags;
34
use unicode_segmentation::UnicodeSegmentation;
45
use unicode_width::UnicodeWidthStr;
56
use xi_unicode::EmojiExt;
@@ -41,46 +42,63 @@ impl SegmentPartial {
4142
}
4243
}
4344

45+
bitflags! {
46+
/// Options for the `LineSegment` formatting
47+
#[derive(Default, PartialEq, Eq, Debug, Clone, Copy)]
48+
pub(crate) struct LineSegmentOptions: u8 {
49+
/// None
50+
const NONE = 0b0000_0000;
51+
/// Dimmed
52+
const DIMMED = 0b0000_0001;
53+
/// Dimmed
54+
const REVERSED = 0b0000_0010;
55+
/// Dimmed
56+
const UNDERLINED = 0b0000_0100;
57+
}
58+
}
59+
60+
impl LineSegmentOptions {
61+
pub(crate) fn conditional(condition: bool, options: Self) -> Self {
62+
if condition { options } else { LineSegmentOptions::NONE }
63+
}
64+
}
65+
4466
/// Represents a segment in a larger line.
4567
#[derive(Clone, Debug)]
4668
pub(crate) struct LineSegment {
4769
color: DisplayColor,
48-
dim: bool,
49-
reverse: bool,
70+
options: LineSegmentOptions,
5071
text: String,
5172
length: usize,
52-
underline: bool,
5373
}
5474

5575
impl LineSegment {
5676
/// Create a new instance with just the content.
5777
#[must_use]
5878
pub(crate) fn new(text: &str) -> Self {
59-
Self::new_with_color_and_style(text, DisplayColor::Normal, false, false, false)
79+
Self::new_with_color_and_style(text, DisplayColor::Normal, LineSegmentOptions::NONE)
80+
}
81+
82+
/// Create a new instance with just the content.
83+
#[must_use]
84+
pub(crate) fn new_copy_style(text: &str, segment: &Self) -> Self {
85+
Self::new_with_color_and_style(text, segment.color, segment.options)
6086
}
6187

6288
/// Create a new instance with added color.
6389
#[must_use]
6490
pub(crate) fn new_with_color(text: &str, color: DisplayColor) -> Self {
65-
Self::new_with_color_and_style(text, color, false, false, false)
91+
Self::new_with_color_and_style(text, color, LineSegmentOptions::NONE)
6692
}
6793

6894
/// Create a new instance with added color and style.
6995
#[must_use]
70-
pub(crate) fn new_with_color_and_style(
71-
text: &str,
72-
color: DisplayColor,
73-
dim: bool,
74-
underline: bool,
75-
reverse: bool,
76-
) -> Self {
96+
pub(crate) fn new_with_color_and_style(text: &str, color: DisplayColor, options: LineSegmentOptions) -> Self {
7797
Self {
7898
text: String::from(text),
99+
options,
79100
color,
80-
reverse,
81-
dim,
82101
length: unicode_column_width(text),
83-
underline,
84102
}
85103
}
86104

@@ -93,15 +111,15 @@ impl LineSegment {
93111
}
94112

95113
pub(crate) const fn is_dimmed(&self) -> bool {
96-
self.dim
114+
self.options.contains(LineSegmentOptions::DIMMED)
97115
}
98116

99117
pub(crate) const fn is_underlined(&self) -> bool {
100-
self.underline
118+
self.options.contains(LineSegmentOptions::UNDERLINED)
101119
}
102120

103121
pub(crate) const fn is_reversed(&self) -> bool {
104-
self.reverse
122+
self.options.contains(LineSegmentOptions::REVERSED)
105123
}
106124

107125
pub(crate) const fn get_length(&self) -> usize {
@@ -187,9 +205,7 @@ mod tests {
187205
let line_segment = LineSegment::new_with_color_and_style(
188206
"Sævör grét áðan því úlpan var ónýt",
189207
DisplayColor::IndicatorColor,
190-
true,
191-
true,
192-
true,
208+
LineSegmentOptions::all(),
193209
);
194210

195211
assert_eq!(line_segment.get_color(), DisplayColor::IndicatorColor);
@@ -204,9 +220,7 @@ mod tests {
204220
let line_segment = LineSegment::new_with_color_and_style(
205221
"? דג סקרן שט בים מאוכזב ולפתע מצא לו חברה איך הקליטה",
206222
DisplayColor::IndicatorColor,
207-
false,
208-
false,
209-
false,
223+
LineSegmentOptions::NONE,
210224
);
211225

212226
assert_eq!(line_segment.get_color(), DisplayColor::IndicatorColor);
@@ -218,8 +232,11 @@ mod tests {
218232

219233
#[test]
220234
fn line_segment_case_new_with_color_and_style_all_styles_dimmed() {
221-
let line_segment =
222-
LineSegment::new_with_color_and_style("Test String", DisplayColor::IndicatorColor, true, false, false);
235+
let line_segment = LineSegment::new_with_color_and_style(
236+
"Test String",
237+
DisplayColor::IndicatorColor,
238+
LineSegmentOptions::DIMMED,
239+
);
223240

224241
assert!(line_segment.is_dimmed());
225242
assert!(!line_segment.is_underlined());
@@ -228,8 +245,11 @@ mod tests {
228245

229246
#[test]
230247
fn line_segment_case_new_with_color_and_style_all_styles_underlined() {
231-
let line_segment =
232-
LineSegment::new_with_color_and_style("Test String", DisplayColor::IndicatorColor, false, true, false);
248+
let line_segment = LineSegment::new_with_color_and_style(
249+
"Test String",
250+
DisplayColor::IndicatorColor,
251+
LineSegmentOptions::UNDERLINED,
252+
);
233253

234254
assert!(!line_segment.is_dimmed());
235255
assert!(line_segment.is_underlined());
@@ -238,8 +258,11 @@ mod tests {
238258

239259
#[test]
240260
fn line_segment_case_new_with_color_and_style_all_styles_reversed() {
241-
let line_segment =
242-
LineSegment::new_with_color_and_style("Test String", DisplayColor::IndicatorColor, false, false, true);
261+
let line_segment = LineSegment::new_with_color_and_style(
262+
"Test String",
263+
DisplayColor::IndicatorColor,
264+
LineSegmentOptions::REVERSED,
265+
);
243266

244267
assert!(!line_segment.is_dimmed());
245268
assert!(!line_segment.is_underlined());

src/view/render_slice.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -422,13 +422,7 @@ impl RenderSlice {
422422
let partial = segment.get_partial_segment(left_start, window_width - cursor);
423423

424424
if partial.get_length() > 0 {
425-
segments.push(LineSegment::new_with_color_and_style(
426-
partial.get_content(),
427-
segment.get_color(),
428-
segment.is_dimmed(),
429-
segment.is_underlined(),
430-
segment.is_reversed(),
431-
));
425+
segments.push(LineSegment::new_copy_style(partial.get_content(), segment));
432426

433427
cursor += partial.get_length();
434428
if cursor >= window_width {
@@ -443,12 +437,9 @@ impl RenderSlice {
443437

444438
if cursor < window_width {
445439
if let Some(padding) = line.get_padding().as_ref() {
446-
segments.push(LineSegment::new_with_color_and_style(
440+
segments.push(LineSegment::new_copy_style(
447441
padding.get_content().repeat(window_width - cursor).as_str(),
448-
padding.get_color(),
449-
padding.is_dimmed(),
450-
padding.is_underlined(),
451-
padding.is_reversed(),
442+
padding,
452443
));
453444
}
454445
}

src/view/render_slice/tests.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::{
88
ExactPattern,
99
LinePattern,
1010
},
11+
view::LineSegmentOptions,
1112
};
1213

1314
fn assert_rendered(render_slice: &RenderSlice, expected: &[&str]) {
@@ -850,9 +851,7 @@ fn with_padding() {
850851
updater.push_line(ViewLine::from("Foo").set_padding_with_color_and_style(
851852
'*',
852853
DisplayColor::ActionPick,
853-
true,
854-
true,
855-
true,
854+
LineSegmentOptions::all(),
856855
));
857856
});
858857
let render_slice = create_render_slice(8, 1, &view_data);
@@ -868,9 +867,7 @@ fn with_padding_no_width() {
868867
updater.push_line(ViewLine::from("Foo").set_padding_with_color_and_style(
869868
'*',
870869
DisplayColor::ActionPick,
871-
true,
872-
true,
873-
true,
870+
LineSegmentOptions::all(),
874871
));
875872
});
876873
let render_slice = create_render_slice(3, 1, &view_data);

0 commit comments

Comments
 (0)