Fix editor config for reedline and config nu/env (#10535)

# Description
This merges @horasal 's changes from #10246 and #10269

Closes #10205
Closes #8714

Fixes the bug that editor paths with spaces are unusable

Closes #10210 
Closes #10269


# User-Facing Changes
You can now either pass a string with the name of the executable or a
list with the executable and any flags to
`$env.config.buffer_editor`/`$env.EDITOR`/`$env.VISUAL`

Both the external buffer editor of reedline (by default bound to
`Ctrl-o`) and the commands `config nu` and `config env` will respect
those variables in the following order:
1. `$env.config.buffer_editor`
2. `$env.EDITOR`
3. `$env.VISUAL`

Example:
```
$env.EDITOR = "nvim"                      # The system-wide EDITOR is neovim
$env.config.buffer_editor = ["vim" "-p2"] # Force vim to open two tabs (not particularly useful)
$env.config.buffer_editor = null          # Unset `buffer_editor` -> Uses `$env.EDITOR` ergo nvim
```
# Tests + Formatting
None

---------

Co-authored-by: Horasal <1991933+horasal@users.noreply.github.com>
This commit is contained in:
Stefan Holderbach
2023-09-29 16:36:03 +02:00
committed by GitHub
parent f2af12af2c
commit 80a183dde2
9 changed files with 108 additions and 71 deletions

View File

@ -5,7 +5,8 @@ use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
};
use super::utils::{gen_command, get_editor};
use super::utils::gen_command;
use nu_cmd_base::util::get_editor;
#[derive(Clone)]
pub struct ConfigEnv;

View File

@ -5,7 +5,8 @@ use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
};
use super::utils::{gen_command, get_editor};
use super::utils::gen_command;
use nu_cmd_base::util::get_editor;
#[derive(Clone)]
pub struct ConfigNu;

View File

@ -1,48 +1,10 @@
use std::collections::HashMap;
use std::path::PathBuf;
use nu_protocol::{
engine::{EngineState, Stack},
ShellError, Span, Spanned,
};
use nu_protocol::{Span, Spanned};
use crate::ExternalCommand;
pub(crate) fn get_editor(
engine_state: &EngineState,
stack: &mut Stack,
span: Span,
) -> Result<(String, Vec<String>), ShellError> {
let config = engine_state.get_config();
let env_vars = stack.get_env_vars(engine_state);
let editor = if !config.buffer_editor.is_empty() {
Ok(config.buffer_editor.clone())
} else if let Some(value) = env_vars.get("EDITOR") {
value.as_string()
} else if let Some(value) = env_vars.get("VISUAL") {
value.as_string()
} else {
Err(ShellError::GenericError(
"No editor configured".into(),
"Please specify one via environment variables $EDITOR or $VISUAL".into(),
Some(span),
Some(
"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)"
.into(),
),
vec![],
))
}?;
if let Some((a, b)) = editor.split_once(' ') {
Ok((
a.to_string(),
b.split(' ').map(|s| s.to_string()).collect::<Vec<String>>(),
))
} else {
Ok((editor, Vec::new()))
}
}
pub(crate) fn gen_command(
span: Span,
config_path: PathBuf,