summaryrefslogtreecommitdiffstats
path: root/src/context.rs
diff options
context:
space:
mode:
authorZhenhui Xie <xiezh0831@yahoo.co.jp>2019-10-28 21:41:16 +0800
committerMatan Kushner <hello@matchai.me>2019-10-28 22:41:16 +0900
commitfed1341e22967d04a8ce26b386be5aade741d926 (patch)
tree08760c93c951b629f594faedb8f549090edbabf2 /src/context.rs
parent7f9726eb1586c142e463d8f49fb5f26ae40ef522 (diff)
feat: Add an option to limit the duration of starship directory scanning (#589)
Diffstat (limited to 'src/context.rs')
-rw-r--r--src/context.rs11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/context.rs b/src/context.rs
index f1ab29977..b306a78d2 100644
--- a/src/context.rs
+++ b/src/context.rs
@@ -10,6 +10,7 @@ use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};
use std::string::String;
+use std::time::{Duration, SystemTime};
/// Context contains data or common methods that may be used by multiple modules.
/// The data contained within Context will be relevant to this particular rendering
@@ -137,13 +138,23 @@ impl<'a> Context<'a> {
}
pub fn get_dir_files(&self) -> Result<&Vec<PathBuf>, std::io::Error> {
+ let start_time = SystemTime::now();
+ let scan_timeout = Duration::from_millis(self.config.get_root_config().scan_timeout);
+
self.dir_files
.get_or_try_init(|| -> Result<Vec<PathBuf>, std::io::Error> {
let dir_files = fs::read_dir(&self.current_dir)?
+ .take_while(|_item| {
+ SystemTime::now().duration_since(start_time).unwrap() < scan_timeout
+ })
.filter_map(Result::ok)
.map(|entry| entry.path())
.collect::<Vec<PathBuf>>();
+ log::trace!(
+ "Building a vector of directory files took {:?}",
+ SystemTime::now().duration_since(start_time).unwrap()
+ );
Ok(dir_files)
})
}