Add text helper method for text_editor::Content

This commit is contained in:
Héctor Ramón Jiménez 2023-09-22 06:00:51 +02:00
parent af21cf8249
commit 8cc19de254
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 22 additions and 18 deletions

View file

@ -114,25 +114,8 @@ impl Application for Editor {
} else {
self.is_loading = true;
let mut contents = self.content.lines().enumerate().fold(
String::new(),
|mut contents, (i, line)| {
if i > 0 {
contents.push('\n');
}
contents.push_str(&line);
contents
},
);
if !contents.ends_with('\n') {
contents.push('\n');
}
Command::perform(
save_file(self.file.clone(), contents),
save_file(self.file.clone(), self.content.text()),
Message::FileSaved,
)
}

View file

@ -190,6 +190,27 @@ where
}
}
pub fn text(&self) -> String {
let mut text = self.lines().enumerate().fold(
String::new(),
|mut contents, (i, line)| {
if i > 0 {
contents.push('\n');
}
contents.push_str(&line);
contents
},
);
if !text.ends_with('\n') {
text.push('\n');
}
text
}
pub fn selection(&self) -> Option<String> {
self.0.borrow().editor.selection()
}