Add text helper method for text_editor::Content
This commit is contained in:
parent
af21cf8249
commit
8cc19de254
2 changed files with 22 additions and 18 deletions
|
|
@ -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,
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue