Add command_prelude module (#12291)

# Description
When implementing a `Command`, one must also import all the types
present in the function signatures for `Command`. This makes it so that
we often import the same set of types in each command implementation
file. E.g., something like this:
```rust
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
    record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
    ShellError, Signature, Span, Type, Value,
};
```

This PR adds the `nu_engine::command_prelude` module which contains the
necessary and commonly used types to implement a `Command`:
```rust
// command_prelude.rs
pub use crate::CallExt;
pub use nu_protocol::{
    ast::{Call, CellPath},
    engine::{Command, EngineState, Stack},
    record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, IntoSpanned,
    PipelineData, Record, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
};
```

This should reduce the boilerplate needed to implement a command and
also gives us a place to track the breadth of the `Command` API. I tried
to be conservative with what went into the prelude modules, since it
might be hard/annoying to remove items from the prelude in the future.
Let me know if something should be included or excluded.
This commit is contained in:
Ian Manske
2024-03-26 21:17:30 +00:00
committed by GitHub
parent f8c1e3ac61
commit c747ec75c9
660 changed files with 1634 additions and 4332 deletions

View File

@ -1,17 +1,14 @@
use std::{io::Result, vec};
use super::{HelpManual, Shortcode, ViewCommand};
use crate::{
nu_common::{self, collect_input},
views::Preview,
};
use nu_color_config::StyleComputer;
use nu_protocol::{
engine::{EngineState, Stack},
Value,
};
use crate::{
nu_common::{self, collect_input},
views::Preview,
};
use super::{HelpManual, Shortcode, ViewCommand};
use std::{io::Result, vec};
#[derive(Default, Clone)]
pub struct ExpandCmd;

View File

@ -1,21 +1,20 @@
use std::collections::HashMap;
use std::io::{self, Result};
use super::{HelpExample, HelpManual, ViewCommand};
use crate::{
nu_common::{collect_input, NuSpan},
pager::{Frame, Transition, ViewInfo},
views::{Layout, Preview, RecordView, View, ViewConfig},
};
use crossterm::event::KeyEvent;
use nu_protocol::{
engine::{EngineState, Stack},
record, Value,
};
use ratatui::layout::Rect;
use crate::{
nu_common::{collect_input, NuSpan},
pager::{Frame, Transition, ViewInfo},
views::{Layout, Preview, RecordView, View, ViewConfig},
use std::{
collections::HashMap,
io::{self, Result},
};
use super::{HelpExample, HelpManual, ViewCommand};
#[derive(Debug, Default, Clone)]
pub struct HelpCmd {
input_command: String,

View File

@ -1,10 +1,8 @@
use super::pager::{Pager, Transition};
use nu_protocol::{
engine::{EngineState, Stack},
Value,
};
use super::pager::{Pager, Transition};
use std::{borrow::Cow, io::Result};
mod expand;

View File

@ -1,18 +1,15 @@
use std::io::{self, Result};
use nu_protocol::{
engine::{EngineState, Stack},
PipelineData, Value,
};
use ratatui::layout::Rect;
use super::{HelpExample, HelpManual, ViewCommand};
use crate::{
nu_common::{collect_pipeline, has_simple_value, run_command_with_value},
pager::Frame,
views::{Layout, Orientation, Preview, RecordView, View, ViewConfig},
};
use super::{HelpExample, HelpManual, ViewCommand};
use nu_protocol::{
engine::{EngineState, Stack},
PipelineData, Value,
};
use ratatui::layout::Rect;
use std::io::{self, Result};
#[derive(Debug, Default, Clone)]
pub struct NuCmd {

View File

@ -1,13 +1,10 @@
use std::io::Result;
use super::{HelpManual, SimpleCommand};
use crate::pager::{Pager, Transition};
use nu_protocol::{
engine::{EngineState, Stack},
Value,
};
use crate::pager::{Pager, Transition};
use super::{HelpManual, SimpleCommand};
use std::io::Result;
#[derive(Default, Clone)]
pub struct QuitCmd;

View File

@ -1,20 +1,17 @@
use std::io::Result;
use super::{
default_color_list, default_int_list, ConfigOption, HelpExample, HelpManual, Shortcode,
ViewCommand,
};
use crate::{
nu_common::collect_input,
views::{Orientation, RecordView},
};
use nu_ansi_term::Style;
use nu_protocol::{
engine::{EngineState, Stack},
Value,
};
use crate::{
nu_common::collect_input,
views::{Orientation, RecordView},
};
use super::{
default_color_list, default_int_list, ConfigOption, HelpExample, HelpManual, Shortcode,
ViewCommand,
};
use std::io::Result;
#[derive(Debug, Default, Clone)]
pub struct TableCmd {

View File

@ -1,13 +1,10 @@
use std::io::{Error, ErrorKind, Result};
use super::{default_color_list, ConfigOption, HelpExample, HelpManual, Shortcode, ViewCommand};
use crate::views::InteractiveView;
use nu_protocol::{
engine::{EngineState, Stack},
Value,
};
use crate::views::InteractiveView;
use super::{default_color_list, ConfigOption, HelpExample, HelpManual, Shortcode, ViewCommand};
use std::io::{Error, ErrorKind, Result};
#[derive(Debug, Default, Clone)]
pub struct TryCmd {

View File

@ -5,12 +5,8 @@ use crate::{
};
use nu_ansi_term::{Color, Style};
use nu_color_config::{get_color_map, StyleComputer};
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use nu_engine::command_prelude::*;
use std::collections::HashMap;
/// A `less` like program to render a [`Value`] as a table.

View File

@ -9,21 +9,18 @@ mod views;
pub use default_context::add_explore_context;
pub use explore::Explore;
use std::io;
use commands::{ExpandCmd, HelpCmd, HelpManual, NuCmd, QuitCmd, TableCmd, TryCmd};
use nu_common::{collect_pipeline, has_simple_value, CtrlC};
use nu_protocol::{
engine::{EngineState, Stack},
PipelineData, Value,
};
use pager::{Page, Pager};
use pager::{Page, Pager, PagerConfig, StyleConfig};
use registry::{Command, CommandRegistry};
use std::io;
use terminal_size::{Height, Width};
use views::{BinaryView, InformationView, Orientation, Preview, RecordView};
use pager::{PagerConfig, StyleConfig};
mod util {
pub use super::nu_common::{create_lscolors, create_map, map_into_value};
}

View File

@ -1,12 +1,11 @@
use std::sync::Arc;
use nu_engine::eval_block;
use nu_parser::parse;
use nu_protocol::debugger::WithoutDebug;
use nu_protocol::{
debugger::WithoutDebug,
engine::{EngineState, Redirection, Stack, StateWorkingSet},
IoStream, PipelineData, ShellError, Value,
};
use std::sync::Arc;
pub fn run_command_with_value(
command: &str,

View File

@ -1,12 +1,10 @@
use std::fs::symlink_metadata;
use super::NuText;
use lscolors::LsColors;
use nu_ansi_term::{Color, Style};
use nu_engine::env_to_string;
use nu_protocol::engine::{EngineState, Stack};
use nu_utils::get_ls_colors;
use super::NuText;
use std::fs::symlink_metadata;
pub fn create_lscolors(engine_state: &EngineState, stack: &Stack) -> LsColors {
let colors = stack

View File

@ -4,10 +4,9 @@ mod string;
mod table;
mod value;
use std::sync::{atomic::AtomicBool, Arc};
use nu_color_config::TextStyle;
use nu_protocol::Value;
use std::sync::{atomic::AtomicBool, Arc};
pub use nu_ansi_term::{Color as NuColor, Style as NuStyle};
pub use nu_protocol::{Config as NuConfig, Span as NuSpan};

View File

@ -1,13 +1,11 @@
use crate::nu_common::NuConfig;
use nu_color_config::StyleComputer;
use nu_protocol::{Record, Span, Value};
use nu_table::{
common::{nu_value_to_string, nu_value_to_string_clean},
ExpandedTable, TableOpts,
};
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use crate::nu_common::NuConfig;
use std::sync::{atomic::AtomicBool, Arc};
pub fn try_build_table(
ctrlc: Option<Arc<AtomicBool>>,

View File

@ -1,9 +1,7 @@
use std::collections::HashMap;
use super::NuSpan;
use nu_engine::get_columns;
use nu_protocol::{record, ListStream, PipelineData, PipelineMetadata, RawStream, Value};
use super::NuSpan;
use std::collections::HashMap;
pub fn collect_pipeline(input: PipelineData) -> (Vec<String>, Vec<Vec<Value>>) {
match input {

View File

@ -3,15 +3,18 @@ mod events;
pub mod report;
mod status_bar;
use std::{
cmp::min,
io::{self, Result, Stdout},
result,
sync::atomic::Ordering,
use self::{
command_bar::CommandBar,
report::{Report, Severity},
status_bar::StatusBar,
};
use super::views::{Layout, View};
use crate::{
nu_common::{CtrlC, NuColor, NuConfig, NuSpan, NuStyle},
registry::{Command, CommandRegistry},
util::map_into_value,
views::{util::nu_style_to_tui, ViewConfig},
};
use std::collections::HashMap;
use crossterm::{
event::{KeyCode, KeyEvent, KeyModifiers},
execute,
@ -20,6 +23,7 @@ use crossterm::{
LeaveAlternateScreen,
},
};
use events::UIEvents;
use lscolors::LsColors;
use nu_color_config::{lookup_ansi_color_style, StyleComputer};
use nu_protocol::{
@ -27,24 +31,14 @@ use nu_protocol::{
Record, Value,
};
use ratatui::{backend::CrosstermBackend, layout::Rect, widgets::Block};
use crate::{
nu_common::{CtrlC, NuColor, NuConfig, NuSpan, NuStyle},
registry::{Command, CommandRegistry},
util::map_into_value,
views::{util::nu_style_to_tui, ViewConfig},
use std::{
cmp::min,
collections::HashMap,
io::{self, Result, Stdout},
result,
sync::atomic::Ordering,
};
use self::{
command_bar::CommandBar,
report::{Report, Severity},
status_bar::StatusBar,
};
use super::views::{Layout, View};
use events::UIEvents;
pub type Frame<'a> = ratatui::Frame<'a>;
pub type Terminal = ratatui::Terminal<CrosstermBackend<Stdout>>;
pub type ConfigMap = HashMap<String, Value>;

View File

@ -1,14 +1,12 @@
use crossterm::event::KeyEvent;
use nu_color_config::TextStyle;
use nu_protocol::engine::{EngineState, Stack};
use ratatui::{layout::Rect, widgets::Paragraph};
use super::{Layout, View, ViewConfig};
use crate::{
nu_common::NuText,
pager::{Frame, Transition, ViewInfo},
};
use super::{Layout, View, ViewConfig};
use crossterm::event::KeyEvent;
use nu_color_config::TextStyle;
use nu_protocol::engine::{EngineState, Stack};
use ratatui::{layout::Rect, widgets::Paragraph};
#[derive(Debug, Default)]
pub struct InformationView;

View File

@ -1,5 +1,13 @@
use std::cmp::min;
use super::{
record::{RecordView, TableTheme},
util::{lookup_tui_color, nu_style_to_tui},
Layout, Orientation, View, ViewConfig,
};
use crate::{
nu_common::{collect_pipeline, run_command_with_value},
pager::{report::Report, Frame, Transition, ViewInfo},
util::create_map,
};
use crossterm::event::{KeyCode, KeyEvent};
use nu_color_config::get_color_map;
use nu_protocol::{
@ -11,18 +19,7 @@ use ratatui::{
style::{Modifier, Style},
widgets::{BorderType, Borders, Paragraph},
};
use crate::{
nu_common::{collect_pipeline, run_command_with_value},
pager::{report::Report, Frame, Transition, ViewInfo},
util::create_map,
};
use super::{
record::{RecordView, TableTheme},
util::{lookup_tui_color, nu_style_to_tui},
Layout, Orientation, View, ViewConfig,
};
use std::cmp::min;
pub struct InteractiveView<'a> {
input: Value,

View File

@ -7,6 +7,11 @@ mod preview;
mod record;
pub mod util;
use super::{
nu_common::NuText,
pager::{Frame, Transition, ViewInfo},
};
use crate::{nu_common::NuConfig, pager::ConfigMap};
use crossterm::event::KeyEvent;
use lscolors::LsColors;
use nu_color_config::StyleComputer;
@ -16,13 +21,6 @@ use nu_protocol::{
};
use ratatui::layout::Rect;
use crate::{nu_common::NuConfig, pager::ConfigMap};
use super::{
nu_common::NuText,
pager::{Frame, Transition, ViewInfo},
};
pub use binary::BinaryView;
pub use information::InformationView;
pub use interactive::InteractiveView;

View File

@ -1,5 +1,8 @@
use std::cmp::max;
use super::{coloredtextw::ColoredTextW, cursor::XYCursor, Layout, View, ViewConfig};
use crate::{
nu_common::{NuSpan, NuText},
pager::{report::Report, Frame, Transition, ViewInfo},
};
use crossterm::event::{KeyCode, KeyEvent};
use nu_color_config::TextStyle;
use nu_protocol::{
@ -7,13 +10,7 @@ use nu_protocol::{
Value,
};
use ratatui::layout::Rect;
use crate::{
nu_common::{NuSpan, NuText},
pager::{report::Report, Frame, Transition, ViewInfo},
};
use super::{coloredtextw::ColoredTextW, cursor::XYCursor, Layout, View, ViewConfig};
use std::cmp::max;
// todo: Add wrap option
#[derive(Debug)]

View File

@ -1,17 +1,11 @@
mod tablew;
use std::borrow::Cow;
use std::collections::HashMap;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use nu_color_config::{get_color_map, StyleComputer};
use nu_protocol::{
engine::{EngineState, Stack},
Record, Value,
use self::tablew::{TableStyle, TableW, TableWState};
use super::{
cursor::XYCursor,
util::{make_styled_string, nu_style_to_tui},
Layout, View, ViewConfig,
};
use ratatui::{layout::Rect, widgets::Block};
use crate::{
nu_common::{collect_input, lscolorize, NuConfig, NuSpan, NuStyle, NuText},
pager::{
@ -21,14 +15,14 @@ use crate::{
util::create_map,
views::ElementInfo,
};
use self::tablew::{TableStyle, TableW, TableWState};
use super::{
cursor::XYCursor,
util::{make_styled_string, nu_style_to_tui},
Layout, View, ViewConfig,
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use nu_color_config::{get_color_map, StyleComputer};
use nu_protocol::{
engine::{EngineState, Stack},
Record, Value,
};
use ratatui::{layout::Rect, widgets::Block};
use std::{borrow::Cow, collections::HashMap};
pub use self::tablew::Orientation;

View File

@ -1,8 +1,8 @@
use std::{
borrow::Cow,
cmp::{max, Ordering},
use super::Layout;
use crate::{
nu_common::{truncate_str, NuStyle, NuText},
views::util::{nu_style_to_tui, text_style_to_tui_style},
};
use nu_color_config::{Alignment, StyleComputer, TextStyle};
use nu_protocol::Value;
use nu_table::string_width;
@ -12,14 +12,11 @@ use ratatui::{
text::Span,
widgets::{Block, Borders, Paragraph, StatefulWidget, Widget},
};
use crate::{
nu_common::{truncate_str, NuStyle, NuText},
views::util::{nu_style_to_tui, text_style_to_tui_style},
use std::{
borrow::Cow,
cmp::{max, Ordering},
};
use super::Layout;
#[derive(Debug, Clone)]
pub struct TableW<'a> {
columns: Cow<'a, [String]>,

View File

@ -1,5 +1,4 @@
use std::borrow::Cow;
use crate::nu_common::{truncate_str, NuColor, NuStyle, NuText};
use nu_color_config::{Alignment, StyleComputer};
use nu_protocol::{ShellError, Value};
use nu_table::{string_width, TextStyle};
@ -8,8 +7,7 @@ use ratatui::{
style::{Color, Modifier, Style},
text::Span,
};
use crate::nu_common::{truncate_str, NuColor, NuStyle, NuText};
use std::borrow::Cow;
pub fn set_span(
buf: &mut Buffer,