Give wrapped rows full width

This commit is contained in:
Alexandre Bury 2018-05-05 09:04:32 -07:00
parent d4ced015f6
commit 20cb033b8d
2 changed files with 34 additions and 10 deletions

View File

@ -1,11 +1,22 @@
use super::segment::Segment; use super::segment::Segment;
/// Non-splittable piece of text. /// Non-splittable piece of text.
///
/// It is made of a list of segments of text.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct Chunk { pub struct Chunk {
/// Total width of this chunk.
pub width: usize, pub width: usize,
/// This is the segments this chunk contains.
pub segments: Vec<Segment>, pub segments: Vec<Segment>,
/// Hard stops are non-optional line breaks (newlines).
pub hard_stop: bool, pub hard_stop: bool,
/// If a chunk of text ends in a space, it can be compressed a bit.
///
/// (We can omit the space if it would result in a perfect fit.)
pub ends_with_space: bool, pub ends_with_space: bool,
} }

View File

@ -111,16 +111,14 @@ where
offset = end; offset = end;
Chunk { Chunk {
width, width,
segments: vec![ segments: vec![Segment {
Segment { width,
width, span_id: seg.span_id,
span_id: seg.span_id, start,
start, end,
end, }],
},
],
hard_stop: false, hard_stop: false,
ends_with_space: false, ends_with_space: false, // should we?
} }
}) })
}); });
@ -152,7 +150,22 @@ where
} }
} }
let width = chunks.iter().map(|c| c.width).sum(); // We can know text was wrapped if the stop was optional,
// and there's more coming.
let text_wrap = !chunks
.last()
.map(|c| c.hard_stop)
.unwrap_or(true)
&& self.iter.peek().is_some();
// If we had to break a line in two, then at least pretent we're
// taking the full width.
let width = if text_wrap {
self.width
} else {
chunks.iter().map(|c| c.width).sum()
};
assert!(width <= self.width); assert!(width <= self.width);
// Concatenate all segments // Concatenate all segments