Fix some style and syntax issues

This commit is contained in:
Timothy Warren 2019-08-29 16:45:54 -04:00
parent 5d1c8d8b8b
commit 19c724d18f

View File

@ -284,7 +284,7 @@ impl Editor {
let char = key.unwrap(); let char = key.unwrap();
match char { match char {
Backspace => _del_or_backspace(Backspace), Backspace => self._del_or_backspace(Backspace),
Enter => { Enter => {
// TODO // TODO
} }
@ -313,7 +313,11 @@ impl Editor {
} }
if c == ctrl_key('s') { if c == ctrl_key('s') {
self.save(); // @TODO show save success/error
match self.save() {
Ok(_) => (),
Err(e) => (),
}
} }
if c == ctrl_key('h') { if c == ctrl_key('h') {
@ -380,7 +384,7 @@ impl Editor {
fn scroll(&mut self) { fn scroll(&mut self) {
self.render_x = 0; self.render_x = 0;
if self.cursor_y < self.rows.len() { 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 // Vertical scrolling
@ -453,10 +457,9 @@ impl Editor {
&self.filename &self.filename
}; };
let mut left_message = format!("{:.20} - {} lines", filename, self.rows.len()); let mut left_message = format!("{:.80} - {} lines", filename, self.rows.len());
let mut right_message = format!("{}/{}", self.cursor_y + 1, self.rows.len()); let right_message = format!("{}/{}", self.cursor_y + 1, self.rows.len());
let mut len = left_message.len(); let mut len = left_message.len();
let mut rlen = right_message.len();
if len > self.screen_cols { if len > self.screen_cols {
len = self.screen_cols; len = self.screen_cols;
left_message.truncate(len); left_message.truncate(len);
@ -464,7 +467,7 @@ impl Editor {
self.append_out(&left_message); self.append_out(&left_message);
for x in len..self.screen_cols { 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); self.append_out(&right_message);
break; break;
} }
@ -538,7 +541,7 @@ impl Editor {
// Row Operations // 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 mut rx: usize = 0;
let row = &mut self.rows[index]; let row = &mut self.rows[index];
@ -599,8 +602,11 @@ impl Editor {
fn rows_to_string(&mut self) -> String { fn rows_to_string(&mut self) -> String {
let mut output = String::new(); let mut output = String::new();
for row in self.rows { for row in &self.rows {
output.push_str(&row.chars) // 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 output