Skip to content

Commit

Permalink
Allow list for editor variable
Browse files Browse the repository at this point in the history
move `get_editor` to `cmd-base`
so `nu-cli` can use it
  • Loading branch information
Horasal authored and sholderbach committed Sep 28, 2023
1 parent 80220b7 commit 546fffe
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 41 deletions.
60 changes: 60 additions & 0 deletions crates/nu-cmd-base/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,63 @@ pub fn process_range(range: &Range) -> Result<(isize, isize), MakeRangeError> {

Ok((start, end))
}

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) -> 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() => {
let mut editor_cmd = vals.iter().map(|l| l.as_string());
match editor_cmd.next().transpose()? {
Some(editor) if !editor.is_empty() => {
let params = editor_cmd.collect::<Result<_, ShellError>>()?;
Ok((editor, params))
}
_ => Err(ShellError::GenericError(
"Editor's executable is missing".into(),
"Set the first element to an executable".into(),
Some(value.span()),
Some(HELP_MSG.into()),
vec![],
)),
}
}
Value::String { .. } | Value::List { .. } => Err(ShellError::GenericError(
"$EDITOR or $VISUAL should be a non-empty string or list<String>".into(),
"Specify an executable here".into(),
Some(value.span()),
Some(HELP_MSG.into()),
vec![],
)),
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,
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);
if !config.buffer_editor.is_empty() {
Ok((config.buffer_editor.clone(), Vec::new()))
} else if let Some(value) = env_vars.get("EDITOR").or_else(|| env_vars.get("VISUAL")) {
get_editor_commandline(value)
} else {
Err(ShellError::GenericError(
"No editor configured".into(),
"Please specify one via environment variables $EDITOR or $VISUAL".into(),
Some(span),
Some(HELP_MSG.into()),
vec![],
))
}
}
3 changes: 2 additions & 1 deletion crates/nu-command/src/env/config/config_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion crates/nu-command/src/env/config/config_nu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 1 addition & 39 deletions crates/nu-command/src/env/config/utils.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 546fffe

Please sign in to comment.