From 19c724d18f27056c7c7b76f6c6c70da3eed7e98c Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 29 Aug 2019 16:45:54 -0400 Subject: [PATCH] Fix some style and syntax issues --- src/editor.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/editor.rs b/src/editor.rs index c02b808..d177f25 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -284,7 +284,7 @@ impl Editor { let char = key.unwrap(); match char { - Backspace => _del_or_backspace(Backspace), + Backspace => self._del_or_backspace(Backspace), Enter => { // TODO } @@ -313,7 +313,11 @@ impl Editor { } if c == ctrl_key('s') { - self.save(); + // @TODO show save success/error + match self.save() { + Ok(_) => (), + Err(e) => (), + } } if c == ctrl_key('h') { @@ -380,7 +384,7 @@ impl Editor { fn scroll(&mut self) { self.render_x = 0; if self.cursor_y < self.rows.len() { - self.render_x = self.row_cx_to_rx(self.cursor_y, self.cursor_x); + self.render_x = self.row_cx_to_rx(self.cursor_y); } // Vertical scrolling @@ -453,10 +457,9 @@ impl Editor { &self.filename }; - let mut left_message = format!("{:.20} - {} lines", filename, self.rows.len()); - let mut right_message = format!("{}/{}", self.cursor_y + 1, self.rows.len()); + let mut left_message = format!("{:.80} - {} lines", filename, self.rows.len()); + let right_message = format!("{}/{}", self.cursor_y + 1, self.rows.len()); let mut len = left_message.len(); - let mut rlen = right_message.len(); if len > self.screen_cols { len = self.screen_cols; left_message.truncate(len); @@ -464,7 +467,7 @@ impl Editor { self.append_out(&left_message); for x in len..self.screen_cols { - if self.screen_cols - x == rlen { + if self.screen_cols - x == right_message.len() { self.append_out(&right_message); break; } @@ -538,7 +541,7 @@ impl Editor { // Row Operations // ------------------------------------------------------------------------ - fn row_cx_to_rx(&mut self, index: usize, cx: usize) -> usize { + fn row_cx_to_rx(&mut self, index: usize) -> usize { let mut rx: usize = 0; let row = &mut self.rows[index]; @@ -599,8 +602,11 @@ impl Editor { fn rows_to_string(&mut self) -> String { let mut output = String::new(); - for row in self.rows { - output.push_str(&row.chars) + for row in &self.rows { + // When the file is opened, newlines are stripped + // make sure to add them back when saving! + let row_chars = row.chars.clone() + "\n"; + output.push_str(&row_chars) } output