mirror of
https://github.com/nushell/nushell
synced 2024-11-10 07:04:13 +00:00
Handle Windows drive paths in auto-cd (#6051)
* Handle Windows drive paths in auto-cd * Limit `use regex` to Windows * Use lazy_static for Windows drive path regex * try fixing Clippy on *nix
This commit is contained in:
parent
3d45f77692
commit
5cc6505512
3 changed files with 40 additions and 9 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -2607,6 +2607,7 @@ dependencies = [
|
|||
"crossterm",
|
||||
"fuzzy-matcher",
|
||||
"is_executable",
|
||||
"lazy_static",
|
||||
"log",
|
||||
"miette 5.1.0",
|
||||
"nu-ansi-term",
|
||||
|
@ -2619,6 +2620,7 @@ dependencies = [
|
|||
"nu-test-support",
|
||||
"nu-utils",
|
||||
"reedline",
|
||||
"regex",
|
||||
"sysinfo",
|
||||
"thiserror",
|
||||
]
|
||||
|
|
|
@ -24,9 +24,11 @@ miette = { version = "5.1.0", features = ["fancy"] }
|
|||
thiserror = "1.0.31"
|
||||
fuzzy-matcher = "0.3.7"
|
||||
|
||||
log = "0.4"
|
||||
is_executable = "1.0.1"
|
||||
chrono = "0.4.19"
|
||||
is_executable = "1.0.1"
|
||||
lazy_static = "1.4.0"
|
||||
log = "0.4"
|
||||
regex = "1.5.4"
|
||||
sysinfo = "0.24.1"
|
||||
|
||||
[features]
|
||||
|
|
|
@ -5,6 +5,7 @@ use crate::{
|
|||
util::{eval_source, get_guaranteed_cwd, report_error, report_error_new},
|
||||
NuHighlighter, NuValidator, NushellPrompt,
|
||||
};
|
||||
use lazy_static::lazy_static;
|
||||
use log::{info, trace};
|
||||
use miette::{IntoDiagnostic, Result};
|
||||
use nu_color_config::get_color_config;
|
||||
|
@ -16,6 +17,7 @@ use nu_protocol::{
|
|||
BlockId, HistoryFileFormat, PipelineData, PositionalArg, ShellError, Span, Type, Value, VarId,
|
||||
};
|
||||
use reedline::{DefaultHinter, Emacs, SqliteBackedHistory, Vi};
|
||||
use regex::Regex;
|
||||
use std::io::{self, Write};
|
||||
use std::{sync::atomic::Ordering, time::Instant};
|
||||
use sysinfo::SystemExt;
|
||||
|
@ -335,13 +337,7 @@ pub fn evaluate_repl(
|
|||
|
||||
let orig = s.clone();
|
||||
|
||||
if (orig.starts_with('.')
|
||||
|| orig.starts_with('~')
|
||||
|| orig.starts_with('/')
|
||||
|| orig.starts_with('\\'))
|
||||
&& path.is_dir()
|
||||
&& tokens.0.len() == 1
|
||||
{
|
||||
if looks_like_path(&orig) && path.is_dir() && tokens.0.len() == 1 {
|
||||
// We have an auto-cd
|
||||
let (path, span) = {
|
||||
if !path.exists() {
|
||||
|
@ -756,3 +752,34 @@ fn run_ansi_sequence(seq: &str) -> Result<(), ShellError> {
|
|||
)
|
||||
})
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
// Absolute paths with a drive letter, like 'C:', 'D:\', 'E:\foo'
|
||||
static ref DRIVE_PATH_REGEX: Regex =
|
||||
Regex::new(r"^[a-zA-Z]:[/\\]?").expect("Internal error: regex creation");
|
||||
}
|
||||
|
||||
// A best-effort "does this string look kinda like a path?" function to determine whether to auto-cd
|
||||
fn looks_like_path(orig: &str) -> bool {
|
||||
#[cfg(windows)]
|
||||
{
|
||||
if DRIVE_PATH_REGEX.is_match(orig) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
orig.starts_with('.')
|
||||
|| orig.starts_with('~')
|
||||
|| orig.starts_with('/')
|
||||
|| orig.starts_with('\\')
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn looks_like_path_windows_drive_path_works() {
|
||||
let on_windows = cfg!(windows);
|
||||
assert_eq!(looks_like_path("C:"), on_windows);
|
||||
assert_eq!(looks_like_path("D:\\"), on_windows);
|
||||
assert_eq!(looks_like_path("E:/"), on_windows);
|
||||
assert_eq!(looks_like_path("F:\\some_dir"), on_windows);
|
||||
assert_eq!(looks_like_path("G:/some_dir"), on_windows);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue