mirror of
https://github.com/nushell/nushell.git
synced 2025-05-18 17:00:46 +02:00
# 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.
89 lines
2.9 KiB
Rust
89 lines
2.9 KiB
Rust
use crate::{Config, Record, ShellError, Span, Value};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
/// Definition of a parsed hook from the config object
|
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
pub struct Hooks {
|
|
pub pre_prompt: Option<Value>,
|
|
pub pre_execution: Option<Value>,
|
|
pub env_change: Option<Value>,
|
|
pub display_output: Option<Value>,
|
|
pub command_not_found: Option<Value>,
|
|
}
|
|
|
|
impl Hooks {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
pre_prompt: None,
|
|
pre_execution: None,
|
|
env_change: None,
|
|
display_output: Some(Value::string(
|
|
"if (term size).columns >= 100 { table -e } else { table }",
|
|
Span::unknown(),
|
|
)),
|
|
command_not_found: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Default for Hooks {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|
|
|
|
/// Parse the hooks to find the blocks to run when the hooks fire
|
|
pub(super) fn create_hooks(value: &Value) -> Result<Hooks, ShellError> {
|
|
let span = value.span();
|
|
match value {
|
|
Value::Record { val, .. } => {
|
|
let mut hooks = Hooks::new();
|
|
|
|
for (col, val) in &**val {
|
|
match col.as_str() {
|
|
"pre_prompt" => hooks.pre_prompt = Some(val.clone()),
|
|
"pre_execution" => hooks.pre_execution = Some(val.clone()),
|
|
"env_change" => hooks.env_change = Some(val.clone()),
|
|
"display_output" => hooks.display_output = Some(val.clone()),
|
|
"command_not_found" => hooks.command_not_found = Some(val.clone()),
|
|
x => {
|
|
return Err(ShellError::UnsupportedConfigValue {
|
|
expected: "'pre_prompt', 'pre_execution', 'env_change', 'display_output', 'command_not_found'".into(),
|
|
value: x.into(),
|
|
span
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
Ok(hooks)
|
|
}
|
|
_ => Err(ShellError::UnsupportedConfigValue {
|
|
expected: "record for 'hooks' config".into(),
|
|
value: "non-record value".into(),
|
|
span,
|
|
}),
|
|
}
|
|
}
|
|
|
|
pub(super) fn reconstruct_hooks(config: &Config, span: Span) -> Value {
|
|
let mut hook = Record::new();
|
|
if let Some(ref value) = config.hooks.pre_prompt {
|
|
hook.push("pre_prompt", value.clone());
|
|
}
|
|
if let Some(ref value) = config.hooks.pre_execution {
|
|
hook.push("pre_execution", value.clone());
|
|
}
|
|
if let Some(ref value) = config.hooks.env_change {
|
|
hook.push("env_change", value.clone());
|
|
}
|
|
if let Some(ref value) = config.hooks.display_output {
|
|
hook.push("display_output", value.clone());
|
|
}
|
|
if let Some(ref value) = config.hooks.command_not_found {
|
|
hook.push("command_not_found", value.clone());
|
|
}
|
|
|
|
Value::record(hook, span)
|
|
}
|