From 7ac003c4de0194ab4861768e85c3a31351296cb3 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Mon, 8 Jan 2018 12:07:07 +0100 Subject: [PATCH] Re-organize utils module --- src/utils/lines/mod.rs | 4 ++ .../{lines_iterator.rs => lines/simple.rs} | 6 ++ .../spans.rs} | 67 ++++++++++++++++++- src/utils/mod.rs | 4 +- src/views/text_area.rs | 3 +- src/views/text_view.rs | 2 +- 6 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 src/utils/lines/mod.rs rename src/utils/{lines_iterator.rs => lines/simple.rs} (96%) rename src/utils/{span_lines_iterator.rs => lines/spans.rs} (92%) diff --git a/src/utils/lines/mod.rs b/src/utils/lines/mod.rs new file mode 100644 index 0000000..01540ea --- /dev/null +++ b/src/utils/lines/mod.rs @@ -0,0 +1,4 @@ +//! Compute rows of text with a width constraint. + +pub mod simple; +pub mod spans; diff --git a/src/utils/lines_iterator.rs b/src/utils/lines/simple.rs similarity index 96% rename from src/utils/lines_iterator.rs rename to src/utils/lines/simple.rs index d41df0f..8bc941b 100644 --- a/src/utils/lines_iterator.rs +++ b/src/utils/lines/simple.rs @@ -1,3 +1,9 @@ +//! Compute lines on simple text. +//! +//! The input is a single `&str`. +//! +//! Computed rows will include start/end byte offsets in the input string. + use With; use unicode_segmentation::UnicodeSegmentation; use unicode_width::UnicodeWidthStr; diff --git a/src/utils/span_lines_iterator.rs b/src/utils/lines/spans.rs similarity index 92% rename from src/utils/span_lines_iterator.rs rename to src/utils/lines/spans.rs index 8a43d0a..7818751 100644 --- a/src/utils/span_lines_iterator.rs +++ b/src/utils/lines/spans.rs @@ -1,4 +1,10 @@ -//! bla +//! Compute lines on multiple spans of text. +//! +//! The input is a list of consecutive text spans. +//! +//! Computed rows will include a list of span segments. +//! Each segment include the source span ID, and start/end byte offsets. + use std::borrow::Cow; use std::iter::Peekable; use theme::Style; @@ -255,6 +261,25 @@ pub struct Row { pub width: usize, } +impl Row { + /// Resolve the row indices into styled spans. + pub fn resolve<'a: 'b, 'b>(&self, spans: &'b [Span<'a>]) -> Vec> { + self.segments + .iter() + .map(|seg| { + let span: &'b Span<'a> = &spans[seg.span_id]; + let text: &'b str = &span.text; + let text: &'b str = &text[seg.start..seg.end]; + + Span { + text: Cow::Borrowed(text), + style: span.style, + } + }) + .collect() + } +} + /// Generates rows of text in constrainted width. /// /// Works on spans of text. @@ -584,6 +609,46 @@ mod tests { let iter = SpanLinesIterator::new(&input, 16); let rows: Vec = iter.collect(); + let spans: Vec<_> = + rows.iter().map(|row| row.resolve(&input)).collect(); + + assert_eq!( + &spans[..], + &[ + vec![ + Span { + text: Cow::Borrowed("A beautiful "), + style: Style::none(), + }, + Span { + text: Cow::Borrowed("boat"), + style: Style::none(), + } + ], + vec![ + Span { + text: Cow::Borrowed("isn\'t it?"), + style: Style::none(), + } + ], + vec![ + Span { + text: Cow::Borrowed("Yes indeed, my "), + style: Style::none(), + } + ], + vec![ + Span { + text: Cow::Borrowed("Super"), + style: Style::none(), + }, + Span { + text: Cow::Borrowed("Captain !"), + style: Style::none(), + } + ] + ] + ); assert_eq!( &rows[..], diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 9eb081b..503f2c0 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -3,11 +3,9 @@ use unicode_segmentation::UnicodeSegmentation; use unicode_width::UnicodeWidthStr; -mod lines_iterator; mod reader; -pub mod span_lines_iterator; +pub mod lines; -pub use self::lines_iterator::{LinesIterator, Row}; pub use self::reader::ProgressReader; /// The length and width of a part of a string. diff --git a/src/views/text_area.rs b/src/views/text_area.rs index 6eb730e..921f414 100644 --- a/src/views/text_area.rs +++ b/src/views/text_area.rs @@ -5,7 +5,8 @@ use std::cmp::min; use theme::{ColorStyle, Effect}; use unicode_segmentation::UnicodeSegmentation; use unicode_width::UnicodeWidthStr; -use utils::{prefix, simple_prefix, LinesIterator, Row}; +use utils::{prefix, simple_prefix}; +use utils::lines::simple::{LinesIterator, Row}; use vec::Vec2; use view::{ScrollBase, SizeCache, View}; diff --git a/src/views/text_view.rs b/src/views/text_view.rs index 7a17788..da2cc65 100644 --- a/src/views/text_view.rs +++ b/src/views/text_view.rs @@ -10,7 +10,7 @@ use std::sync::{Mutex, MutexGuard}; use std::sync::Arc; use theme::Effect; use unicode_width::UnicodeWidthStr; -use utils::{LinesIterator, Row}; +use utils::lines::simple::{LinesIterator, Row}; use vec::Vec2; use view::{ScrollBase, ScrollStrategy, SizeCache, View};