2021-10-30 06:29:17 +00:00
|
|
|
use crate::app::{
|
2022-06-02 16:40:58 +00:00
|
|
|
DirectoryBuffer, ExplorerConfig, ExternalMsg, InternalMsg, MsgIn, Node, Task,
|
2021-10-30 06:29:17 +00:00
|
|
|
};
|
|
|
|
use anyhow::{Error, Result};
|
2021-03-31 11:50:37 +00:00
|
|
|
use std::fs;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::sync::mpsc::Sender;
|
|
|
|
use std::thread;
|
|
|
|
|
2022-10-28 20:41:53 +00:00
|
|
|
pub fn explore(parent: &PathBuf, config: &ExplorerConfig) -> Result<Vec<Node>> {
|
2022-11-12 20:09:24 +00:00
|
|
|
let dirs = fs::read_dir(parent)?;
|
2023-03-19 19:37:04 +00:00
|
|
|
let nodes = dirs
|
2021-05-09 17:09:49 +00:00
|
|
|
.filter_map(|d| {
|
|
|
|
d.ok().map(|e| {
|
|
|
|
e.path()
|
|
|
|
.file_name()
|
|
|
|
.map(|n| n.to_string_lossy().to_string())
|
|
|
|
.unwrap_or_default()
|
2021-03-31 11:50:37 +00:00
|
|
|
})
|
2021-05-09 17:09:49 +00:00
|
|
|
})
|
2021-06-04 11:41:13 +00:00
|
|
|
.map(|name| Node::new(parent.to_string_lossy().to_string(), name))
|
2023-03-19 19:37:04 +00:00
|
|
|
.filter(|n| config.filter(n));
|
2021-05-09 17:09:49 +00:00
|
|
|
|
2023-03-19 19:37:04 +00:00
|
|
|
let mut nodes = if let Some(searcher) = config.searcher.as_ref() {
|
|
|
|
searcher.search(nodes)
|
2022-09-25 07:22:24 +00:00
|
|
|
} else {
|
2023-03-19 19:37:04 +00:00
|
|
|
nodes.collect()
|
2022-09-25 07:22:24 +00:00
|
|
|
};
|
2021-05-09 17:09:49 +00:00
|
|
|
|
2023-03-19 19:37:04 +00:00
|
|
|
let is_ordered_search = config
|
|
|
|
.searcher
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| !s.unordered)
|
|
|
|
.unwrap_or(false);
|
|
|
|
|
|
|
|
if !is_ordered_search {
|
|
|
|
nodes.sort_by(|a, b| config.sort(a, b));
|
|
|
|
}
|
|
|
|
|
2022-10-28 20:41:53 +00:00
|
|
|
Ok(nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn explore_sync(
|
|
|
|
config: ExplorerConfig,
|
|
|
|
parent: PathBuf,
|
|
|
|
focused_path: Option<PathBuf>,
|
|
|
|
fallback_focus: usize,
|
|
|
|
) -> Result<DirectoryBuffer> {
|
|
|
|
let nodes = explore(&parent, &config)?;
|
2022-09-25 07:22:24 +00:00
|
|
|
let focus_index = if config.searcher.is_some() {
|
|
|
|
0
|
|
|
|
} else if let Some(focus) = focused_path {
|
2021-06-04 11:41:13 +00:00
|
|
|
let focus_str = focus.to_string_lossy().to_string();
|
2021-05-09 17:09:49 +00:00
|
|
|
nodes
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
2021-10-06 07:35:07 +00:00
|
|
|
.find(|(_, n)| n.relative_path == focus_str)
|
2021-05-09 17:09:49 +00:00
|
|
|
.map(|(i, _)| i)
|
2023-03-19 19:37:04 +00:00
|
|
|
.unwrap_or_else(|| fallback_focus.min(nodes.len().saturating_sub(1)))
|
2021-05-09 17:09:49 +00:00
|
|
|
} else {
|
2021-07-29 17:25:44 +00:00
|
|
|
0
|
2021-05-09 17:09:49 +00:00
|
|
|
};
|
2021-03-31 11:50:37 +00:00
|
|
|
|
2021-06-04 11:41:13 +00:00
|
|
|
Ok(DirectoryBuffer::new(
|
|
|
|
parent.to_string_lossy().to_string(),
|
|
|
|
nodes,
|
|
|
|
focus_index,
|
|
|
|
))
|
2021-05-09 17:09:49 +00:00
|
|
|
}
|
2021-03-31 11:50:37 +00:00
|
|
|
|
2021-06-04 11:41:13 +00:00
|
|
|
pub(crate) fn explore_async(
|
2021-05-09 17:09:49 +00:00
|
|
|
config: ExplorerConfig,
|
2021-06-04 11:41:13 +00:00
|
|
|
parent: PathBuf,
|
|
|
|
focused_path: Option<PathBuf>,
|
2021-06-04 17:21:10 +00:00
|
|
|
fallback_focus: usize,
|
2021-05-11 06:16:21 +00:00
|
|
|
tx_msg_in: Sender<Task>,
|
2021-05-09 17:09:49 +00:00
|
|
|
) {
|
|
|
|
thread::spawn(move || {
|
2021-06-04 17:21:10 +00:00
|
|
|
explore_sync(config, parent.clone(), focused_path, fallback_focus)
|
2021-10-30 04:23:39 +00:00
|
|
|
.and_then(|buf| {
|
2021-05-11 06:16:21 +00:00
|
|
|
tx_msg_in
|
|
|
|
.send(Task::new(
|
2021-07-29 17:25:44 +00:00
|
|
|
MsgIn::Internal(InternalMsg::SetDirectory(buf)),
|
2021-05-11 06:16:21 +00:00
|
|
|
None,
|
|
|
|
))
|
2021-10-30 04:23:39 +00:00
|
|
|
.map_err(Error::new)
|
2021-04-04 04:35:52 +00:00
|
|
|
})
|
|
|
|
.unwrap_or_else(|e| {
|
2021-05-11 06:16:21 +00:00
|
|
|
tx_msg_in
|
|
|
|
.send(Task::new(
|
|
|
|
MsgIn::External(ExternalMsg::LogError(e.to_string())),
|
|
|
|
None,
|
|
|
|
))
|
2021-10-30 04:23:39 +00:00
|
|
|
.unwrap_or_default(); // Let's not panic if xplr closes.
|
2021-04-04 04:35:52 +00:00
|
|
|
})
|
2021-03-31 11:50:37 +00:00
|
|
|
});
|
2021-05-09 17:09:49 +00:00
|
|
|
}
|
2021-03-31 11:50:37 +00:00
|
|
|
|
2021-06-04 11:41:13 +00:00
|
|
|
pub(crate) fn explore_recursive_async(
|
2021-05-09 17:09:49 +00:00
|
|
|
config: ExplorerConfig,
|
2021-06-04 11:41:13 +00:00
|
|
|
parent: PathBuf,
|
|
|
|
focused_path: Option<PathBuf>,
|
2021-06-04 17:21:10 +00:00
|
|
|
fallback_focus: usize,
|
2021-05-11 06:16:21 +00:00
|
|
|
tx_msg_in: Sender<Task>,
|
2021-05-09 17:09:49 +00:00
|
|
|
) {
|
2021-06-04 11:41:13 +00:00
|
|
|
explore_async(
|
|
|
|
config.clone(),
|
|
|
|
parent.clone(),
|
|
|
|
focused_path,
|
2021-06-04 17:21:10 +00:00
|
|
|
fallback_focus,
|
2021-06-04 11:41:13 +00:00
|
|
|
tx_msg_in.clone(),
|
|
|
|
);
|
|
|
|
if let Some(grand_parent) = parent.parent() {
|
2022-10-27 16:17:25 +00:00
|
|
|
explore_recursive_async(
|
|
|
|
config,
|
|
|
|
grand_parent.into(),
|
|
|
|
parent.file_name().map(|p| p.into()),
|
|
|
|
0,
|
|
|
|
tx_msg_in,
|
|
|
|
);
|
2021-03-31 11:50:37 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-24 05:05:51 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_explore_sync() {
|
|
|
|
let config = ExplorerConfig::default();
|
2021-12-11 16:26:52 +00:00
|
|
|
let path = PathBuf::from(".");
|
2021-10-24 05:05:51 +00:00
|
|
|
|
|
|
|
let r = explore_sync(config, path, None, 0);
|
|
|
|
|
|
|
|
assert!(r.is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_failed_explore_sync() {
|
|
|
|
let config = ExplorerConfig::default();
|
|
|
|
let path = PathBuf::from("/there/is/no/path");
|
|
|
|
|
|
|
|
let r = explore_sync(config, path, None, 0);
|
|
|
|
|
|
|
|
assert!(r.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_dirbuf_from_msg(msg: MsgIn) -> DirectoryBuffer {
|
|
|
|
assert!(matches!(msg, MsgIn::Internal(_)));
|
|
|
|
|
|
|
|
let msgin = match msg {
|
|
|
|
MsgIn::Internal(m) => m,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
assert!(matches!(msgin, InternalMsg::SetDirectory(_)));
|
|
|
|
|
|
|
|
match msgin {
|
|
|
|
InternalMsg::SetDirectory(dbuf) => dbuf,
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use std::sync::mpsc;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_explore_async() {
|
|
|
|
let config = ExplorerConfig::default();
|
2021-12-11 16:26:52 +00:00
|
|
|
let path = PathBuf::from(".");
|
2021-10-24 05:05:51 +00:00
|
|
|
let (tx_msg_in, rx_msg_in) = mpsc::channel();
|
|
|
|
|
|
|
|
explore_async(config, path, None, 0, tx_msg_in.clone());
|
|
|
|
|
|
|
|
let task = rx_msg_in.recv().unwrap();
|
|
|
|
let dbuf = extract_dirbuf_from_msg(task.msg);
|
|
|
|
|
2021-12-11 16:26:52 +00:00
|
|
|
assert_eq!(dbuf.parent, ".");
|
2021-10-24 05:05:51 +00:00
|
|
|
|
|
|
|
drop(tx_msg_in);
|
|
|
|
assert!(rx_msg_in.recv().is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
//XXX: explore_recursive_async() generates messages with random order.
|
|
|
|
// Discussing on GitHub (https://github.com/sayanarijit/xplr/issues/372)
|
|
|
|
//#[test]
|
|
|
|
//fn test_explore_recursive_async() {
|
|
|
|
// let config = ExplorerConfig::default();
|
|
|
|
// let path = PathBuf::from("/usr/bin");
|
|
|
|
// let (tx_msg_in, rx_msg_in) = mpsc::channel();
|
|
|
|
|
|
|
|
// explore_recursive_async(config, path, None, 0, tx_msg_in.clone());
|
|
|
|
|
|
|
|
// let mut task = rx_msg_in.recv().unwrap();
|
|
|
|
// let mut dbuf = extract_dirbuf_from_msg(task.msg);
|
|
|
|
|
|
|
|
// assert_eq!(dbuf.parent, "/");
|
|
|
|
|
|
|
|
// task = rx_msg_in.recv().unwrap();
|
|
|
|
// dbuf = extract_dirbuf_from_msg(task.msg);
|
|
|
|
|
|
|
|
// assert_eq!(dbuf.parent, "/usr");
|
|
|
|
|
|
|
|
// task = rx_msg_in.recv().unwrap();
|
|
|
|
// dbuf = extract_dirbuf_from_msg(task.msg);
|
|
|
|
|
|
|
|
// assert_eq!(dbuf.parent, "/usr/bin");
|
|
|
|
|
|
|
|
// drop(tx_msg_in);
|
|
|
|
// assert!(rx_msg_in.recv().is_err());
|
|
|
|
//}
|
|
|
|
}
|