Add Save As prompt
This commit is contained in:
parent
aadbccaf9d
commit
dd9166d465
@ -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<char>) {
|
||||
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)?;
|
||||
|
@ -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)
|
||||
} */
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user