diff --git a/crates/nu-cli/src/cli.rs b/crates/nu-cli/src/cli.rs index 272a39046..f20f77445 100644 --- a/crates/nu-cli/src/cli.rs +++ b/crates/nu-cli/src/cli.rs @@ -9,7 +9,7 @@ use crate::path::canonicalize; use crate::prelude::*; use crate::EnvironmentSyncer; use futures_codec::FramedRead; -use nu_errors::ShellError; +use nu_errors::{ProximateShellError, ShellDiagnostic, ShellError}; use nu_protocol::hir::{ClassifiedCommand, Expression, InternalCommand, Literal, NamedArguments}; use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value}; @@ -591,7 +591,20 @@ pub async fn cli( rl.set_helper(Some(crate::shell::Helper::new(context.clone()))); - let config = config::config(Tag::unknown())?; + let config = match config::config(Tag::unknown()) { + Ok(config) => config, + Err(e) => { + eprintln!("Config could not be loaded."); + if let ShellError { + error: ProximateShellError::Diagnostic(ShellDiagnostic { diagnostic }), + .. + } = e + { + eprintln!("{}", diagnostic.message); + } + IndexMap::new() + } + }; let use_starship = match config.get("use_starship") { Some(b) => match b.as_bool() { @@ -657,46 +670,58 @@ pub async fn cli( } else if let Some(prompt) = config.get("prompt") { let prompt_line = prompt.as_string()?; - if let Ok(result) = nu_parser::lite_parse(&prompt_line, 0).map_err(ShellError::from) - { - let mut prompt_block = nu_parser::classify_block(&result, context.registry()); + match nu_parser::lite_parse(&prompt_line, 0).map_err(ShellError::from) { + Ok(result) => { + let mut prompt_block = + nu_parser::classify_block(&result, context.registry()); - let env = context.get_env(); + let env = context.get_env(); - prompt_block.block.expand_it_usage(); + prompt_block.block.expand_it_usage(); - match run_block( - &prompt_block.block, - &mut context, - InputStream::empty(), - &Value::nothing(), - &IndexMap::new(), - &env, - ) - .await - { - Ok(result) => { - context.clear_errors(); - - match result.collect_string(Tag::unknown()).await { - Ok(string_result) => string_result.item, - Err(_) => { + match run_block( + &prompt_block.block, + &mut context, + InputStream::empty(), + &Value::nothing(), + &IndexMap::new(), + &env, + ) + .await + { + Ok(result) => match result.collect_string(Tag::unknown()).await { + Ok(string_result) => { + let errors = context.get_errors(); context.maybe_print_errors(Text::from(prompt_line)); context.clear_errors(); - "Error running prompt> ".to_string() + if !errors.is_empty() { + "> ".to_string() + } else { + string_result.item + } } + Err(e) => { + crate::cli::print_err(e, &Text::from(prompt_line)); + context.clear_errors(); + + "> ".to_string() + } + }, + Err(e) => { + crate::cli::print_err(e, &Text::from(prompt_line)); + context.clear_errors(); + + "> ".to_string() } } - Err(_) => { - context.maybe_print_errors(Text::from(prompt_line)); - context.clear_errors(); - - "Error running prompt> ".to_string() - } } - } else { - "Error parsing prompt> ".to_string() + Err(e) => { + crate::cli::print_err(e, &Text::from(prompt_line)); + context.clear_errors(); + + "> ".to_string() + } } } else { format!( diff --git a/crates/nu-cli/src/commands/do_.rs b/crates/nu-cli/src/commands/do_.rs index 615ce95f1..fa4519ef3 100644 --- a/crates/nu-cli/src/commands/do_.rs +++ b/crates/nu-cli/src/commands/do_.rs @@ -72,7 +72,7 @@ async fn do_( }, input, ) = raw_args.process(®istry).await?; - block.set_is_last(!is_last); + block.set_is_last(is_last); let result = run_block( &block, diff --git a/crates/nu-cli/src/commands/run_alias.rs b/crates/nu-cli/src/commands/run_alias.rs index 3d0696ee6..6b045f8cf 100644 --- a/crates/nu-cli/src/commands/run_alias.rs +++ b/crates/nu-cli/src/commands/run_alias.rs @@ -41,7 +41,7 @@ impl WholeStreamCommand for AliasCommand { let call_info = args.call_info.clone(); let registry = registry.clone(); let mut block = self.block.clone(); - block.set_is_last(!call_info.args.is_last); + block.set_is_last(call_info.args.is_last); let alias_command = self.clone(); let mut context = Context::from_args(&args, ®istry); diff --git a/crates/nu-cli/src/stream/input.rs b/crates/nu-cli/src/stream/input.rs index 385992ac0..479ac81a9 100644 --- a/crates/nu-cli/src/stream/input.rs +++ b/crates/nu-cli/src/stream/input.rs @@ -74,6 +74,12 @@ impl InputStream { value_tag = value_t; bytes.extend_from_slice(&b); } + Some(Value { + value: UntaggedValue::Primitive(Primitive::Nothing), + tag: value_t, + }) => { + value_tag = value_t; + } Some(Value { tag: value_tag, value, diff --git a/crates/nu-errors/src/lib.rs b/crates/nu-errors/src/lib.rs index 0714ed1d8..792180911 100644 --- a/crates/nu-errors/src/lib.rs +++ b/crates/nu-errors/src/lib.rs @@ -177,8 +177,8 @@ impl PrettyDebug for ArgumentError { /// creating a cause chain. #[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Clone, Serialize, Deserialize, Hash)] pub struct ShellError { - error: ProximateShellError, - cause: Option>, + pub error: ProximateShellError, + pub cause: Option>, } /// `PrettyDebug` is for internal debugging. For user-facing debugging, [into_diagnostic](ShellError::into_diagnostic) @@ -773,7 +773,7 @@ impl HasFallibleSpan for ProximateShellError { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ShellDiagnostic { - pub(crate) diagnostic: Diagnostic, + pub diagnostic: Diagnostic, } impl std::hash::Hash for ShellDiagnostic {