Skip to content

Commit

Permalink
cli: improve discoverability of template-aliases
Browse files Browse the repository at this point in the history
When user types `jj log -T` without passing a template name we can list all of them
  • Loading branch information
zummenix committed Mar 2, 2024
1 parent b926fd8 commit 5b33309
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
18 changes: 18 additions & 0 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,24 @@ impl ErrorWithMessage {
}
}

pub fn user_error_missing_template(workspace_command: &WorkspaceCommandHelper) -> CommandError {
let mut template_names = workspace_command
.template_aliases_map()
.symbol_aliases_keys();
template_names.sort_unstable();
let mut hint = String::new();
let padding = " ";
for name in template_names {
hint.push_str("\n");
hint.push_str(padding);
hint.push_str(name);
}
user_error_with_hint(
String::from("Template is required"),
format!("The following template-aliases are defined:{}", hint),
)
}

pub fn user_error(err: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> CommandError {
user_error_with_hint_opt(err, None)
}
Expand Down
9 changes: 6 additions & 3 deletions cli/src/commands/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use jj_lib::revset_graph::{
};
use tracing::instrument;

use crate::cli_util::{CommandError, CommandHelper, LogContentFormat, RevisionArg};
use crate::cli_util::{
user_error_missing_template, CommandError, CommandHelper, LogContentFormat, RevisionArg,
};
use crate::diff_util::{self, DiffFormatArgs};
use crate::graphlog::{get_graphlog, Edge};
use crate::ui::Ui;
Expand Down Expand Up @@ -51,7 +53,7 @@ pub(crate) struct LogArgs {
///
/// For the syntax, see https://github.com/martinvonz/jj/blob/main/docs/templates.md
#[arg(long, short = 'T')]
template: Option<String>,
template: Option<Option<String>>,
/// Show patch
#[arg(long, short = 'p')]
patch: bool,
Expand Down Expand Up @@ -100,7 +102,8 @@ pub(crate) fn cmd_log(
diff_util::diff_formats_for_log(command.settings(), &args.diff_format, args.patch)?;

let template_string = match &args.template {
Some(value) => value.to_string(),
Some(Some(value)) => value.to_string(),
Some(None) => return Err(user_error_missing_template(&workspace_command)),
None => command.settings().config().get_string("templates.log")?,
};
let use_elided_nodes = command
Expand Down
4 changes: 4 additions & 0 deletions cli/src/template_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

/// Adds new substitution rule `decl = defn`.
///
/// Returns error if `decl` is invalid. The `defn` part isn't checked. A bad
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/test_log_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]...
For more information, try '--help'.
"###);
Expand Down

0 comments on commit 5b33309

Please sign in to comment.