Deliver a few fixes for explore command (#7310)

ref #6984

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
This commit is contained in:
Maxim Zhiburt 2022-12-01 21:14:56 +03:00 committed by GitHub
parent f71a45235a
commit 64a028cc76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 11 deletions

View File

@ -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<Value>,
) -> Result<Self::View> {
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)));

View File

@ -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));

View File

@ -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<PipelineData, ShellError> {
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)
}

View File

@ -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<Arc<AtomicBool>>;
pub type NuStyleTable = HashMap<String, NuStyle>;
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<Value>]) -> bool {
let has_single_value = data.len() == 1 && data[0].len() == 1;
has_single_value && !matches!(&data[0][0], Value::List { .. } | Value::Record { .. })
}

View File

@ -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);