diff --git a/src/editor.rs b/src/editor.rs index 34be91c..59cc415 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -2,7 +2,7 @@ use buffer::Buffer; use std::path::Path; -//use files::{load_file_to_buffer, save_buffer_to_file}; +use files::{load_file_to_buffer, save_buffer_to_file}; use string_utils::grapheme_count; @@ -33,25 +33,25 @@ impl Editor { } } - // pub fn new_from_file(path: &Path) -> Editor { - // let buf = load_file_to_buffer(path).unwrap(); - // - // Editor { - // buffer: buf, - // file_path: path.clone(), - // dirty: false, - // view_dim: (0, 0), - // view_pos: (0, 0), - // cursor: (0, 0), - // } - // } + pub fn new_from_file(path: &Path) -> Editor { + let buf = load_file_to_buffer(path).unwrap(); + + Editor { + buffer: buf, + file_path: path.clone(), + dirty: false, + view_dim: (0, 0), + view_pos: (0, 0), + cursor: (0, 0), + } + } - // pub fn save_if_dirty(&mut self) { - // if self.dirty && self.file_path != Path::new("") { - // let _ = save_buffer_to_file(&self.buffer, &self.file_path); - // self.dirty = false; - // } - // } + pub fn save_if_dirty(&mut self) { + if self.dirty && self.file_path != Path::new("") { + let _ = save_buffer_to_file(&self.buffer, &self.file_path); + self.dirty = false; + } + } pub fn update_dim(&mut self, h: uint, w: uint) { self.view_dim = (h, w); diff --git a/src/files.rs b/src/files.rs index 5201934..2a8aec9 100644 --- a/src/files.rs +++ b/src/files.rs @@ -2,7 +2,7 @@ use std::io::{IoResult, BufferedReader, BufferedWriter}; use std::io::fs::File; use std::path::Path; -use buffer::TextBuffer; +use buffer::Buffer as TextBuffer; pub fn load_file_to_buffer(path: &Path) -> IoResult { let mut tb = TextBuffer::new(); @@ -24,11 +24,11 @@ pub fn load_file_to_buffer(path: &Path) -> IoResult { pub fn save_buffer_to_file(tb: &TextBuffer, path: &Path) -> IoResult<()> { // TODO: make save atomic - let mut iter = tb.root_iter(); + let mut iter = tb.grapheme_iter(); let mut f = BufferedWriter::new(try!(File::create(path))); - for c in iter { - let _ = f.write_char(c); + for g in iter { + let _ = f.write_str(g); } return Ok(()); diff --git a/src/main.rs b/src/main.rs index eb4de3b..77f049f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ use term_ui::TermUI; mod string_utils; mod buffer; -//mod files; +mod files; mod editor; mod term_ui; @@ -40,17 +40,14 @@ fn main() { // Get command-line arguments let args: Args = Docopt::new(USAGE).and_then(|d| d.decode()).unwrap_or_else(|e| e.exit()); - // // Load file, if specified - // let editor = if let Option::Some(s) = args.arg_file { - // Editor::new_from_file(&Path::new(s.as_slice())) - // } - // else { - // Editor::new() - // }; - // - - let editor = Editor::new(); - + // Load file, if specified + let editor = if let Option::Some(s) = args.arg_file { + Editor::new_from_file(&Path::new(s.as_slice())) + } + else { + Editor::new() + }; + // Initialize and start UI let mut ui = TermUI::new_from_editor(editor); ui.ui_loop(); diff --git a/src/term_ui.rs b/src/term_ui.rs index cef5428..3a87e79 100644 --- a/src/term_ui.rs +++ b/src/term_ui.rs @@ -74,9 +74,9 @@ impl TermUI { break; }, - // K_CTRL_S => { - // self.editor.save_if_dirty(); - // }, + K_CTRL_S => { + self.editor.save_if_dirty(); + }, K_PAGEUP => { self.editor.page_up();