Start chapter 4

This commit is contained in:
Timothy Warren 2019-08-27 08:30:51 -04:00
parent a0c926dee9
commit a67562b3a2

View File

@ -6,6 +6,10 @@ use std::io;
use std::io::prelude::*;
use std::io::BufReader;
pub struct EditorRow {
row_chars: String,
}
/// Main structure for the editor
///
/// impl blocks are split similarly to the original C implementation
@ -13,6 +17,8 @@ use std::io::BufReader;
pub struct Editor {
cursor_x: usize,
cursor_y: usize,
row: EditorRow,
num_rows: usize,
screen_cols: usize,
screen_rows: usize,
output_buffer: String,
@ -123,15 +129,13 @@ impl Editor {
}
}
return Some(output);
}
fn get_window_size(&mut self) -> TermSize {
match get_term_size() {
Some(size) => size,
None => unimplemented!("The easy way usually works")
None => unimplemented!("The easy way usually works"),
}
}
}
@ -144,22 +148,22 @@ impl Editor {
if self.cursor_x != 0 {
self.cursor_x -= 1;
}
},
}
ArrowRight => {
if self.cursor_x != self.screen_cols - 1 {
self.cursor_x += 1;
}
},
}
ArrowUp => {
if self.cursor_y != 0 {
self.cursor_y -= 1;
}
},
}
ArrowDown => {
if self.cursor_y != self.screen_rows - 1 {
self.cursor_y += 1;
}
},
}
_ => (),
};
}
@ -182,13 +186,13 @@ impl Editor {
// Break out of the input loop
return None;
}
},
}
HomeKey => {
self.cursor_x = 0;
},
}
EndKey => {
self.cursor_x = self.screen_cols - 1;
},
}
PageUp => self.page_up_or_down(PageUp),
PageDown => self.page_up_or_down(PageDown),
ArrowUp => self.move_cursor(&ArrowUp),
@ -279,3 +283,8 @@ impl Editor {
handle.write_all(&self.output_buffer.as_bytes())
}
}
// File I/O
impl Editor {
pub fn open(&mut self) {}
}