Skip to content

Commit

Permalink
feat: Add option to control git directory truncation (starship#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
saghm authored and chipbuster committed Aug 17, 2019
1 parent d065dff commit 84c394e
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Cargo.lock

# Intellij IDE configuration
.idea/
/*.iml

# Compiled files for documentation
docs/node_modules
Expand Down
9 changes: 5 additions & 4 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,11 @@ git repo that you're currently in.

### Options

| Variable | Default | Description |
| ------------------- | ------- | ------------------------------------------------------------------------------- |
| `truncation_length` | `3` | The number of parent folders that the current directory should be truncated to. |
| `disabled` | `false` | Disables the `directory` module. |
| Variable | Default | Description |
| ------------------- | ------- | -------------------------------------------------------------------------------- |
| `truncation_length` | `3` | The number of parent folders that the current directory should be truncated to. |
| `truncate_to_repo` | `true` | Whether or not to truncate to the root of the git repo that you're currently in. |
| `disabled` | `false` | Disables the `directory` module. |

### Example

Expand Down
26 changes: 15 additions & 11 deletions src/modules/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,26 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let truncation_length = module
.config_value_i64("truncation_length")
.unwrap_or(DIR_TRUNCATION_LENGTH);
let truncate_to_repo = module.config_value_bool("truncate_to_repo").unwrap_or(true);

let current_dir = &context.current_dir;
log::debug!("Current directory: {:?}", current_dir);

let dir_string;
if let Some(repo_root) = &context.repo_root {
// Contract the path to the git repo root
let repo_folder_name = repo_root.file_name().unwrap().to_str().unwrap();

dir_string = contract_path(current_dir, repo_root, repo_folder_name);
} else {
// Contract the path to the home directory
let home_dir = dirs::home_dir().unwrap();

dir_string = contract_path(current_dir, &home_dir, HOME_SYMBOL);
}
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();

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();

dir_string = contract_path(current_dir, &home_dir, HOME_SYMBOL);
}
};

// Truncate the dir string to the maximum number of path components
let truncated_dir_string = truncate(dir_string, truncation_length as usize);
Expand Down
62 changes: 62 additions & 0 deletions tests/testsuite/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,65 @@ fn truncated_directory_in_git_repo() -> io::Result<()> {
assert_eq!(expected, actual);
Ok(())
}

#[test]
#[ignore]
fn directory_in_git_repo_truncate_to_repo_false() -> io::Result<()> {
let tmp_dir = TempDir::new_in(dirs::home_dir().unwrap())?;
let repo_dir = tmp_dir.path().join("above-repo").join("rocket-controls");
let dir = repo_dir.join("src/meters/fuel-gauge");
fs::create_dir_all(&dir)?;
Repository::init(&repo_dir).unwrap();

let output = common::render_module("directory")
.use_config(toml::toml! {
[directory]
// Don't truncate the path at all.
truncation_length = 5
truncate_to_repo = false
})
.arg("--path")
.arg(dir)
.output()?;
let actual = String::from_utf8(output.stdout).unwrap();

let expected = format!(
"in {} ",
Color::Cyan
.bold()
.paint("above-repo/rocket-controls/src/meters/fuel-gauge")
);
assert_eq!(expected, actual);
Ok(())
}

#[test]
#[ignore]
fn directory_in_git_repo_truncate_to_repo_true() -> io::Result<()> {
let tmp_dir = TempDir::new_in(dirs::home_dir().unwrap())?;
let repo_dir = tmp_dir.path().join("above-repo").join("rocket-controls");
let dir = repo_dir.join("src/meters/fuel-gauge");
fs::create_dir_all(&dir)?;
Repository::init(&repo_dir).unwrap();

let output = common::render_module("directory")
.use_config(toml::toml! {
[directory]
// `truncate_to_repo = true` should display the truncated path
truncation_length = 5
truncate_to_repo = true
})
.arg("--path")
.arg(dir)
.output()?;
let actual = String::from_utf8(output.stdout).unwrap();

let expected = format!(
"in {} ",
Color::Cyan
.bold()
.paint("rocket-controls/src/meters/fuel-gauge")
);
assert_eq!(expected, actual);
Ok(())
}

0 comments on commit 84c394e

Please sign in to comment.