mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-14 13:13:08 +00:00
Ignore last line in newline-terminated strings
Files for instance always end in a newline, but it doesn't mean we want an empty line after the text.
This commit is contained in:
parent
b3be2286dd
commit
1f3e17b591
@ -10,6 +10,14 @@ pub struct TextView {
|
|||||||
content: String,
|
content: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn strip_last_newline(content: &str) -> &str {
|
||||||
|
if !content.is_empty() && content.chars().last().unwrap() == '\n' {
|
||||||
|
&content[..content.len() - 1]
|
||||||
|
} else {
|
||||||
|
content
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// 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 {
|
||||||
@ -29,6 +37,7 @@ fn get_line_span(line: &str, max_width: usize) -> usize {
|
|||||||
impl TextView {
|
impl TextView {
|
||||||
/// Creates a new TextView with the given content.
|
/// Creates a new TextView with the given content.
|
||||||
pub fn new(content: &str) -> Self {
|
pub fn new(content: &str) -> Self {
|
||||||
|
let content = strip_last_newline(content);
|
||||||
TextView {
|
TextView {
|
||||||
content: content.to_string(),
|
content: content.to_string(),
|
||||||
}
|
}
|
||||||
@ -36,6 +45,7 @@ impl TextView {
|
|||||||
|
|
||||||
/// Replace the text in this view.
|
/// Replace the text in this view.
|
||||||
pub fn set_content(&mut self, content: &str) {
|
pub fn set_content(&mut self, content: &str) {
|
||||||
|
let content = strip_last_newline(content);
|
||||||
self.content = content.to_string();
|
self.content = content.to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user