Re-organize utils module

This commit is contained in:
Alexandre Bury 2018-01-08 12:07:07 +01:00
parent 943da46e82
commit 7ac003c4de
6 changed files with 80 additions and 6 deletions

4
src/utils/lines/mod.rs Normal file
View File

@ -0,0 +1,4 @@
//! Compute rows of text with a width constraint.
pub mod simple;
pub mod spans;

View File

@ -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 With;
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;

View File

@ -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::borrow::Cow;
use std::iter::Peekable; use std::iter::Peekable;
use theme::Style; use theme::Style;
@ -255,6 +261,25 @@ pub struct Row {
pub width: usize, pub width: usize,
} }
impl Row {
/// Resolve the row indices into styled spans.
pub fn resolve<'a: 'b, 'b>(&self, spans: &'b [Span<'a>]) -> Vec<Span<'b>> {
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. /// Generates rows of text in constrainted width.
/// ///
/// Works on spans of text. /// Works on spans of text.
@ -584,6 +609,46 @@ mod tests {
let iter = SpanLinesIterator::new(&input, 16); let iter = SpanLinesIterator::new(&input, 16);
let rows: Vec<Row> = iter.collect(); let rows: Vec<Row> = 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!( assert_eq!(
&rows[..], &rows[..],

View File

@ -3,11 +3,9 @@
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
mod lines_iterator;
mod reader; mod reader;
pub mod span_lines_iterator; pub mod lines;
pub use self::lines_iterator::{LinesIterator, Row};
pub use self::reader::ProgressReader; pub use self::reader::ProgressReader;
/// The length and width of a part of a string. /// The length and width of a part of a string.

View File

@ -5,7 +5,8 @@ use std::cmp::min;
use theme::{ColorStyle, Effect}; use theme::{ColorStyle, Effect};
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr; 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 vec::Vec2;
use view::{ScrollBase, SizeCache, View}; use view::{ScrollBase, SizeCache, View};

View File

@ -10,7 +10,7 @@ use std::sync::{Mutex, MutexGuard};
use std::sync::Arc; use std::sync::Arc;
use theme::Effect; use theme::Effect;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
use utils::{LinesIterator, Row}; use utils::lines::simple::{LinesIterator, Row};
use vec::Vec2; use vec::Vec2;
use view::{ScrollBase, ScrollStrategy, SizeCache, View}; use view::{ScrollBase, ScrollStrategy, SizeCache, View};