starship/src/modules/directory.rs

1737 lines
57 KiB
Rust
Raw Normal View History

#[cfg(not(target_os = "windows"))]
use super::utils::directory_nix as directory_utils;
#[cfg(target_os = "windows")]
use super::utils::directory_win as directory_utils;
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
use super::utils::path::PathExt as SPathExt;
use indexmap::IndexMap;
use path_slash::{PathBufExt, PathExt};
use std::iter::FromIterator;
use std::path::{Path, PathBuf};
use unicode_segmentation::UnicodeSegmentation;
2019-04-04 18:18:15 +00:00
2019-05-01 20:34:24 +00:00
use super::{Context, Module};
use super::utils::directory::truncate;
use crate::config::RootModuleConfig;
use crate::configs::directory::DirectoryConfig;
use crate::formatter::StringFormatter;
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
/// Creates a module with the current logical or physical directory
2019-04-11 23:31:30 +00:00
///
/// Will perform path contraction, substitution, and truncation.
///
2019-04-09 03:35:14 +00:00
/// **Contraction**
/// - Paths beginning with the home directory or with a git repo right inside
/// the home directory will be contracted to `~`, or the set HOME_SYMBOL
/// - Paths containing a git repo will contract to begin at the repo root
2019-04-11 23:31:30 +00:00
///
/// **Substitution**
/// Paths will undergo user-provided substitutions of substrings
///
2019-04-09 03:35:14 +00:00
/// **Truncation**
/// Paths will be limited in length to `3` path components by default.
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("directory");
let config: DirectoryConfig = DirectoryConfig::try_load(module.config);
let home_symbol = String::from(config.home_symbol);
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
let home_dir = context
.get_home()
.expect("Unable to determine HOME_DIR for user");
let physical_dir = &context.current_dir;
let display_dir = if config.use_logical_path {
&context.logical_dir
} else {
&context.current_dir
};
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
log::debug!("Home dir: {:?}", &home_dir);
log::debug!("Physical dir: {:?}", &physical_dir);
log::debug!("Display dir: {:?}", &display_dir);
2019-04-04 18:18:15 +00:00
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
// Attempt repository path contraction (if we are in a git repository)
2021-11-09 19:12:40 +00:00
// Otherwise use the logical path, automatically contracting
let repo = context.get_repo().ok();
let dir_string = if config.truncate_to_repo {
repo.and_then(|r| r.workdir.as_ref())
2021-11-09 19:12:40 +00:00
.filter(|&root| root != &home_dir)
.and_then(|root| contract_repo_path(display_dir, root))
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
} else {
None
};
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
let mut is_truncated = dir_string.is_some();
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
// the home directory if required.
let dir_string =
dir_string.unwrap_or_else(|| contract_path(display_dir, &home_dir, &home_symbol));
2019-04-04 18:18:15 +00:00
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
#[cfg(windows)]
let dir_string = remove_extended_path_prefix(dir_string);
// Apply path substitutions
let dir_string = substitute_path(dir_string, &config.substitutions);
2019-04-09 03:35:14 +00:00
// Truncate the dir string to the maximum number of path components
let dir_string =
if let Some(truncated) = truncate(&dir_string, config.truncation_length as usize) {
is_truncated = true;
truncated
} else {
dir_string
};
let prefix = if is_truncated {
// Substitutions could have changed the prefix, so don't allow them and
// fish-style path contraction together
if config.fish_style_pwd_dir_length > 0 && config.substitutions.is_empty() {
// If user is using fish style path, we need to add the segment first
let contracted_home_dir = contract_path(display_dir, &home_dir, &home_symbol);
to_fish_style(
config.fish_style_pwd_dir_length as usize,
contracted_home_dir,
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
&dir_string,
)
} else {
String::from(config.truncation_symbol)
}
} else {
String::from("")
};
let path_vec = match &repo.and_then(|r| r.workdir.as_ref()) {
2021-11-09 19:12:40 +00:00
Some(repo_root) if config.repo_root_style.is_some() => {
let contracted_path = contract_repo_path(display_dir, repo_root)?;
let repo_path_vec: Vec<&str> = contracted_path.split('/').collect();
let after_repo_root = contracted_path.replacen(repo_path_vec[0], "", 1);
let num_segments_after_root = after_repo_root.split('/').count();
if ((num_segments_after_root - 1) as i64) < config.truncation_length {
let root = repo_path_vec[0];
let before = dir_string.replace(&contracted_path, "");
[prefix + &before, root.to_string(), after_repo_root]
} else {
["".to_string(), "".to_string(), prefix + &dir_string]
}
}
_ => ["".to_string(), "".to_string(), prefix + &dir_string],
};
let path_vec = if config.use_os_path_sep {
path_vec.map(|i| convert_path_sep(&i))
} else {
path_vec
};
let lock_symbol = String::from(config.read_only);
2021-11-09 19:12:40 +00:00
let display_format = if path_vec[0].is_empty() && path_vec[1].is_empty() {
config.format
} else {
config.repo_root_format
};
let repo_root_style = config.repo_root_style.unwrap_or(config.style);
2021-11-09 19:12:40 +00:00
let parsed = StringFormatter::new(display_format).and_then(|formatter| {
formatter
.map_style(|variable| match variable {
"style" => Some(Ok(config.style)),
"read_only_style" => Some(Ok(config.read_only_style)),
2021-11-09 19:12:40 +00:00
"repo_root_style" => Some(Ok(repo_root_style)),
_ => None,
})
.map(|variable| match variable {
2021-11-09 19:12:40 +00:00
"path" => Some(Ok(&path_vec[2])),
"before_root_path" => Some(Ok(&path_vec[0])),
"repo_root" => Some(Ok(&path_vec[1])),
"read_only" => {
if is_readonly_dir(physical_dir) {
Some(Ok(&lock_symbol))
} else {
None
}
}
_ => None,
})
.parse(None, Some(context))
});
module.set_segments(match parsed {
Ok(segments) => segments,
Err(error) => {
log::warn!("Error in module `directory`:\n{}", error);
return None;
}
});
2019-05-01 20:34:24 +00:00
Some(module)
2019-04-04 18:18:15 +00:00
}
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
#[cfg(windows)]
fn remove_extended_path_prefix(path: String) -> String {
fn try_trim_prefix<'a>(s: &'a str, prefix: &str) -> Option<&'a str> {
if !s.starts_with(prefix) {
return None;
}
Some(&s[prefix.len()..])
}
// Trim any Windows extended-path prefix from the display path
if let Some(unc) = try_trim_prefix(&path, r"\\?\UNC\") {
return format!(r"\\{}", unc);
}
if let Some(p) = try_trim_prefix(&path, r"\\?\") {
return p.to_string();
}
path
}
fn is_readonly_dir(path: &Path) -> bool {
match directory_utils::is_write_allowed(path) {
Ok(res) => !res,
Err(e) => {
log::debug!(
"Failed to determine read only status of directory '{:?}': {}",
path,
e
);
false
}
}
}
2019-04-09 03:35:14 +00:00
/// Contract the root component of a path
2019-04-15 18:38:45 +00:00
///
/// Replaces the `top_level_path` in a given `full_path` with the provided
/// `top_level_replacement`.
2019-04-12 23:11:40 +00:00
fn contract_path(full_path: &Path, top_level_path: &Path, top_level_replacement: &str) -> String {
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
if !full_path.normalised_starts_with(top_level_path) {
return full_path.to_slash_lossy();
2019-04-09 03:35:14 +00:00
}
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
if full_path.normalised_equals(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
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
// Because we've done a normalised path comparison above
// we can safely ignore the Prefix components when doing this
// strip_prefix operation.
let sub_path = full_path
.without_prefix()
.strip_prefix(top_level_path.without_prefix())
.unwrap_or(full_path);
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
2019-04-08 19:32:59 +00:00
format!(
2019-04-08 21:35:38 +00:00
"{replacement}{separator}{path}",
replacement = top_level_replacement,
separator = "/",
path = sub_path.to_slash_lossy()
2019-04-08 19:32:59 +00:00
)
2019-04-07 21:12:22 +00:00
}
2019-04-07 20:43:11 +00:00
/// Contract the root component of a path based on the real path
///
/// Replaces the `top_level_path` in a given `full_path` with the provided
/// `top_level_replacement` by walking ancestors and comparing its real path.
fn contract_repo_path(full_path: &Path, top_level_path: &Path) -> Option<String> {
let top_level_real_path = real_path(top_level_path);
// Walk ancestors to preserve logical path in `full_path`.
// If we'd just `full_real_path.strip_prefix(top_level_real_path)`,
// then it wouldn't preserve logical path. It would've returned physical path.
for (i, ancestor) in full_path.ancestors().enumerate() {
let ancestor_real_path = real_path(ancestor);
if ancestor_real_path != top_level_real_path {
continue;
}
let components: Vec<_> = full_path.components().collect();
let repo_name = components[components.len() - i - 1]
.as_os_str()
.to_string_lossy();
if i == 0 {
return Some(repo_name.to_string());
}
let path = PathBuf::from_iter(&components[components.len() - i..]);
return Some(format!(
"{repo_name}{separator}{path}",
repo_name = repo_name,
separator = "/",
path = path.to_slash_lossy()
));
}
None
}
fn real_path<P: AsRef<Path>>(path: P) -> PathBuf {
let path = path.as_ref();
let mut buf = PathBuf::new();
for component in path.components() {
let next = buf.join(component);
if let Ok(realpath) = next.read_link() {
if realpath.is_absolute() {
buf = realpath;
} else {
buf.push(realpath);
}
} else {
buf = next;
}
}
buf.canonicalize().unwrap_or_else(|_| path.into())
}
/// Perform a list of string substitutions on the path
///
/// Given a list of (from, to) pairs, this will perform the string
/// substitutions, in order, on the path. Any non-pair of strings is ignored.
fn substitute_path(dir_string: String, substitutions: &IndexMap<String, &str>) -> String {
let mut substituted_dir = dir_string;
for substitution_pair in substitutions {
substituted_dir = substituted_dir.replace(substitution_pair.0, substitution_pair.1);
}
substituted_dir
}
/// Takes part before contracted path and replaces it with fish style path
///
/// Will take the first letter of each directory before the contracted path and
/// use that in the path instead. See the following example.
///
/// Absolute Path: `/Users/Bob/Projects/work/a_repo`
/// Contracted Path: `a_repo`
/// With Fish Style: `~/P/w/a_repo`
///
/// Absolute Path: `/some/Path/not/in_a/repo/but_nested`
/// Contracted Path: `in_a/repo/but_nested`
/// With Fish Style: `/s/P/n/in_a/repo/but_nested`
fn to_fish_style(pwd_dir_length: usize, dir_string: String, truncated_dir_string: &str) -> String {
let replaced_dir_string = dir_string.trim_end_matches(truncated_dir_string).to_owned();
let components = replaced_dir_string.split('/').collect::<Vec<&str>>();
if components.is_empty() {
return replaced_dir_string;
}
components
.into_iter()
.map(|word| -> String {
let chars = UnicodeSegmentation::graphemes(word, true).collect::<Vec<&str>>();
match word {
"" => "".to_string(),
_ if chars.len() <= pwd_dir_length => word.to_string(),
_ if word.starts_with('.') => chars[..=pwd_dir_length].join(""),
_ => chars[..pwd_dir_length].join(""),
}
})
.collect::<Vec<_>>()
.join("/")
}
/// Convert the path separators in `path` to the OS specific path separators.
fn convert_path_sep(path: &str) -> String {
return PathBuf::from_slash(path).to_string_lossy().into_owned();
}
2019-04-04 18:18:15 +00:00
#[cfg(test)]
mod tests {
use super::*;
use crate::test::ModuleRenderer;
use crate::utils::create_command;
2021-07-04 19:32:58 +00:00
use crate::utils::home_dir;
use ansi_term::Color;
#[cfg(not(target_os = "windows"))]
use std::os::unix::fs::symlink;
#[cfg(target_os = "windows")]
use std::os::windows::fs::symlink_dir as symlink;
use std::path::Path;
use std::{fs, io};
use tempfile::TempDir;
2019-04-04 18:18:15 +00:00
#[test]
fn contract_home_directory() {
let full_path = Path::new("/Users/astronaut/schematics/rocket");
let home = Path::new("/Users/astronaut");
let output = contract_path(full_path, home, "~");
assert_eq!(output, "~/schematics/rocket");
}
#[test]
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
fn contract_repo_directory() -> io::Result<()> {
let tmp_dir = TempDir::new_in(home_dir().unwrap().as_path())?;
let repo_dir = tmp_dir.path().join("dev").join("rocket-controls");
let src_dir = repo_dir.join("src");
fs::create_dir_all(&src_dir)?;
init_repo(&repo_dir)?;
let src_variations = [src_dir.clone(), dunce::canonicalize(src_dir).unwrap()];
let repo_variations = [repo_dir.clone(), dunce::canonicalize(repo_dir).unwrap()];
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
for src_dir in &src_variations {
for repo_dir in &repo_variations {
let output = contract_repo_path(src_dir, repo_dir);
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
assert_eq!(output, Some("rocket-controls/src".to_string()));
}
}
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
tmp_dir.close()
}
#[test]
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
#[cfg(windows)]
fn contract_windows_style_home_directory() {
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
let path_variations = [
r"\\?\C:\Users\astronaut\schematics\rocket",
r"C:\Users\astronaut\schematics\rocket",
];
let home_path_variations = [r"\\?\C:\Users\astronaut", r"C:\Users\astronaut"];
for path in &path_variations {
for home_path in &home_path_variations {
let path = Path::new(path);
let home_path = Path::new(home_path);
let output = contract_path(path, home_path, "~");
assert_eq!(output, "~/schematics/rocket");
}
}
}
#[test]
#[cfg(target_os = "windows")]
fn contract_windows_style_repo_directory() {
let full_path = Path::new("C:\\Users\\astronaut\\dev\\rocket-controls\\src");
let repo_root = Path::new("C:\\Users\\astronaut\\dev\\rocket-controls");
let output = contract_path(full_path, repo_root, "rocket-controls");
assert_eq!(output, "rocket-controls/src");
}
#[test]
#[cfg(target_os = "windows")]
fn contract_windows_style_no_top_level_directory() {
let full_path = Path::new("C:\\Some\\Other\\Path");
let top_level_path = Path::new("C:\\Users\\astronaut");
let output = contract_path(full_path, top_level_path, "~");
assert_eq!(output, "C:/Some/Other/Path");
}
#[test]
#[cfg(target_os = "windows")]
fn contract_windows_style_root_directory() {
let full_path = Path::new("C:\\");
let top_level_path = Path::new("C:\\Users\\astronaut");
let output = contract_path(full_path, top_level_path, "~");
assert_eq!(output, "C:");
}
#[test]
fn substitute_prefix_and_middle() {
let full_path = "/absolute/path/foo/bar/baz";
let mut substitutions = IndexMap::new();
substitutions.insert("/absolute/path".to_string(), "");
substitutions.insert("/bar/".to_string(), "/");
let output = substitute_path(full_path.to_string(), &substitutions);
assert_eq!(output, "/foo/baz");
}
#[test]
fn fish_style_with_user_home_contracted_path() {
let path = "~/starship/engines/booster/rocket";
let output = to_fish_style(1, path.to_string(), "engines/booster/rocket");
assert_eq!(output, "~/s/");
}
#[test]
fn fish_style_with_user_home_contracted_path_and_dot_dir() {
let path = "~/.starship/engines/booster/rocket";
let output = to_fish_style(1, path.to_string(), "engines/booster/rocket");
assert_eq!(output, "~/.s/");
}
#[test]
fn fish_style_with_no_contracted_path() {
// `truncation_length = 2`
let path = "/absolute/Path/not/in_a/repo/but_nested";
let output = to_fish_style(1, path.to_string(), "repo/but_nested");
assert_eq!(output, "/a/P/n/i/");
}
#[test]
fn fish_style_with_pwd_dir_len_no_contracted_path() {
// `truncation_length = 2`
let path = "/absolute/Path/not/in_a/repo/but_nested";
let output = to_fish_style(2, path.to_string(), "repo/but_nested");
assert_eq!(output, "/ab/Pa/no/in/");
}
#[test]
fn fish_style_with_duplicate_directories() {
let path = "~/starship/tmp/C++/C++/C++";
let output = to_fish_style(1, path.to_string(), "C++");
assert_eq!(output, "~/s/t/C/C/");
}
#[test]
fn fish_style_with_unicode() {
let path = "~/starship/tmp/目录/a̐éö̲/目录";
let output = to_fish_style(1, path.to_string(), "目录");
assert_eq!(output, "~/s/t/目/a̐/");
}
fn init_repo(path: &Path) -> io::Result<()> {
create_command("git")?
.args(&["init"])
.current_dir(path)
.output()
.map(|_| ())
}
fn make_known_tempdir(root: &Path) -> io::Result<(TempDir, String)> {
fs::create_dir_all(root)?;
let dir = TempDir::new_in(root)?;
let path = dir
.path()
.file_name()
.unwrap()
.to_string_lossy()
.to_string();
Ok((dir, path))
}
#[cfg(not(target_os = "windows"))]
mod linux {
use super::*;
#[test]
#[ignore]
fn symlinked_subdirectory_git_repo_out_of_tree() -> io::Result<()> {
let tmp_dir = TempDir::new_in(home_dir().unwrap().as_path())?;
let repo_dir = tmp_dir.path().join("above-repo").join("rocket-controls");
let src_dir = repo_dir.join("src/meters/fuel-gauge");
let symlink_dir = tmp_dir.path().join("fuel-gauge");
fs::create_dir_all(&src_dir)?;
init_repo(&repo_dir)?;
symlink(&src_dir, &symlink_dir)?;
let actual = ModuleRenderer::new("directory")
.env("HOME", tmp_dir.path().to_str().unwrap())
.path(symlink_dir)
.collect();
let expected = Some(format!("{} ", Color::Cyan.bold().paint("~/fuel-gauge")));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
#[ignore]
fn git_repo_in_home_directory_truncate_to_repo_true() -> io::Result<()> {
let tmp_dir = TempDir::new_in(home_dir().unwrap().as_path())?;
let dir = tmp_dir.path().join("src/fuel-gauge");
fs::create_dir_all(&dir)?;
init_repo(tmp_dir.path())?;
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
// `truncate_to_repo = true` should attempt to display the truncated path
truncate_to_repo = true
truncation_length = 5
})
.path(dir)
.env("HOME", tmp_dir.path().to_str().unwrap())
.collect();
let expected = Some(format!("{} ", Color::Cyan.bold().paint("~/src/fuel-gauge")));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
#[ignore]
fn directory_in_root() {
let actual = ModuleRenderer::new("directory").path("/etc").collect();
let expected = Some(format!(
"{}{} ",
Color::Cyan.bold().paint("/etc"),
Color::Red.normal().paint("🔒")
));
assert_eq!(expected, actual);
}
}
#[test]
fn home_directory_default_home_symbol() {
let actual = ModuleRenderer::new("directory")
.path(home_dir().unwrap())
.collect();
let expected = Some(format!("{} ", Color::Cyan.bold().paint("~")));
assert_eq!(expected, actual);
}
#[test]
fn home_directory_custom_home_symbol() {
let actual = ModuleRenderer::new("directory")
.path(home_dir().unwrap())
.config(toml::toml! {
[directory]
home_symbol = "🚀"
})
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan.bold().paint(convert_path_sep("🚀"))
));
assert_eq!(expected, actual);
}
#[test]
fn home_directory_custom_home_symbol_subdirectories() {
let actual = ModuleRenderer::new("directory")
.path(home_dir().unwrap().join("path/subpath"))
.config(toml::toml! {
[directory]
home_symbol = "🚀"
})
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan
.bold()
.paint(convert_path_sep("🚀/path/subpath"))
));
assert_eq!(expected, actual);
}
#[test]
fn substituted_truncated_path() {
let actual = ModuleRenderer::new("directory")
.path("/some/long/network/path/workspace/a/b/c/dev")
.config(toml::toml! {
[directory]
truncation_length = 4
[directory.substitutions]
"/some/long/network/path" = "/some/net"
"a/b/c" = "d"
})
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan
.bold()
.paint(convert_path_sep("net/workspace/d/dev"))
));
assert_eq!(expected, actual);
}
#[test]
fn substitution_order() {
let actual = ModuleRenderer::new("directory")
.path("/path/to/sub")
.config(toml::toml! {
[directory.substitutions]
"/path/to/sub" = "/correct/order"
"/to/sub" = "/wrong/order"
})
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan.bold().paint(convert_path_sep("/correct/order"))
));
assert_eq!(expected, actual);
}
#[test]
fn strange_substitution() {
let strange_sub = "/\\/;,!";
let actual = ModuleRenderer::new("directory")
.path("/foo/bar/regular/path")
.config(toml::toml! {
[directory]
truncation_length = 0
fish_style_pwd_dir_length = 2 // Overridden by substitutions
[directory.substitutions]
"regular" = strange_sub
})
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan
.bold()
.paint(convert_path_sep(&format!("/foo/bar/{}/path", strange_sub)))
));
assert_eq!(expected, actual);
}
#[test]
fn directory_in_home() -> io::Result<()> {
let (tmp_dir, name) = make_known_tempdir(home_dir().unwrap().as_path())?;
let dir = tmp_dir.path().join("starship");
fs::create_dir_all(&dir)?;
let actual = ModuleRenderer::new("directory").path(dir).collect();
let expected = Some(format!(
"{} ",
Color::Cyan
.bold()
.paint(convert_path_sep(&format!("~/{}/starship", name)))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
fn truncated_directory_in_home() -> io::Result<()> {
let (tmp_dir, name) = make_known_tempdir(home_dir().unwrap().as_path())?;
let dir = tmp_dir.path().join("engine/schematics");
fs::create_dir_all(&dir)?;
let actual = ModuleRenderer::new("directory").path(dir).collect();
let expected = Some(format!(
"{} ",
Color::Cyan
.bold()
.paint(convert_path_sep(&format!("{}/engine/schematics", name)))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
fn fish_directory_in_home() -> io::Result<()> {
let (tmp_dir, name) = make_known_tempdir(home_dir().unwrap().as_path())?;
let dir = tmp_dir.path().join("starship/schematics");
fs::create_dir_all(&dir)?;
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
truncation_length = 1
fish_style_pwd_dir_length = 2
})
.path(&dir)
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan.bold().paint(convert_path_sep(&format!(
"~/{}/st/schematics",
name.split_at(3).0
)))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
fn root_directory() {
// Note: We have disable the read_only settings here due to false positives when running
// the tests on Windows as a non-admin.
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
read_only = ""
read_only_style = ""
})
.path("/")
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan.bold().paint(convert_path_sep("/"))
));
assert_eq!(expected, actual);
}
#[test]
fn truncated_directory_in_root() -> io::Result<()> {
let (tmp_dir, name) = make_known_tempdir(Path::new("/tmp"))?;
let dir = tmp_dir.path().join("thrusters/rocket");
fs::create_dir_all(&dir)?;
let actual = ModuleRenderer::new("directory").path(dir).collect();
let expected = Some(format!(
"{} ",
Color::Cyan
.bold()
.paint(convert_path_sep(&format!("{}/thrusters/rocket", name)))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
fn truncated_directory_config_large() -> io::Result<()> {
let (tmp_dir, _) = make_known_tempdir(Path::new("/tmp"))?;
let dir = tmp_dir.path().join("thrusters/rocket");
fs::create_dir_all(&dir)?;
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
truncation_length = 100
})
.path(&dir)
.collect();
let dir_str = dir.to_slash_lossy();
let expected = Some(format!(
"{} ",
Color::Cyan.bold().paint(convert_path_sep(
&truncate(&dir_str, 100).unwrap_or(dir_str)
))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
fn fish_style_directory_config_large() -> io::Result<()> {
let (tmp_dir, _) = make_known_tempdir(Path::new("/tmp"))?;
let dir = tmp_dir.path().join("thrusters/rocket");
fs::create_dir_all(&dir)?;
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
truncation_length = 1
fish_style_pwd_dir_length = 100
})
.path(&dir)
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan.bold().paint(convert_path_sep(&to_fish_style(
100,
dir.to_slash_lossy(),
""
)))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
fn truncated_directory_config_small() -> io::Result<()> {
let (tmp_dir, name) = make_known_tempdir(Path::new("/tmp"))?;
let dir = tmp_dir.path().join("rocket");
fs::create_dir_all(&dir)?;
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
truncation_length = 2
})
.path(dir)
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan
.bold()
.paint(convert_path_sep(&format!("{}/rocket", name)))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
fn fish_directory_config_small() -> io::Result<()> {
let (tmp_dir, _) = make_known_tempdir(Path::new("/tmp"))?;
let dir = tmp_dir.path().join("thrusters/rocket");
fs::create_dir_all(&dir)?;
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
truncation_length = 2
fish_style_pwd_dir_length = 1
})
.path(&dir)
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan.bold().paint(convert_path_sep(&format!(
"{}/thrusters/rocket",
to_fish_style(1, dir.to_slash_lossy(), "/thrusters/rocket")
)))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
#[ignore]
fn git_repo_root() -> io::Result<()> {
let tmp_dir = TempDir::new()?;
let repo_dir = tmp_dir.path().join("rocket-controls");
fs::create_dir(&repo_dir)?;
init_repo(&repo_dir).unwrap();
let actual = ModuleRenderer::new("directory").path(repo_dir).collect();
let expected = Some(format!(
"{} ",
Color::Cyan
.bold()
.paint(convert_path_sep("rocket-controls"))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
#[ignore]
fn directory_in_git_repo() -> io::Result<()> {
let tmp_dir = TempDir::new()?;
let repo_dir = tmp_dir.path().join("rocket-controls");
let dir = repo_dir.join("src");
fs::create_dir_all(&dir)?;
init_repo(&repo_dir).unwrap();
let actual = ModuleRenderer::new("directory").path(dir).collect();
let expected = Some(format!(
"{} ",
Color::Cyan
.bold()
.paint(convert_path_sep("rocket-controls/src"))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
#[ignore]
fn truncated_directory_in_git_repo() -> io::Result<()> {
let tmp_dir = TempDir::new()?;
let repo_dir = tmp_dir.path().join("rocket-controls");
let dir = repo_dir.join("src/meters/fuel-gauge");
fs::create_dir_all(&dir)?;
init_repo(&repo_dir).unwrap();
let actual = ModuleRenderer::new("directory").path(dir).collect();
let expected = Some(format!(
"{} ",
Color::Cyan
.bold()
.paint(convert_path_sep("src/meters/fuel-gauge"))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
#[ignore]
fn directory_in_git_repo_truncate_to_repo_false() -> io::Result<()> {
let tmp_dir = TempDir::new()?;
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)?;
init_repo(&repo_dir).unwrap();
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
// Don't truncate the path at all.
truncation_length = 5
truncate_to_repo = false
})
.path(dir)
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan.bold().paint(convert_path_sep(
"above-repo/rocket-controls/src/meters/fuel-gauge"
))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
#[ignore]
fn fish_path_directory_in_git_repo_truncate_to_repo_false() -> io::Result<()> {
let (tmp_dir, _) = make_known_tempdir(Path::new("/tmp"))?;
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)?;
init_repo(&repo_dir).unwrap();
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
// Don't truncate the path at all.
truncation_length = 5
truncate_to_repo = false
fish_style_pwd_dir_length = 1
})
.path(dir)
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan.bold().paint(convert_path_sep(&format!(
"{}/above-repo/rocket-controls/src/meters/fuel-gauge",
to_fish_style(1, tmp_dir.path().to_slash_lossy(), "")
)))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
#[ignore]
fn fish_path_directory_in_git_repo_truncate_to_repo_true() -> io::Result<()> {
let (tmp_dir, _) = make_known_tempdir(Path::new("/tmp"))?;
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)?;
init_repo(&repo_dir).unwrap();
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
// `truncate_to_repo = true` should display the truncated path
truncation_length = 5
truncate_to_repo = true
fish_style_pwd_dir_length = 1
})
.path(dir)
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan.bold().paint(convert_path_sep(&format!(
"{}/rocket-controls/src/meters/fuel-gauge",
to_fish_style(1, tmp_dir.path().join("above-repo").to_slash_lossy(), "")
)))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
#[ignore]
fn directory_in_git_repo_truncate_to_repo_true() -> io::Result<()> {
let (tmp_dir, _) = make_known_tempdir(Path::new("/tmp"))?;
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)?;
init_repo(&repo_dir).unwrap();
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
// `truncate_to_repo = true` should display the truncated path
truncation_length = 5
truncate_to_repo = true
})
.path(dir)
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan
.bold()
.paint(convert_path_sep("rocket-controls/src/meters/fuel-gauge"))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
#[ignore]
fn symlinked_git_repo_root() -> io::Result<()> {
let (tmp_dir, _) = make_known_tempdir(Path::new("/tmp"))?;
let repo_dir = tmp_dir.path().join("rocket-controls");
let symlink_dir = tmp_dir.path().join("rocket-controls-symlink");
fs::create_dir(&repo_dir)?;
init_repo(&repo_dir).unwrap();
symlink(&repo_dir, &symlink_dir)?;
let actual = ModuleRenderer::new("directory").path(symlink_dir).collect();
let expected = Some(format!(
"{} ",
Color::Cyan
.bold()
.paint(convert_path_sep("rocket-controls-symlink"))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
#[ignore]
fn directory_in_symlinked_git_repo() -> io::Result<()> {
let (tmp_dir, _) = make_known_tempdir(Path::new("/tmp"))?;
let repo_dir = tmp_dir.path().join("rocket-controls");
let src_dir = repo_dir.join("src");
let symlink_dir = tmp_dir.path().join("rocket-controls-symlink");
let symlink_src_dir = symlink_dir.join("src");
fs::create_dir_all(&src_dir)?;
init_repo(&repo_dir).unwrap();
symlink(&repo_dir, &symlink_dir)?;
let actual = ModuleRenderer::new("directory")
.path(symlink_src_dir)
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan
.bold()
.paint(convert_path_sep("rocket-controls-symlink/src"))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
#[ignore]
fn truncated_directory_in_symlinked_git_repo() -> io::Result<()> {
let (tmp_dir, _) = make_known_tempdir(Path::new("/tmp"))?;
let repo_dir = tmp_dir.path().join("rocket-controls");
let src_dir = repo_dir.join("src/meters/fuel-gauge");
let symlink_dir = tmp_dir.path().join("rocket-controls-symlink");
let symlink_src_dir = symlink_dir.join("src/meters/fuel-gauge");
fs::create_dir_all(&src_dir)?;
init_repo(&repo_dir).unwrap();
symlink(&repo_dir, &symlink_dir)?;
let actual = ModuleRenderer::new("directory")
.path(symlink_src_dir)
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan
.bold()
.paint(convert_path_sep("src/meters/fuel-gauge"))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
#[ignore]
fn directory_in_symlinked_git_repo_truncate_to_repo_false() -> io::Result<()> {
let (tmp_dir, _) = make_known_tempdir(Path::new("/tmp"))?;
let repo_dir = tmp_dir.path().join("above-repo").join("rocket-controls");
let src_dir = repo_dir.join("src/meters/fuel-gauge");
let symlink_dir = tmp_dir
.path()
.join("above-repo")
.join("rocket-controls-symlink");
let symlink_src_dir = symlink_dir.join("src/meters/fuel-gauge");
fs::create_dir_all(&src_dir)?;
init_repo(&repo_dir).unwrap();
symlink(&repo_dir, &symlink_dir)?;
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
// Don't truncate the path at all.
truncation_length = 5
truncate_to_repo = false
})
.path(symlink_src_dir)
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan.bold().paint(convert_path_sep(
"above-repo/rocket-controls-symlink/src/meters/fuel-gauge"
))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
#[ignore]
fn fish_path_directory_in_symlinked_git_repo_truncate_to_repo_false() -> io::Result<()> {
let (tmp_dir, _) = make_known_tempdir(Path::new("/tmp"))?;
let repo_dir = tmp_dir.path().join("above-repo").join("rocket-controls");
let src_dir = repo_dir.join("src/meters/fuel-gauge");
let symlink_dir = tmp_dir
.path()
.join("above-repo")
.join("rocket-controls-symlink");
let symlink_src_dir = symlink_dir.join("src/meters/fuel-gauge");
fs::create_dir_all(&src_dir)?;
init_repo(&repo_dir).unwrap();
symlink(&repo_dir, &symlink_dir)?;
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
// Don't truncate the path at all.
truncation_length = 5
truncate_to_repo = false
fish_style_pwd_dir_length = 1
})
.path(symlink_src_dir)
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan.bold().paint(convert_path_sep(&format!(
"{}/above-repo/rocket-controls-symlink/src/meters/fuel-gauge",
to_fish_style(1, tmp_dir.path().to_slash_lossy(), "")
)))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
#[ignore]
fn fish_path_directory_in_symlinked_git_repo_truncate_to_repo_true() -> io::Result<()> {
let (tmp_dir, _) = make_known_tempdir(Path::new("/tmp"))?;
let repo_dir = tmp_dir.path().join("above-repo").join("rocket-controls");
let src_dir = repo_dir.join("src/meters/fuel-gauge");
let symlink_dir = tmp_dir
.path()
.join("above-repo")
.join("rocket-controls-symlink");
let symlink_src_dir = symlink_dir.join("src/meters/fuel-gauge");
fs::create_dir_all(&src_dir)?;
init_repo(&repo_dir).unwrap();
symlink(&repo_dir, &symlink_dir)?;
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
// `truncate_to_repo = true` should display the truncated path
truncation_length = 5
truncate_to_repo = true
fish_style_pwd_dir_length = 1
})
.path(symlink_src_dir)
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan.bold().paint(convert_path_sep(&format!(
"{}/rocket-controls-symlink/src/meters/fuel-gauge",
to_fish_style(1, tmp_dir.path().join("above-repo").to_slash_lossy(), "")
)))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
#[ignore]
fn directory_in_symlinked_git_repo_truncate_to_repo_true() -> io::Result<()> {
let (tmp_dir, _) = make_known_tempdir(Path::new("/tmp"))?;
let repo_dir = tmp_dir.path().join("above-repo").join("rocket-controls");
let src_dir = repo_dir.join("src/meters/fuel-gauge");
let symlink_dir = tmp_dir
.path()
.join("above-repo")
.join("rocket-controls-symlink");
let symlink_src_dir = symlink_dir.join("src/meters/fuel-gauge");
fs::create_dir_all(&src_dir)?;
init_repo(&repo_dir).unwrap();
symlink(&repo_dir, &symlink_dir)?;
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
// `truncate_to_repo = true` should display the truncated path
truncation_length = 5
truncate_to_repo = true
})
.path(symlink_src_dir)
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan.bold().paint(convert_path_sep(
"rocket-controls-symlink/src/meters/fuel-gauge"
))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
#[ignore]
fn symlinked_directory_in_git_repo() -> io::Result<()> {
let (tmp_dir, _) = make_known_tempdir(Path::new("/tmp"))?;
let repo_dir = tmp_dir.path().join("rocket-controls");
let dir = repo_dir.join("src");
fs::create_dir_all(&dir)?;
init_repo(&repo_dir).unwrap();
symlink(&dir, repo_dir.join("src/loop"))?;
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
// `truncate_to_repo = true` should display the truncated path
truncation_length = 5
truncate_to_repo = true
})
.path(repo_dir.join("src/loop/loop"))
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan
.bold()
.paint(convert_path_sep("rocket-controls/src/loop/loop"))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
fn truncation_symbol_truncated_root() {
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
truncation_length = 3
truncation_symbol = "…/"
})
.path(Path::new("/a/four/element/path"))
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan
.bold()
.paint(convert_path_sep("…/four/element/path"))
));
assert_eq!(expected, actual);
}
#[test]
fn truncation_symbol_not_truncated_root() {
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
truncation_length = 4
truncation_symbol = "…/"
})
.path(Path::new("/a/four/element/path"))
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan
.bold()
.paint(convert_path_sep("/a/four/element/path"))
));
assert_eq!(expected, actual);
}
#[test]
fn truncation_symbol_truncated_home() -> io::Result<()> {
let (tmp_dir, name) = make_known_tempdir(home_dir().unwrap().as_path())?;
let dir = tmp_dir.path().join("a/subpath");
fs::create_dir_all(&dir)?;
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
truncation_length = 3
truncation_symbol = "…/"
})
.path(dir)
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan
.bold()
.paint(convert_path_sep(&format!("…/{}/a/subpath", name)))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
fn truncation_symbol_not_truncated_home() -> io::Result<()> {
let (tmp_dir, name) = make_known_tempdir(home_dir().unwrap().as_path())?;
let dir = tmp_dir.path().join("a/subpath");
fs::create_dir_all(&dir)?;
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
truncate_to_repo = false // Necessary if homedir is a git repo
truncation_length = 4
truncation_symbol = "…/"
})
.path(dir)
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan
.bold()
.paint(convert_path_sep(&format!("~/{}/a/subpath", name)))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
fn truncation_symbol_truncated_in_repo() -> io::Result<()> {
let (tmp_dir, _) = make_known_tempdir(Path::new("/tmp"))?;
let repo_dir = tmp_dir.path().join("above").join("repo");
let dir = repo_dir.join("src/sub/path");
fs::create_dir_all(&dir)?;
init_repo(&repo_dir).unwrap();
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
truncation_length = 3
truncation_symbol = "…/"
})
.path(dir)
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan.bold().paint(convert_path_sep("…/src/sub/path"))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
fn truncation_symbol_not_truncated_in_repo() -> io::Result<()> {
let (tmp_dir, _) = make_known_tempdir(Path::new("/tmp"))?;
let repo_dir = tmp_dir.path().join("above").join("repo");
let dir = repo_dir.join("src/sub/path");
fs::create_dir_all(&dir)?;
init_repo(&repo_dir).unwrap();
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
truncation_length = 5
truncation_symbol = "…/"
truncate_to_repo = true
})
.path(dir)
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan
.bold()
.paint(convert_path_sep("…/repo/src/sub/path"))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
#[cfg(target_os = "windows")]
fn truncation_symbol_windows_root_not_truncated() {
let dir = Path::new("C:\\temp");
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
truncation_length = 2
truncation_symbol = "…/"
})
.path(dir)
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan.bold().paint(convert_path_sep("C:/temp"))
));
assert_eq!(expected, actual);
}
#[test]
#[cfg(target_os = "windows")]
fn truncation_symbol_windows_root_truncated() {
let dir = Path::new("C:\\temp");
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
truncation_length = 1
truncation_symbol = "…/"
})
.path(dir)
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan.bold().paint(convert_path_sep("…/temp"))
));
assert_eq!(expected, actual);
}
#[test]
#[cfg(target_os = "windows")]
fn truncation_symbol_windows_root_truncated_backslash() {
let dir = Path::new("C:\\temp");
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
truncation_length = 1
truncation_symbol = r"…\"
})
.path(dir)
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan.bold().paint(convert_path_sep("\\temp"))
));
assert_eq!(expected, actual);
}
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
#[test]
fn use_logical_path_true_should_render_logical_dir_path() -> io::Result<()> {
let tmp_dir = TempDir::new()?;
let path = tmp_dir.path().join("src/meters/fuel-gauge");
fs::create_dir_all(&path)?;
let logical_path = "Logical:/fuel-gauge";
let expected = Some(format!(
"{} ",
Color::Cyan
.bold()
.paint(convert_path_sep("Logical:/fuel-gauge"))
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
));
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
use_logical_path = true
truncation_length = 3
})
.path(path)
.logical_path(logical_path)
.collect();
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
fn use_logical_path_false_should_render_current_dir_path() -> io::Result<()> {
let tmp_dir = TempDir::new()?;
let path = tmp_dir.path().join("src/meters/fuel-gauge");
fs::create_dir_all(&path)?;
let logical_path = "Logical:/fuel-gauge";
let expected = Some(format!(
"{} ",
Color::Cyan
.bold()
.paint(convert_path_sep("src/meters/fuel-gauge"))
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
));
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
use_logical_path = false
truncation_length = 3
})
.path(path)
.logical_path(logical_path) // logical_path should be ignored
.collect();
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
#[cfg(windows)]
fn windows_trims_extended_path_prefix() {
// Under Windows, path canonicalization returns the paths using extended-path prefixes `\\?\`
// We expect this prefix to be trimmed before being rendered.
let sys32_path = Path::new(r"\\?\C:\Windows\System32");
let expected = Some(format!(
"{} ",
Color::Cyan
.bold()
.paint(convert_path_sep("C:/Windows/System32"))
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
));
// Note: We have disable the read_only settings here due to false positives when running
// the tests on Windows as a non-admin.
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
use_logical_path = false
truncation_length = 0
read_only = ""
read_only_style = ""
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
})
.path(sys32_path)
.collect();
assert_eq!(expected, actual);
}
#[test]
#[cfg(windows)]
fn windows_trims_extended_unc_path_prefix() {
// Under Windows, path canonicalization may return UNC paths using extended-path prefixes `\\?\UNC\`
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
// We expect this prefix to be trimmed before being rendered.
let unc_path = Path::new(r"\\?\UNC\server\share\a\b\c");
// NOTE: path-slash doesn't convert slashes which are part of path prefixes under Windows,
// which is why the first part of this string still includes backslashes
let expected = Some(format!(
"{} ",
Color::Cyan
.bold()
.paint(convert_path_sep(r"\\server\share/a/b/c"))
refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path (#2104) * refactor(directory): Introduce `logical-path` argument which allows a shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix. * fix(powershell): Fix an issue with trailing backslashes on file paths causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped. * fix(powershell): Use Invoke-Native pattern for safely invoking native executables with strings which may contain characters which need to be escaped carefully. * fix(context): Remove superfluous argument trims These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place. * refactor(directory): Clean up the semantics of `logical_dir` defaulting it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`. * fix(directory) :Use logical_dir for contracting the home directory This keeps the two calls to contract_path in sync. * fix(directory): Remove test script * refactor(directory): Convert current_dir to canonical filesystem path when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage * fix(directory): Use AsRef style for passing Path arguments * fix(directory): Strip the windows extended-path prefix from the display string later in the render process * fix(docs): Update docs/config/README.md for use_logical_path * refactor(context): Populate `current_dir` from `--path` or `std::env::current_dir`, populate `logical_dir` from `--logical-path` or the `PWD` env var - `current_dir` is always canonicalized - On Windows, `current_dir` will have an extended-path prefix - `logical_dir` is now always set - `directory::module` now just selects between `current_dir` and `logical_dir` when picking which path to render - Test coverage * fix(directory): Fix path comparison operations in directory to ignore differences between path prefixes - Added PathExt extension trait which adds `normalised_equals`, `normalised_starts_with` and `without_prefix` * fix(path): Add test coverage for PathExt on *nix * fix(directory): Test coverage for `contract_repo_path`, `contract_path` with variations of verbatim and non-verbatim paths * fix(directory): Update path-slash to latest This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:` * fix(powershell): Improve UTF8 output handling, argument encoding - Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope * fix(path): Make PathExt methods no-ops on *nix * fix(path): Cargo fmt * fix(powershell): Remove typo ';'. Fix variable assignment lint.
2021-02-08 14:14:59 +00:00
));
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
use_logical_path = false
truncation_length = 0
})
.path(unc_path)
.collect();
assert_eq!(expected, actual);
}
2021-11-09 19:12:40 +00:00
#[test]
fn highlight_git_root_dir() -> io::Result<()> {
let (tmp_dir, _) = make_known_tempdir(Path::new("/tmp"))?;
let repo_dir = tmp_dir.path().join("above").join("repo");
let dir = repo_dir.join("src/sub/path");
fs::create_dir_all(&dir)?;
init_repo(&repo_dir).unwrap();
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
truncation_length = 5
truncate_to_repo = true
repo_root_style = "bold red"
})
.path(dir)
.collect();
let expected = Some(format!(
"{}{}repo{} ",
Color::Cyan.bold().prefix(),
Color::Red.prefix(),
Color::Cyan.paint(convert_path_sep("/src/sub/path"))
2021-11-09 19:12:40 +00:00
));
assert_eq!(expected, actual);
tmp_dir.close()
}
#[test]
fn highlight_git_root_dir_config_change() -> io::Result<()> {
let (tmp_dir, _) = make_known_tempdir(Path::new("/tmp"))?;
let repo_dir = tmp_dir.path().join("above").join("repo");
let dir = repo_dir.join("src/sub/path");
fs::create_dir_all(&dir)?;
init_repo(&repo_dir).unwrap();
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
truncation_length = 5
truncation_symbol = "…/"
truncate_to_repo = false
repo_root_style = "green"
})
.path(dir)
.collect();
let expected = Some(format!(
"{}{}repo{} ",
Color::Cyan.bold().paint(convert_path_sep("…/above/")),
2021-11-09 19:12:40 +00:00
Color::Green.prefix(),
Color::Cyan.bold().paint(convert_path_sep("/src/sub/path"))
2021-11-09 19:12:40 +00:00
));
assert_eq!(expected, actual);
tmp_dir.close()
}
// sample for invalid unicode from https://doc.rust-lang.org/std/ffi/struct.OsStr.html#method.to_string_lossy
#[cfg(any(unix, target_os = "redox"))]
fn invalid_path() -> PathBuf {
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
// Here, the values 0x66 and 0x6f correspond to 'f' and 'o'
// respectively. The value 0x80 is a lone continuation byte, invalid
// in a UTF-8 sequence.
let source = [0x66, 0x6f, 0x80, 0x6f];
let os_str = OsStr::from_bytes(&source[..]);
PathBuf::from(os_str)
}
#[cfg(windows)]
fn invalid_path() -> PathBuf {
use std::ffi::OsString;
use std::os::windows::prelude::*;
// Here the values 0x0066 and 0x006f correspond to 'f' and 'o'
// respectively. The value 0xD800 is a lone surrogate half, invalid
// in a UTF-16 sequence.
let source = [0x0066, 0x006f, 0xD800, 0x006f];
let os_string = OsString::from_wide(&source[..]);
PathBuf::from(os_string)
}
#[test]
#[cfg(any(unix, windows, target_os = "redox"))]
fn invalid_unicode() {
let path = invalid_path();
let expected = Some(format!(
"{} ",
Color::Cyan.bold().paint(path.to_string_lossy())
));
let actual = ModuleRenderer::new("directory").path(path).collect();
assert_eq!(expected, actual);
}
#[test]
fn use_os_path_sep_false() -> io::Result<()> {
let (tmp_dir, name) = make_known_tempdir(home_dir().unwrap().as_path())?;
let dir = tmp_dir.path().join("starship");
fs::create_dir_all(&dir)?;
let actual = ModuleRenderer::new("directory")
.config(toml::toml! {
[directory]
use_os_path_sep = false
})
.path(dir)
.collect();
let expected = Some(format!(
"{} ",
Color::Cyan.bold().paint(format!("~/{}/starship", name))
));
assert_eq!(expected, actual);
tmp_dir.close()
}
2019-04-04 18:18:15 +00:00
}