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::prelude::*;
use std::io::BufReader; use std::io::BufReader;
pub struct EditorRow {
row_chars: String,
}
/// Main structure for the editor /// Main structure for the editor
/// ///
/// impl blocks are split similarly to the original C implementation /// impl blocks are split similarly to the original C implementation
@ -13,6 +17,8 @@ use std::io::BufReader;
pub struct Editor { pub struct Editor {
cursor_x: usize, cursor_x: usize,
cursor_y: usize, cursor_y: usize,
row: EditorRow,
num_rows: usize,
screen_cols: usize, screen_cols: usize,
screen_rows: usize, screen_rows: usize,
output_buffer: String, output_buffer: String,
@ -123,15 +129,13 @@ impl Editor {
} }
} }
return Some(output); return Some(output);
} }
fn get_window_size(&mut self) -> TermSize { fn get_window_size(&mut self) -> TermSize {
match get_term_size() { match get_term_size() {
Some(size) => 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 { if self.cursor_x != 0 {
self.cursor_x -= 1; self.cursor_x -= 1;
} }
}, }
ArrowRight => { ArrowRight => {
if self.cursor_x != self.screen_cols - 1 { if self.cursor_x != self.screen_cols - 1 {
self.cursor_x += 1; self.cursor_x += 1;
} }
}, }
ArrowUp => { ArrowUp => {
if self.cursor_y != 0 { if self.cursor_y != 0 {
self.cursor_y -= 1; self.cursor_y -= 1;
} }
}, }
ArrowDown => { ArrowDown => {
if self.cursor_y != self.screen_rows - 1 { if self.cursor_y != self.screen_rows - 1 {
self.cursor_y += 1; self.cursor_y += 1;
} }
}, }
_ => (), _ => (),
}; };
} }
@ -182,13 +186,13 @@ impl Editor {
// Break out of the input loop // Break out of the input loop
return None; return None;
} }
}, }
HomeKey => { HomeKey => {
self.cursor_x = 0; self.cursor_x = 0;
}, }
EndKey => { EndKey => {
self.cursor_x = self.screen_cols - 1; self.cursor_x = self.screen_cols - 1;
}, }
PageUp => self.page_up_or_down(PageUp), PageUp => self.page_up_or_down(PageUp),
PageDown => self.page_up_or_down(PageDown), PageDown => self.page_up_or_down(PageDown),
ArrowUp => self.move_cursor(&ArrowUp), ArrowUp => self.move_cursor(&ArrowUp),
@ -204,7 +208,7 @@ impl Editor {
} }
fn page_up_or_down(&mut self, key: EditorKey<char>) { fn page_up_or_down(&mut self, key: EditorKey<char>) {
let mut times = self.screen_rows; let mut times = self.screen_rows;
while times > 1 { while times > 1 {
times -= 1; times -= 1;
@ -243,7 +247,7 @@ impl Editor {
} }
while padding > 0 { while padding > 0 {
self.append_out(" "); self.append_out(" ");
padding -=1; padding -= 1;
} }
self.append_out(&welcome); self.append_out(&welcome);
@ -279,3 +283,8 @@ impl Editor {
handle.write_all(&self.output_buffer.as_bytes()) handle.write_all(&self.output_buffer.as_bytes())
} }
} }
// File I/O
impl Editor {
pub fn open(&mut self) {}
}