1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-27 03:42:39 +00:00

feat: Add an option to limit the duration of starship directory scanning (#589)

This commit is contained in:
Zhenhui Xie 2019-10-28 21:41:16 +08:00 committed by Matan Kushner
parent 7f9726eb15
commit fed1341e22
4 changed files with 33 additions and 0 deletions

View File

@ -65,6 +65,7 @@ This is the list of prompt-wide configuration options.
| -------------- | ----------------------------- | ------------------------------------------------------ | | -------------- | ----------------------------- | ------------------------------------------------------ |
| `add_newline` | `true` | Add a new line before the start of the prompt. | | `add_newline` | `true` | Add a new line before the start of the prompt. |
| `prompt_order` | [link](#default-prompt-order) | Configure the order in which the prompt module occurs. | | `prompt_order` | [link](#default-prompt-order) | Configure the order in which the prompt module occurs. |
| `scan_timeout` | `30` | Timeout for starship to scan files (in milliseconds). |
### Example ### Example
@ -75,6 +76,8 @@ This is the list of prompt-wide configuration options.
add_newline = false add_newline = false
# Overwrite a default_prompt_order and use custom prompt_order # Overwrite a default_prompt_order and use custom prompt_order
prompt_order=["rust","line_break","package","line_break","character"] prompt_order=["rust","line_break","package","line_break","character"]
# Wait 10 milliseconds for starship to check files under the current directory.
scan_timeout = 10
``` ```
### Default Prompt Order ### Default Prompt Order

View File

@ -75,6 +75,23 @@ impl<'a> ModuleConfig<'a> for i64 {
} }
} }
impl<'a> ModuleConfig<'a> for u64 {
fn from_config(config: &Value) -> Option<Self> {
match config {
Value::Integer(value) => {
// Converting i64 to u64
if *value > 0 {
Some(*value as u64)
} else {
None
}
}
Value::String(value) => value.parse::<u64>().ok(),
_ => None,
}
}
}
impl<'a> ModuleConfig<'a> for f64 { impl<'a> ModuleConfig<'a> for f64 {
fn from_config(config: &Value) -> Option<Self> { fn from_config(config: &Value) -> Option<Self> {
config.as_float() config.as_float()

View File

@ -6,6 +6,7 @@ use starship_module_config_derive::ModuleConfig;
pub struct StarshipRootConfig<'a> { pub struct StarshipRootConfig<'a> {
pub add_newline: bool, pub add_newline: bool,
pub prompt_order: Vec<&'a str>, pub prompt_order: Vec<&'a str>,
pub scan_timeout: u64,
} }
impl<'a> RootModuleConfig<'a> for StarshipRootConfig<'a> { impl<'a> RootModuleConfig<'a> for StarshipRootConfig<'a> {
@ -47,6 +48,7 @@ impl<'a> RootModuleConfig<'a> for StarshipRootConfig<'a> {
"time", "time",
"character", "character",
], ],
scan_timeout: 30,
} }
} }
} }

View File

@ -10,6 +10,7 @@ use std::ffi::OsStr;
use std::fs; use std::fs;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::string::String; use std::string::String;
use std::time::{Duration, SystemTime};
/// Context contains data or common methods that may be used by multiple modules. /// 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 /// 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> { 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 self.dir_files
.get_or_try_init(|| -> Result<Vec<PathBuf>, std::io::Error> { .get_or_try_init(|| -> Result<Vec<PathBuf>, std::io::Error> {
let dir_files = fs::read_dir(&self.current_dir)? 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) .filter_map(Result::ok)
.map(|entry| entry.path()) .map(|entry| entry.path())
.collect::<Vec<PathBuf>>(); .collect::<Vec<PathBuf>>();
log::trace!(
"Building a vector of directory files took {:?}",
SystemTime::now().duration_since(start_time).unwrap()
);
Ok(dir_files) Ok(dir_files)
}) })
} }