Repository: nushell/nushell
File: crates/nu-cmd-extra/src/extra/strings/format/command.rs (Rust)

Code before:
                "pattern",
                SyntaxShape::String,
                "The pattern to output. e.g.) \"{foo}: {bar}\".",
            )
            .allow_variants_without_examples(true)
            .category(Category::Strings)
    }

    fn description(&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<PipelineData, ShellError> {
        let mut working_set = StateWorkingSet::new(engine_state);

        let pattern: Spanned<String> = call.req(engine_state, stack, 0)?;
        let input_val = input.into_value(call.head)?;
        // add '$it' variable to support format like this: $it.column1.column2.
        let it_id = working_set.add_variable(b"$it".to_vec(), call.head, Type::Any, false);
        stack.add_var(it_id, input_val.clone());

        let config = stack.get_config(engine_state);

        // the string span is start as `"`, we don't need the character
        // to generate proper span for sub expression.
        let ops = extract_formatting_operations(pattern, call.head)?;

        format(input_val, &ops, engine_state, &config, call.head)
    }

    fn examples(&self) -> Vec<Example<'_>> {
        vec![
            Example {
                description: "Print filenames with their sizes",
                example: "ls | format pattern '{name}: {size}'",
                result: None,
            },
            Example {
                description: "Print elements from some columns of a table",
                example: "[[col1, col2]; [v1, v2] [v3, v4]] | format pattern '{col2}'",
                result: Some(Value::test_list(vec![
                    Value::test_string("v2"),
                    Value::test_string("v4"),
                ])),
            },

Review comment (refactor):
I should remove this too, since using `$it` (or any variable) in the pattern is not supported now.

Code after:
                "The pattern to output. e.g.) \"{foo}: {bar}\".",
            )
            .allow_variants_without_examples(true)
            .category(Category::Strings)
    }

    fn description(&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<PipelineData, ShellError> {
        let pattern: Spanned<String> = call.req(engine_state, stack, 0)?;
        let input_val = input.into_value(call.head)?;

        let ops = extract_formatting_operations(pattern, call.head)?;
        let config = stack.get_config(engine_state);

        format(input_val, &ops, engine_state, &config, call.head)
    }

    fn examples(&self) -> Vec<Example<'_>> {
        vec![
            Example {
                description: "Print filenames with their sizes",
                example: "ls | format pattern '{name}: {size}'",
                result: None,
            },
            Example {
                description: "Print elements from some columns of a table",
                example: "[[col1, col2]; [v1, v2] [v3, v4]] | format pattern '{col2}'",
                result: Some(Value::test_list(vec![
                    Value::test_string("v2"),
                    Value::test_string("v4"),
                ])),
            },
            Example {
                description: "Escape braces by repeating them",
                example: r#"{start: 3, end: 5} | format pattern 'if {start} < {end} {{ "correct" }} else {{ "incorrect" }}'"#,
                result: Some(Value::test_string(
                    r#"if 3 < 5 { "correct" } else { "incorrect" }"#,
                )),
            },
        ]
    }
}