diff --git a/src/editor.rs b/src/editor.rs index 86ab9e1..bfe34f6 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -1,6 +1,8 @@ use std::{ + fs, io::{stdin, stdout, Write}, ops::Range, + process::exit, }; use termion::{ clear, cursor, @@ -26,10 +28,18 @@ struct Cursor { type Line = Range; impl Editor { - pub fn new() -> Self { + pub fn new(path: Option) -> Self { + let text = path + .map(|path| { + fs::read_to_string(path).unwrap_or_else(|err| { + println!("Error: {err}"); + exit(1); + }) + }) + .unwrap_or_default(); + Editor { - // text: String::new(), - text: include_str!("editor.rs").into(), + text, lines: Vec::new(), cursor: Cursor { line: 0, column: 0 }, quit: false, diff --git a/src/main.rs b/src/main.rs index bf43878..d25ad80 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ +use std::env; + mod editor; use editor::Editor; fn main() { - Editor::new().run(); + Editor::new(env::args().nth(1)).run(); }