2015-05-26 05:35:50 +00:00
|
|
|
use std::cmp::{max,min};
|
2015-05-18 18:18:04 +00:00
|
|
|
|
2015-05-26 05:35:50 +00:00
|
|
|
use color;
|
2015-05-18 18:51:30 +00:00
|
|
|
use vec::Vec2;
|
2015-05-15 00:41:17 +00:00
|
|
|
use view::{View,DimensionRequest,SizeRequest};
|
|
|
|
use div::*;
|
2015-05-15 18:58:47 +00:00
|
|
|
use printer::Printer;
|
2015-05-28 01:04:33 +00:00
|
|
|
use event::*;
|
2015-05-15 00:41:17 +00:00
|
|
|
|
|
|
|
/// A simple view showing a fixed text
|
|
|
|
pub struct TextView {
|
|
|
|
content: String,
|
2015-05-25 08:10:43 +00:00
|
|
|
rows: Vec<Row>,
|
|
|
|
start_line: usize,
|
2015-05-26 05:35:50 +00:00
|
|
|
view_height: usize,
|
2015-05-25 08:10:43 +00:00
|
|
|
}
|
|
|
|
|
2015-05-25 08:24:40 +00:00
|
|
|
// Subset of the main content representing a row on the display.
|
2015-05-25 08:10:43 +00:00
|
|
|
struct Row {
|
|
|
|
start: usize,
|
|
|
|
end: usize,
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
|
|
|
|
2015-05-25 08:24:40 +00:00
|
|
|
// If the last character is a newline, strip it.
|
2015-05-25 01:51:49 +00:00
|
|
|
fn strip_last_newline(content: &str) -> &str {
|
|
|
|
if !content.is_empty() && content.chars().last().unwrap() == '\n' {
|
|
|
|
&content[..content.len() - 1]
|
|
|
|
} else {
|
|
|
|
content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 00:41:17 +00:00
|
|
|
/// Returns the number of lines required to display the given text with the
|
|
|
|
/// specified maximum line width.
|
2015-05-15 00:48:24 +00:00
|
|
|
fn get_line_span(line: &str, max_width: usize) -> usize {
|
2015-05-25 08:24:40 +00:00
|
|
|
// TODO: this method is stupid. Look at LinesIterator and do the same
|
|
|
|
// (Or use a common function? Better!)
|
2015-05-15 00:41:17 +00:00
|
|
|
let mut lines = 1;
|
|
|
|
let mut length = 0;
|
2015-05-29 00:32:28 +00:00
|
|
|
for l in line.split(" ").map(|word| word.chars().count()) {
|
2015-05-15 00:48:24 +00:00
|
|
|
length += l;
|
2015-05-25 08:30:18 +00:00
|
|
|
if length > max_width {
|
2015-05-15 00:48:24 +00:00
|
|
|
length = l;
|
|
|
|
lines += 1;
|
|
|
|
}
|
2015-05-24 03:12:11 +00:00
|
|
|
length += 1;
|
2015-05-15 00:48:24 +00:00
|
|
|
}
|
2015-05-15 00:41:17 +00:00
|
|
|
lines
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TextView {
|
|
|
|
/// Creates a new TextView with the given content.
|
|
|
|
pub fn new(content: &str) -> Self {
|
2015-05-25 01:51:49 +00:00
|
|
|
let content = strip_last_newline(content);
|
2015-05-15 00:41:17 +00:00
|
|
|
TextView {
|
|
|
|
content: content.to_string(),
|
2015-05-25 08:10:43 +00:00
|
|
|
start_line: 0,
|
|
|
|
rows: Vec::new(),
|
2015-05-26 05:35:50 +00:00
|
|
|
view_height: 0,
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-23 23:46:38 +00:00
|
|
|
/// Replace the text in this view.
|
2015-05-23 17:33:29 +00:00
|
|
|
pub fn set_content(&mut self, content: &str) {
|
2015-05-25 01:51:49 +00:00
|
|
|
let content = strip_last_newline(content);
|
2015-05-23 17:33:29 +00:00
|
|
|
self.content = content.to_string();
|
|
|
|
}
|
|
|
|
|
2015-05-23 23:46:38 +00:00
|
|
|
/// Returns the current text in this view.
|
|
|
|
pub fn get_content(&self) -> &str {
|
|
|
|
&self.content
|
|
|
|
}
|
|
|
|
|
2015-05-15 00:41:17 +00:00
|
|
|
/// Returns the number of lines required to display the content
|
|
|
|
/// with the given width.
|
2015-05-15 00:48:24 +00:00
|
|
|
fn get_num_lines(&self, max_width: usize) -> usize {
|
2015-05-15 00:41:17 +00:00
|
|
|
self.content.split("\n")
|
2015-05-15 00:48:24 +00:00
|
|
|
.map(|line| get_line_span(line, max_width))
|
2015-05-15 00:41:17 +00:00
|
|
|
.fold(0, |sum, x| sum + x)
|
|
|
|
}
|
|
|
|
|
2015-05-25 08:24:40 +00:00
|
|
|
// Given the specified height, how many columns do we need to properly display?
|
2015-05-15 00:48:24 +00:00
|
|
|
fn get_num_cols(&self, max_height: usize) -> usize {
|
2015-05-29 00:32:28 +00:00
|
|
|
let len = self.content.chars().count();
|
|
|
|
(div_up_usize(len, max_height)..len)
|
2015-05-15 00:48:24 +00:00
|
|
|
.find(|w| self.get_num_lines(*w) <= max_height)
|
2015-05-15 00:41:17 +00:00
|
|
|
.unwrap()
|
|
|
|
}
|
2015-05-18 18:18:04 +00:00
|
|
|
|
2015-05-25 08:24:40 +00:00
|
|
|
// In the absence of any constraint, what size would we like?
|
2015-05-18 18:18:04 +00:00
|
|
|
fn get_ideal_size(&self) -> Vec2 {
|
2015-05-18 22:31:55 +00:00
|
|
|
let mut max_width = 0;
|
2015-05-18 18:18:04 +00:00
|
|
|
let mut height = 0;
|
|
|
|
|
|
|
|
for line in self.content.split("\n") {
|
|
|
|
height += 1;
|
2015-05-29 00:32:28 +00:00
|
|
|
max_width = max(max_width, line.chars().count());
|
2015-05-18 18:18:04 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 22:31:55 +00:00
|
|
|
Vec2::new(max_width, height)
|
2015-05-18 18:18:04 +00:00
|
|
|
}
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
|
|
|
|
2015-05-25 08:24:40 +00:00
|
|
|
// Given a multiline string, and a given maximum width,
|
|
|
|
// iterates on the computed rows.
|
2015-05-15 22:00:20 +00:00
|
|
|
struct LinesIterator<'a> {
|
2015-05-25 08:10:43 +00:00
|
|
|
content: &'a str,
|
2015-05-15 22:00:20 +00:00
|
|
|
start: usize,
|
|
|
|
width: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <'a> LinesIterator<'a> {
|
2015-05-25 08:24:40 +00:00
|
|
|
// Start an iterator on the given content.
|
2015-05-25 08:10:43 +00:00
|
|
|
fn new(content: &'a str, width: usize) -> Self {
|
|
|
|
LinesIterator {
|
|
|
|
content: content,
|
|
|
|
width: width,
|
|
|
|
start: 0,
|
2015-05-15 22:00:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <'a> Iterator for LinesIterator<'a> {
|
|
|
|
|
2015-05-25 08:10:43 +00:00
|
|
|
type Item = Row;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Row> {
|
|
|
|
if self.start >= self.content.len() {
|
2015-05-25 08:24:40 +00:00
|
|
|
// This is the end.
|
2015-05-25 08:10:43 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let start = self.start;
|
|
|
|
let content = &self.content[self.start..];
|
|
|
|
|
|
|
|
if let Some(next) = content.find("\n") {
|
2015-05-29 00:32:28 +00:00
|
|
|
if content[..next].chars().count() <= self.width {
|
2015-05-25 08:24:40 +00:00
|
|
|
// We found a newline before the allowed limit.
|
|
|
|
// Break early.
|
2015-05-25 08:10:43 +00:00
|
|
|
self.start += next+1;
|
|
|
|
return Some(Row {
|
|
|
|
start: start,
|
|
|
|
end: next + start,
|
|
|
|
});
|
2015-05-15 23:06:48 +00:00
|
|
|
}
|
2015-05-15 22:00:20 +00:00
|
|
|
}
|
2015-05-25 08:10:43 +00:00
|
|
|
|
2015-05-29 00:32:28 +00:00
|
|
|
let content_len = content.chars().count();
|
|
|
|
if content_len <= self.width {
|
2015-05-25 08:24:40 +00:00
|
|
|
// I thought it would be longer! -- that's what she said :(
|
2015-05-25 08:10:43 +00:00
|
|
|
self.start += content.len();
|
|
|
|
return Some(Row{
|
|
|
|
start: start,
|
|
|
|
end: start + content.len(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-05-29 00:32:28 +00:00
|
|
|
let i = if content_len == self.width+1 {
|
|
|
|
// We can't look at the index if we're looking at the end of the string
|
|
|
|
content.len()
|
|
|
|
} else {
|
|
|
|
content.char_indices().nth(self.width+1).unwrap().0
|
|
|
|
};
|
|
|
|
let substr = &content[..i];
|
|
|
|
if let Some(i) = substr.rfind(" ") {
|
2015-05-25 08:24:40 +00:00
|
|
|
// If we have to break, try to find a whitespace for that.
|
2015-05-25 08:10:43 +00:00
|
|
|
self.start += i+1;
|
|
|
|
return Some(Row {
|
|
|
|
start: start,
|
|
|
|
end: i + start,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-05-25 08:24:40 +00:00
|
|
|
// Meh, no whitespace, so just cut in this mess.
|
|
|
|
// TODO: look for ponctuation instead?
|
2015-05-25 08:10:43 +00:00
|
|
|
self.start += self.width;
|
|
|
|
return Some(Row {
|
|
|
|
start: start,
|
|
|
|
end: start + self.width,
|
|
|
|
});
|
2015-05-15 22:00:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 00:41:17 +00:00
|
|
|
impl View for TextView {
|
2015-05-26 05:38:11 +00:00
|
|
|
fn draw(&mut self, printer: &Printer, focused: bool) {
|
2015-05-19 22:54:11 +00:00
|
|
|
// We don't have a focused view
|
2015-05-25 08:10:43 +00:00
|
|
|
for (i,line) in self.rows.iter().skip(self.start_line).map(|row| &self.content[row.start..row.end]).enumerate() {
|
2015-05-15 22:00:20 +00:00
|
|
|
printer.print((0,i), line);
|
|
|
|
}
|
2015-05-26 05:35:50 +00:00
|
|
|
if self.view_height < self.rows.len() {
|
|
|
|
// We directly compute the size of the scrollbar (this allow use to avoid using floats).
|
|
|
|
// (ratio) * max_height
|
|
|
|
// Where ratio is ({start or end} / content.height)
|
|
|
|
let start = self.view_height * self.start_line / self.rows.len();
|
|
|
|
let end = self.view_height * (self.start_line + self.view_height) / self.rows.len();
|
2015-05-27 04:45:00 +00:00
|
|
|
printer.with_color(
|
2015-05-26 05:38:11 +00:00
|
|
|
if focused { color::HIGHLIGHT } else { color::HIGHLIGHT_INACTIVE },
|
|
|
|
|printer| {
|
|
|
|
printer.print_vline((printer.size.x-1, start), end-start, ' ' as u64);
|
|
|
|
});
|
2015-05-26 05:35:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-28 01:04:33 +00:00
|
|
|
fn on_event(&mut self, event: Event) -> EventResult {
|
2015-05-26 05:35:50 +00:00
|
|
|
if self.view_height >= self.rows.len() {
|
|
|
|
return EventResult::Ignored;
|
|
|
|
}
|
|
|
|
|
2015-05-28 01:04:33 +00:00
|
|
|
match event {
|
2015-05-28 21:44:10 +00:00
|
|
|
Event::KeyEvent(Key::Up) if self.start_line > 0 => self.start_line -= 1,
|
|
|
|
Event::KeyEvent(Key::Down) if self.start_line+self.view_height < self.rows.len() => self.start_line += 1,
|
2015-05-29 00:32:28 +00:00
|
|
|
Event::KeyEvent(Key::PageDown) => self.start_line = min(self.start_line+10, self.rows.len()-self.view_height),
|
|
|
|
Event::KeyEvent(Key::PageUp) => self.start_line -= min(self.start_line, 10),
|
2015-05-26 05:35:50 +00:00
|
|
|
_ => return EventResult::Ignored,
|
|
|
|
}
|
|
|
|
|
|
|
|
return EventResult::Consumed(None);
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 18:58:47 +00:00
|
|
|
fn get_min_size(&self, size: SizeRequest) -> Vec2 {
|
2015-05-15 00:41:17 +00:00
|
|
|
match (size.w,size.h) {
|
2015-05-25 08:10:43 +00:00
|
|
|
// If we have no directive, ask for a single big line.
|
|
|
|
// TODO: what if the text has newlines??
|
2015-05-25 08:24:40 +00:00
|
|
|
(DimensionRequest::Unknown, DimensionRequest::Unknown) => self.get_ideal_size(),
|
2015-05-15 00:41:17 +00:00
|
|
|
(DimensionRequest::Fixed(w),_) => {
|
2015-05-25 21:46:29 +00:00
|
|
|
let h = self.get_num_lines(w);
|
2015-05-15 18:58:47 +00:00
|
|
|
Vec2::new(w, h)
|
2015-05-15 00:41:17 +00:00
|
|
|
},
|
|
|
|
(_,DimensionRequest::Fixed(h)) => {
|
2015-05-25 21:46:29 +00:00
|
|
|
let w = self.get_num_cols(h);
|
2015-05-15 18:58:47 +00:00
|
|
|
Vec2::new(w, h)
|
2015-05-15 00:41:17 +00:00
|
|
|
},
|
2015-05-15 23:06:48 +00:00
|
|
|
(DimensionRequest::AtMost(w),_) => {
|
2015-05-25 08:10:43 +00:00
|
|
|
// Don't _force_ the max width, but take it if we have to.
|
2015-05-18 18:18:04 +00:00
|
|
|
let ideal = self.get_ideal_size();
|
|
|
|
|
|
|
|
if w >= ideal.x {
|
|
|
|
ideal
|
2015-05-15 23:06:48 +00:00
|
|
|
} else {
|
2015-05-25 21:46:29 +00:00
|
|
|
let h = self.get_num_lines(w);
|
2015-05-15 23:06:48 +00:00
|
|
|
Vec2::new(w, h)
|
|
|
|
}
|
|
|
|
},
|
2015-05-15 00:41:17 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
2015-05-25 08:10:43 +00:00
|
|
|
|
|
|
|
fn layout(&mut self, size: Vec2) {
|
|
|
|
// Compute the text rows.
|
2015-05-26 05:35:50 +00:00
|
|
|
self.view_height = size.y;
|
|
|
|
self.rows = LinesIterator::new(&self.content, size.x ).collect();
|
|
|
|
if self.rows.len() > size.y {
|
|
|
|
self.rows = LinesIterator::new(&self.content, size.x - 1).collect();
|
|
|
|
self.start_line = min(self.start_line, self.rows.len() - size.y);
|
|
|
|
} else {
|
|
|
|
self.start_line = 0;
|
|
|
|
}
|
2015-05-25 08:10:43 +00:00
|
|
|
}
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|