Files loading/saving is working again.

This commit is contained in:
Nathan Vegdahl 2014-12-31 20:36:23 -08:00
parent cb5b79ec9c
commit 02fdbcf027
4 changed files with 35 additions and 38 deletions

View File

@ -2,7 +2,7 @@
use buffer::Buffer; use buffer::Buffer;
use std::path::Path; 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; use string_utils::grapheme_count;
@ -33,25 +33,25 @@ impl Editor {
} }
} }
// pub fn new_from_file(path: &Path) -> Editor { pub fn new_from_file(path: &Path) -> Editor {
// let buf = load_file_to_buffer(path).unwrap(); 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) { Editor {
// if self.dirty && self.file_path != Path::new("") { buffer: buf,
// let _ = save_buffer_to_file(&self.buffer, &self.file_path); file_path: path.clone(),
// self.dirty = false; 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 update_dim(&mut self, h: uint, w: uint) { pub fn update_dim(&mut self, h: uint, w: uint) {
self.view_dim = (h, w); self.view_dim = (h, w);

View File

@ -2,7 +2,7 @@ use std::io::{IoResult, BufferedReader, BufferedWriter};
use std::io::fs::File; use std::io::fs::File;
use std::path::Path; use std::path::Path;
use buffer::TextBuffer; use buffer::Buffer as TextBuffer;
pub fn load_file_to_buffer(path: &Path) -> IoResult<TextBuffer> { pub fn load_file_to_buffer(path: &Path) -> IoResult<TextBuffer> {
let mut tb = TextBuffer::new(); let mut tb = TextBuffer::new();
@ -24,11 +24,11 @@ pub fn load_file_to_buffer(path: &Path) -> IoResult<TextBuffer> {
pub fn save_buffer_to_file(tb: &TextBuffer, path: &Path) -> IoResult<()> { pub fn save_buffer_to_file(tb: &TextBuffer, path: &Path) -> IoResult<()> {
// TODO: make save atomic // 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))); let mut f = BufferedWriter::new(try!(File::create(path)));
for c in iter { for g in iter {
let _ = f.write_char(c); let _ = f.write_str(g);
} }
return Ok(()); return Ok(());

View File

@ -9,7 +9,7 @@ use term_ui::TermUI;
mod string_utils; mod string_utils;
mod buffer; mod buffer;
//mod files; mod files;
mod editor; mod editor;
mod term_ui; mod term_ui;
@ -40,16 +40,13 @@ fn main() {
// Get command-line arguments // Get command-line arguments
let args: Args = Docopt::new(USAGE).and_then(|d| d.decode()).unwrap_or_else(|e| e.exit()); let args: Args = Docopt::new(USAGE).and_then(|d| d.decode()).unwrap_or_else(|e| e.exit());
// // Load file, if specified // Load file, if specified
// let editor = if let Option::Some(s) = args.arg_file { let editor = if let Option::Some(s) = args.arg_file {
// Editor::new_from_file(&Path::new(s.as_slice())) Editor::new_from_file(&Path::new(s.as_slice()))
// } }
// else { else {
// Editor::new() Editor::new()
// }; };
//
let editor = Editor::new();
// Initialize and start UI // Initialize and start UI
let mut ui = TermUI::new_from_editor(editor); let mut ui = TermUI::new_from_editor(editor);

View File

@ -74,9 +74,9 @@ impl TermUI {
break; break;
}, },
// K_CTRL_S => { K_CTRL_S => {
// self.editor.save_if_dirty(); self.editor.save_if_dirty();
// }, },
K_PAGEUP => { K_PAGEUP => {
self.editor.page_up(); self.editor.page_up();