open several files when provided as args

This commit is contained in:
Crispy 2023-02-28 20:56:24 +01:00
parent 8194ff2ec2
commit 33ee68acad
2 changed files with 20 additions and 17 deletions

View file

@ -3,6 +3,7 @@ use std::{
io::{stdin, stdout, Write}, io::{stdin, stdout, Write},
ops::Range, ops::Range,
process::exit, process::exit,
vec,
}; };
use termion::{ use termion::{
clear, cursor, clear, cursor,
@ -34,29 +35,31 @@ struct Cursor {
type Line = Range<usize>; type Line = Range<usize>;
impl Editor { impl Editor {
pub fn new(path: Option<String>) -> Self { pub fn new(path: String) -> Self {
let text = path let text = fs::read_to_string(&path).unwrap_or_default();
.as_ref()
.map(|path| {
fs::read_to_string(path).unwrap_or_else(|err| {
println!("Error: {err}");
exit(1);
})
})
.unwrap_or_default();
let mut this = Editor { let mut this = Editor {
text, text,
lines: Vec::new(), lines: Vec::new(),
scroll: 0, scroll: 0,
cursor: Cursor { line: 0, column: 0 }, cursor: Cursor { line: 0, column: 0 },
path, path: Some(path),
active: false, active: false,
}; };
this.find_lines(); this.find_lines();
this this
} }
pub fn new_empty() -> Self {
Editor {
text: String::new(),
lines: vec![0..0],
scroll: 0,
cursor: Cursor { line: 0, column: 0 },
path: None,
active: false,
}
}
pub fn name(&self) -> &str { pub fn name(&self) -> &str {
self.path.as_ref().map_or("untitled", |s| s) self.path.as_ref().map_or("untitled", |s| s)
} }

View file

@ -16,7 +16,7 @@ mod util;
use editor::Editor; use editor::Editor;
fn main() { fn main() {
Navigator::new(env::args().nth(1)).run(); Navigator::new().run();
} }
struct Navigator { struct Navigator {
@ -27,13 +27,13 @@ struct Navigator {
} }
impl Navigator { impl Navigator {
fn new(immediate_file: Option<String>) -> Self { fn new() -> Self {
let term = stdout().into_raw_mode().unwrap(); let term = stdout().into_raw_mode().unwrap();
let editors = vec![Editor::new(immediate_file)]; let editors = env::args().skip(1).map(Editor::new).collect();
Self { Self {
editors, editors,
selected: Some(0), selected: Some(0),
path: String::new(), // TODO path: String::new(),
_term: term, _term: term,
} }
} }
@ -105,7 +105,7 @@ impl Navigator {
fn new_editor(&mut self) { fn new_editor(&mut self) {
self.selected = Some(self.editors.len()); self.selected = Some(self.editors.len());
self.editors.push(Editor::new(None)); self.editors.push(Editor::new_empty());
self.open_selected(); self.open_selected();
} }