diff --git a/docs/config/README.md b/docs/config/README.md index 7aae0099..fb293b42 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -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. | + +
+This module has a few advanced configuration options that control how the directory is displayed. + +| 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. | + +
+ ### 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 diff --git a/src/modules/directory.rs b/src/modules/directory.rs index fa6b5c7a..01cd6c01 100644 --- a/src/modules/directory.rs +++ b/src/modules/directory.rs @@ -32,8 +32,26 @@ pub fn module<'a>(context: &'a Context) -> Option> { .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> { 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(¤t_dir, &home_dir, HOME_SYMBOL); let fish_style_dir = to_fish_style( fish_style_pwd_dir_length as usize, contracted_home_dir, diff --git a/tests/testsuite/directory.rs b/tests/testsuite/directory.rs index dce7f798..25926a24 100644 --- a/tests/testsuite/directory.rs +++ b/tests/testsuite/directory.rs @@ -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(()) +}