2020-07-13 21:55:42 +00:00
|
|
|
#[cfg(not(target_os = "windows"))]
|
|
|
|
use super::utils::directory_nix as directory_utils;
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
use super::utils::directory_win as directory_utils;
|
2019-07-29 00:15:40 +00:00
|
|
|
use path_slash::PathExt;
|
2020-05-31 17:32:35 +00:00
|
|
|
use std::collections::HashMap;
|
2020-06-18 03:44:51 +00:00
|
|
|
use std::iter::FromIterator;
|
2020-04-30 08:40:56 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-11-29 05:02:22 +00:00
|
|
|
use unicode_segmentation::UnicodeSegmentation;
|
2019-04-04 18:18:15 +00:00
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
use super::{Context, Module};
|
2019-04-19 20:57:14 +00:00
|
|
|
|
2019-12-09 17:59:02 +00:00
|
|
|
use super::utils::directory::truncate;
|
2020-07-07 22:45:32 +00:00
|
|
|
use crate::config::RootModuleConfig;
|
2019-10-15 11:34:48 +00:00
|
|
|
use crate::configs::directory::DirectoryConfig;
|
2020-07-07 22:45:32 +00:00
|
|
|
use crate::formatter::StringFormatter;
|
2019-10-15 11:34:48 +00:00
|
|
|
|
2019-07-19 20:18:52 +00:00
|
|
|
/// Creates a module with the current directory
|
2019-04-11 23:31:30 +00:00
|
|
|
///
|
2020-05-31 17:32:35 +00:00
|
|
|
/// Will perform path contraction, substitution, and truncation.
|
2019-04-09 03:35:14 +00:00
|
|
|
/// **Contraction**
|
2019-09-29 03:50:38 +00:00
|
|
|
/// - Paths beginning with the home directory or with a git repo right
|
|
|
|
/// inside the home directory will be contracted to `~`
|
2019-04-09 03:35:14 +00:00
|
|
|
/// - Paths containing a git repo will contract to begin at the repo root
|
2019-04-11 23:31:30 +00:00
|
|
|
///
|
2020-05-31 17:32:35 +00:00
|
|
|
/// **Substitution**
|
|
|
|
/// Paths will undergo user-provided substitutions of substrings
|
|
|
|
///
|
2019-04-09 03:35:14 +00:00
|
|
|
/// **Truncation**
|
|
|
|
/// Paths will be limited in length to `3` path components by default.
|
2019-07-02 20:12:53 +00:00
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2019-04-08 19:32:59 +00:00
|
|
|
const HOME_SYMBOL: &str = "~";
|
2019-05-01 20:34:24 +00:00
|
|
|
|
2019-09-09 23:14:38 +00:00
|
|
|
let mut module = context.new_module("directory");
|
2019-10-15 11:34:48 +00:00
|
|
|
let config: DirectoryConfig = DirectoryConfig::try_load(module.config);
|
|
|
|
|
2019-09-20 16:28:09 +00:00
|
|
|
// Using environment PWD is the standard approach for determining logical path
|
|
|
|
// If this is None for any reason, we fall back to reading the os-provided path
|
2019-10-24 10:37:44 +00:00
|
|
|
let physical_current_dir = if config.use_logical_path {
|
2020-04-30 08:40:56 +00:00
|
|
|
match std::env::var("PWD") {
|
|
|
|
Ok(x) => Some(PathBuf::from(x)),
|
|
|
|
Err(e) => {
|
|
|
|
log::debug!("Error getting PWD environment variable: {}", e);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2019-10-24 10:37:44 +00:00
|
|
|
} else {
|
|
|
|
match std::env::current_dir() {
|
2019-09-20 16:28:09 +00:00
|
|
|
Ok(x) => Some(x),
|
2019-10-24 10:37:44 +00:00
|
|
|
Err(e) => {
|
|
|
|
log::debug!("Error getting physical current directory: {}", e);
|
2019-09-20 16:28:09 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-10-24 10:37:44 +00:00
|
|
|
let current_dir = Path::new(
|
|
|
|
physical_current_dir
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_or_else(|| &context.current_dir),
|
|
|
|
);
|
2019-09-20 16:28:09 +00:00
|
|
|
|
2020-06-20 17:59:35 +00:00
|
|
|
let home_dir = dirs_next::home_dir().unwrap();
|
2019-05-16 15:40:30 +00:00
|
|
|
log::debug!("Current directory: {:?}", current_dir);
|
2019-04-04 18:18:15 +00:00
|
|
|
|
2019-09-09 23:14:38 +00:00
|
|
|
let repo = &context.get_repo().ok()?;
|
|
|
|
|
|
|
|
let dir_string = match &repo.root {
|
2019-10-15 11:34:48 +00:00
|
|
|
Some(repo_root) if config.truncate_to_repo && (repo_root != &home_dir) => {
|
2020-06-18 03:44:51 +00:00
|
|
|
log::debug!("Repo root: {:?}", repo_root);
|
2019-08-27 01:52:45 +00:00
|
|
|
// Contract the path to the git repo root
|
2020-06-18 03:44:51 +00:00
|
|
|
contract_repo_path(current_dir, repo_root)
|
|
|
|
.unwrap_or_else(|| contract_path(current_dir, &home_dir, HOME_SYMBOL))
|
2019-08-17 03:29:22 +00:00
|
|
|
}
|
2019-08-27 01:52:45 +00:00
|
|
|
// Contract the path to the home directory
|
|
|
|
_ => contract_path(current_dir, &home_dir, HOME_SYMBOL),
|
2019-08-17 03:29:22 +00:00
|
|
|
};
|
2020-06-18 03:44:51 +00:00
|
|
|
log::debug!("Dir string: {}", dir_string);
|
2019-04-04 18:18:15 +00:00
|
|
|
|
2020-05-31 17:32:35 +00:00
|
|
|
let substituted_dir = substitute_path(dir_string, &config.substitutions);
|
|
|
|
|
2019-04-09 03:35:14 +00:00
|
|
|
// Truncate the dir string to the maximum number of path components
|
2020-05-31 17:32:35 +00:00
|
|
|
let truncated_dir_string = truncate(substituted_dir, config.truncation_length as usize);
|
2019-08-27 01:52:45 +00:00
|
|
|
|
2020-05-31 17:32:35 +00:00
|
|
|
// Substitutions could have changed the prefix, so don't allow them and
|
|
|
|
// fish-style path contraction together
|
2020-07-07 22:45:32 +00:00
|
|
|
let fish_prefix = if config.fish_style_pwd_dir_length > 0 && config.substitutions.is_empty() {
|
2019-08-27 01:52:45 +00:00
|
|
|
// If user is using fish style path, we need to add the segment first
|
2019-09-20 16:28:09 +00:00
|
|
|
let contracted_home_dir = contract_path(¤t_dir, &home_dir, HOME_SYMBOL);
|
2020-07-07 22:45:32 +00:00
|
|
|
to_fish_style(
|
2019-10-15 11:34:48 +00:00
|
|
|
config.fish_style_pwd_dir_length as usize,
|
2019-08-27 01:52:45 +00:00
|
|
|
contracted_home_dir,
|
|
|
|
&truncated_dir_string,
|
2020-07-07 22:45:32 +00:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
String::from("")
|
|
|
|
};
|
|
|
|
let final_dir_string = format!("{}{}", fish_prefix, truncated_dir_string);
|
2020-07-13 21:55:42 +00:00
|
|
|
let lock_symbol = String::from(config.read_only_symbol);
|
2020-07-07 22:45:32 +00:00
|
|
|
|
|
|
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
|
|
|
formatter
|
|
|
|
.map_style(|variable| match variable {
|
|
|
|
"style" => Some(Ok(config.style)),
|
2020-07-13 21:55:42 +00:00
|
|
|
"read_only_style" => Some(Ok(config.read_only_symbol_style)),
|
2020-07-07 22:45:32 +00:00
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map(|variable| match variable {
|
|
|
|
"path" => Some(Ok(&final_dir_string)),
|
2020-07-13 21:55:42 +00:00
|
|
|
"read_only" => {
|
|
|
|
if is_readonly_dir(current_dir.to_str()?) {
|
|
|
|
Some(Ok(&lock_symbol))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2020-07-07 22:45:32 +00:00
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.parse(None)
|
|
|
|
});
|
|
|
|
|
|
|
|
module.set_segments(match parsed {
|
|
|
|
Ok(segments) => segments,
|
|
|
|
Err(error) => {
|
|
|
|
log::warn!("Error in module `directory`:\n{}", error);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
});
|
2019-05-20 02:26:12 +00:00
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
Some(module)
|
2019-04-04 18:18:15 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 21:55:42 +00:00
|
|
|
fn is_readonly_dir(path: &str) -> bool {
|
|
|
|
match directory_utils::is_write_allowed(path) {
|
|
|
|
Ok(res) => !res,
|
|
|
|
Err(e) => {
|
|
|
|
log::debug!(
|
|
|
|
"Failed to detemine read only status of directory '{}': {}",
|
|
|
|
path,
|
|
|
|
e
|
|
|
|
);
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-09 03:35:14 +00:00
|
|
|
/// Contract the root component of a path
|
2019-04-15 18:38:45 +00:00
|
|
|
///
|
2019-04-15 16:04:53 +00:00
|
|
|
/// Replaces the `top_level_path` in a given `full_path` with the provided
|
|
|
|
/// `top_level_replacement`.
|
2019-04-12 23:11:40 +00:00
|
|
|
fn contract_path(full_path: &Path, top_level_path: &Path, top_level_replacement: &str) -> String {
|
2019-04-09 03:35:14 +00:00
|
|
|
if !full_path.starts_with(top_level_path) {
|
2020-04-30 08:40:56 +00:00
|
|
|
return full_path.to_slash().unwrap();
|
2019-04-09 03:35:14 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 19:32:59 +00:00
|
|
|
if full_path == top_level_path {
|
2020-04-30 08:40:56 +00:00
|
|
|
return top_level_replacement.to_string();
|
2019-04-07 21:12:22 +00:00
|
|
|
}
|
2019-04-07 20:43:11 +00:00
|
|
|
|
2019-04-08 19:32:59 +00:00
|
|
|
format!(
|
2019-04-08 21:35:38 +00:00
|
|
|
"{replacement}{separator}{path}",
|
|
|
|
replacement = top_level_replacement,
|
2019-05-16 15:40:30 +00:00
|
|
|
separator = "/",
|
2020-04-30 08:40:56 +00:00
|
|
|
path = full_path
|
|
|
|
.strip_prefix(top_level_path)
|
|
|
|
.unwrap()
|
|
|
|
.to_slash()
|
|
|
|
.unwrap()
|
2019-04-08 19:32:59 +00:00
|
|
|
)
|
2019-04-07 21:12:22 +00:00
|
|
|
}
|
2019-04-07 20:43:11 +00:00
|
|
|
|
2020-06-18 03:44:51 +00:00
|
|
|
/// Contract the root component of a path based on the real path
|
|
|
|
///
|
|
|
|
/// Replaces the `top_level_path` in a given `full_path` with the provided
|
|
|
|
/// `top_level_replacement` by walking ancestors and comparing its real path.
|
|
|
|
fn contract_repo_path(full_path: &Path, top_level_path: &Path) -> Option<String> {
|
|
|
|
let top_level_real_path = real_path(top_level_path);
|
|
|
|
// Walk ancestors to preserve logical path in `full_path`.
|
|
|
|
// If we'd just `full_real_path.strip_prefix(top_level_real_path)`,
|
|
|
|
// then it wouldn't preserve logical path. It would've returned physical path.
|
|
|
|
for (i, ancestor) in full_path.ancestors().enumerate() {
|
|
|
|
let ancestor_real_path = real_path(ancestor);
|
|
|
|
if ancestor_real_path != top_level_real_path {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let components: Vec<_> = full_path.components().collect();
|
|
|
|
let repo_name = components[components.len() - i - 1].as_os_str().to_str()?;
|
|
|
|
|
|
|
|
if i == 0 {
|
|
|
|
return Some(repo_name.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
let path = PathBuf::from_iter(&components[components.len() - i..]);
|
|
|
|
return Some(format!(
|
|
|
|
"{repo_name}{separator}{path}",
|
|
|
|
repo_name = repo_name,
|
|
|
|
separator = "/",
|
|
|
|
path = path.to_slash()?
|
|
|
|
));
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn real_path<P: AsRef<Path>>(path: P) -> PathBuf {
|
|
|
|
let path = path.as_ref();
|
|
|
|
let mut buf = PathBuf::new();
|
|
|
|
for component in path.components() {
|
|
|
|
let next = buf.join(component);
|
|
|
|
if let Ok(realpath) = next.read_link() {
|
|
|
|
if realpath.is_absolute() {
|
|
|
|
buf = realpath;
|
|
|
|
} else {
|
|
|
|
buf.push(realpath);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buf = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf.canonicalize().unwrap_or_else(|_| path.into())
|
|
|
|
}
|
|
|
|
|
2020-05-31 17:32:35 +00:00
|
|
|
/// Perform a list of string substitutions on the path
|
|
|
|
///
|
|
|
|
/// Given a list of (from, to) pairs, this will perform the string
|
|
|
|
/// substitutions, in order, on the path. Any non-pair of strings is ignored.
|
|
|
|
fn substitute_path(dir_string: String, substitutions: &HashMap<String, &str>) -> String {
|
|
|
|
let mut substituted_dir = dir_string;
|
|
|
|
for substitution_pair in substitutions.iter() {
|
|
|
|
substituted_dir = substituted_dir.replace(substitution_pair.0, substitution_pair.1);
|
|
|
|
}
|
|
|
|
substituted_dir
|
|
|
|
}
|
|
|
|
|
2019-08-27 01:52:45 +00:00
|
|
|
/// Takes part before contracted path and replaces it with fish style path
|
|
|
|
///
|
|
|
|
/// Will take the first letter of each directory before the contracted path and
|
|
|
|
/// use that in the path instead. See the following example.
|
|
|
|
///
|
|
|
|
/// Absolute Path: `/Users/Bob/Projects/work/a_repo`
|
|
|
|
/// Contracted Path: `a_repo`
|
|
|
|
/// With Fish Style: `~/P/w/a_repo`
|
|
|
|
///
|
|
|
|
/// Absolute Path: `/some/Path/not/in_a/repo/but_nested`
|
|
|
|
/// Contracted Path: `in_a/repo/but_nested`
|
|
|
|
/// With Fish Style: `/s/P/n/in_a/repo/but_nested`
|
|
|
|
fn to_fish_style(pwd_dir_length: usize, dir_string: String, truncated_dir_string: &str) -> String {
|
2019-09-20 14:37:55 +00:00
|
|
|
let replaced_dir_string = dir_string.trim_end_matches(truncated_dir_string).to_owned();
|
2019-08-27 01:52:45 +00:00
|
|
|
let components = replaced_dir_string.split('/').collect::<Vec<&str>>();
|
|
|
|
|
|
|
|
if components.is_empty() {
|
|
|
|
return replaced_dir_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
components
|
|
|
|
.into_iter()
|
2019-11-29 05:02:22 +00:00
|
|
|
.map(|word| -> String {
|
|
|
|
let chars = UnicodeSegmentation::graphemes(word, true).collect::<Vec<&str>>();
|
|
|
|
match word {
|
|
|
|
"" => "".to_string(),
|
|
|
|
_ if chars.len() <= pwd_dir_length => word.to_string(),
|
|
|
|
_ if word.starts_with('.') => chars[..=pwd_dir_length].join(""),
|
|
|
|
_ => chars[..pwd_dir_length].join(""),
|
|
|
|
}
|
2019-08-27 01:52:45 +00:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("/")
|
|
|
|
}
|
|
|
|
|
2019-04-04 18:18:15 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2019-04-15 16:04:53 +00:00
|
|
|
#[test]
|
|
|
|
fn contract_home_directory() {
|
|
|
|
let full_path = Path::new("/Users/astronaut/schematics/rocket");
|
|
|
|
let home = Path::new("/Users/astronaut");
|
|
|
|
|
|
|
|
let output = contract_path(full_path, home, "~");
|
|
|
|
assert_eq!(output, "~/schematics/rocket");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn contract_repo_directory() {
|
|
|
|
let full_path = Path::new("/Users/astronaut/dev/rocket-controls/src");
|
|
|
|
let repo_root = Path::new("/Users/astronaut/dev/rocket-controls");
|
|
|
|
|
|
|
|
let output = contract_path(full_path, repo_root, "rocket-controls");
|
|
|
|
assert_eq!(output, "rocket-controls/src");
|
|
|
|
}
|
|
|
|
|
2019-07-29 00:15:40 +00:00
|
|
|
#[test]
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
fn contract_windows_style_home_directory() {
|
|
|
|
let full_path = Path::new("C:\\Users\\astronaut\\schematics\\rocket");
|
|
|
|
let home = Path::new("C:\\Users\\astronaut");
|
|
|
|
|
|
|
|
let output = contract_path(full_path, home, "~");
|
|
|
|
assert_eq!(output, "~/schematics/rocket");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
fn contract_windows_style_repo_directory() {
|
|
|
|
let full_path = Path::new("C:\\Users\\astronaut\\dev\\rocket-controls\\src");
|
|
|
|
let repo_root = Path::new("C:\\Users\\astronaut\\dev\\rocket-controls");
|
|
|
|
|
|
|
|
let output = contract_path(full_path, repo_root, "rocket-controls");
|
|
|
|
assert_eq!(output, "rocket-controls/src");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
fn contract_windows_style_no_top_level_directory() {
|
|
|
|
let full_path = Path::new("C:\\Some\\Other\\Path");
|
|
|
|
let top_level_path = Path::new("C:\\Users\\astronaut");
|
|
|
|
|
|
|
|
let output = contract_path(full_path, top_level_path, "~");
|
2020-06-22 22:41:05 +00:00
|
|
|
assert_eq!(output, "C:/Some/Other/Path");
|
2019-07-29 00:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
fn contract_windows_style_root_directory() {
|
|
|
|
let full_path = Path::new("C:\\");
|
|
|
|
let top_level_path = Path::new("C:\\Users\\astronaut");
|
|
|
|
|
|
|
|
let output = contract_path(full_path, top_level_path, "~");
|
2020-06-22 22:41:05 +00:00
|
|
|
assert_eq!(output, "C:");
|
2019-07-29 00:15:40 +00:00
|
|
|
}
|
|
|
|
|
2020-05-31 17:32:35 +00:00
|
|
|
#[test]
|
|
|
|
fn substitute_prefix_and_middle() {
|
|
|
|
let full_path = "/absolute/path/foo/bar/baz";
|
|
|
|
let mut substitutions = HashMap::new();
|
|
|
|
substitutions.insert("/absolute/path".to_string(), "");
|
|
|
|
substitutions.insert("/bar/".to_string(), "/");
|
|
|
|
|
|
|
|
let output = substitute_path(full_path.to_string(), &substitutions);
|
|
|
|
assert_eq!(output, "/foo/baz");
|
|
|
|
}
|
|
|
|
|
2019-08-27 01:52:45 +00:00
|
|
|
#[test]
|
|
|
|
fn fish_style_with_user_home_contracted_path() {
|
|
|
|
let path = "~/starship/engines/booster/rocket";
|
|
|
|
let output = to_fish_style(1, path.to_string(), "engines/booster/rocket");
|
|
|
|
assert_eq!(output, "~/s/");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fish_style_with_user_home_contracted_path_and_dot_dir() {
|
|
|
|
let path = "~/.starship/engines/booster/rocket";
|
|
|
|
let output = to_fish_style(1, path.to_string(), "engines/booster/rocket");
|
|
|
|
assert_eq!(output, "~/.s/");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fish_style_with_no_contracted_path() {
|
|
|
|
// `truncatation_length = 2`
|
|
|
|
let path = "/absolute/Path/not/in_a/repo/but_nested";
|
|
|
|
let output = to_fish_style(1, path.to_string(), "repo/but_nested");
|
|
|
|
assert_eq!(output, "/a/P/n/i/");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fish_style_with_pwd_dir_len_no_contracted_path() {
|
|
|
|
// `truncatation_length = 2`
|
|
|
|
let path = "/absolute/Path/not/in_a/repo/but_nested";
|
|
|
|
let output = to_fish_style(2, path.to_string(), "repo/but_nested");
|
|
|
|
assert_eq!(output, "/ab/Pa/no/in/");
|
|
|
|
}
|
2019-09-20 14:37:55 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fish_style_with_duplicate_directories() {
|
|
|
|
let path = "~/starship/tmp/C++/C++/C++";
|
|
|
|
let output = to_fish_style(1, path.to_string(), "C++");
|
|
|
|
assert_eq!(output, "~/s/t/C/C/");
|
|
|
|
}
|
2019-11-29 05:02:22 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fish_style_with_unicode() {
|
|
|
|
let path = "~/starship/tmp/目录/a̐éö̲/目录";
|
|
|
|
let output = to_fish_style(1, path.to_string(), "目录");
|
|
|
|
assert_eq!(output, "~/s/t/目/a̐/");
|
|
|
|
}
|
2019-04-04 18:18:15 +00:00
|
|
|
}
|