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 f76830a commit ff4d0c3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
19 changes: 19 additions & 0 deletions cli/src/command_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use jj_lib::working_copy::{ResetError, SnapshotError, WorkingCopyStateError};
use jj_lib::workspace::WorkspaceInitError;
use thiserror::Error;

use crate::cli_util::WorkspaceCommandHelper;
use crate::merge_tools::{
ConflictResolveError, DiffEditError, DiffGenerateError, MergeToolConfigError,
};
Expand Down Expand Up @@ -76,6 +77,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('\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 error::Error + Send + Sync>>) -> CommandError {
user_error_with_hint_opt(err, None)
}
Expand Down
7 changes: 4 additions & 3 deletions cli/src/commands/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use jj_lib::revset_graph::{
use tracing::instrument;

use crate::cli_util::{CommandHelper, LogContentFormat, RevisionArg};
use crate::command_error::CommandError;
use crate::command_error::{user_error_missing_template, CommandError};
use crate::diff_util::{self, DiffFormatArgs};
use crate::graphlog::{get_graphlog, Edge};
use crate::ui::Ui;
Expand Down Expand Up @@ -52,7 +52,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 @@ -101,7 +101,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 ff4d0c3

Please sign in to comment.