Auto-expand table based on terminal width (#9934)

# Description

This PR adds back the functionality to auto-expand tables based on the
terminal width, using the logic that if the terminal is over 100 columns
to expand.

This sets the default config value in both the Rust and the default
nushell config.

To do so, it also adds back the ability for hooks to be strings of code
and not just code blocks.

Fixed a couple tests: two which assumed that the builtin display hook
didn't use a table -e, and one that assumed a hook couldn't be a string.

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
This commit is contained in:
JT 2023-08-08 22:47:23 +12:00 committed by GitHub
parent 7694d09d14
commit 839010b89d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 16 deletions

View File

@ -87,6 +87,65 @@ pub fn eval_hook(
};
match value {
Value::String { val, span } => {
let (block, delta, vars) = {
let mut working_set = StateWorkingSet::new(engine_state);
let mut vars: Vec<(VarId, Value)> = vec![];
for (name, val) in arguments {
let var_id = working_set.add_variable(
name.as_bytes().to_vec(),
val.span()?,
Type::Any,
false,
);
vars.push((var_id, val));
}
let output = parse(&mut working_set, Some("hook"), val.as_bytes(), false);
if let Some(err) = working_set.parse_errors.first() {
report_error(&working_set, err);
return Err(ShellError::UnsupportedConfigValue(
"valid source code".into(),
"source code with syntax errors".into(),
*span,
));
}
(output, working_set.render(), vars)
};
engine_state.merge_delta(delta)?;
let input = if let Some(input) = input {
input
} else {
PipelineData::empty()
};
let var_ids: Vec<VarId> = vars
.into_iter()
.map(|(var_id, val)| {
stack.add_var(var_id, val);
var_id
})
.collect();
match eval_block(engine_state, stack, &block, input, false, false) {
Ok(pipeline_data) => {
output = pipeline_data;
}
Err(err) => {
report_error_new(engine_state, &err);
}
}
for var_id in var_ids.iter() {
stack.remove_var(*var_id);
}
}
Value::List { vals, .. } => {
for val in vals {
eval_hook(engine_state, stack, None, arguments.clone(), val)?;
@ -274,7 +333,7 @@ pub fn eval_hook(
}
other => {
return Err(ShellError::UnsupportedConfigValue(
"block, record, or list of records".into(),
"string, block, record, or list of commands".into(),
format!("{}", other.get_type()),
other.span()?,
));

View File

@ -42,7 +42,10 @@ impl Hooks {
pre_prompt: None,
pre_execution: None,
env_change: None,
display_output: None,
display_output: Some(Value::string(
"if (term size).columns >= 100 { table -e } else { table }",
Span::unknown(),
)),
command_not_found: None,
}
}

View File

@ -137,7 +137,6 @@ let light_theme = {
# carapace $spans.0 nushell $spans | from json
# }
# The default config record. This is where much of your global configuration is setup.
$env.config = {
show_banner: true # true or false to enable or disable the welcome banner at startup
@ -251,7 +250,7 @@ $env.config = {
env_change: {
PWD: [{|before, after| null }] # run if the PWD environment is different since the last repl input
}
display_output: { table } # run before the output of a command is drawn, example: `{ if (term size).columns >= 100 { table -e } else { table } }`
display_output: "if (term size).columns >= 100 { table -e } else { table }" # run to display the output of a pipeline
command_not_found: { null } # return an error message when a command is not found
}

View File

@ -138,12 +138,12 @@ fn help_not_present_in_extern() -> TestResult {
#[test]
fn override_table() -> TestResult {
run_test(r#"def table [] { "hi" }; table"#, "hi")
run_test(r#"def table [-e] { "hi" }; table"#, "hi")
}
#[test]
fn override_table_eval_file() {
let actual = nu!(r#"def table [] { "hi" }; table"#);
let actual = nu!(r#"def table [-e] { "hi" }; table"#);
assert_eq!(actual.out, "hi");
}

View File

@ -551,13 +551,3 @@ fn err_hook_parse_error() {
assert!(actual_repl.err.contains("unsupported_config_value"));
assert_eq!(actual_repl.out, "");
}
#[test]
fn err_hook_dont_allow_string() {
let inp = &[&pre_prompt_hook(r#"'def foo [] { "got foo!" }'"#), "foo"];
let actual_repl = nu!(nu_repl_code(inp));
assert!(actual_repl.out.is_empty());
assert!(actual_repl.err.contains("unsupported_config_value"));
}