Add some comments to TextView

This commit is contained in:
Alexandre Bury 2015-05-25 01:24:40 -07:00
parent 7fb1108ff8
commit 9ffcaef7ab

View File

@ -12,11 +12,13 @@ pub struct TextView {
start_line: usize, start_line: usize,
} }
// Subset of the main content representing a row on the display.
struct Row { struct Row {
start: usize, start: usize,
end: usize, end: usize,
} }
// If the last character is a newline, strip it.
fn strip_last_newline(content: &str) -> &str { fn strip_last_newline(content: &str) -> &str {
if !content.is_empty() && content.chars().last().unwrap() == '\n' { if !content.is_empty() && content.chars().last().unwrap() == '\n' {
&content[..content.len() - 1] &content[..content.len() - 1]
@ -28,6 +30,8 @@ fn strip_last_newline(content: &str) -> &str {
/// Returns the number of lines required to display the given text with the /// Returns the number of lines required to display the given text with the
/// specified maximum line width. /// specified maximum line width.
fn get_line_span(line: &str, max_width: usize) -> usize { fn get_line_span(line: &str, max_width: usize) -> usize {
// TODO: this method is stupid. Look at LinesIterator and do the same
// (Or use a common function? Better!)
let mut lines = 1; let mut lines = 1;
let mut length = 0; let mut length = 0;
for l in line.split(" ").map(|word| word.len()) { for l in line.split(" ").map(|word| word.len()) {
@ -71,12 +75,14 @@ impl TextView {
.fold(0, |sum, x| sum + x) .fold(0, |sum, x| sum + x)
} }
// Given the specified height, how many columns do we need to properly display?
fn get_num_cols(&self, max_height: usize) -> usize { fn get_num_cols(&self, max_height: usize) -> usize {
(div_up_usize(self.content.len(), max_height)..self.content.len()) (div_up_usize(self.content.len(), max_height)..self.content.len())
.find(|w| self.get_num_lines(*w) <= max_height) .find(|w| self.get_num_lines(*w) <= max_height)
.unwrap() .unwrap()
} }
// In the absence of any constraint, what size would we like?
fn get_ideal_size(&self) -> Vec2 { fn get_ideal_size(&self) -> Vec2 {
let mut max_width = 0; let mut max_width = 0;
let mut height = 0; let mut height = 0;
@ -90,6 +96,8 @@ impl TextView {
} }
} }
// Given a multiline string, and a given maximum width,
// iterates on the computed rows.
struct LinesIterator<'a> { struct LinesIterator<'a> {
content: &'a str, content: &'a str,
start: usize, start: usize,
@ -97,6 +105,7 @@ struct LinesIterator<'a> {
} }
impl <'a> LinesIterator<'a> { impl <'a> LinesIterator<'a> {
// Start an iterator on the given content.
fn new(content: &'a str, width: usize) -> Self { fn new(content: &'a str, width: usize) -> Self {
LinesIterator { LinesIterator {
content: content, content: content,
@ -112,7 +121,7 @@ impl <'a> Iterator for LinesIterator<'a> {
fn next(&mut self) -> Option<Row> { fn next(&mut self) -> Option<Row> {
if self.start >= self.content.len() { if self.start >= self.content.len() {
// This is the end // This is the end.
return None; return None;
} }
@ -121,6 +130,8 @@ impl <'a> Iterator for LinesIterator<'a> {
if let Some(next) = content.find("\n") { if let Some(next) = content.find("\n") {
if next < self.width { if next < self.width {
// We found a newline before the allowed limit.
// Break early.
self.start += next+1; self.start += next+1;
return Some(Row { return Some(Row {
start: start, start: start,
@ -130,6 +141,7 @@ impl <'a> Iterator for LinesIterator<'a> {
} }
if content.len() <= self.width { if content.len() <= self.width {
// I thought it would be longer! -- that's what she said :(
self.start += content.len(); self.start += content.len();
return Some(Row{ return Some(Row{
start: start, start: start,
@ -137,8 +149,8 @@ impl <'a> Iterator for LinesIterator<'a> {
}); });
} }
if let Some(i) = content[..self.width].rfind(" ") { if let Some(i) = content[..self.width].rfind(" ") {
// If we have to break, try to find a whitespace for that.
self.start += i+1; self.start += i+1;
return Some(Row { return Some(Row {
start: start, start: start,
@ -146,6 +158,8 @@ impl <'a> Iterator for LinesIterator<'a> {
}); });
} }
// Meh, no whitespace, so just cut in this mess.
// TODO: look for ponctuation instead?
self.start += self.width; self.start += self.width;
return Some(Row { return Some(Row {
start: start, start: start,
@ -166,7 +180,7 @@ impl View for TextView {
match (size.w,size.h) { match (size.w,size.h) {
// If we have no directive, ask for a single big line. // If we have no directive, ask for a single big line.
// TODO: what if the text has newlines?? // TODO: what if the text has newlines??
(DimensionRequest::Unknown, DimensionRequest::Unknown) => Vec2::new(self.content.len() as u32, 1), (DimensionRequest::Unknown, DimensionRequest::Unknown) => self.get_ideal_size(),
(DimensionRequest::Fixed(w),_) => { (DimensionRequest::Fixed(w),_) => {
let h = self.get_num_lines(w as usize) as u32; let h = self.get_num_lines(w as usize) as u32;
Vec2::new(w, h) Vec2::new(w, h)
@ -192,7 +206,6 @@ impl View for TextView {
fn layout(&mut self, size: Vec2) { fn layout(&mut self, size: Vec2) {
// Compute the text rows. // Compute the text rows.
self.rows = LinesIterator::new(&self.content, size.x as usize).collect(); self.rows = LinesIterator::new(&self.content, size.x as usize).collect();
} }
} }