2019-04-04 18:18:15 +00:00
|
|
|
use super::Segment;
|
|
|
|
use ansi_term::{Color, Style};
|
|
|
|
use clap::ArgMatches;
|
2019-04-05 01:35:24 +00:00
|
|
|
use dirs;
|
2019-04-08 03:23:37 +00:00
|
|
|
use git2::Repository;
|
|
|
|
use std::env;
|
|
|
|
use std::path::PathBuf;
|
2019-04-04 18:18:15 +00:00
|
|
|
|
|
|
|
/// Creates a segment with the current directory
|
2019-04-04 18:32:22 +00:00
|
|
|
pub fn segment(_: &ArgMatches) -> Segment {
|
2019-04-04 18:18:15 +00:00
|
|
|
const COLOR_DIR: Color = Color::Cyan;
|
2019-04-08 19:32:59 +00:00
|
|
|
const DIR_TRUNCATION_LENGTH: usize = 3;
|
|
|
|
const HOME_SYMBOL: &str = "~";
|
2019-04-04 18:18:15 +00:00
|
|
|
|
2019-04-08 19:32:59 +00:00
|
|
|
let current_path = env::current_dir()
|
2019-04-08 03:23:37 +00:00
|
|
|
.expect("Unable to identify current directory")
|
|
|
|
.canonicalize()
|
|
|
|
.expect("Unable to canonicalize 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-07 20:43:11 +00:00
|
|
|
let repo_root = get_repo_root(repo);
|
2019-04-08 03:23:37 +00:00
|
|
|
|
2019-04-08 19:32:59 +00:00
|
|
|
// The folder name is the last component to the repo path
|
|
|
|
let repo_folder_name = repo_root
|
|
|
|
.components()
|
|
|
|
.last()
|
|
|
|
.unwrap()
|
|
|
|
.as_os_str()
|
|
|
|
.to_str()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
dir_string = truncate_path(
|
|
|
|
&DIR_TRUNCATION_LENGTH,
|
|
|
|
¤t_path,
|
|
|
|
&repo_root,
|
|
|
|
&repo_folder_name,
|
|
|
|
);
|
2019-04-07 20:43:11 +00:00
|
|
|
} else {
|
2019-04-08 19:32:59 +00:00
|
|
|
let home_dir = dirs::home_dir().unwrap();
|
|
|
|
|
|
|
|
dir_string = truncate_path(
|
|
|
|
&DIR_TRUNCATION_LENGTH,
|
|
|
|
¤t_path,
|
|
|
|
&home_dir,
|
|
|
|
HOME_SYMBOL,
|
|
|
|
);
|
2019-04-04 18:18:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Segment {
|
2019-04-08 03:28:38 +00:00
|
|
|
value: dir_string,
|
2019-04-04 18:18:15 +00:00
|
|
|
style: Style::from(COLOR_DIR).bold(),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 21:17:40 +00:00
|
|
|
/// Get the root directory of a git repo
|
2019-04-07 20:43:11 +00:00
|
|
|
fn get_repo_root(repo: Repository) -> PathBuf {
|
2019-04-08 03:28:38 +00:00
|
|
|
if repo.is_bare() {
|
|
|
|
// Bare repos will return the repo root
|
|
|
|
repo.path().to_path_buf()
|
|
|
|
} else {
|
2019-04-07 20:43:11 +00:00
|
|
|
// Non-bare repos will return the path of `.git`
|
2019-04-08 03:28:38 +00:00
|
|
|
repo.path().parent().unwrap().to_path_buf()
|
2019-04-07 20:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 19:32:59 +00:00
|
|
|
/// Truncate a path to a predefined number of path components
|
|
|
|
fn truncate_path(
|
|
|
|
length: &usize,
|
|
|
|
full_path: &PathBuf,
|
|
|
|
top_level_path: &PathBuf,
|
|
|
|
top_level_replacement: &str,
|
|
|
|
) -> String {
|
|
|
|
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
|
|
|
let full_path_depth = full_path.components().count();
|
|
|
|
let top_level_path_depth = top_level_path.components().count();
|
2019-04-08 03:23:37 +00:00
|
|
|
|
2019-04-08 19:32:59 +00:00
|
|
|
// Don't bother with replacing top level path if length is long enough
|
|
|
|
if full_path_depth - top_level_path_depth >= *length {
|
|
|
|
return full_path
|
|
|
|
.iter()
|
|
|
|
.skip(full_path_depth - length)
|
|
|
|
.collect::<PathBuf>()
|
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
.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!(
|
|
|
|
"{}{}{}",
|
|
|
|
top_level_replacement,
|
|
|
|
std::path::MAIN_SEPARATOR,
|
|
|
|
full_path
|
|
|
|
.iter()
|
|
|
|
.skip(top_level_path_depth)
|
|
|
|
.collect::<PathBuf>()
|
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
)
|
2019-04-07 21:12:22 +00:00
|
|
|
}
|
2019-04-07 20:43:11 +00:00
|
|
|
|
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::*;
|
|
|
|
use clap::{App, Arg};
|
2019-04-04 18:32:22 +00:00
|
|
|
use std::path::Path;
|
2019-04-04 18:18:15 +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:32:22 +00: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 18:18:15 +00:00
|
|
|
let segment = segment(&args);
|
2019-04-04 18:32:22 +00:00
|
|
|
assert_eq!(segment.value, "/");
|
2019-04-04 18:18:15 +00:00
|
|
|
}
|
|
|
|
}
|