open files

This commit is contained in:
Crispy 2023-02-26 01:19:24 +01:00
parent c656dc0931
commit b75fb1ae47
2 changed files with 16 additions and 4 deletions

View file

@ -1,6 +1,8 @@
use std::{ use std::{
fs,
io::{stdin, stdout, Write}, io::{stdin, stdout, Write},
ops::Range, ops::Range,
process::exit,
}; };
use termion::{ use termion::{
clear, cursor, clear, cursor,
@ -26,10 +28,18 @@ struct Cursor {
type Line = Range<usize>; type Line = Range<usize>;
impl Editor { impl Editor {
pub fn new() -> Self { pub fn new(path: Option<String>) -> Self {
let text = path
.map(|path| {
fs::read_to_string(path).unwrap_or_else(|err| {
println!("Error: {err}");
exit(1);
})
})
.unwrap_or_default();
Editor { Editor {
// text: String::new(), text,
text: include_str!("editor.rs").into(),
lines: Vec::new(), lines: Vec::new(),
cursor: Cursor { line: 0, column: 0 }, cursor: Cursor { line: 0, column: 0 },
quit: false, quit: false,

View file

@ -1,6 +1,8 @@
use std::env;
mod editor; mod editor;
use editor::Editor; use editor::Editor;
fn main() { fn main() {
Editor::new().run(); Editor::new(env::args().nth(1)).run();
} }