Compare commits

..

No commits in common. "5035e4c295cd4366b6a8691bfbce024fc5389a29" and "2bc0cde14b96ffec193743497fd4c4f18859c636" have entirely different histories.

2 changed files with 19 additions and 44 deletions

View file

@ -42,9 +42,9 @@ struct Cursor {
type Line = Range<usize>; type Line = Range<usize>;
impl Editor { impl Editor {
pub fn open_file(clipboard: Clipboard, path: PathBuf) -> std::io::Result<Self> { pub fn open_file(clipboard: Clipboard, path: PathBuf) -> Option<Self> {
let text = fs::read_to_string(&path)?; let text = fs::read_to_string(&path).ok()?;
Ok(Editor { Some(Editor {
text, text,
lines: Vec::new(), lines: Vec::new(),
scroll: 0, scroll: 0,

View file

@ -4,7 +4,7 @@ use crossterm::{
execute, queue, execute, queue,
style::{Color, Colors, ResetColor, SetColors}, style::{Color, Colors, ResetColor, SetColors},
terminal::{ terminal::{
self, disable_raw_mode, enable_raw_mode, Clear, ClearType, EnterAlternateScreen, disable_raw_mode, enable_raw_mode, Clear, ClearType, EnterAlternateScreen,
LeaveAlternateScreen, LeaveAlternateScreen,
}, },
}; };
@ -33,7 +33,6 @@ struct Navigator {
path: PathBuf, path: PathBuf,
init_path: PathBuf, init_path: PathBuf,
immediate_open: bool, immediate_open: bool,
message: Option<String>,
} }
impl Navigator { impl Navigator {
@ -50,7 +49,7 @@ impl Navigator {
path = arg.canonicalize().unwrap(); path = arg.canonicalize().unwrap();
break; break;
} else if arg.is_file() { } else if arg.is_file() {
if let Ok(editor) = Editor::open_file(clipboard.clone(), arg) { if let Some(editor) = Editor::open_file(clipboard.clone(), arg) {
editors.push(editor); editors.push(editor);
} }
} else { } else {
@ -69,7 +68,6 @@ impl Navigator {
init_path: path.clone(), init_path: path.clone(),
path, path,
immediate_open, immediate_open,
message: None,
} }
} }
@ -84,7 +82,6 @@ impl Navigator {
loop { loop {
self.get_files(); self.get_files();
self.draw(); self.draw();
self.message = None;
self.input(); self.input();
} }
} }
@ -126,11 +123,6 @@ impl Navigator {
queue!(stdout(), ResetColor).unwrap(); queue!(stdout(), ResetColor).unwrap();
} }
if let Some(text) = &self.message {
queue!(stdout(), MoveTo(0, terminal::size().unwrap().1)).unwrap();
print!("{text}");
}
stdout().flush().unwrap(); stdout().flush().unwrap();
} }
@ -152,10 +144,6 @@ impl Navigator {
} }
} }
fn message(&mut self, text: String) {
self.message = Some(text);
}
fn nav_up(&mut self) { fn nav_up(&mut self) {
self.selected = self.selected.saturating_sub(1); self.selected = self.selected.saturating_sub(1);
} }
@ -171,52 +159,39 @@ impl Navigator {
let i = self.selected - self.editors.len(); let i = self.selected - self.editors.len();
if i == 0 { if i == 0 {
if let Some(parent) = self.path.parent() { if let Some(parent) = self.path.parent() {
self.set_path(self.path.join(parent)); self.path = parent.to_owned();
env::set_current_dir(&self.path).unwrap();
} }
} else { } else {
let path = &self.files[i]; let path = &self.files[i];
if path.is_dir() { if path.is_dir() {
self.set_path(self.path.join(path)); self.path = self.path.join(path);
env::set_current_dir(&self.path).unwrap();
self.selected = self.editors.len();
} else if path.is_file() { } else if path.is_file() {
let path = path.canonicalize().unwrap(); let path = path.canonicalize().unwrap();
let mut editor_index = self.editors.len(); self.selected = self.editors.len();
for (i, editor) in self.editors.iter().enumerate() { for (i, editor) in self.editors.iter().enumerate() {
if editor.path() == Some(&path) { if editor.path() == Some(&path) {
editor_index = i; self.selected = i;
break; break;
} }
} }
if editor_index == self.editors.len() { if self.selected == self.editors.len() {
match Editor::open_file( if let Some(editor) =
self.clipboard.clone(), Editor::open_file(self.clipboard.clone(), path.canonicalize().unwrap())
path.canonicalize().unwrap(), {
) { self.editors.push(editor);
Ok(editor) => self.editors.push(editor), } else {
Err(err) => {
self.message(format!("Could not open file: {err}"));
return; return;
} }
} }
}
self.selected = editor_index;
self.open_selected(); self.open_selected();
} }
} }
} }
} }
fn set_path(&mut self, new_path: PathBuf) {
match env::set_current_dir(&new_path) {
Ok(()) => {
self.path = new_path;
self.selected = self.editors.len();
}
Err(err) => {
self.message(format!("Could not navigate to directory: {err}"));
}
}
}
fn open_selected(&mut self) { fn open_selected(&mut self) {
if self.selected < self.editors.len() { if self.selected < self.editors.len() {
self.editors[self.selected].enter(); self.editors[self.selected].enter();