Move CLI related commands to nu-cli (#8832)

# Description

Part of the larger cratification effort.

Moves all `reedline` or shell line editor specific commands to `nu-cli`.

## From `nu-cmd-lang`:
- `commandline`
- This shouldn't have moved there. Doesn't directly depend on reedline
but assumes parts in the engine state that are specific to the use of
reedline or a REPL

## From `nu-command`:
- `keybindings` and subcommands
  - `keybindings default`
  - `keybindings list`
  - `keybindings listen`
    - very `reedline` specific
- `history`
  - needs `reedline`
- `history session`

## internal use
Instead of having a separate `create_default_context()` that calls
`nu-command`'s `create_default_context()`, I added a `add_cli_context()`
that updates an `EngineState`


# User-Facing Changes

None

## Build time comparison

`cargo build --timings` from a `cargo clean --profile dev`

### total
main: 64 secs
this: 59 secs

### `nu-command` build time

branch | total| codegen | fraction  
---|---|---|---
main | 14.0s | 6.2s | (44%)
this | 12.5s | 5.5s | (44%)

`nu-cli` depends on `nu-command` at the moment.
Thus it is built during the code-gen phase of `nu-command` (on 16
virtual cores)

# Tests + Formatting

I removed the `test_example()` facilities for now as we had not run any
of the commands in an `Example` test and importing the right context for
those tests seemed more of a hassle than the duplicated
`test_examples()` implementations in `nu-cmd-lang` and `nu-command`
This commit is contained in:
Stefan Holderbach 2023-04-10 00:56:47 +02:00 committed by GitHub
parent 58b96fdede
commit 57510f2fd2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 49 additions and 33 deletions

3
Cargo.lock generated
View file

@ -2755,6 +2755,7 @@ dependencies = [
"rstest 0.17.0",
"sysinfo 0.28.2",
"thiserror",
"unicode-segmentation",
]
[[package]]
@ -2772,7 +2773,6 @@ dependencies = [
"nu-test-support",
"nu-utils",
"shadow-rs",
"unicode-segmentation",
]
[[package]]
@ -2861,7 +2861,6 @@ dependencies = [
"quickcheck_macros",
"rand 0.8.5",
"rayon",
"reedline",
"regex",
"roxmltree",
"rstest 0.17.0",

View file

@ -38,6 +38,7 @@ miette = { version = "5.7.0", features = ["fancy-no-backtrace"] }
percent-encoding = "2"
sysinfo = "0.28.2"
thiserror = "1.0.31"
unicode-segmentation = "1.10.0"
[features]
plugin = []

View file

@ -0,0 +1,33 @@
use nu_protocol::engine::{EngineState, StateWorkingSet};
use crate::commands::*;
pub fn add_cli_context(mut engine_state: EngineState) -> EngineState {
let delta = {
let mut working_set = StateWorkingSet::new(&engine_state);
macro_rules! bind_command {
( $( $command:expr ),* $(,)? ) => {
$( working_set.add_decl(Box::new($command)); )*
};
}
bind_command! {
Commandline,
History,
HistorySession,
Keybindings,
KeybindingsDefault,
KeybindingsList,
KeybindingsListen,
};
working_set.render()
};
if let Err(err) = engine_state.merge_delta(delta) {
eprintln!("Error creating default context: {err:?}");
}
engine_state
}

View file

@ -144,14 +144,3 @@ fn print_events_helper(event: Event) -> Result<Value, ShellError> {
Ok(record)
}
}
#[cfg(test)]
mod tests {
use crate::KeybindingsListen;
#[test]
fn examples_work_as_expected() {
use crate::test_examples;
test_examples(KeybindingsListen {})
}
}

View file

@ -1,9 +1,18 @@
mod commandline;
mod default_context;
mod history;
mod history_session;
mod keybindings;
mod keybindings_default;
mod keybindings_list;
mod keybindings_listen;
pub use commandline::Commandline;
pub use history::History;
pub use history_session::HistorySession;
pub use keybindings::Keybindings;
pub use keybindings_default::KeybindingsDefault;
pub use keybindings_list::KeybindingsList;
pub use keybindings_listen::KeybindingsListen;
pub use default_context::add_cli_context;

View file

@ -1,6 +1,7 @@
mod commands;
mod completions;
mod config_files;
mod eval_cmds;
mod eval_file;
mod menus;
mod nu_highlight;
@ -13,9 +14,10 @@ mod syntax_highlight;
mod util;
mod validation;
pub use commands::evaluate_commands;
pub use commands::add_cli_context;
pub use completions::{FileCompletion, NuCompleter};
pub use config_files::eval_config_contents;
pub use eval_cmds::evaluate_commands;
pub use eval_file::evaluate_file;
pub use menus::{DescriptionMenu, NuHelpCompleter};
pub use nu_command::util::get_init_cwd;

View file

@ -24,7 +24,6 @@ fancy-regex = "0.11.0"
itertools = "0.10.0"
log = "0.4.14"
shadow-rs = { version = "0.21.0", default-features = false }
unicode-segmentation = "1.10.0"
[build-dependencies]
shadow-rs = { version = "0.21.0", default-features = false }

View file

@ -1,7 +1,6 @@
mod alias;
mod break_;
mod collect;
mod commandline;
mod const_;
mod continue_;
mod def;
@ -43,7 +42,6 @@ mod while_;
pub use alias::Alias;
pub use break_::Break;
pub use collect::Collect;
pub use commandline::Commandline;
pub use const_::Const;
pub use continue_::Continue;
pub use def::Def;

View file

@ -19,7 +19,6 @@ pub fn create_default_context() -> EngineState {
Alias,
Break,
Collect,
Commandline,
Const,
Continue,
Def,

View file

@ -85,7 +85,6 @@ serde_yaml = "0.9.4"
sha2 = "0.10.0"
# Disable default features b/c the default features build Git (very slow to compile)
percent-encoding = "2.2.0"
reedline = { version = "0.18.0", features = ["bashisms", "sqlite"] }
rusqlite = { version = "0.28.0", features = ["bundled"], optional = true }
sqlparser = { version = "0.32.0", features = ["serde"], optional = true }
sysinfo = "0.28.2"

View file

@ -102,9 +102,7 @@ pub fn create_default_context() -> EngineState {
// Misc
bind_command! {
History,
Tutor,
HistorySession,
};
// Path
@ -256,12 +254,8 @@ pub fn create_default_context() -> EngineState {
AnsiLink,
Clear,
Du,
KeybindingsDefault,
Input,
KeybindingsListen,
Keybindings,
Kill,
KeybindingsList,
Sleep,
TermSize,
};

View file

@ -1,7 +1,3 @@
mod history;
mod history_session;
mod tutor;
pub use history::History;
pub use history_session::HistorySession;
pub use tutor::Tutor;

View file

@ -4,7 +4,6 @@ mod dir_info;
mod du;
mod input;
mod kill;
mod reedline_commands;
mod sleep;
mod term_size;
@ -14,6 +13,5 @@ pub use dir_info::{DirBuilder, DirInfo, FileInfo};
pub use du::Du;
pub use input::Input;
pub use kill::Kill;
pub use reedline_commands::{Keybindings, KeybindingsDefault, KeybindingsList, KeybindingsListen};
pub use sleep::Sleep;
pub use term_size::TermSize;

View file

@ -42,7 +42,7 @@ fn main() -> Result<()> {
// Get initial current working directory.
let init_cwd = get_init_cwd();
let mut engine_state = create_default_context();
let mut engine_state = nu_cli::add_cli_context(create_default_context());
// Custom additions
let delta = {

View file

@ -172,7 +172,7 @@ pub fn nu_repl() {
let cwd = std::env::current_dir().expect("Could not get current working directory.");
let source_lines = args();
let mut engine_state = create_default_context();
let mut engine_state = nu_cli::add_cli_context(create_default_context());
let mut stack = Stack::new();
stack.add_env_var("PWD".to_string(), Value::test_string(cwd.to_string_lossy()));