From 1ce3e9fe44d09534c6e8eb879922aa28a71f077d Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Thu, 25 Apr 2024 18:23:12 +0900 Subject: [PATCH] cli: migrate "tag list" to template I'm going to add more detailed output there. This is a step towards "branch list" template. "tag list -T" wouldn't be that useful, but it shares primitives with "branch list -T". --- cli/src/commands/tag.rs | 26 ++++++++++++++++++++++---- cli/src/config/templates.toml | 4 ++++ cli/tests/cli-reference@.md.snap | 6 +++++- cli/tests/test_tag_command.rs | 8 ++++++++ 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/cli/src/commands/tag.rs b/cli/src/commands/tag.rs index fb1d46a80..c4f6da2df 100644 --- a/cli/src/commands/tag.rs +++ b/cli/src/commands/tag.rs @@ -16,6 +16,7 @@ use jj_lib::str_util::StringPattern; use crate::cli_util::CommandHelper; use crate::command_error::CommandError; +use crate::commit_templater::{CommitTemplateLanguage, RefName}; use crate::ui::Ui; /// Manage tags. @@ -35,6 +36,13 @@ pub struct TagListArgs { /// https://github.com/martinvonz/jj/blob/main/docs/revsets.md#string-patterns. #[arg(value_parser = StringPattern::parse)] pub names: Vec, + /// Render each tag using the given template + /// + /// All 0-argument methods of the `RefName` type are available as keywords. + /// + /// For the syntax, see https://github.com/martinvonz/jj/blob/main/docs/templates.md + #[arg(long, short = 'T')] + template: Option, } pub fn cmd_tag( @@ -56,16 +64,26 @@ fn cmd_tag_list( let repo = workspace_command.repo(); let view = repo.view(); + let template = { + let language = workspace_command.commit_template_language()?; + let text = match &args.template { + Some(value) => value.to_owned(), + None => command.settings().config().get("templates.tag_list")?, + }; + workspace_command + .parse_template(&language, &text, CommitTemplateLanguage::wrap_ref_name)? + .labeled("tag_list") + }; + ui.request_pager(); let mut formatter = ui.stdout_formatter(); - let formatter = formatter.as_mut(); - for name in view.tags().keys() { + for (name, target) in view.tags() { if !args.names.is_empty() && !args.names.iter().any(|pattern| pattern.matches(name)) { continue; } - - writeln!(formatter.labeled("tag"), "{name}")?; + let ref_name = RefName::local_only(name, target.clone()); + template.format(&ref_name, formatter.as_mut())?; } Ok(()) diff --git a/cli/src/config/templates.toml b/cli/src/config/templates.toml index f188f43b2..416863639 100644 --- a/cli/src/config/templates.toml +++ b/cli/src/config/templates.toml @@ -17,6 +17,10 @@ log = 'builtin_log_compact' op_log = 'builtin_op_log_compact' show = 'builtin_log_detailed' +tag_list = ''' +label("tag", name) ++ "\n" +''' + [template-aliases] builtin_log_oneline = ''' if(root, diff --git a/cli/tests/cli-reference@.md.snap b/cli/tests/cli-reference@.md.snap index a542c8d23..432437256 100644 --- a/cli/tests/cli-reference@.md.snap +++ b/cli/tests/cli-reference@.md.snap @@ -1824,12 +1824,16 @@ Manage tags List tags -**Usage:** `jj tag list [NAMES]...` +**Usage:** `jj tag list [OPTIONS] [NAMES]...` ###### **Arguments:** * `` — Show tags whose local name matches +###### **Options:** + +* `-T`, `--template