Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: improve discoverability of template-aliases #3189

Closed
wants to merge 1 commit into from

Conversation

zummenix
Copy link
Collaborator

@zummenix zummenix commented Mar 2, 2024

This is follow up of #3175. This is a draft. Looking for initial feedback.

When user types jj log -T without passing a template name we can list all of them:

Error: Template is required
Hint: The following template-aliases are defined:
   builtin_change_id_with_hidden_and_divergent_info
   builtin_log_comfortable
   builtin_log_compact
   builtin_log_detailed
   builtin_log_oneline
   builtin_op_log_comfortable
   builtin_op_log_compact
   commit_summary_separator
   description_placeholder
   email_placeholder
   name_placeholder

Checklist

If applicable:

  • I have updated CHANGELOG.md
  • I have updated the documentation (README.md, docs/, demos/)
  • I have updated the config schema (cli/src/config-schema.json)
  • I have added tests to cover my changes

@joyously
Copy link

joyously commented Mar 2, 2024

This looks useful at first glance.
When I looked closer, I wondered if the file name could be output, so the definitions could be examined.
And then I wondered if templates work for any command or just the one it's designed for (email_placeholder on a status command).

@zummenix
Copy link
Collaborator Author

zummenix commented Mar 2, 2024

When I looked closer, I wondered if the file name could be output, so the definitions could be examined.

I'm not sure about the file name but one can explore a template using jj config get command, for example jj config get template-aliases.builtin_op_log_compact. Perhaps, we should mention that in the hint?

And then I wondered if templates work for any command or just the one it's designed for (email_placeholder on a status command).

Currently, there is no connection between templates and their corresponding commands. For instance, using builtin_op_log_compact for jj log fails with an error:

$ jj log -T builtin_op_log_compact
Error: Failed to parse template:  --> 1:1
  |
1 | builtin_op_log_compact
  | ^--------------------^
  |
  = Alias "builtin_op_log_compact" cannot be expanded
 --> 2:3
  |
2 |   builtin_op_log_root(id),
  |   ^---------------------^
  |
  = Alias "builtin_op_log_root()" cannot be expanded
 --> 2:23
  |
2 |   builtin_op_log_root(id),
  |                       ^^
  |
  = Keyword "id" doesn't exist
Hint: Did you mean "hidden"?

When user types `jj log -T` without passing a template name we can list all of them
Copy link
Collaborator

@yuja yuja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems useful.

builtin_change_id_with_hidden_and_divergent_info
builtin_log_comfortable
builtin_log_compact
builtin_log_detailed
builtin_log_oneline
builtin_op_log_comfortable
builtin_op_log_compact
commit_summary_separator
description_placeholder
email_placeholder
name_placeholder

Not all aliases are supposed to be used as a top-level template, but I have no idea how we can improve that. We could use a static list of builtin templates, but that means user aliases wouldn't be included.

for name in template_names {
hint.push('\n');
hint.push_str(padding);
hint.push_str(name);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: something like writeln!(hint, " {name}").unwrap() also works. Or simply .map(|name| format!(..)).join("\n").

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

format! will allocate a new string. It's not that important here of course, but still... I think writeln!(hint, " {name}").unwrap() will be fine.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. It's not important here, but clippy might complain about that.

@@ -561,6 +561,10 @@ impl TemplateAliasesMap {
Self::default()
}

pub fn symbol_aliases_keys(&self) -> Vec<&str> {
self.symbol_aliases.keys().map(|s| s.as_str()).collect_vec()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: simply return impl Iterator<Item = &str> as the caller need to post-process the items anyway?

@@ -561,6 +561,10 @@ impl TemplateAliasesMap {
Self::default()
}

pub fn symbol_aliases_keys(&self) -> Vec<&str> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: symbol_names or symbol_alias_names ("key" sounds more like an implementation detail.)

@@ -179,7 +179,7 @@ fn test_log_with_or_without_diff() {
insta::assert_snapshot!(stderr, @r###"
error: the argument '--git' cannot be used with '--color-words'

Usage: jj log --template <TEMPLATE> --no-graph --patch --git [PATHS]...
Usage: jj log --template [<TEMPLATE>] --no-graph --patch --git [PATHS]...
Copy link
Collaborator

@yuja yuja Mar 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unfortunate that the --template value has to be optional.

Another implementation idea is to inspect the clap::Error at handle_command_result() or CliRunner::run_internal().

[cli/src/command_error.rs:478:13] (inner.kind(), inner.context().collect_vec()) = (
    InvalidValue,
    [
        (
            InvalidArg,
            String(
                "--template <TEMPLATE>",
            ),
        ),
        (
            InvalidValue,
            String(
                "",
            ),
        ),
        (
            ValidValue,
            Strings(
                [],
            ),
        ),
    ],
)

Doing that seems intrusive, but I think it's okay because --template is the concept apply to many commands. A downside is that we'll need to build template aliases from the config object available at that point (because WorkspaceCommandHelper isn't instantiated yet.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've implemented this variant in #3200. Please take a look.

@zummenix
Copy link
Collaborator Author

zummenix commented Mar 3, 2024

Not all aliases are supposed to be used as a top-level template, but I have no idea how we can improve that. We could use a static list of builtin templates, but that means user aliases wouldn't be included.

Yeah, I think it's important to include user templates in the output.

On improvement: I was thinking maybe we could have some kind of introspection for templates to extract "features" and match these set of "features" with the set of "features" of a command. In my opinion, this greatly overcomplicate things and maybe not worth it in this context.

@zummenix
Copy link
Collaborator Author

zummenix commented Mar 4, 2024

Closing in favor of #3200

@zummenix zummenix closed this Mar 4, 2024
@zummenix zummenix deleted the template-aliases-hint branch March 4, 2024 04:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants