summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTim Oram <dev@mitmaro.ca>2024-03-07 09:35:21 -0330
committerTim Oram <dev@mitmaro.ca>2024-03-08 08:58:25 -0330
commitb2f058479f4331f8cc2fca1b63440876154c10d2 (patch)
treed974effbd6b9dda32f2d095981127d14bfcb9c04
parentea756cf3529705c1af3b26d42090f227a902e77c (diff)
Move LineSegment styles to bitflags Struct
-rw-r--r--src/components/edit.rs6
-rw-r--r--src/components/help.rs10
-rw-r--r--src/components/shared/editable_line.rs10
-rw-r--r--src/modules/list/utils.rs14
-rw-r--r--src/modules/show_commit/view_builder.rs10
-rw-r--r--src/view.rs2
-rw-r--r--src/view/line_segment.rs83
-rw-r--r--src/view/render_slice.rs15
-rw-r--r--src/view/render_slice/tests.rs9
-rw-r--r--src/view/view_line.rs20
10 files changed, 92 insertions, 87 deletions
diff --git a/src/components/edit.rs b/src/components/edit.rs
index ed86f10..d9c6721 100644
--- a/src/components/edit.rs
+++ b/src/components/edit.rs
@@ -7,7 +7,7 @@ use crate::{
components::shared::EditableLine,
display::DisplayColor,
input::{Event, InputOptions, KeyCode, KeyEvent, KeyModifiers},
- view::{LineSegment, ViewData, ViewDataUpdater, ViewLine},
+ view::{LineSegment, LineSegmentOptions, ViewData, ViewDataUpdater, ViewLine},
};
lazy_static! {
@@ -74,9 +74,7 @@ impl Edit {
self.editable_line.set_label(LineSegment::new_with_color_and_style(
label,
DisplayColor::Normal,
- true,
- false,
- false,
+ LineSegmentOptions::DIMMED,
));
}
diff --git a/src/components/help.rs b/src/components/help.rs
index 2402ff0..4769db0 100644
--- a/src/components/help.rs
+++ b/src/components/help.rs
@@ -9,7 +9,7 @@ use crate::{
process::Results,
select,
util::handle_view_data_scroll,
- view::{LineSegment, ViewData, ViewLine},
+ view::{LineSegment, LineSegmentOptions, ViewData, ViewLine},
};
const INPUT_OPTIONS: InputOptions = InputOptions::RESIZE
@@ -44,11 +44,9 @@ impl Help {
ViewLine::new_pinned(vec![LineSegment::new_with_color_and_style(
format!(" {0:width$} Action", "Key", width = max_key_length).as_str(),
DisplayColor::Normal,
- false,
- true,
- false,
+ LineSegmentOptions::UNDERLINED,
)])
- .set_padding_with_color_and_style(' ', DisplayColor::Normal, false, true, false),
+ .set_padding_with_color_and_style(' ', DisplayColor::Normal, LineSegmentOptions::UNDERLINED),
);
for line in keybindings {
@@ -58,7 +56,7 @@ impl Help {
format!(" {0:width$}", line.0.join(", "), width = max_key_length).as_str(),
DisplayColor::IndicatorColor,
),
- LineSegment::new_with_color_and_style("|", DisplayColor::Normal, true, false, false),
+ LineSegment::new_with_color_and_style("|", DisplayColor::Normal, LineSegmentOptions::DIMMED),
LineSegment::new(line.1.as_str()),
],
2,
diff --git a/src/components/shared/editable_line.rs b/src/components/shared/editable_line.rs
index 08088fb..c60d763 100644
--- a/src/components/shared/editable_line.rs
+++ b/src/components/shared/editable_line.rs
@@ -3,7 +3,7 @@ use unicode_segmentation::UnicodeSegmentation;
use crate::{
display::DisplayColor,
input::{Event, KeyCode, KeyEvent, KeyModifiers},
- view::LineSegment,
+ view::{LineSegment, LineSegmentOptions},
};
#[derive(Debug, PartialEq, Eq)]
@@ -79,10 +79,14 @@ impl EditableLine {
}
segments.push(
if indicator.is_empty() {
- LineSegment::new_with_color_and_style(" ", DisplayColor::Normal, false, true, false)
+ LineSegment::new_with_color_and_style(" ", DisplayColor::Normal, LineSegmentOptions::UNDERLINED)
}
else {
- LineSegment::new_with_color_and_style(indicator.as_str(), DisplayColor::Normal, false, true, false)
+ LineSegment::new_with_color_and_style(
+ indicator.as_str(),
+ DisplayColor::Normal,
+ LineSegmentOptions::UNDERLINED,
+ )
},
);
if !end.is_empty() {
diff --git a/src/modules/list/utils.rs b/src/modules/list/utils.rs
index 300eeed..b938eac 100644
--- a/src/modules/list/utils.rs
+++ b/src/modules/list/utils.rs
@@ -8,7 +8,7 @@ use crate::{
display::DisplayColor,
modules::list::search::LineMatch,
todo_file::{Action, Line, TodoFile},
- view::LineSegment,
+ view::{LineSegment, LineSegmentOptions},
};
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -269,9 +269,7 @@ pub(super) fn get_todo_line_segments(
segments.push(LineSegment::new_with_color_and_style(
indicator,
DisplayColor::Normal,
- !is_cursor_line && selected,
- false,
- false,
+ LineSegmentOptions::conditional(!is_cursor_line && selected, LineSegmentOptions::DIMMED),
));
let action_padding = cmp::max(maximum_action_width, 6);
@@ -317,9 +315,7 @@ pub(super) fn get_todo_line_segments(
else {
DisplayColor::Normal
},
- false,
- search_hash_match && is_search_index,
- false,
+ LineSegmentOptions::conditional(search_hash_match && is_search_index, LineSegmentOptions::UNDERLINED),
));
segments.push(LineSegment::new(" "));
},
@@ -345,9 +341,7 @@ pub(super) fn get_todo_line_segments(
segments.push(LineSegment::new_with_color_and_style(
term,
DisplayColor::IndicatorColor,
- false,
- is_search_index,
- false,
+ LineSegmentOptions::conditional(is_search_index, LineSegmentOptions::UNDERLINED),
));
if !split.is_empty() {
segments.push(LineSegment::new(split));
diff --git a/src/modules/show_commit/view_builder.rs b/src/modules/show_commit/view_builder.rs
index 81ae94e..1e5c5e5 100644
--- a/src/modules/show_commit/view_builder.rs
+++ b/src/modules/show_commit/view_builder.rs
@@ -6,7 +6,7 @@ use crate::{
get_partition_index_on_whitespace_for_line,
get_stat_item_segments,
},
- view::{LineSegment, ViewDataUpdater, ViewLine},
+ view::{LineSegment, LineSegmentOptions, ViewDataUpdater, ViewLine},
};
const PADDING_CHARACTER: char = '\u{2015}'; // '―'
@@ -253,7 +253,7 @@ impl ViewBuilder {
for delta in status.deltas() {
updater.push_line(ViewLine::new_empty_line());
updater.push_line(ViewLine::from(vec![
- LineSegment::new_with_color_and_style("@@", DisplayColor::Normal, true, false, false),
+ LineSegment::new_with_color_and_style("@@", DisplayColor::Normal, LineSegmentOptions::DIMMED),
LineSegment::new_with_color(
format!(
" -{},{} +{},{} ",
@@ -265,7 +265,7 @@ impl ViewBuilder {
.as_str(),
DisplayColor::DiffContextColor,
),
- LineSegment::new_with_color_and_style("@@", DisplayColor::Normal, true, false, false),
+ LineSegment::new_with_color_and_style("@@", DisplayColor::Normal, LineSegmentOptions::DIMMED),
LineSegment::new_with_color(
format!(" {}", delta.context()).as_str(),
DisplayColor::DiffContextColor,
@@ -274,9 +274,7 @@ impl ViewBuilder {
updater.push_line(ViewLine::new_pinned(vec![]).set_padding_with_color_and_style(
PADDING_CHARACTER,
DisplayColor::Normal,
- true,
- false,
- false,
+ LineSegmentOptions::DIMMED,
));
for line in delta.lines() {
diff --git a/src/view.rs b/src/view.rs
index b1cbdf0..fba4240 100644
--- a/src/view.rs
+++ b/src/view.rs
@@ -27,7 +27,7 @@ use anyhow::{Error, Result};
#[cfg(test)]
pub(crate) use self::render_slice::RenderAction;
pub(crate) use self::{
- line_segment::LineSegment,
+ line_segment::{LineSegment, LineSegmentOptions},
render_context::RenderContext,
render_slice::RenderSlice,
scroll_position::ScrollPosition,
diff --git a/src/view/line_segment.rs b/src/view/line_segment.rs
index 4ca243d..ba73bc6 100644
--- a/src/view/line_segment.rs
+++ b/src/view/line_segment.rs
@@ -1,5 +1,6 @@
use std::cell::RefCell;
+use bitflags::bitflags;
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;
use xi_unicode::EmojiExt;
@@ -41,46 +42,63 @@ impl SegmentPartial {
}
}
+bitflags! {
+ /// Options for the `LineSegment` formatting
+ #[derive(Default, PartialEq, Eq, Debug, Clone, Copy)]
+ pub(crate) struct LineSegmentOptions: u8 {
+ /// None
+ const NONE = 0b0000_0000;
+ /// Dimmed
+ const DIMMED = 0b0000_0001;
+ /// Dimmed
+ const REVERSED = 0b0000_0010;
+ /// Dimmed
+ const UNDERLINED = 0b0000_0100;
+ }
+}
+
+impl LineSegmentOptions {
+ pub(crate) fn conditional(condition: bool, options: Self) -> Self {
+ if condition { options } else { LineSegmentOptions::NONE }
+ }
+}
+
/// Represents a segment in a larger line.
#[derive(Clone, Debug)]
pub(crate) struct LineSegment {
color: DisplayColor,
- dim: bool,
- reverse: bool,
+ options: LineSegmentOptions,
text: String,
length: usize,
- underline: bool,
}
impl LineSegment {
/// Create a new instance with just the content.
#[must_use]
pub(crate) fn new(text: &str) -> Self {
- Self::new_with_color_and_style(text, DisplayColor::Normal, false, false, false)
+ Self::new_with_color_and_style(text, DisplayColor::Normal, LineSegmentOptions::NONE)
+ }
+
+ /// Create a new instance with just the content.
+ #[must_use]
+ pub(crate) fn new_copy_style(text: &str, segment: &Self) -> Self {
+ Self::new_with_color_and_style(text, segment.color, segment.options)
}
/// Create a new instance with added color.
#[must_use]
pub(crate) fn new_with_color(text: &str, color: DisplayColor) -> Self {
- Self::new_with_color_and_style(text, color, false, false, false)
+ Self::new_with_color_and_style(text, color, LineSegmentOptions::NONE)
}
/// Create a new instance with added color and style.
#[must_use]
- pub(crate) fn new_with_color_and_style(
- text: &str,
- color: DisplayColor,
- dim: bool,
- underline: bool,
- reverse: bool,
- ) -> Self {
+ pub(crate) fn new_with_color_and_style(text: &str, color: DisplayColor, options: LineSegmentOptions) -> Self {
Self {
text: String::from(text),
+ options,
color,
- reverse,
- dim,
length: unicode_column_width(text),
- underline,
}
}
@@ -93,15 +111,15 @@ impl LineSegment {
}
pub(crate) const fn is_dimmed(&self) -> bool {
- self.dim
+ self.options.contains(LineSegmentOptions::DIMMED)
}
pub(crate) const fn is_underlined(&self) -> bool {
- self.underline
+ self.options.contains(LineSegmentOptions::UNDERLINED)
}
pub(crate) const fn is_reversed(&self) -> bool {
- self.reverse
+ self.options.contains(LineSegmentOptions::REVERSED)
}
pub(crate) const fn get_length(&self) -> usize {
@@ -187,9 +205,7 @@ mod tests {
let line_segment = LineSegment::new_with_color_and_style(
"Sævör grét áðan því úlpan var ónýt",
DisplayColor::IndicatorColor,
- true,
- true,
- true,
+ LineSegmentOptions::all(),
);
assert_eq!(line_segment.get_color(), DisplayColor::IndicatorColor);
@@ -204,9 +220,7 @@ mod tests {
let line_segment = LineSegment::new_with_color_and_style(
"? דג סקרן שט בים מאוכזב ולפתע מצא לו חברה איך הקליטה",
DisplayColor::IndicatorColor,
- false,
- false,
- false,
+ LineSegmentOptions::NONE,
);
assert_eq!(line_segment.get_color(), DisplayColor::IndicatorColor);
@@ -218,8 +232,11 @@ mod tests {
#[test]
fn line_segment_case_new_with_color_and_style_all_styles_dimmed() {
- let line_segment =
- LineSegment::new_with_color_and_style("Test String", DisplayColor::IndicatorColor, true, false, false);
+ let line_segment = LineSegment::new_with_color_and_style(
+ "Test String",
+ DisplayColor::IndicatorColor,
+ LineSegmentOptions::DIMMED,
+ );
assert!(line_segment.is_dimmed());
assert!(!line_segment.is_underlined());
@@ -228,8 +245,11 @@ mod tests {
#[test]
fn line_segment_case_new_with_color_and_style_all_styles_underlined() {
- let line_segment =
- LineSegment::new_with_color_and_style("Test String", DisplayColor::IndicatorColor, false, true, false);
+ let line_segment = LineSegment::new_with_color_and_style(
+ "Test String",
+ DisplayColor::IndicatorColor,
+ LineSegmentOptions::UNDERLINED,
+ );
assert!(!line_segment.is_dimmed());
assert!(line_segment.is_underlined());
@@ -238,8 +258,11 @@ mod tests {
#[test]
fn line_segment_case_new_with_color_and_style_all_styles_reversed() {
- let line_segment =
- LineSegment::new_with_color_and_style("Test String", DisplayColor::IndicatorColor, false, false, true);
+ let line_segment = LineSegment::new_with_color_and_style(
+ "Test String",
+ DisplayColor::IndicatorColor,
+ LineSegmentOptions::REVERSED,
+ );
assert!(!line_segment.is_dimmed());
assert!(!line_segment.is_underlined());
diff --git a/src/view/render_slice.rs b/src/view/render_slice.rs
index 5eb7920..ef570a2 100644
--- a/src/view/render_slice.rs
+++ b/src/view/render_slice.rs
@@ -422,13 +422,7 @@ impl RenderSlice {
let partial = segment.get_partial_segment(left_start, window_width - cursor);
if partial.get_length() > 0 {
- segments.push(LineSegment::new_with_color_and_style(
- partial.get_content(),
- segment.get_color(),
- segment.is_dimmed(),
- segment.is_underlined(),
- segment.is_reversed(),
- ));
+ segments.push(LineSegment::new_copy_style(partial.get_content(), segment));
cursor += partial.get_length();
if cursor >= window_width {
@@ -443,12 +437,9 @@ impl RenderSlice {
if cursor < window_width {
if let Some(padding) = line.get_padding().as_ref() {
- segments.push(LineSegment::new_with_color_and_style(
+ segments.push(LineSegment::new_copy_style(
padding.get_content().repeat(window_width - cursor).as_str(),
- padding.get_color(),
- padding.is_dimmed(),
- padding.is_underlined(),
- padding.is_reversed(),
+ padding,
));
}
}
diff --git a/src/view/render_slice/tests.rs b/src/view/render_slice/tests.rs
index 4faf797..d585532 100644
--- a/src/view/render_slice/tests.rs
+++ b/src/view/render_slice/tests.rs
@@ -8,6 +8,7 @@ use crate::{
ExactPattern,
LinePattern,
},
+ view::LineSegmentOptions,
};
fn assert_rendered(render_slice: &RenderSlice, expected: &[&str]) {
@@ -850,9 +851,7 @@ fn with_padding() {
updater.push_line(ViewLine::from("Foo").set_padding_with_color_and_style(
'*',
DisplayColor::ActionPick,
- true,
- true,
- true,
+ LineSegmentOptions::all(),
));
});
let render_slice = create_render_slice(8, 1, &view_data);
@@ -868,9 +867,7 @@ fn with_padding_no_width() {
updater.push_line(ViewLine::from("Foo").set_padding_with_color_and_style(
'*',
DisplayColor::ActionPick,
- true,
- true,
- true,
+ LineSegmentOptions::all(),
));
});
let render_slice = create_render_slice(3, 1, &view_data);
diff --git a/src/view/view_line.rs b/src/view/view_line.rs
index c8e9761..c9f2e61 100644
--- a/src/view/view_line.rs
+++ b/src/view/view_line.rs
@@ -1,4 +1,7 @@
-use crate::{display::DisplayColor, view::LineSegment};
+use crate::{
+ display::DisplayColor,
+ view::{LineSegment, LineSegmentOptions},
+};
/// Represents a line in the view.
#[derive(Debug)]
@@ -54,16 +57,12 @@ impl ViewLine {
mut self,
c: char,
color: DisplayColor,
- dim: bool,
- underline: bool,
- reverse: bool,
+ options: LineSegmentOptions,
) -> Self {
self.padding = Some(LineSegment::new_with_color_and_style(
String::from(c).as_str(),
color,
- dim,
- underline,
- reverse,
+ options,
));
self
}
@@ -202,8 +201,11 @@ mod tests {
#[test]
fn set_padding_with_color_and_style() {
- let view_line =
- ViewLine::from("foo").set_padding_with_color_and_style(' ', DisplayColor::IndicatorColor, true, true, true);
+ let view_line = ViewLine::from("foo").set_padding_with_color_and_style(
+ ' ',
+ DisplayColor::IndicatorColor,
+ LineSegmentOptions::all(),
+ );
let padding = view_line.get_padding().as_ref().unwrap();
assert_eq!(padding.get_content(), " ");