diff options
author | Tim Oram <dev@mitmaro.ca> | 2019-06-07 22:44:48 -0230 |
---|---|---|
committer | Tim Oram <dev@mitmaro.ca> | 2019-06-07 22:44:48 -0230 |
commit | 123ed65ce3ae6140546d5f159827588a6e9ae11f (patch) | |
tree | dac3767a0490533255cb5baf71ff8ae775310450 | |
parent | 15a7ddc98718862270c33cfc05fbf66c6ccd7575 (diff) |
Move view into module
-rw-r--r-- | src/view/line_segment.rs | 59 | ||||
-rw-r--r-- | src/view/mod.rs | 8 | ||||
-rw-r--r-- | src/view/view.rs (renamed from src/view.rs) | 60 | ||||
-rw-r--r-- | src/view/view_line.rs | 5 |
4 files changed, 75 insertions, 57 deletions
diff --git a/src/view/line_segment.rs b/src/view/line_segment.rs new file mode 100644 index 0000000..b0e8a72 --- /dev/null +++ b/src/view/line_segment.rs @@ -0,0 +1,59 @@ +use crate::window::{Window, WindowColor}; +use unicode_segmentation::UnicodeSegmentation; + +pub struct LineSegment { + color: WindowColor, + dim: bool, + reverse: bool, + text: String, + underline: bool, +} + +impl LineSegment { + pub fn new(text: &str) -> Self { + Self { + text: String::from(text), + color: WindowColor::Foreground, + reverse: false, + dim: false, + underline: false, + } + } + + pub fn new_with_color(text: &str, color: WindowColor) -> Self { + Self { + text: String::from(text), + color, + reverse: false, + dim: false, + underline: false, + } + } + + pub fn new_with_color_and_style(text: &str, color: WindowColor, dim: bool, underline: bool, reverse: bool) -> Self { + Self { + text: String::from(text), + color, + reverse, + dim, + underline, + } + } + + pub fn draw(&self, max_width: usize, window: &Window) -> usize { + window.color(self.color); + window.set_style(self.dim, self.underline, self.reverse); + let segment_length = UnicodeSegmentation::graphemes(self.text.as_str(), true).count(); + + if segment_length >= max_width { + let graphemes = UnicodeSegmentation::graphemes(self.text.as_str(), true); + let partial_line = graphemes.take(max_width).collect::<String>(); + window.draw_str(partial_line.as_str()); + max_width + } + else { + window.draw_str(self.text.as_str()); + segment_length + } + } +} diff --git a/src/view/mod.rs b/src/view/mod.rs new file mode 100644 index 0000000..a166ad7 --- /dev/null +++ b/src/view/mod.rs @@ -0,0 +1,8 @@ +mod line_segment; +#[allow(clippy::module_inception)] +mod view; +mod view_line; + +pub use self::line_segment::LineSegment; +pub use self::view::View; +pub use self::view_line::ViewLine; diff --git a/src/view.rs b/src/view/view.rs index 403c61d..92f074c 100644 --- a/src/view.rs +++ b/src/view/view.rs @@ -27,56 +27,13 @@ use crate::constants::{ }; use crate::line::Line; use crate::scroll::{get_scroll_position, ScrollPosition}; +use crate::view::{LineSegment, ViewLine}; use crate::window::Window; use crate::window::WindowColor; use git2::Delta; use std::cmp; use unicode_segmentation::UnicodeSegmentation; -pub struct LineSegment { - color: WindowColor, - dim: bool, - reverse: bool, - text: String, - underline: bool, -} - -impl LineSegment { - pub fn new(text: &str) -> Self { - Self { - text: String::from(text), - color: WindowColor::Foreground, - reverse: false, - dim: false, - underline: false, - } - } - - pub fn new_with_color(text: &str, color: WindowColor) -> Self { - Self { - text: String::from(text), - color, - reverse: false, - dim: false, - underline: false, - } - } - - pub fn new_with_color_and_style(text: &str, color: WindowColor, dim: bool, underline: bool, reverse: bool) -> Self { - Self { - text: String::from(text), - color, - reverse, - dim, - underline, - } - } -} - -pub struct ViewLine { - segments: Vec<LineSegment>, -} - pub struct View<'v> { commit_top: ScrollPosition, help_top: ScrollPosition, @@ -174,21 +131,10 @@ impl<'v> View<'v> { let mut start = 0; for segment in &line.segments { - self.window.color(segment.color); - self.window.set_style(segment.dim, segment.underline, segment.reverse); - let graphemes = UnicodeSegmentation::graphemes(segment.text.as_str(), true); - let segment_length = graphemes.clone().count(); - - if (start + segment_length) >= window_width { - let partial_line = graphemes.take(window_width - start).collect::<String>(); - self.window.draw_str(partial_line.as_str()); - start += segment_length; + start += segment.draw(window_width - start, &self.window); + if start >= window_width { break; } - else { - self.window.draw_str(segment.text.as_str()); - start += segment_length; - } } if start < window_width { diff --git a/src/view/view_line.rs b/src/view/view_line.rs new file mode 100644 index 0000000..c8f921c --- /dev/null +++ b/src/view/view_line.rs @@ -0,0 +1,5 @@ +use crate::view::LineSegment; + +pub struct ViewLine { + pub segments: Vec<LineSegment>, +} |