2023-04-02 18:28:36 +02:00
|
|
|
use nu_protocol::{
|
2024-05-10 01:29:27 +02:00
|
|
|
engine::{EngineState, Stack},
|
2024-05-10 18:06:33 +02:00
|
|
|
Range, ShellError, Span, Value,
|
2023-04-02 18:28:36 +02:00
|
|
|
};
|
2024-04-06 16:04:56 +02:00
|
|
|
use std::{ops::Bound, path::PathBuf};
|
2023-03-20 05:05:22 +01:00
|
|
|
|
|
|
|
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 {
|
2024-05-10 18:06:33 +02:00
|
|
|
engine_state
|
|
|
|
.cwd(Some(stack))
|
|
|
|
.unwrap_or(crate::util::get_init_cwd())
|
2023-03-20 05:05:22 +01:00
|
|
|
}
|
2023-04-02 18:28:36 +02:00
|
|
|
|
|
|
|
type MakeRangeError = fn(&str, Span) -> ShellError;
|
|
|
|
|
|
|
|
pub fn process_range(range: &Range) -> Result<(isize, isize), MakeRangeError> {
|
2024-04-06 16:04:56 +02:00
|
|
|
match range {
|
|
|
|
Range::IntRange(range) => {
|
|
|
|
let start = range.start().try_into().unwrap_or(0);
|
|
|
|
let end = match range.end() {
|
|
|
|
Bound::Included(v) => v as isize,
|
|
|
|
Bound::Excluded(v) => (v - 1) as isize,
|
|
|
|
Bound::Unbounded => isize::MAX,
|
|
|
|
};
|
|
|
|
Ok((start, end))
|
2023-04-02 18:28:36 +02:00
|
|
|
}
|
2024-04-06 16:04:56 +02:00
|
|
|
Range::FloatRange(_) => Err(|msg, span| ShellError::TypeMismatch {
|
|
|
|
err_message: msg.to_string(),
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
}
|
2023-04-02 18:28:36 +02:00
|
|
|
}
|
2023-09-29 16:36:03 +02:00
|
|
|
|
|
|
|
const HELP_MSG: &str = "Nushell's config file can be found with the command: $nu.config-path. \
|
|
|
|
For more help: (https://nushell.sh/book/configuration.html#configurations-with-built-in-commands)";
|
|
|
|
|
|
|
|
fn get_editor_commandline(
|
|
|
|
value: &Value,
|
|
|
|
var_name: &str,
|
|
|
|
) -> Result<(String, Vec<String>), ShellError> {
|
|
|
|
match value {
|
|
|
|
Value::String { val, .. } if !val.is_empty() => Ok((val.to_string(), Vec::new())),
|
|
|
|
Value::List { vals, .. } if !vals.is_empty() => {
|
2024-02-17 19:14:16 +01:00
|
|
|
let mut editor_cmd = vals.iter().map(|l| l.coerce_string());
|
2023-09-29 16:36:03 +02:00
|
|
|
match editor_cmd.next().transpose()? {
|
|
|
|
Some(editor) if !editor.is_empty() => {
|
|
|
|
let params = editor_cmd.collect::<Result<_, ShellError>>()?;
|
|
|
|
Ok((editor, params))
|
|
|
|
}
|
2023-12-07 00:40:03 +01:00
|
|
|
_ => Err(ShellError::GenericError {
|
|
|
|
error: "Editor executable is missing".into(),
|
|
|
|
msg: "Set the first element to an executable".into(),
|
|
|
|
span: Some(value.span()),
|
|
|
|
help: Some(HELP_MSG.into()),
|
|
|
|
inner: vec![],
|
|
|
|
}),
|
2023-09-29 16:36:03 +02:00
|
|
|
}
|
|
|
|
}
|
2023-12-07 00:40:03 +01:00
|
|
|
Value::String { .. } | Value::List { .. } => Err(ShellError::GenericError {
|
|
|
|
error: format!("{var_name} should be a non-empty string or list<String>"),
|
|
|
|
msg: "Specify an executable here".into(),
|
|
|
|
span: Some(value.span()),
|
|
|
|
help: Some(HELP_MSG.into()),
|
|
|
|
inner: vec![],
|
|
|
|
}),
|
2023-09-29 16:36:03 +02:00
|
|
|
x => Err(ShellError::CantConvert {
|
|
|
|
to_type: "string or list<string>".into(),
|
|
|
|
from_type: x.get_type().to_string(),
|
|
|
|
span: value.span(),
|
|
|
|
help: None,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_editor(
|
|
|
|
engine_state: &EngineState,
|
2024-03-09 17:55:39 +01:00
|
|
|
stack: &Stack,
|
2023-09-29 16:36:03 +02:00
|
|
|
span: Span,
|
|
|
|
) -> Result<(String, Vec<String>), ShellError> {
|
|
|
|
let config = engine_state.get_config();
|
|
|
|
let env_vars = stack.get_env_vars(engine_state);
|
|
|
|
|
|
|
|
if let Ok(buff_editor) =
|
|
|
|
get_editor_commandline(&config.buffer_editor, "$env.config.buffer_editor")
|
|
|
|
{
|
|
|
|
Ok(buff_editor)
|
|
|
|
} else if let Some(value) = env_vars.get("EDITOR") {
|
|
|
|
get_editor_commandline(value, "$env.EDITOR")
|
|
|
|
} else if let Some(value) = env_vars.get("VISUAL") {
|
|
|
|
get_editor_commandline(value, "$env.VISUAL")
|
|
|
|
} else {
|
2023-12-07 00:40:03 +01:00
|
|
|
Err(ShellError::GenericError {
|
|
|
|
error: "No editor configured".into(),
|
|
|
|
msg:
|
|
|
|
"Please specify one via `$env.config.buffer_editor` or `$env.EDITOR`/`$env.VISUAL`"
|
|
|
|
.into(),
|
|
|
|
span: Some(span),
|
|
|
|
help: Some(HELP_MSG.into()),
|
|
|
|
inner: vec![],
|
|
|
|
})
|
2023-09-29 16:36:03 +02:00
|
|
|
}
|
|
|
|
}
|