2019-09-30 14:10:35 +02:00
|
|
|
use crate::config::StarshipConfig;
|
2019-06-10 16:56:17 +02:00
|
|
|
use crate::module::Module;
|
|
|
|
|
2019-04-19 22:57:14 +02:00
|
|
|
use clap::ArgMatches;
|
2019-09-10 01:14:38 +02:00
|
|
|
use git2::{Repository, RepositoryState};
|
|
|
|
use once_cell::sync::OnceCell;
|
2019-04-19 22:57:14 +02:00
|
|
|
use std::env;
|
2019-05-12 19:37:23 +02:00
|
|
|
use std::ffi::OsStr;
|
2019-04-23 20:51:08 +02:00
|
|
|
use std::fs;
|
2019-09-10 01:14:38 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-04-19 22:57:14 +02:00
|
|
|
|
2019-07-16 00:18:19 +02:00
|
|
|
/// 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
|
|
|
|
/// of the prompt.
|
2019-04-19 22:57:14 +02:00
|
|
|
pub struct Context<'a> {
|
2019-07-16 00:18:19 +02:00
|
|
|
/// The deserialized configuration map from the user's `starship.toml` file.
|
2019-09-30 14:10:35 +02:00
|
|
|
pub config: StarshipConfig,
|
2019-07-16 00:18:19 +02:00
|
|
|
|
|
|
|
/// The current working directory that starship is being called in.
|
2019-04-19 22:57:14 +02:00
|
|
|
pub current_dir: PathBuf,
|
2019-07-16 00:18:19 +02:00
|
|
|
|
|
|
|
/// A vector containing the full paths of all the files in `current_dir`.
|
2019-09-14 16:23:53 +02:00
|
|
|
dir_files: OnceCell<Vec<PathBuf>>,
|
2019-07-16 00:18:19 +02:00
|
|
|
|
|
|
|
/// The map of arguments that were passed when starship was called.
|
2019-04-19 22:57:14 +02:00
|
|
|
pub arguments: ArgMatches<'a>,
|
2019-07-16 00:18:19 +02:00
|
|
|
|
2019-09-10 01:14:38 +02:00
|
|
|
/// Private field to store Git information for modules who need it
|
|
|
|
repo: OnceCell<Repo>,
|
2019-04-19 22:57:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Context<'a> {
|
2019-07-16 00:18:19 +02:00
|
|
|
/// Identify the current working directory and create an instance of Context
|
|
|
|
/// for it.
|
2019-04-19 22:57:14 +02:00
|
|
|
pub fn new(arguments: ArgMatches) -> Context {
|
2019-08-18 05:50:42 +02:00
|
|
|
// Retrieve the "path" flag. If unavailable, use the current directory instead.
|
2019-06-06 14:18:00 +02:00
|
|
|
let path = arguments
|
|
|
|
.value_of("path")
|
|
|
|
.map(From::from)
|
|
|
|
.unwrap_or_else(|| env::current_dir().expect("Unable to identify current directory."));
|
|
|
|
|
|
|
|
Context::new_with_dir(arguments, path)
|
2019-04-19 22:57:14 +02:00
|
|
|
}
|
|
|
|
|
2019-07-16 00:18:19 +02:00
|
|
|
/// Create a new instance of Context for the provided directory
|
2019-04-19 22:57:14 +02:00
|
|
|
pub fn new_with_dir<T>(arguments: ArgMatches, dir: T) -> Context
|
|
|
|
where
|
|
|
|
T: Into<PathBuf>,
|
|
|
|
{
|
2019-09-30 14:10:35 +02:00
|
|
|
let config = StarshipConfig::initialize();
|
2019-06-10 16:56:17 +02:00
|
|
|
|
2019-04-23 20:51:08 +02:00
|
|
|
// TODO: Currently gets the physical directory. Get the logical directory.
|
|
|
|
let current_dir = Context::expand_tilde(dir.into());
|
|
|
|
|
2019-04-19 22:57:14 +02:00
|
|
|
Context {
|
2019-06-10 16:56:17 +02:00
|
|
|
config,
|
2019-04-19 22:57:14 +02:00
|
|
|
arguments,
|
2019-04-27 04:07:07 +02:00
|
|
|
current_dir,
|
2019-09-14 16:23:53 +02:00
|
|
|
dir_files: OnceCell::new(),
|
2019-09-10 01:14:38 +02:00
|
|
|
repo: OnceCell::new(),
|
2019-04-23 20:51:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert a `~` in a path to the home directory
|
|
|
|
fn expand_tilde(dir: PathBuf) -> PathBuf {
|
|
|
|
if dir.starts_with("~") {
|
|
|
|
let without_home = dir.strip_prefix("~").unwrap();
|
|
|
|
return dirs::home_dir().unwrap().join(without_home);
|
2019-04-19 22:57:14 +02:00
|
|
|
}
|
2019-04-23 20:51:08 +02:00
|
|
|
dir
|
2019-04-19 22:57:14 +02:00
|
|
|
}
|
2019-05-12 19:37:23 +02:00
|
|
|
|
2019-07-02 22:12:53 +02:00
|
|
|
/// Create a new module
|
2019-09-10 01:14:38 +02:00
|
|
|
pub fn new_module(&self, name: &str) -> Module {
|
|
|
|
let config = self.config.get_module_config(name);
|
|
|
|
|
|
|
|
Module::new(name, config)
|
|
|
|
}
|
|
|
|
|
2019-10-05 16:10:16 +02:00
|
|
|
/// Check if `disabled` option of the module is true in configuration file.
|
|
|
|
pub fn is_module_disabled_in_config(&self, name: &str) -> bool {
|
2019-07-02 22:12:53 +02:00
|
|
|
let config = self.config.get_module_config(name);
|
|
|
|
|
|
|
|
// If the segment has "disabled" set to "true", don't show it
|
2019-09-30 14:10:35 +02:00
|
|
|
let disabled = config.and_then(|table| table.as_table()?.get("disabled")?.as_bool());
|
2019-07-02 22:12:53 +02:00
|
|
|
|
2019-10-05 16:10:16 +02:00
|
|
|
disabled == Some(true)
|
2019-06-10 16:56:17 +02:00
|
|
|
}
|
|
|
|
|
2019-05-12 19:37:23 +02:00
|
|
|
// returns a new ScanDir struct with reference to current dir_files of context
|
|
|
|
// see ScanDir for methods
|
2019-09-14 16:23:53 +02:00
|
|
|
pub fn try_begin_scan(&'a self) -> Option<ScanDir<'a>> {
|
|
|
|
Some(ScanDir {
|
|
|
|
dir_files: self.get_dir_files().ok()?,
|
2019-05-12 19:37:23 +02:00
|
|
|
files: &[],
|
|
|
|
folders: &[],
|
|
|
|
extensions: &[],
|
2019-09-14 16:23:53 +02:00
|
|
|
})
|
2019-05-12 19:37:23 +02:00
|
|
|
}
|
2019-09-10 01:14:38 +02:00
|
|
|
|
|
|
|
/// Will lazily get repo root and branch when a module requests it.
|
|
|
|
pub fn get_repo(&self) -> Result<&Repo, std::io::Error> {
|
2019-09-14 16:23:53 +02:00
|
|
|
self.repo
|
2019-09-10 01:14:38 +02:00
|
|
|
.get_or_try_init(|| -> Result<Repo, std::io::Error> {
|
|
|
|
let repository = Repository::discover(&self.current_dir).ok();
|
|
|
|
let branch = repository
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|repo| get_current_branch(repo));
|
|
|
|
let root = repository
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|repo| repo.workdir().map(Path::to_path_buf));
|
|
|
|
let state = repository.as_ref().map(|repo| repo.state());
|
|
|
|
|
|
|
|
Ok(Repo {
|
|
|
|
branch,
|
|
|
|
root,
|
|
|
|
state,
|
|
|
|
})
|
2019-09-14 16:23:53 +02:00
|
|
|
})
|
|
|
|
}
|
2019-09-10 01:14:38 +02:00
|
|
|
|
2019-09-14 16:23:53 +02:00
|
|
|
pub fn get_dir_files(&self) -> Result<&Vec<PathBuf>, std::io::Error> {
|
|
|
|
self.dir_files
|
|
|
|
.get_or_try_init(|| -> Result<Vec<PathBuf>, std::io::Error> {
|
|
|
|
let dir_files = fs::read_dir(&self.current_dir)?
|
|
|
|
.filter_map(Result::ok)
|
|
|
|
.map(|entry| entry.path())
|
|
|
|
.collect::<Vec<PathBuf>>();
|
|
|
|
|
|
|
|
Ok(dir_files)
|
|
|
|
})
|
2019-09-10 01:14:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Repo {
|
|
|
|
/// If `current_dir` is a git repository or is contained within one,
|
|
|
|
/// this is the current branch name of that repo.
|
|
|
|
pub branch: Option<String>,
|
|
|
|
|
|
|
|
/// If `current_dir` is a git repository or is contained within one,
|
|
|
|
/// this is the path to the root of that repo.
|
|
|
|
pub root: Option<PathBuf>,
|
|
|
|
|
|
|
|
/// State
|
|
|
|
pub state: Option<RepositoryState>,
|
2019-05-12 19:37:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// A struct of Criteria which will be used to verify current PathBuf is
|
|
|
|
// of X language, criteria can be set via the builder pattern
|
|
|
|
pub struct ScanDir<'a> {
|
2019-09-14 16:23:53 +02:00
|
|
|
dir_files: &'a Vec<PathBuf>,
|
2019-05-12 19:37:23 +02:00
|
|
|
files: &'a [&'a str],
|
|
|
|
folders: &'a [&'a str],
|
|
|
|
extensions: &'a [&'a str],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ScanDir<'a> {
|
2019-08-01 01:48:51 +02:00
|
|
|
pub const fn set_files(mut self, files: &'a [&'a str]) -> Self {
|
2019-05-12 19:37:23 +02:00
|
|
|
self.files = files;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-08-01 01:48:51 +02:00
|
|
|
pub const fn set_extensions(mut self, extensions: &'a [&'a str]) -> Self {
|
2019-05-12 19:37:23 +02:00
|
|
|
self.extensions = extensions;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-08-01 01:48:51 +02:00
|
|
|
pub const fn set_folders(mut self, folders: &'a [&'a str]) -> Self {
|
2019-05-12 19:37:23 +02:00
|
|
|
self.folders = folders;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// based on the current Pathbuf check to see
|
|
|
|
/// if any of this criteria match or exist and returning a boolean
|
2019-09-14 16:23:53 +02:00
|
|
|
pub fn is_match(&self) -> bool {
|
2019-05-12 19:37:23 +02:00
|
|
|
self.dir_files.iter().any(|path| {
|
2019-05-22 18:04:51 +02:00
|
|
|
if path.is_dir() {
|
2019-08-01 01:48:51 +02:00
|
|
|
path_has_name(path, self.folders)
|
2019-05-22 18:04:51 +02:00
|
|
|
} else {
|
2019-08-01 01:48:51 +02:00
|
|
|
path_has_name(path, self.files) || has_extension(path, self.extensions)
|
2019-05-22 18:04:51 +02:00
|
|
|
}
|
2019-05-12 19:37:23 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// checks to see if the pathbuf matches a file or folder name
|
|
|
|
pub fn path_has_name<'a>(dir_entry: &PathBuf, names: &'a [&'a str]) -> bool {
|
2019-07-14 23:54:45 +02:00
|
|
|
let found_file_or_folder_name = names.iter().find(|file_or_folder_name| {
|
2019-05-12 19:37:23 +02:00
|
|
|
dir_entry
|
|
|
|
.file_name()
|
|
|
|
.and_then(OsStr::to_str)
|
|
|
|
.unwrap_or_default()
|
|
|
|
== **file_or_folder_name
|
|
|
|
});
|
|
|
|
|
|
|
|
match found_file_or_folder_name {
|
|
|
|
Some(name) => !name.is_empty(),
|
|
|
|
None => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-07 17:05:25 +02:00
|
|
|
/// checks if pathbuf doesn't start with a dot and matches any provided extension
|
2019-05-12 19:37:23 +02:00
|
|
|
pub fn has_extension<'a>(dir_entry: &PathBuf, extensions: &'a [&'a str]) -> bool {
|
2019-09-07 17:05:25 +02:00
|
|
|
if let Some(file_name) = dir_entry.file_name() {
|
|
|
|
if file_name.to_string_lossy().starts_with('.') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return extensions.iter().any(|ext| {
|
|
|
|
dir_entry
|
|
|
|
.extension()
|
|
|
|
.and_then(OsStr::to_str)
|
|
|
|
.map_or(false, |e| e == *ext)
|
|
|
|
});
|
2019-05-12 19:37:23 +02:00
|
|
|
}
|
2019-09-07 17:05:25 +02:00
|
|
|
false
|
2019-05-12 19:37:23 +02:00
|
|
|
}
|
|
|
|
|
2019-05-22 18:04:51 +02:00
|
|
|
fn get_current_branch(repository: &Repository) -> Option<String> {
|
|
|
|
let head = repository.head().ok()?;
|
|
|
|
let shorthand = head.shorthand();
|
|
|
|
|
2019-08-01 01:48:51 +02:00
|
|
|
shorthand.map(std::string::ToString::to_string)
|
2019-05-22 18:04:51 +02:00
|
|
|
}
|
|
|
|
|
2019-05-12 19:37:23 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_path_has_name() {
|
|
|
|
let mut buf = PathBuf::from("/");
|
|
|
|
let files = vec!["package.json"];
|
|
|
|
|
|
|
|
assert_eq!(path_has_name(&buf, &files), false);
|
|
|
|
|
|
|
|
buf.set_file_name("some-file.js");
|
|
|
|
assert_eq!(path_has_name(&buf, &files), false);
|
|
|
|
|
|
|
|
buf.set_file_name("package.json");
|
|
|
|
assert_eq!(path_has_name(&buf, &files), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_has_extension() {
|
|
|
|
let mut buf = PathBuf::from("/");
|
|
|
|
let extensions = vec!["js"];
|
|
|
|
|
|
|
|
assert_eq!(has_extension(&buf, &extensions), false);
|
|
|
|
|
|
|
|
buf.set_file_name("some-file.rs");
|
|
|
|
assert_eq!(has_extension(&buf, &extensions), false);
|
|
|
|
|
2019-09-07 17:05:25 +02:00
|
|
|
buf.set_file_name(".some-file.js");
|
|
|
|
assert_eq!(has_extension(&buf, &extensions), false);
|
|
|
|
|
2019-05-12 19:37:23 +02:00
|
|
|
buf.set_file_name("some-file.js");
|
|
|
|
assert_eq!(has_extension(&buf, &extensions), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-05-22 18:04:51 +02:00
|
|
|
fn test_criteria_scan_fails() {
|
2019-09-14 16:23:53 +02:00
|
|
|
let failing_criteria = ScanDir {
|
2019-05-12 19:37:23 +02:00
|
|
|
dir_files: &vec![PathBuf::new()],
|
|
|
|
files: &["package.json"],
|
|
|
|
extensions: &["js"],
|
|
|
|
folders: &["node_modules"],
|
|
|
|
};
|
|
|
|
|
|
|
|
// fails if buffer does not match any criteria
|
2019-09-14 16:23:53 +02:00
|
|
|
assert_eq!(failing_criteria.is_match(), false);
|
2019-05-12 19:37:23 +02:00
|
|
|
|
2019-09-14 16:23:53 +02:00
|
|
|
let failing_dir_criteria = ScanDir {
|
2019-05-22 18:04:51 +02:00
|
|
|
dir_files: &vec![PathBuf::from("/package.js/dog.go")],
|
|
|
|
files: &["package.json"],
|
|
|
|
extensions: &["js"],
|
|
|
|
folders: &["node_modules"],
|
|
|
|
};
|
|
|
|
|
|
|
|
// fails when passed a pathbuf dir matches extension path
|
2019-09-14 16:23:53 +02:00
|
|
|
assert_eq!(failing_dir_criteria.is_match(), false);
|
2019-05-22 18:04:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_criteria_scan_passes() {
|
2019-09-14 16:23:53 +02:00
|
|
|
let passing_criteria = ScanDir {
|
2019-05-12 19:37:23 +02:00
|
|
|
dir_files: &vec![PathBuf::from("package.json")],
|
|
|
|
files: &["package.json"],
|
|
|
|
extensions: &["js"],
|
|
|
|
folders: &["node_modules"],
|
|
|
|
};
|
|
|
|
|
2019-09-14 16:23:53 +02:00
|
|
|
assert_eq!(passing_criteria.is_match(), true);
|
2019-05-12 19:37:23 +02:00
|
|
|
}
|
2019-04-19 22:57:14 +02:00
|
|
|
}
|