Skip to content

Commit

Permalink
Fix final line ending (#635)
Browse files Browse the repository at this point in the history
* Fix panic on empty files

* Add the final newline implicitely
  • Loading branch information
vinc authored Jun 5, 2024
1 parent 8925e1a commit 8a21f2e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/usr/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ impl Editor {
for line in contents.lines() {
lines.push(line.into());
}
if lines.is_empty() {
lines.push(String::new());
}
}
Err(_) => {
lines.push(String::new());
Expand All @@ -76,16 +79,10 @@ impl Editor {
}

pub fn save(&mut self) -> Result<(), ExitCode> {
let mut contents = String::new();
let n = self.lines.len();
for i in 0..n {
contents.push_str(&self.lines[i]);
if i < n - 1 {
contents.push('\n');
}
}
let contents = self.lines.join("\n") + "\n";

if fs::write(&self.pathname, contents.as_bytes()).is_ok() {
let n = self.lines.len();
let status = format!("Wrote {}L to '{}'", n, self.pathname);
self.print_status(&status, "yellow");
Ok(())
Expand Down
3 changes: 3 additions & 0 deletions src/usr/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ impl Viewer {
lines.push(line.into());
width = cmp::max(width, line.chars().count());
}
if lines.is_empty() {
lines.push(String::new());
}
}
Err(_) => {
lines.push(String::new());
Expand Down

0 comments on commit 8a21f2e

Please sign in to comment.