Files
starship/src/modules/directory.rs
T

142 lines
4.1 KiB
Rust
Raw Normal View History

2019-04-04 14:18:15 -04:00
use super::Segment;
2019-04-08 23:35:14 -04:00
use std::env;
use std::path::PathBuf;
2019-04-04 14:18:15 -04:00
use ansi_term::{Color, Style};
use clap::ArgMatches;
2019-04-04 21:35:24 -04:00
use dirs;
2019-04-07 23:23:37 -04:00
use git2::Repository;
2019-04-04 14:18:15 -04:00
/// Creates a segment with the current directory
2019-04-08 23:35:14 -04: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
///
/// **Truncation**
/// Paths will be limited in length to `3` path components by default.
2019-04-04 14:32:22 -04:00
pub fn segment(_: &ArgMatches) -> Segment {
2019-04-04 14:18:15 -04:00
const COLOR_DIR: Color = Color::Cyan;
2019-04-08 15:32:59 -04:00
const DIR_TRUNCATION_LENGTH: usize = 3;
const HOME_SYMBOL: &str = "~";
2019-04-04 14:18:15 -04:00
2019-04-08 15:32:59 -04:00
let current_path = env::current_dir()
2019-04-07 23:23:37 -04:00
.expect("Unable to identify current directory")
.canonicalize()
.expect("Unable to canonicalize current directory");
2019-04-04 14:18:15 -04:00
2019-04-07 17:12:22 -04:00
let dir_string;
2019-04-08 15:32:59 -04:00
if let Ok(repo) = git2::Repository::discover(&current_path) {
2019-04-08 23:35:14 -04:00
// Contract the path to the git repo root
2019-04-07 16:43:11 -04:00
let repo_root = get_repo_root(repo);
2019-04-07 23:23:37 -04:00
2019-04-08 15:32:59 -04:00
let repo_folder_name = repo_root
.components()
.last()
.unwrap()
.as_os_str()
.to_str()
.unwrap();
2019-04-08 23:35:14 -04:00
dir_string = contract_path(&current_path, &repo_root, &repo_folder_name);
2019-04-07 16:43:11 -04:00
} else {
2019-04-08 23:35:14 -04:00
// Contract the path to the home directory
2019-04-08 15:32:59 -04:00
let home_dir = dirs::home_dir().unwrap();
2019-04-08 23:35:14 -04:00
dir_string = contract_path(&current_path, &home_dir, HOME_SYMBOL);
2019-04-04 14:18:15 -04:00
}
2019-04-08 23:35:14 -04:00
// Truncate the dir string to the maximum number of path components
let truncated_dir_string = truncate(dir_string, DIR_TRUNCATION_LENGTH);
2019-04-04 14:18:15 -04:00
Segment {
2019-04-08 23:35:14 -04:00
value: truncated_dir_string,
2019-04-04 14:18:15 -04:00
style: Style::from(COLOR_DIR).bold(),
..Default::default()
}
}
2019-04-07 17:17:40 -04:00
/// Get the root directory of a git repo
2019-04-07 16:43:11 -04:00
fn get_repo_root(repo: Repository) -> PathBuf {
2019-04-07 23:28:38 -04:00
if repo.is_bare() {
// Bare repos will return the repo root
repo.path().to_path_buf()
} else {
2019-04-07 16:43:11 -04:00
// Non-bare repos will return the path of `.git`
2019-04-07 23:28:38 -04:00
repo.path().parent().unwrap().to_path_buf()
2019-04-07 16:43:11 -04:00
}
}
2019-04-08 23:35:14 -04:00
/// Contract the root component of a path
fn contract_path(full_path: &PathBuf, top_level_path: &PathBuf, top_level_replacement: &str) -> String {
if !full_path.starts_with(top_level_path) {
return full_path.to_str().unwrap().to_string();
}
2019-04-08 15:32:59 -04:00
if full_path == top_level_path {
return top_level_replacement.to_string();
2019-04-07 17:12:22 -04:00
}
2019-04-07 16:43:11 -04:00
2019-04-08 15:32:59 -04:00
let top_level_path_depth = top_level_path.components().count();
2019-04-07 23:23:37 -04:00
2019-04-08 15:32:59 -04:00
format!(
2019-04-08 17:35:38 -04:00
"{replacement}{separator}{path}",
replacement = top_level_replacement,
separator = std::path::MAIN_SEPARATOR,
path = full_path
2019-04-08 15:32:59 -04:00
.iter()
.skip(top_level_path_depth)
.collect::<PathBuf>()
.to_str()
.unwrap()
)
2019-04-07 17:12:22 -04:00
}
2019-04-07 16:43:11 -04:00
2019-04-08 23:35:14 -04:00
/// Truncate a path to only have a set number of path components
fn truncate(dir_string: String, length: usize) -> String {
if length == 0 {
return dir_string;
}
let components = dir_string.split(std::path::MAIN_SEPARATOR).collect::<Vec<&str>>();
if components.len() < length {
return dir_string;
}
let truncated_components = &components[..length];
truncated_components.join(&std::path::MAIN_SEPARATOR.to_string())
}
2019-04-04 14:18:15 -04:00
#[cfg(test)]
mod tests {
2019-04-04 14:32:22 -04:00
// TODO: Look into stubbing `env` so that tests can be run in parallel
2019-04-04 14:18:15 -04:00
use super::*;
use clap::{App, Arg};
2019-04-04 14:32:22 -04:00
use std::path::Path;
2019-04-04 14:18:15 -04: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 14:32:22 -04:00
let home_dir = dirs::home_dir().unwrap();
env::set_current_dir(&home_dir).unwrap();
let segment = segment(&args);
assert_eq!(segment.value, "~");
}
#[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();
2019-04-04 14:18:15 -04:00
let segment = segment(&args);
2019-04-04 14:32:22 -04:00
assert_eq!(segment.value, "/");
2019-04-04 14:18:15 -04:00
}
}