mirror of
https://github.com/nushell/nushell.git
synced 2025-06-03 16:45:41 +02:00
Allow path relative-to to use parent directory symbols
This commit is contained in:
parent
3bedbd0669
commit
fd2430062f
@ -144,14 +144,41 @@ path."#
|
|||||||
fn relative_to(path: &Path, span: Span, args: &Arguments) -> Value {
|
fn relative_to(path: &Path, span: Span, args: &Arguments) -> Value {
|
||||||
let lhs = expand_to_real_path(path);
|
let lhs = expand_to_real_path(path);
|
||||||
let rhs = expand_to_real_path(&args.path.item);
|
let rhs = expand_to_real_path(&args.path.item);
|
||||||
match lhs.strip_prefix(&rhs) {
|
|
||||||
Ok(p) => Value::string(p.to_string_lossy(), span),
|
match (lhs.to_str(), rhs.to_str()) {
|
||||||
Err(e) => Value::error(
|
(Some(child_str), Some(parent_str)) => {
|
||||||
ShellError::CantConvert {
|
let common: String = child_str
|
||||||
to_type: e.to_string(),
|
.split("/")
|
||||||
from_type: "string".into(),
|
.zip(parent_str.split("/"))
|
||||||
|
.take_while(|(x, y)| x == y)
|
||||||
|
.map(|(x, _)| x.to_string() + "/")
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if let Some(x) = child_str.strip_prefix(&common) {
|
||||||
|
if parent_str == common.trim_end_matches("/") {
|
||||||
|
return Value::string(x.to_string(), span);
|
||||||
|
}
|
||||||
|
if let Some(remaining_parent) = parent_str.strip_prefix(&common) {
|
||||||
|
let mut path: String = remaining_parent.split("/").map(|_| "../").collect();
|
||||||
|
path.push_str(x);
|
||||||
|
return Value::string(path.to_string(), span);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Value::error(
|
||||||
|
ShellError::IncorrectValue {
|
||||||
|
msg: "Either the input or the argument path is incorrect".into(),
|
||||||
|
val_span: span,
|
||||||
|
call_span: span,
|
||||||
|
},
|
||||||
span,
|
span,
|
||||||
help: None,
|
)
|
||||||
|
}
|
||||||
|
_ => Value::error(
|
||||||
|
ShellError::IncorrectValue {
|
||||||
|
msg: "Either the input or the argument path is incorrect".into(),
|
||||||
|
val_span: span,
|
||||||
|
call_span: span,
|
||||||
},
|
},
|
||||||
span,
|
span,
|
||||||
),
|
),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user