From 64a028cc7675d621bc428267c467f9b540a00b01 Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Thu, 1 Dec 2022 21:14:56 +0300 Subject: [PATCH] Deliver a few fixes for `explore` command (#7310) ref #6984 Signed-off-by: Maxim Zhiburt --- crates/nu-explore/src/commands/nu.rs | 13 +++++++++---- crates/nu-explore/src/lib.rs | 6 ++---- crates/nu-explore/src/nu_common/command.rs | 14 +++++++++++++- crates/nu-explore/src/nu_common/mod.rs | 8 +++++++- crates/nu-explore/src/views/interative.rs | 7 ++++++- 5 files changed, 37 insertions(+), 11 deletions(-) diff --git a/crates/nu-explore/src/commands/nu.rs b/crates/nu-explore/src/commands/nu.rs index 18a605c6e..f33c1977b 100644 --- a/crates/nu-explore/src/commands/nu.rs +++ b/crates/nu-explore/src/commands/nu.rs @@ -6,7 +6,7 @@ use nu_protocol::{ }; use crate::{ - nu_common::{collect_pipeline, run_nu_command}, + nu_common::{collect_pipeline, has_simple_value, is_ignored_command, run_nu_command}, pager::TableConfig, views::{Preview, RecordView, View}, }; @@ -75,6 +75,13 @@ impl ViewCommand for NuCmd { stack: &mut Stack, value: Option, ) -> Result { + if is_ignored_command(&self.command) { + return Err(io::Error::new( + io::ErrorKind::Other, + "The command is ignored", + )); + } + let value = value.unwrap_or_default(); let pipeline = PipelineData::Value(value, None); @@ -83,9 +90,7 @@ impl ViewCommand for NuCmd { let (columns, values) = collect_pipeline(pipeline); - let has_single_value = values.len() == 1 && values[0].len() == 1; - let is_simple_type = !matches!(&values[0][0], Value::List { .. } | Value::Record { .. }); - if has_single_value && is_simple_type { + if has_simple_value(&values) { let config = &engine_state.config; let text = values[0][0].into_abbreviated_string(config); return Ok(NuView::Preview(Preview::new(&text))); diff --git a/crates/nu-explore/src/lib.rs b/crates/nu-explore/src/lib.rs index cb5bb88fe..044be0db6 100644 --- a/crates/nu-explore/src/lib.rs +++ b/crates/nu-explore/src/lib.rs @@ -7,7 +7,7 @@ mod views; use std::io; -use nu_common::{collect_pipeline, CtrlC}; +use nu_common::{collect_pipeline, has_simple_value, CtrlC}; use nu_protocol::{ engine::{EngineState, Stack}, PipelineData, Value, @@ -38,9 +38,7 @@ pub fn run_pager( return p.run(engine_state, stack, ctrlc, view, commands); } - let has_single_value = data.len() == 1 && data[0].len() == 1; - let is_simple_type = !matches!(&data[0][0], Value::List { .. } | Value::Record { .. }); - if has_single_value && is_simple_type { + if has_simple_value(&data) { let text = data[0][0].into_abbreviated_string(view_cfg.config); let view = Some(Page::new(Preview::new(&text), true)); diff --git a/crates/nu-explore/src/nu_common/command.rs b/crates/nu-explore/src/nu_common/command.rs index 527fe89d9..810d53eb0 100644 --- a/crates/nu-explore/src/nu_common/command.rs +++ b/crates/nu-explore/src/nu_common/command.rs @@ -15,6 +15,10 @@ pub fn run_nu_command( eval_source2(&engine_state, stack, cmd.as_bytes(), "", current) } +pub fn is_ignored_command(command: &str) -> bool { + command.starts_with("clear") +} + fn eval_source2( engine_state: &EngineState, stack: &mut Stack, @@ -22,7 +26,7 @@ fn eval_source2( fname: &str, input: PipelineData, ) -> Result { - let (block, _) = { + let (mut block, _) = { let mut working_set = StateWorkingSet::new(engine_state); let (output, err) = parse( &mut working_set, @@ -38,5 +42,13 @@ fn eval_source2( (output, working_set.render()) }; + // eval_block outputs all expressions expept the last to STDOUT; + // we don't wont that. + // + // So we LITERALLY ignore all expressions except the LAST. + if block.len() > 1 { + block.pipelines.drain(..block.pipelines.len() - 1); + } + eval_block(engine_state, stack, &block, input, false, false) } diff --git a/crates/nu-explore/src/nu_common/mod.rs b/crates/nu-explore/src/nu_common/mod.rs index ba6a3d3c0..4c0651079 100644 --- a/crates/nu-explore/src/nu_common/mod.rs +++ b/crates/nu-explore/src/nu_common/mod.rs @@ -7,6 +7,7 @@ use std::{ sync::{atomic::AtomicBool, Arc}, }; +use nu_protocol::Value; use nu_table::TextStyle; pub use nu_ansi_term::{Color as NuColor, Style as NuStyle}; @@ -16,6 +17,11 @@ pub type NuText = (String, TextStyle); pub type CtrlC = Option>; pub type NuStyleTable = HashMap; -pub use command::run_nu_command; +pub use command::{is_ignored_command, run_nu_command}; pub use table::try_build_table; pub use value::{collect_input, collect_pipeline}; + +pub fn has_simple_value(data: &[Vec]) -> bool { + let has_single_value = data.len() == 1 && data[0].len() == 1; + has_single_value && !matches!(&data[0][0], Value::List { .. } | Value::Record { .. }) +} diff --git a/crates/nu-explore/src/views/interative.rs b/crates/nu-explore/src/views/interative.rs index 17091988f..80dd3f638 100644 --- a/crates/nu-explore/src/views/interative.rs +++ b/crates/nu-explore/src/views/interative.rs @@ -12,7 +12,7 @@ use tui::{ }; use crate::{ - nu_common::{collect_pipeline, run_nu_command}, + nu_common::{collect_pipeline, is_ignored_command, run_nu_command}, pager::{Frame, Report, TableConfig, Transition, ViewConfig, ViewInfo}, }; @@ -176,6 +176,11 @@ impl View for InteractiveView<'_> { Some(Transition::Ok) } KeyCode::Enter => { + if is_ignored_command(&self.command) { + info.report = Some(Report::error(String::from("The command is ignored"))); + return Some(Transition::Ok); + } + let pipeline = PipelineData::Value(self.input.clone(), None); let pipeline = run_nu_command(engine_state, stack, &self.command, pipeline);