Save new file functionality
This commit is contained in:
parent
2607f8bd63
commit
49ec4084c2
@ -23,6 +23,10 @@ struct StatusMessage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl StatusMessage {
|
impl StatusMessage {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::from("")
|
||||||
|
}
|
||||||
|
|
||||||
fn from<S: Into<String>>(message: S) -> Self {
|
fn from<S: Into<String>>(message: S) -> Self {
|
||||||
Self {
|
Self {
|
||||||
time: Instant::now(),
|
time: Instant::now(),
|
||||||
@ -141,17 +145,29 @@ impl Editor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process_keypress(&mut self) -> Result<(), std::io::Error> {
|
fn save(&mut self) {
|
||||||
let pressed_key = Terminal::read_key()?;
|
if self.document.file_name.is_none() {
|
||||||
match pressed_key {
|
let new_name = self.prompt("Save as: ").unwrap_or(None);
|
||||||
Key::Ctrl('q') => self.should_quit = true,
|
if new_name.is_none() {
|
||||||
Key::Ctrl('s') => {
|
self.status_message = StatusMessage::from("Save aborted.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.document.file_name = new_name;
|
||||||
|
}
|
||||||
|
|
||||||
if self.document.save().is_ok() {
|
if self.document.save().is_ok() {
|
||||||
self.status_message = StatusMessage::from("File saved successfully.");
|
self.status_message = StatusMessage::from("File saved successfully.");
|
||||||
} else {
|
} else {
|
||||||
self.status_message = StatusMessage::from("Error writing file!");
|
self.status_message = StatusMessage::from("Error writing file!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn process_keypress(&mut self) -> Result<(), std::io::Error> {
|
||||||
|
let pressed_key = Terminal::read_key()?;
|
||||||
|
match pressed_key {
|
||||||
|
Key::Ctrl('q') => self.should_quit = true,
|
||||||
|
Key::Ctrl('s') => self.save(),
|
||||||
Key::Char(c) => {
|
Key::Char(c) => {
|
||||||
self.document.insert(&self.cursor_position, c);
|
self.document.insert(&self.cursor_position, c);
|
||||||
self.move_cursor(Key::Right);
|
self.move_cursor(Key::Right);
|
||||||
@ -179,6 +195,42 @@ impl Editor {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn prompt(&mut self, prompt: &str) -> Result<Option<String>, std::io::Error> {
|
||||||
|
let mut result = String::new();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
self.status_message = StatusMessage::from(format!("{}{}", prompt, result));
|
||||||
|
self.refresh_screen()?;
|
||||||
|
|
||||||
|
match Terminal::read_key()? {
|
||||||
|
Key::Backspace => {
|
||||||
|
if !result.is_empty() {
|
||||||
|
result.truncate(result.len() - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Key::Char('\n') => break,
|
||||||
|
Key::Char(c) => {
|
||||||
|
if !c.is_control() {
|
||||||
|
result.push(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Key::Esc => {
|
||||||
|
result.truncate(0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.status_message = StatusMessage::default();
|
||||||
|
|
||||||
|
if result.is_empty() {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Some(result))
|
||||||
|
}
|
||||||
|
|
||||||
fn scroll(&mut self) {
|
fn scroll(&mut self) {
|
||||||
let Position { x, y } = self.cursor_position;
|
let Position { x, y } = self.cursor_position;
|
||||||
let width = self.terminal.size().width as usize;
|
let width = self.terminal.size().width as usize;
|
||||||
|
Loading…
Reference in New Issue
Block a user