Basic save functionality

This commit is contained in:
Timothy Warren 2019-08-29 16:32:17 -04:00
parent 0c81912cac
commit 5d1c8d8b8b

View File

@ -49,7 +49,9 @@ pub struct Editor {
/// Keycode mapping enum
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum EditorKey<T> {
Enter,
Escape,
Backspace,
ArrowLeft,
ArrowRight,
ArrowUp,
@ -124,7 +126,9 @@ impl Editor {
for char in in_str.chars() {
input.push(match char {
'\x7f' => Backspace,
'\x1b' => Escape,
'\r' => Enter,
_ => OtherKey(char),
});
}
@ -278,23 +282,19 @@ impl Editor {
let key = self.read_key();
if key.is_some() {
let char = key.unwrap();
match char {
OtherKey(c) => {
if c == ctrl_key('q') {
print!("\x1b[2J");
print!("\x1b[H");
// Break out of the input loop
return None;
Backspace => _del_or_backspace(Backspace),
Enter => {
// TODO
}
}
DeleteKey => (),
Escape => (),
ArrowUp => self.move_cursor(&ArrowUp),
ArrowDown => self.move_cursor(&ArrowDown),
ArrowLeft => self.move_cursor(&ArrowLeft),
ArrowRight => self.move_cursor(&ArrowRight),
PageUp => self.page_up_or_down(PageUp),
PageDown => self.page_up_or_down(PageDown),
PageUp => self._page_up_or_down(PageUp),
PageDown => self._page_up_or_down(PageDown),
HomeKey => {
self.cursor_x = 0;
}
@ -303,6 +303,26 @@ impl Editor {
self.cursor_x = self.rows[self.cursor_y].chars.len();
}
}
OtherKey(c) => {
if c.is_ascii_control() {
if c == ctrl_key('q') {
print!("\x1b[2J");
print!("\x1b[H");
// Break out of the input loop
return None;
}
if c == ctrl_key('s') {
self.save();
}
if c == ctrl_key('h') {
self._del_or_backspace(Backspace);
}
} else {
self.insert_char(c);
}
}
_ => (),
};
@ -313,7 +333,11 @@ impl Editor {
Some(OtherKey('\0'))
}
fn page_up_or_down(&mut self, key: EditorKey<char>) {
fn _del_or_backspace(&mut self, key: EditorKey<char>) {
// TODO
}
fn _page_up_or_down(&mut self, key: EditorKey<char>) {
let mut times = self.screen_rows;
// Update the cursor position
@ -542,10 +566,46 @@ impl Editor {
self.update_row(self.rows.len() - 1);
}
fn row_insert_char(&mut self, row_index: usize, char_index: usize, ch: char) {
let mut at = char_index;
let row = &mut self.rows[row_index];
if at > row.chars.len() {
at = row.chars.len();
}
row.chars.insert(at, ch);
self.update_row(row_index);
}
// ------------------------------------------------------------------------
// Editor Operations
// ------------------------------------------------------------------------
fn insert_char(&mut self, ch: char) {
if self.cursor_y == self.rows.len() {
self.append_row("");
}
self.row_insert_char(self.cursor_y, self.cursor_x, ch);
self.cursor_x += 1;
}
// ------------------------------------------------------------------------
// File I/O
// ------------------------------------------------------------------------
fn rows_to_string(&mut self) -> String {
let mut output = String::new();
for row in self.rows {
output.push_str(&row.chars)
}
output
}
/// Open a file for display
pub fn open(&mut self, filename: &str) -> io::Result<()> {
self.filename = filename.to_owned();
@ -560,4 +620,18 @@ impl Editor {
Ok(())
}
fn save(&mut self) -> io::Result<()> {
if self.filename.len() == 0 {
return Ok(())
}
let mut file = File::create(&self.filename)?;
let data = &mut self.rows_to_string();
file.write_all(data.as_bytes())?;
file.sync_all()?;
Ok(())
}
}