From 52cb50b93722e98d23d3be5d6b412e3167afceac Mon Sep 17 00:00:00 2001 From: Antonio Natilla Date: Tue, 2 Nov 2021 18:13:06 +0100 Subject: [PATCH] Base Command implementation for Format Note that run is not implemented yet --- crates/nu-command/src/default_context.rs | 1 + .../nu-command/src/strings/format/format.rs | 53 +++++++++++++++++++ crates/nu-command/src/strings/format/mod.rs | 3 ++ crates/nu-command/src/strings/mod.rs | 2 + 4 files changed, 59 insertions(+) create mode 100644 crates/nu-command/src/strings/format/format.rs create mode 100644 crates/nu-command/src/strings/format/mod.rs diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 3d41f2735..754693c2c 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -42,6 +42,7 @@ pub fn create_default_context() -> EngineState { External, First, For, + Format, From, FromJson, Get, diff --git a/crates/nu-command/src/strings/format/format.rs b/crates/nu-command/src/strings/format/format.rs new file mode 100644 index 000000000..5f64661a3 --- /dev/null +++ b/crates/nu-command/src/strings/format/format.rs @@ -0,0 +1,53 @@ +//use nu_engine::CallExt; +use nu_protocol::ast::Call; +use nu_protocol::engine::{Command, EngineState, Stack}; +use nu_protocol::{Example, PipelineData, ShellError, Signature, SyntaxShape}; + +#[derive(Clone)] +pub struct Format; + +impl Command for Format { + fn name(&self) -> &str { + "format" + } + + fn signature(&self) -> Signature { + Signature::build("format").required( + "pattern", + SyntaxShape::String, + "the pattern to output. e.g.) \"{foo}: {bar}\"", + ) + } + + fn usage(&self) -> &str { + "Format columns into a string using a simple pattern." + } + + fn run( + &self, + _engine_state: &EngineState, + _stack: &mut Stack, + _call: &Call, + _input: PipelineData, + ) -> Result { + todo!() + } + + fn examples(&self) -> Vec { + vec![Example { + description: "Print filenames with their sizes", + example: "ls | format '{name}: {size}'", + result: None, + }] + } +} + +#[cfg(test)] +mod test { + #[test] + fn test_examples() { + use super::Format; + use crate::test_examples; + test_examples(Format {}) + } +} diff --git a/crates/nu-command/src/strings/format/mod.rs b/crates/nu-command/src/strings/format/mod.rs new file mode 100644 index 000000000..c20d41633 --- /dev/null +++ b/crates/nu-command/src/strings/format/mod.rs @@ -0,0 +1,3 @@ +pub mod format; + +pub use format::Format; diff --git a/crates/nu-command/src/strings/mod.rs b/crates/nu-command/src/strings/mod.rs index bbb78a24d..a23eba5ff 100644 --- a/crates/nu-command/src/strings/mod.rs +++ b/crates/nu-command/src/strings/mod.rs @@ -1,7 +1,9 @@ mod build_string; +mod format; mod size; mod split; pub use build_string::BuildString; +pub use format::*; pub use size::Size; pub use split::*;