From dd9166d465e3adc29200e73f72b5b3f29bbb6c53 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 30 Aug 2019 16:17:06 -0400 Subject: [PATCH] Add Save As prompt --- src/editor.rs | 48 ++++++++++++++++++++++++++++++++++++++++-------- src/main.rs | 6 +++--- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/editor.rs b/src/editor.rs index cd8780f..17fa90a 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -241,6 +241,35 @@ impl Editor { // Input // ------------------------------------------------------------------------ + fn prompt(&mut self, prompt: &str) -> String { + let mut buffer = String::new(); + + loop { + self.set_status_message(&format!("{} {}", prompt, buffer)); + self.refresh_screen(); + + let char = self.read_key(); + if char.is_some() { + let char = char.unwrap(); + match char { + Enter => { + if buffer.len() != 0 { + self.set_status_message(""); + return buffer; + } + } + OtherKey(ch) => { + if (!ch.is_ascii_control()) && (ch as u8) < 128 { + buffer.push(ch); + continue; + } + } + _ => (), + }; + } + } + } + fn move_cursor(&mut self, key: &EditorKey) { let row = self.rows.get(self.cursor_y); match key { @@ -516,7 +545,7 @@ impl Editor { } } - pub fn refresh_screen(&mut self) -> io::Result<()> { + pub fn refresh_screen(&mut self) { self.scroll(); self.output_buffer.clear(); @@ -539,7 +568,9 @@ impl Editor { let stdout = io::stdout(); let mut handle = stdout.lock(); - handle.write_all(&self.output_buffer.as_bytes()) + + // If you can't write to stdout, you might as well just panic + handle.write_all(&self.output_buffer.as_bytes()).unwrap(); } /// Set the status bar message @@ -579,6 +610,7 @@ impl Editor { rx } + /// Convert tab characters to spaces for display fn update_row(&mut self, index: usize) { let row = &mut self.rows[index]; let str = row.chars.clone(); @@ -664,15 +696,15 @@ impl Editor { if self.cursor_x == 0 { self.insert_row(self.cursor_y, ""); } else { + // Clone the contents of the current row let row = &mut self.rows[self.cursor_y]; let row_chars = row.chars.clone(); - let len = row.chars.len() - self.cursor_x; - // Truncate the old row if you aren't at the end of a line - if self.cursor_x < row.chars.len() { - row.chars.truncate(self.cursor_x); - } + // Truncate the original row up to the cursor + row.chars.truncate(self.cursor_x); + // Create the new row as a slice of the contents of the old + // row, from the cursor to the end of the line let slice = &row_chars[self.cursor_x..]; self.insert_row(self.cursor_y + 1, slice); @@ -741,7 +773,7 @@ impl Editor { fn save(&mut self) -> io::Result<()> { if self.filename.len() == 0 { - return Ok(()); + self.filename = self.prompt("Save as:"); } let mut file = File::create(&self.filename)?; diff --git a/src/main.rs b/src/main.rs index 9f5999f..14a52df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ mod editor; mod helpers; -use crate::editor::{Editor, EditorKey}; +use crate::editor::Editor; use crate::helpers::*; use libc::STDIN_FILENO; use std::env; @@ -31,7 +31,7 @@ fn main() -> Result<(), Error> { // `None` is returned on a quit action, in other cases, `Some(())` is returned, // continuing the loop loop { - editor.refresh_screen()?; + editor.refresh_screen(); let key = editor.process_keypress(); if key.is_none() { @@ -39,7 +39,7 @@ fn main() -> Result<(), Error> { } /*match key.unwrap() { - EditorKey::OtherKey('\0') => (), + editor::EditorKey::OtherKey('\0') => (), _ => println!("{:?}\r\n", key) } */ }