Skip to content

Commit

Permalink
templates: add plain_text to strip formatting
Browse files Browse the repository at this point in the history
Change-Id: Id000000040ea6fd8e2d720219931485960c570dd
  • Loading branch information
avamsi committed Oct 7, 2024
1 parent 68f4860 commit c4988b5
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions cli/src/template_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,15 @@ fn builtin_functions<'a, L: TemplateLanguage<'a> + ?Sized>() -> TemplateBuildFun
content, labels,
))))
});
map.insert(
"plain_text",
|language, diagnostics, build_ctx, function| {
let [content_node] = function.expect_exact_arguments()?;
let content =
expect_plain_text_expression(language, diagnostics, build_ctx, content_node)?;
Ok(L::wrap_string(content))
},
);
map.insert("if", |language, diagnostics, build_ctx, function| {
let ([condition_node, true_node], [false_node]) = function.expect_arguments()?;
let condition =
Expand Down Expand Up @@ -2334,6 +2343,30 @@ mod tests {
@"text");
}

#[test]
fn test_plain_text_function() {
// Note: same as test_label_function above, but wrapped with plain_text.
let mut env = TestTemplateEnv::new();
env.add_keyword("empty", || L::wrap_boolean(Literal(true)));
env.add_color("error", crossterm::style::Color::DarkRed);
env.add_color("warning", crossterm::style::Color::DarkYellow);

// Literal
insta::assert_snapshot!(
env.render_ok(r#"plain_text(label("error", "text"))"#),
@"text");

// Evaluated property
insta::assert_snapshot!(
env.render_ok(r#"plain_text(label("error".first_line(), "text"))"#),
@"text");

// Template
insta::assert_snapshot!(
env.render_ok(r#"plain_text(label(if(empty, "error", "warning"), "text"))"#),
@"text");
}

#[test]
fn test_coalesce_function() {
let mut env = TestTemplateEnv::new();
Expand Down

0 comments on commit c4988b5

Please sign in to comment.