2023-04-08 11:53:43 +00:00
|
|
|
use nu_protocol::report_error;
|
2023-04-02 16:28:36 +00:00
|
|
|
use nu_protocol::{
|
|
|
|
ast::RangeInclusion,
|
|
|
|
engine::{EngineState, Stack, StateWorkingSet},
|
2023-04-08 11:53:43 +00:00
|
|
|
Range, ShellError, Span, Value,
|
2023-04-02 16:28:36 +00:00
|
|
|
};
|
2023-03-20 04:05:22 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
pub fn get_init_cwd() -> PathBuf {
|
|
|
|
std::env::current_dir().unwrap_or_else(|_| {
|
|
|
|
std::env::var("PWD")
|
|
|
|
.map(Into::into)
|
|
|
|
.unwrap_or_else(|_| nu_path::home_dir().unwrap_or_default())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_guaranteed_cwd(engine_state: &EngineState, stack: &Stack) -> PathBuf {
|
|
|
|
nu_engine::env::current_dir(engine_state, stack).unwrap_or_else(|e| {
|
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
|
|
report_error(&working_set, &e);
|
|
|
|
crate::util::get_init_cwd()
|
|
|
|
})
|
|
|
|
}
|
2023-04-02 16:28:36 +00:00
|
|
|
|
|
|
|
type MakeRangeError = fn(&str, Span) -> ShellError;
|
|
|
|
|
|
|
|
pub fn process_range(range: &Range) -> Result<(isize, isize), MakeRangeError> {
|
|
|
|
let start = match &range.from {
|
|
|
|
Value::Int { val, .. } => *val as isize,
|
|
|
|
Value::Nothing { .. } => 0,
|
|
|
|
_ => {
|
|
|
|
return Err(|msg, span| ShellError::TypeMismatch {
|
|
|
|
err_message: msg.to_string(),
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let end = match &range.to {
|
|
|
|
Value::Int { val, .. } => {
|
|
|
|
if matches!(range.inclusion, RangeInclusion::Inclusive) {
|
|
|
|
*val as isize
|
|
|
|
} else {
|
|
|
|
*val as isize - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Value::Nothing { .. } => isize::max_value(),
|
|
|
|
_ => {
|
|
|
|
return Err(|msg, span| ShellError::TypeMismatch {
|
|
|
|
err_message: msg.to_string(),
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((start, end))
|
|
|
|
}
|