feat: add support for logical paths in directory (#366)

Adds the ability for starship to display the logical path instead of the physical path.
This commit is contained in:
Kevin Song 2019-09-20 11:28:09 -05:00 committed by Matan Kushner
parent cd892ebb60
commit c2875d99b6
3 changed files with 118 additions and 27 deletions

View File

@ -146,7 +146,7 @@ style = "bold red"
The `display` option is an array of the following table.
| Variable | Description |
|-------------|-------------------------------------------------|
| ----------- | ----------------------------------------------- |
| `threshold` | The upper bound for the display option. |
| `style` | The style used if the display option is in use. |
@ -246,13 +246,23 @@ it would have been `nixpkgs/pkgs`.
### Options
| 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. |
| `fish_style_pwd_dir_length` | `0` | The number of characters to use when applying fish shell pwd path logic. |
| `style` | `"bold cyan"` | The style for the module. |
| `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. |
| `style` | `"bold cyan"` | The style for the module. |
| `disabled` | `false` | Disables the `directory` module. |
<details>
<summary>This module has a few advanced configuration options that control how the directory is displayed.</summary>
| Variable | Default | Description |
| --------------------------- | ------- | ---------------------------------------------------------------------------------------- |
| `fish_style_pwd_dir_length` | `0` | The number of characters to use when applying fish shell pwd path logic. |
| `use_logical_path` | `true` | Displays the logical path provided by the shell (`PWD`) instead of the path from the OS. |
</details>
### Example
@ -327,23 +337,23 @@ current directory.
### Options
| Variable | Default | Description |
| ------------------------ | ------------ | ------------------------------------------------------- |
| `conflicted` | `"="` | This branch has merge conflicts. |
| `ahead` | `"⇡"` | This branch is ahead of the branch being tracked. |
| `behind` | `"⇣"` | This branch is behind of the branch being tracked. |
| `diverged` | `"⇕"` | This branch has diverged from the branch being tracked. |
| `untracked` | `"?"` | There are untracked files in the working directory. |
| `stashed` | `"$"` | A stash exists for the local repository. |
| `modified` | `"!"` | There are file modifications in the working directory. |
| `staged` | `"+"` | A new file has been added to the staging area. |
| `renamed` | `"»"` | A renamed file has been added to the staging area. |
| `deleted` | `"✘"` | A file's deletion has been added to the staging area. |
| `show_sync_count` | `false` | Show ahead/behind count of the branch being tracked. |
| `prefix` | `[` | Prefix to display immediately before git status. |
| `suffix` | `]` | Suffix to display immediately after git status. |
| `style` | `"bold red"` | The style for the module. |
| `disabled` | `false` | Disables the `git_status` module. |
| Variable | Default | Description |
| ----------------- | ------------ | ------------------------------------------------------- |
| `conflicted` | `"="` | This branch has merge conflicts. |
| `ahead` | `"⇡"` | This branch is ahead of the branch being tracked. |
| `behind` | `"⇣"` | This branch is behind of the branch being tracked. |
| `diverged` | `"⇕"` | This branch has diverged from the branch being tracked. |
| `untracked` | `"?"` | There are untracked files in the working directory. |
| `stashed` | `"$"` | A stash exists for the local repository. |
| `modified` | `"!"` | There are file modifications in the working directory. |
| `staged` | `"+"` | A new file has been added to the staging area. |
| `renamed` | `"»"` | A renamed file has been added to the staging area. |
| `deleted` | `"✘"` | A file's deletion has been added to the staging area. |
| `show_sync_count` | `false` | Show ahead/behind count of the branch being tracked. |
| `prefix` | `[` | Prefix to display immediately before git status. |
| `suffix` | `]` | Suffix to display immediately after git status. |
| `style` | `"bold red"` | The style for the module. |
| `disabled` | `false` | Disables the `git_status` module. |
### Example

View File

@ -32,8 +32,26 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
.config_value_i64("fish_style_pwd_dir_length")
.unwrap_or(FISH_STYLE_PWD_DIR_LENGTH);
// Using environment PWD is the standard approach for determining logical path
let use_logical_path = module.config_value_bool("use_logical_path").unwrap_or(true);
// If this is None for any reason, we fall back to reading the os-provided path
let logical_current_dir = if use_logical_path {
match std::env::var("PWD") {
Ok(x) => Some(x),
Err(_) => {
log::debug!("Asked for logical path, but PWD was invalid.");
None
}
}
} else {
None
};
let current_dir = logical_current_dir
.as_ref()
.map(|d| Path::new(d))
.unwrap_or_else(|| context.current_dir.as_ref());
let home_dir = dirs::home_dir().unwrap();
let current_dir = &context.current_dir;
log::debug!("Current directory: {:?}", current_dir);
let repo = &context.get_repo().ok()?;
@ -54,7 +72,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
if fish_style_pwd_dir_length > 0 {
// If user is using fish style path, we need to add the segment first
let contracted_home_dir = contract_path(current_dir, &home_dir, HOME_SYMBOL);
let contracted_home_dir = contract_path(&current_dir, &home_dir, HOME_SYMBOL);
let fish_style_dir = to_fish_style(
fish_style_pwd_dir_length as usize,
contracted_home_dir,

View File

@ -418,3 +418,66 @@ fn directory_in_git_repo_truncate_to_repo_true() -> io::Result<()> {
assert_eq!(expected, actual);
Ok(())
}
#[test]
#[ignore]
fn use_logical_and_physical_paths() -> io::Result<()> {
/* This test is a bit of a smoke + mirrors trick because all it shows is that
the application is reading the PWD envar correctly (if the shell doesn't
correctly set PWD, we're still in trouble). */
let tmp_dir = Path::new("/tmp/starship/porthole/viewport");
let dir = tmp_dir.join("directory");
let sym = tmp_dir.join("symlink_to_directory");
fs::create_dir_all(&dir)?;
// Create a symlink on the appropriate system
#[cfg(target_family = "unix")]
std::os::unix::fs::symlink(&dir, &sym).unwrap();
#[cfg(target_family = "windows")]
std::os::windows::fs::symlink_file(&dir, &sym).unwrap();
// Test when using physical paths
let output = common::render_module("directory")
.use_config(toml::toml! {
[directory]
use_logical_path = false
})
.arg("--path")
.arg(&dir)
.env(
"PWD",
"/tmp/starship/porthole/viewport/symlink_to_directory",
)
.output()?;
let actual = String::from_utf8(output.stdout).unwrap();
let expected = format!(
"in {} ",
Color::Cyan.bold().paint("porthole/viewport/directory")
);
assert_eq!(expected, actual);
// Test when using logical paths
let output = common::render_module("directory")
.use_config(toml::toml! {
[directory]
use_logical_path = true
})
.arg("--path")
.arg(&sym)
.env(
"PWD",
"/tmp/starship/porthole/viewport/symlink_to_directory",
)
.output()?;
let actual = String::from_utf8(output.stdout).unwrap();
let expected = format!(
"in {} ",
Color::Cyan
.bold()
.paint("porthole/viewport/symlink_to_directory")
);
assert_eq!(expected, actual);
Ok(())
}