From 4d2caf512e23a6792e9f0d54ac223c38842afb77 Mon Sep 17 00:00:00 2001 From: Arijit Basu Date: Sun, 4 Apr 2021 10:05:52 +0530 Subject: [PATCH] Log explorer errors --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/app.rs | 2 +- src/explorer.rs | 74 ++++++++++++++++++++++++++----------------------- 4 files changed, 43 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 84afa4d..84325f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1671,7 +1671,7 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "xplr" -version = "0.2.15" +version = "0.2.16" dependencies = [ "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index c2925e3..c97d95e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "xplr" -version = "0.2.15" # Update app.rs +version = "0.2.16" # Update app.rs authors = ["Arijit Basu "] edition = "2018" description = "A hackable, minimal, fast TUI file explorer, stealing ideas from nnn and fzf" diff --git a/src/app.rs b/src/app.rs index f041590..2dd519b 100644 --- a/src/app.rs +++ b/src/app.rs @@ -14,7 +14,7 @@ use std::fs; use std::io; use std::path::PathBuf; -pub const VERSION: &str = "v0.2.15"; // Update Cargo.toml +pub const VERSION: &str = "v0.2.16"; // Update Cargo.toml pub const TEMPLATE_TABLE_ROW: &str = "TEMPLATE_TABLE_ROW"; diff --git a/src/explorer.rs b/src/explorer.rs index 87a86be..8a3d20b 100644 --- a/src/explorer.rs +++ b/src/explorer.rs @@ -1,8 +1,4 @@ -use crate::app::DirectoryBuffer; -use crate::app::ExplorerConfig; -use crate::app::Node; -use crate::app::Task; -use crate::app::{InternalMsg, MsgIn}; +use crate::app::{DirectoryBuffer, ExplorerConfig, ExternalMsg, InternalMsg, MsgIn, Node, Task}; use std::fs; use std::path::PathBuf; use std::sync::mpsc::Sender; @@ -20,39 +16,49 @@ pub fn explore( let config_cloned = config.clone(); thread::spawn(move || { - let nodes: Vec = fs::read_dir(&path) - .unwrap() - .filter_map(|d| { - d.ok().map(|e| { - e.path() - .file_name() - .map(|n| n.to_string_lossy().to_string()) - .unwrap_or_default() + fs::read_dir(&path) + .map(|dirs| { + dirs.filter_map(|d| { + d.ok().map(|e| { + e.path() + .file_name() + .map(|n| n.to_string_lossy().to_string()) + .unwrap_or_default() + }) }) + .map(|name| Node::new(parent.clone(), name)) + .filter(|n| config.apply(n)) + .collect::>() }) - .map(|name| Node::new(parent.clone(), name)) - .filter(|n| config.apply(n)) - .collect(); + .map(|nodes| { + let focus_index = if let Some(focus) = focused_path { + nodes + .iter() + .enumerate() + .find(|(_, n)| n.relative_path == focus) + .map(|(i, _)| i) + .unwrap_or(0) + } else { + 0 + }; - let focus_index = if let Some(focus) = focused_path { - nodes - .iter() - .enumerate() - .find(|(_, n)| n.relative_path == focus) - .map(|(i, _)| i) - .unwrap_or(0) - } else { - 0 - }; + let dir = DirectoryBuffer::new(parent.clone(), nodes, focus_index); - let dir = DirectoryBuffer::new(parent.clone(), nodes, focus_index); - - tx.send(Task::new( - 1, - MsgIn::Internal(InternalMsg::AddDirectory(parent, dir)), - None, - )) - .unwrap(); + tx.send(Task::new( + 1, + MsgIn::Internal(InternalMsg::AddDirectory(parent, dir)), + None, + )) + .unwrap(); + }) + .unwrap_or_else(|e| { + tx.send(Task::new( + 1, + MsgIn::External(ExternalMsg::LogError(e.to_string())), + None, + )) + .unwrap(); + }) }); if let Some(grand_parent) = path_cloned.parent() {