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

View file

@ -16,7 +16,7 @@ mod util;
use editor::Editor;
fn main() {
Navigator::new(env::args().nth(1)).run();
Navigator::new().run();
}
struct Navigator {
@ -27,13 +27,13 @@ struct Navigator {
}
impl Navigator {
fn new(immediate_file: Option<String>) -> Self {
fn new() -> Self {
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 {
editors,
selected: Some(0),
path: String::new(), // TODO
path: String::new(),
_term: term,
}
}
@ -105,7 +105,7 @@ impl Navigator {
fn new_editor(&mut self) {
self.selected = Some(self.editors.len());
self.editors.push(Editor::new(None));
self.editors.push(Editor::new_empty());
self.open_selected();
}