Skip to content

Commit

Permalink
Move CLI related commands to nu-cli (nushell#8832)
Browse files Browse the repository at this point in the history
# 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`
  • Loading branch information
sholderbach committed Apr 9, 2023
1 parent 58b96fd commit 57510f2
Show file tree
Hide file tree
Showing 22 changed files with 49 additions and 33 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/nu-cli/Cargo.toml
Expand Up @@ -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 = []
File renamed without changes.
33 changes: 33 additions & 0 deletions crates/nu-cli/src/commands/default_context.rs
@@ -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
}
File renamed without changes.
File renamed without changes.
Expand Up @@ -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 {})
}
}
@@ -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;
File renamed without changes.
4 changes: 3 additions & 1 deletion crates/nu-cli/src/lib.rs
@@ -1,6 +1,7 @@
mod commands;
mod completions;
mod config_files;
mod eval_cmds;
mod eval_file;
mod menus;
mod nu_highlight;
Expand All @@ -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;
Expand Down
1 change: 0 additions & 1 deletion crates/nu-cmd-lang/Cargo.toml
Expand Up @@ -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 }
Expand Down
2 changes: 0 additions & 2 deletions crates/nu-cmd-lang/src/core_commands/mod.rs
@@ -1,7 +1,6 @@
mod alias;
mod break_;
mod collect;
mod commandline;
mod const_;
mod continue_;
mod def;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion crates/nu-cmd-lang/src/default_context.rs
Expand Up @@ -19,7 +19,6 @@ pub fn create_default_context() -> EngineState {
Alias,
Break,
Collect,
Commandline,
Const,
Continue,
Def,
Expand Down
1 change: 0 additions & 1 deletion crates/nu-command/Cargo.toml
Expand Up @@ -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"
Expand Down
6 changes: 0 additions & 6 deletions crates/nu-command/src/default_context.rs
Expand Up @@ -102,9 +102,7 @@ pub fn create_default_context() -> EngineState {

// Misc
bind_command! {
History,
Tutor,
HistorySession,
};

// Path
Expand Down Expand Up @@ -256,12 +254,8 @@ pub fn create_default_context() -> EngineState {
AnsiLink,
Clear,
Du,
KeybindingsDefault,
Input,
KeybindingsListen,
Keybindings,
Kill,
KeybindingsList,
Sleep,
TermSize,
};
Expand Down
4 changes: 0 additions & 4 deletions crates/nu-command/src/misc/mod.rs
@@ -1,7 +1,3 @@
mod history;
mod history_session;
mod tutor;

pub use history::History;
pub use history_session::HistorySession;
pub use tutor::Tutor;
2 changes: 0 additions & 2 deletions crates/nu-command/src/platform/mod.rs
Expand Up @@ -4,7 +4,6 @@ mod dir_info;
mod du;
mod input;
mod kill;
mod reedline_commands;
mod sleep;
mod term_size;

Expand All @@ -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;
2 changes: 1 addition & 1 deletion src/main.rs
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion src/test_bins.rs
Expand Up @@ -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()));
Expand Down

0 comments on commit 57510f2

Please sign in to comment.