2019-04-04 18:18:15 +00:00
|
|
|
use super::Segment;
|
2019-04-12 23:11:40 +00:00
|
|
|
use ansi_term::Color;
|
2019-04-04 18:18:15 +00:00
|
|
|
use clap::ArgMatches;
|
2019-04-05 01:35:24 +00:00
|
|
|
use dirs;
|
2019-04-08 03:23:37 +00:00
|
|
|
use git2::Repository;
|
2019-04-11 23:31:30 +00:00
|
|
|
use std::env;
|
2019-04-12 23:11:40 +00:00
|
|
|
use std::path::Path;
|
2019-04-04 18:18:15 +00:00
|
|
|
|
|
|
|
/// Creates a segment with the current directory
|
2019-04-11 23:31:30 +00:00
|
|
|
///
|
2019-04-09 03:35:14 +00:00
|
|
|
/// Will perform path contraction and truncation.
|
|
|
|
/// **Contraction**
|
|
|
|
/// - Paths begining with the home directory will be contracted to `~`
|
|
|
|
/// - Paths containing a git repo will contract to begin at the repo root
|
2019-04-11 23:31:30 +00:00
|
|
|
///
|
2019-04-09 03:35:14 +00:00
|
|
|
/// **Truncation**
|
|
|
|
/// Paths will be limited in length to `3` path components by default.
|
2019-04-13 03:06:48 +00:00
|
|
|
pub fn segment(_: &ArgMatches) -> Option<Segment> {
|
2019-04-08 19:32:59 +00:00
|
|
|
const HOME_SYMBOL: &str = "~";
|
2019-04-12 23:11:40 +00:00
|
|
|
const DIR_TRUNCATION_LENGTH: usize = 3;
|
|
|
|
const SECTION_COLOR: Color = Color::Cyan;
|
|
|
|
|
|
|
|
let mut segment = Segment::new("dir");
|
2019-04-04 18:18:15 +00:00
|
|
|
|
2019-04-09 04:04:50 +00:00
|
|
|
// TODO: Currently gets the physical directory. Get the logical directory.
|
2019-04-11 23:31:30 +00:00
|
|
|
let current_path = env::current_dir().expect("Unable to identify current directory");
|
2019-04-04 18:18:15 +00:00
|
|
|
|
2019-04-07 21:12:22 +00:00
|
|
|
let dir_string;
|
2019-04-08 19:32:59 +00:00
|
|
|
if let Ok(repo) = git2::Repository::discover(¤t_path) {
|
2019-04-09 03:35:14 +00:00
|
|
|
// Contract the path to the git repo root
|
2019-04-12 23:11:40 +00:00
|
|
|
let repo_root = get_repo_root(&repo);
|
|
|
|
let repo_folder_name = repo_root.file_name().unwrap().to_str().unwrap();
|
2019-04-08 19:32:59 +00:00
|
|
|
|
2019-04-12 23:11:40 +00:00
|
|
|
dir_string = contract_path(¤t_path, repo_root, repo_folder_name);
|
2019-04-07 20:43:11 +00:00
|
|
|
} else {
|
2019-04-09 03:35:14 +00:00
|
|
|
// Contract the path to the home directory
|
2019-04-08 19:32:59 +00:00
|
|
|
let home_dir = dirs::home_dir().unwrap();
|
2019-04-12 23:11:40 +00:00
|
|
|
|
2019-04-09 03:35:14 +00:00
|
|
|
dir_string = contract_path(¤t_path, &home_dir, HOME_SYMBOL);
|
2019-04-04 18:18:15 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 03:35:14 +00:00
|
|
|
// Truncate the dir string to the maximum number of path components
|
|
|
|
let truncated_dir_string = truncate(dir_string, DIR_TRUNCATION_LENGTH);
|
|
|
|
|
2019-04-12 23:11:40 +00:00
|
|
|
segment
|
|
|
|
.set_value(truncated_dir_string)
|
2019-04-13 03:06:48 +00:00
|
|
|
.set_style(SECTION_COLOR.bold());
|
|
|
|
|
|
|
|
Some(segment)
|
2019-04-04 18:18:15 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 21:17:40 +00:00
|
|
|
/// Get the root directory of a git repo
|
2019-04-12 23:18:47 +00:00
|
|
|
fn get_repo_root(repo: &Repository) -> &Path {
|
2019-04-08 03:28:38 +00:00
|
|
|
if repo.is_bare() {
|
|
|
|
// Bare repos will return the repo root
|
2019-04-12 23:11:40 +00:00
|
|
|
repo.path()
|
2019-04-08 03:28:38 +00:00
|
|
|
} else {
|
2019-04-07 20:43:11 +00:00
|
|
|
// Non-bare repos will return the path of `.git`
|
2019-04-12 23:11:40 +00:00
|
|
|
repo.path().parent().unwrap()
|
2019-04-07 20:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-09 03:35:14 +00:00
|
|
|
/// Contract the root component of a path
|
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) {
|
|
|
|
return full_path.to_str().unwrap().to_string();
|
|
|
|
}
|
|
|
|
|
2019-04-08 19:32:59 +00:00
|
|
|
if full_path == top_level_path {
|
|
|
|
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,
|
|
|
|
separator = std::path::MAIN_SEPARATOR,
|
|
|
|
path = full_path
|
2019-04-12 23:11:40 +00:00
|
|
|
.strip_prefix(top_level_path)
|
|
|
|
.unwrap()
|
2019-04-08 19:32:59 +00:00
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
)
|
2019-04-07 21:12:22 +00:00
|
|
|
}
|
2019-04-07 20:43:11 +00:00
|
|
|
|
2019-04-09 03:35:14 +00:00
|
|
|
/// Truncate a path to only have a set number of path components
|
2019-04-15 16:04:53 +00: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 03:35:14 +00:00
|
|
|
fn truncate(dir_string: String, length: usize) -> String {
|
|
|
|
if length == 0 {
|
|
|
|
return dir_string;
|
|
|
|
}
|
|
|
|
|
2019-04-11 23:31:30 +00:00
|
|
|
let components = dir_string
|
|
|
|
.split(std::path::MAIN_SEPARATOR)
|
|
|
|
.collect::<Vec<&str>>();
|
2019-04-15 15:40:18 +00:00
|
|
|
if components.len() <= length {
|
2019-04-09 03:35:14 +00:00
|
|
|
return dir_string;
|
|
|
|
}
|
|
|
|
|
2019-04-15 15:40:18 +00:00
|
|
|
let truncated_components = &components[components.len() - length..];
|
2019-04-09 03:35:14 +00:00
|
|
|
truncated_components.join(&std::path::MAIN_SEPARATOR.to_string())
|
|
|
|
}
|
|
|
|
|
2019-04-04 18:18:15 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-04-04 18:32:22 +00:00
|
|
|
// TODO: Look into stubbing `env` so that tests can be run in parallel
|
2019-04-04 18:18:15 +00:00
|
|
|
use super::*;
|
|
|
|
|
2019-04-15 15:40:18 +00:00
|
|
|
// #[test]
|
|
|
|
// fn truncate_home_dir() {
|
|
|
|
// let args = App::new("starship")
|
|
|
|
// .arg(Arg::with_name("status_code"))
|
|
|
|
// .get_matches_from(vec!["starship", "0"]);
|
2019-04-04 18:18:15 +00:00
|
|
|
|
2019-04-15 15:40:18 +00:00
|
|
|
// let home_dir = dirs::home_dir().unwrap();
|
|
|
|
// env::set_current_dir(&home_dir).unwrap();
|
2019-04-04 18:32:22 +00:00
|
|
|
|
2019-04-15 15:40:18 +00:00
|
|
|
// let segment = segment(&args).unwrap();
|
|
|
|
// assert_eq!(segment.output(), "~");
|
|
|
|
// }
|
2019-04-04 18:32:22 +00:00
|
|
|
|
2019-04-15 15:40:18 +00:00
|
|
|
// #[test]
|
|
|
|
// fn dont_truncate_non_home_dir() {
|
|
|
|
// let args = App::new("starship")
|
|
|
|
// .arg(Arg::with_name("status_code"))
|
|
|
|
// .get_matches_from(vec!["starship", "0"]);
|
|
|
|
|
|
|
|
// let root_dir = Path::new("/");
|
|
|
|
// env::set_current_dir(&root_dir).unwrap();
|
|
|
|
|
|
|
|
// let segment = segment(&args).unwrap();
|
|
|
|
// assert_eq!(segment.output(), "/");
|
|
|
|
// }
|
|
|
|
|
|
|
|
// #[test]
|
|
|
|
// fn do_not_canonicalize_paths() {
|
|
|
|
// let args = App::new("starship")
|
|
|
|
// .arg(Arg::with_name("status_code"))
|
|
|
|
// .get_matches_from(vec!["starship", "0"]);
|
2019-04-04 18:32:22 +00:00
|
|
|
|
2019-04-15 15:40:18 +00:00
|
|
|
// let root_dir = Path::new("/var");
|
|
|
|
// env::set_current_dir(&root_dir).unwrap();
|
2019-04-04 18:32:22 +00:00
|
|
|
|
2019-04-15 15:40:18 +00:00
|
|
|
// let segment = segment(&args).unwrap();
|
|
|
|
// assert_eq!(segment.output(), "/var");
|
|
|
|
// }
|
|
|
|
|
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-04-15 15:40:18 +00: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 18:18:15 +00:00
|
|
|
}
|
2019-04-09 04:04:50 +00:00
|
|
|
|
|
|
|
#[test]
|
2019-04-15 15:40:18 +00: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 04:04:50 +00:00
|
|
|
|
2019-04-15 15:40:18 +00:00
|
|
|
#[test]
|
2019-04-15 16:04:53 +00:00
|
|
|
fn truncate_slightly_larger_path_than_provided_length() {
|
2019-04-15 15:40:18 +00:00
|
|
|
let path = "~/starship/engines/booster";
|
|
|
|
let output = truncate(path.to_string(), 3);
|
|
|
|
assert_eq!(output, "starship/engines/booster")
|
|
|
|
}
|
2019-04-09 04:04:50 +00:00
|
|
|
|
2019-04-15 15:40:18 +00: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 04:04:50 +00:00
|
|
|
}
|
2019-04-04 18:18:15 +00:00
|
|
|
}
|