fix: fix commandline when called with no arguments (#8207)

# Description

This fixes the `commandline` command when it's run with no arguments, so
it outputs the command being run. New line characters are included.

This allows for:

- [A way to get current command inside pre_execution hook · Issue #6264
· nushell/nushell](https://github.com/nushell/nushell/issues/6264)
- The possibility of *Atuin* to work work *Nushell*. *Atuin* hooks need
to know the current repl input before it is run.

# User-Facing 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` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

# 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:
Steven Xu
2023-03-17 09:45:35 +11:00
committed by GitHub
parent 0903a891e4
commit b2a557d4ed
9 changed files with 244 additions and 57 deletions

View File

@ -209,6 +209,12 @@ pub fn nu_repl() {
// Check for pre_execution hook
let config = engine_state.get_config();
*engine_state
.repl_buffer_state
.lock()
.expect("repl buffer state mutex") = line.to_string();
if let Some(hook) = config.hooks.pre_execution.clone() {
if let Err(err) = eval_hook(&mut engine_state, &mut stack, None, vec![], &hook) {
outcome_err(&engine_state, &err);

View File

@ -1,5 +1,6 @@
mod test_bits;
mod test_cell_path;
mod test_commandline;
mod test_conditionals;
mod test_config_path;
mod test_converters;

View File

@ -0,0 +1,109 @@
use crate::tests::{fail_test, run_test, TestResult};
#[test]
fn commandline_test_get_empty() -> TestResult {
run_test("commandline", "")
}
#[test]
fn commandline_test_append() -> TestResult {
run_test(
"commandline --replace '0👩👩2'\n\
commandline --cursor '2'\n\
commandline --append 'ab'\n\
commandline\n\
commandline --cursor",
"0👩👩2ab\n\
2",
)
}
#[test]
fn commandline_test_insert() -> TestResult {
run_test(
"commandline --replace '0👩👩2'\n\
commandline --cursor '2'\n\
commandline --insert 'ab'\n\
commandline\n\
commandline --cursor",
"0👩👩ab2\n\
4",
)
}
#[test]
fn commandline_test_replace() -> TestResult {
run_test(
"commandline --replace '0👩👩2'\n\
commandline --replace 'ab'\n\
commandline\n\
commandline --cursor",
"ab\n\
2",
)
}
#[test]
fn commandline_test_cursor() -> TestResult {
run_test(
"commandline --replace '0👩👩2'\n\
commandline --cursor '1' \n\
commandline --insert 'x'\n\
commandline",
"0x👩👩2",
)?;
run_test(
"commandline --replace '0👩👩2'\n\
commandline --cursor '2' \n\
commandline --insert 'x'\n\
commandline",
"0👩👩x2",
)
}
#[test]
fn commandline_test_cursor_show_pos() -> TestResult {
run_test(
"commandline --replace '0👩👩2'\n\
commandline --cursor '1' \n\
commandline --cursor",
"1",
)?;
run_test(
"commandline --replace '0👩👩2'\n\
commandline --cursor '2' \n\
commandline --cursor",
"2",
)
}
#[test]
fn commandline_test_cursor_too_small() -> TestResult {
run_test(
"commandline --replace '123456'\n\
commandline --cursor '-1' \n\
commandline --insert '0'\n\
commandline",
"0123456",
)
}
#[test]
fn commandline_test_cursor_too_large() -> TestResult {
run_test(
"commandline --replace '123456'\n\
commandline --cursor '10' \n\
commandline --insert '0'\n\
commandline",
"1234560",
)
}
#[test]
fn commandline_test_cursor_invalid() -> TestResult {
fail_test(
"commandline --replace '123456'\n\
commandline --cursor 'abc'",
r#"string "abc" does not represent a valid integer"#,
)
}