mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
Add TextView::append_content
Also don't strip the newline anymore when setting content. Instead, just ignore trailing newline when computing rows.
This commit is contained in:
parent
b8621df74d
commit
c970712c3b
@ -29,17 +29,18 @@ pub struct TextView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If the last character is a newline, strip it.
|
// If the last character is a newline, strip it.
|
||||||
fn strip_last_newline(content: &mut String) {
|
fn strip_last_newline(content: &str) -> &str {
|
||||||
if content.ends_with('\n') {
|
if content.ends_with('\n') {
|
||||||
content.pop().unwrap();
|
&content[..content.len() - 1]
|
||||||
|
} else {
|
||||||
|
content
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TextView {
|
impl TextView {
|
||||||
/// Creates a new TextView with the given content.
|
/// Creates a new TextView with the given content.
|
||||||
pub fn new<S: Into<String>>(content: S) -> Self {
|
pub fn new<S: Into<String>>(content: S) -> Self {
|
||||||
let mut content = content.into();
|
let content = content.into();
|
||||||
strip_last_newline(&mut content);
|
|
||||||
TextView {
|
TextView {
|
||||||
content: content,
|
content: content,
|
||||||
rows: Vec::new(),
|
rows: Vec::new(),
|
||||||
@ -99,12 +100,17 @@ impl TextView {
|
|||||||
|
|
||||||
/// Replace the text in this view.
|
/// Replace the text in this view.
|
||||||
pub fn set_content<S: Into<String>>(&mut self, content: S) {
|
pub fn set_content<S: Into<String>>(&mut self, content: S) {
|
||||||
let mut content = content.into();
|
let content = content.into();
|
||||||
strip_last_newline(&mut content);
|
|
||||||
self.content = content;
|
self.content = content;
|
||||||
self.invalidate();
|
self.invalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Append content to the end of a TextView.
|
||||||
|
pub fn append_content(&mut self, content: &str) {
|
||||||
|
self.content.push_str(content);
|
||||||
|
self.invalidate();
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the current text in this view.
|
/// Returns the current text in this view.
|
||||||
pub fn get_content(&self) -> &str {
|
pub fn get_content(&self) -> &str {
|
||||||
&self.content
|
&self.content
|
||||||
@ -163,7 +169,9 @@ impl TextView {
|
|||||||
|
|
||||||
// First attempt: naively hope that we won't need a scrollbar_width
|
// First attempt: naively hope that we won't need a scrollbar_width
|
||||||
// (This means we try to use the entire available width for text).
|
// (This means we try to use the entire available width for text).
|
||||||
self.rows = LinesIterator::new(&self.content, size.x).collect();
|
self.rows = LinesIterator::new(strip_last_newline(&self.content),
|
||||||
|
size.x)
|
||||||
|
.collect();
|
||||||
|
|
||||||
// Width taken by the scrollbar. Without a scrollbar, it's 0.
|
// Width taken by the scrollbar. Without a scrollbar, it's 0.
|
||||||
let mut scrollbar_width = 0;
|
let mut scrollbar_width = 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user