Add Save As prompt

This commit is contained in:
Timothy Warren 2019-08-30 16:17:06 -04:00
parent aadbccaf9d
commit dd9166d465
2 changed files with 43 additions and 11 deletions

View File

@ -241,6 +241,35 @@ impl Editor {
// Input // 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<char>) { fn move_cursor(&mut self, key: &EditorKey<char>) {
let row = self.rows.get(self.cursor_y); let row = self.rows.get(self.cursor_y);
match key { 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.scroll();
self.output_buffer.clear(); self.output_buffer.clear();
@ -539,7 +568,9 @@ impl Editor {
let stdout = io::stdout(); let stdout = io::stdout();
let mut handle = stdout.lock(); 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 /// Set the status bar message
@ -579,6 +610,7 @@ impl Editor {
rx rx
} }
/// Convert tab characters to spaces for display
fn update_row(&mut self, index: usize) { fn update_row(&mut self, index: usize) {
let row = &mut self.rows[index]; let row = &mut self.rows[index];
let str = row.chars.clone(); let str = row.chars.clone();
@ -664,15 +696,15 @@ impl Editor {
if self.cursor_x == 0 { if self.cursor_x == 0 {
self.insert_row(self.cursor_y, ""); self.insert_row(self.cursor_y, "");
} else { } else {
// Clone the contents of the current row
let row = &mut self.rows[self.cursor_y]; let row = &mut self.rows[self.cursor_y];
let row_chars = row.chars.clone(); 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 // Truncate the original row up to the cursor
if self.cursor_x < row.chars.len() {
row.chars.truncate(self.cursor_x); 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..]; let slice = &row_chars[self.cursor_x..];
self.insert_row(self.cursor_y + 1, slice); self.insert_row(self.cursor_y + 1, slice);
@ -741,7 +773,7 @@ impl Editor {
fn save(&mut self) -> io::Result<()> { fn save(&mut self) -> io::Result<()> {
if self.filename.len() == 0 { if self.filename.len() == 0 {
return Ok(()); self.filename = self.prompt("Save as:");
} }
let mut file = File::create(&self.filename)?; let mut file = File::create(&self.filename)?;

View File

@ -1,7 +1,7 @@
mod editor; mod editor;
mod helpers; mod helpers;
use crate::editor::{Editor, EditorKey}; use crate::editor::Editor;
use crate::helpers::*; use crate::helpers::*;
use libc::STDIN_FILENO; use libc::STDIN_FILENO;
use std::env; use std::env;
@ -31,7 +31,7 @@ fn main() -> Result<(), Error> {
// `None` is returned on a quit action, in other cases, `Some(())` is returned, // `None` is returned on a quit action, in other cases, `Some(())` is returned,
// continuing the loop // continuing the loop
loop { loop {
editor.refresh_screen()?; editor.refresh_screen();
let key = editor.process_keypress(); let key = editor.process_keypress();
if key.is_none() { if key.is_none() {
@ -39,7 +39,7 @@ fn main() -> Result<(), Error> {
} }
/*match key.unwrap() { /*match key.unwrap() {
EditorKey::OtherKey('\0') => (), editor::EditorKey::OtherKey('\0') => (),
_ => println!("{:?}\r\n", key) _ => println!("{:?}\r\n", key)
} */ } */
} }