2019-04-13 01:11:40 +02:00
|
|
|
use ansi_term::Color;
|
2019-07-29 02:15:40 +02:00
|
|
|
use path_slash::PathExt;
|
2019-04-13 01:11:40 +02:00
|
|
|
use std::path::Path;
|
2019-04-04 20:18:15 +02:00
|
|
|
|
2019-05-01 22:34:24 +02:00
|
|
|
use super::{Context, Module};
|
2019-04-19 22:57:14 +02:00
|
|
|
|
2019-07-19 22:18:52 +02:00
|
|
|
/// Creates a module with the current directory
|
2019-04-12 01:31:30 +02:00
|
|
|
///
|
2019-04-09 05:35:14 +02:00
|
|
|
/// Will perform path contraction and truncation.
|
|
|
|
/// **Contraction**
|
2019-08-16 20:16:29 +02:00
|
|
|
/// - Paths beginning with the home directory will be contracted to `~`
|
2019-04-09 05:35:14 +02:00
|
|
|
/// - Paths containing a git repo will contract to begin at the repo root
|
2019-04-12 01:31:30 +02:00
|
|
|
///
|
2019-04-09 05:35:14 +02:00
|
|
|
/// **Truncation**
|
|
|
|
/// Paths will be limited in length to `3` path components by default.
|
2019-07-02 22:12:53 +02:00
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2019-04-08 21:32:59 +02:00
|
|
|
const HOME_SYMBOL: &str = "~";
|
2019-07-29 03:05:13 +02:00
|
|
|
const DIR_TRUNCATION_LENGTH: i64 = 3;
|
2019-05-01 22:34:24 +02:00
|
|
|
let module_color = Color::Cyan.bold();
|
|
|
|
|
2019-07-02 22:12:53 +02:00
|
|
|
let mut module = context.new_module("directory")?;
|
2019-05-01 22:34:24 +02:00
|
|
|
module.set_style(module_color);
|
2019-04-13 01:11:40 +02:00
|
|
|
|
2019-07-29 03:05:13 +02:00
|
|
|
let truncation_length = module
|
|
|
|
.config_value_i64("truncation_length")
|
|
|
|
.unwrap_or(DIR_TRUNCATION_LENGTH);
|
2019-08-17 05:29:22 +02:00
|
|
|
let truncate_to_repo = module.config_value_bool("truncate_to_repo").unwrap_or(true);
|
2019-07-29 03:05:13 +02:00
|
|
|
|
2019-04-19 22:57:14 +02:00
|
|
|
let current_dir = &context.current_dir;
|
2019-05-16 17:40:30 +02:00
|
|
|
log::debug!("Current directory: {:?}", current_dir);
|
2019-04-04 20:18:15 +02:00
|
|
|
|
2019-04-07 23:12:22 +02:00
|
|
|
let dir_string;
|
2019-08-17 05:29:22 +02:00
|
|
|
match &context.repo_root {
|
|
|
|
Some(repo_root) if truncate_to_repo => {
|
|
|
|
// Contract the path to the git repo root
|
|
|
|
let repo_folder_name = repo_root.file_name().unwrap().to_str().unwrap();
|
2019-04-08 21:32:59 +02:00
|
|
|
|
2019-08-17 05:29:22 +02:00
|
|
|
dir_string = contract_path(current_dir, repo_root, repo_folder_name);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Contract the path to the home directory
|
|
|
|
let home_dir = dirs::home_dir().unwrap();
|
2019-04-13 01:11:40 +02:00
|
|
|
|
2019-08-17 05:29:22 +02:00
|
|
|
dir_string = contract_path(current_dir, &home_dir, HOME_SYMBOL);
|
|
|
|
}
|
|
|
|
};
|
2019-04-04 20:18:15 +02:00
|
|
|
|
2019-04-09 05:35:14 +02:00
|
|
|
// Truncate the dir string to the maximum number of path components
|
2019-07-29 03:05:13 +02:00
|
|
|
let truncated_dir_string = truncate(dir_string, truncation_length as usize);
|
2019-07-14 17:15:47 +02:00
|
|
|
module.new_segment("path", &truncated_dir_string);
|
2019-04-09 05:35:14 +02:00
|
|
|
|
2019-05-20 04:26:12 +02:00
|
|
|
module.get_prefix().set_value("in ");
|
|
|
|
|
2019-05-01 22:34:24 +02:00
|
|
|
Some(module)
|
2019-04-04 20:18:15 +02:00
|
|
|
}
|
|
|
|
|
2019-04-09 05:35:14 +02:00
|
|
|
/// Contract the root component of a path
|
2019-04-15 20:38:45 +02:00
|
|
|
///
|
2019-04-15 18:04:53 +02:00
|
|
|
/// Replaces the `top_level_path` in a given `full_path` with the provided
|
|
|
|
/// `top_level_replacement`.
|
2019-04-13 01:11:40 +02:00
|
|
|
fn contract_path(full_path: &Path, top_level_path: &Path, top_level_replacement: &str) -> String {
|
2019-04-09 05:35:14 +02:00
|
|
|
if !full_path.starts_with(top_level_path) {
|
2019-07-29 02:15:40 +02:00
|
|
|
return replace_c_dir(full_path.to_slash().unwrap());
|
2019-04-09 05:35:14 +02:00
|
|
|
}
|
|
|
|
|
2019-04-08 21:32:59 +02:00
|
|
|
if full_path == top_level_path {
|
2019-07-29 02:15:40 +02:00
|
|
|
return replace_c_dir(top_level_replacement.to_string());
|
2019-04-07 23:12:22 +02:00
|
|
|
}
|
2019-04-07 22:43:11 +02:00
|
|
|
|
2019-04-08 21:32:59 +02:00
|
|
|
format!(
|
2019-04-08 23:35:38 +02:00
|
|
|
"{replacement}{separator}{path}",
|
|
|
|
replacement = top_level_replacement,
|
2019-05-16 17:40:30 +02:00
|
|
|
separator = "/",
|
2019-07-29 02:15:40 +02:00
|
|
|
path = replace_c_dir(
|
|
|
|
full_path
|
|
|
|
.strip_prefix(top_level_path)
|
|
|
|
.unwrap()
|
|
|
|
.to_slash()
|
|
|
|
.unwrap()
|
|
|
|
)
|
2019-04-08 21:32:59 +02:00
|
|
|
)
|
2019-04-07 23:12:22 +02:00
|
|
|
}
|
2019-04-07 22:43:11 +02:00
|
|
|
|
2019-07-29 02:15:40 +02:00
|
|
|
/// Replaces "C://" with "/c/" within a Windows path
|
|
|
|
///
|
|
|
|
/// On non-Windows OS, does nothing
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
fn replace_c_dir(path: String) -> String {
|
|
|
|
return path.replace("C:/", "/c");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Replaces "C://" with "/c/" within a Windows path
|
|
|
|
///
|
|
|
|
/// On non-Windows OS, does nothing
|
|
|
|
#[cfg(not(target_os = "windows"))]
|
2019-08-01 01:48:51 +02:00
|
|
|
const fn replace_c_dir(path: String) -> String {
|
|
|
|
path
|
2019-07-29 02:15:40 +02:00
|
|
|
}
|
|
|
|
|
2019-04-09 05:35:14 +02:00
|
|
|
/// Truncate a path to only have a set number of path components
|
2019-04-15 20:38:45 +02:00
|
|
|
///
|
2019-04-15 18:04:53 +02:00
|
|
|
/// Will truncate a path to only show the last `length` components in a path.
|
|
|
|
/// If a length of `0` is provided, the path will not be truncated.
|
2019-04-09 05:35:14 +02:00
|
|
|
fn truncate(dir_string: String, length: usize) -> String {
|
|
|
|
if length == 0 {
|
|
|
|
return dir_string;
|
|
|
|
}
|
|
|
|
|
2019-07-14 23:54:45 +02:00
|
|
|
let components = dir_string.split('/').collect::<Vec<&str>>();
|
2019-04-15 17:40:18 +02:00
|
|
|
if components.len() <= length {
|
2019-04-09 05:35:14 +02:00
|
|
|
return dir_string;
|
|
|
|
}
|
|
|
|
|
2019-04-15 17:40:18 +02:00
|
|
|
let truncated_components = &components[components.len() - length..];
|
2019-05-16 17:40:30 +02:00
|
|
|
truncated_components.join("/")
|
2019-04-09 05:35:14 +02:00
|
|
|
}
|
|
|
|
|
2019-04-04 20:18:15 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2019-04-15 18:04:53 +02: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 02:15:40 +02: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, "~");
|
|
|
|
assert_eq!(output, "/c/Some/Other/Path");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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, "~");
|
|
|
|
assert_eq!(output, "/c");
|
|
|
|
}
|
|
|
|
|
2019-04-15 17:40:18 +02:00
|
|
|
#[test]
|
|
|
|
fn truncate_smaller_path_than_provided_length() {
|
|
|
|
let path = "~/starship";
|
|
|
|
let output = truncate(path.to_string(), 3);
|
|
|
|
assert_eq!(output, "~/starship")
|
2019-04-04 20:18:15 +02:00
|
|
|
}
|
2019-04-09 06:04:50 +02:00
|
|
|
|
|
|
|
#[test]
|
2019-04-15 17:40:18 +02:00
|
|
|
fn truncate_same_path_as_provided_length() {
|
|
|
|
let path = "~/starship/engines";
|
|
|
|
let output = truncate(path.to_string(), 3);
|
|
|
|
assert_eq!(output, "~/starship/engines")
|
|
|
|
}
|
2019-04-09 06:04:50 +02:00
|
|
|
|
2019-04-15 17:40:18 +02:00
|
|
|
#[test]
|
2019-04-15 18:04:53 +02:00
|
|
|
fn truncate_slightly_larger_path_than_provided_length() {
|
2019-04-15 17:40:18 +02:00
|
|
|
let path = "~/starship/engines/booster";
|
|
|
|
let output = truncate(path.to_string(), 3);
|
|
|
|
assert_eq!(output, "starship/engines/booster")
|
|
|
|
}
|
2019-04-09 06:04:50 +02:00
|
|
|
|
2019-04-15 17:40:18 +02:00
|
|
|
#[test]
|
|
|
|
fn truncate_larger_path_than_provided_length() {
|
|
|
|
let path = "~/starship/engines/booster/rocket";
|
|
|
|
let output = truncate(path.to_string(), 3);
|
|
|
|
assert_eq!(output, "engines/booster/rocket")
|
2019-04-09 06:04:50 +02:00
|
|
|
}
|
2019-04-04 20:18:15 +02:00
|
|
|
}
|