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 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);

View File

@ -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<TextBuffer> {
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<()> {
// 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(());

View File

@ -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();

View File

@ -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();