Skip to content

Commit

Permalink
cli: Split out ascii template versions and migrate config to toml
Browse files Browse the repository at this point in the history
This should hopefully unblock #3381.
  • Loading branch information
algmyr committed Mar 31, 2024
1 parent 8eed08b commit f53c7d6
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 21 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Graph node symbols are now configurable via templates
* `templates.log_node`
* `templates.log_node_ascii`
* `templates.op_log_node`
* `templates.op_log_node_ascii`

* `jj log` now includes synthetic nodes in the graph where some revisions were
elided.
Expand Down
5 changes: 5 additions & 0 deletions cli/src/config/templates.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ log = 'builtin_log_compact'
op_log = 'builtin_op_log_compact'
show = 'builtin_log_detailed'

log_node = 'if(self, if(current_working_copy, "@", "◉"), "◌")'
log_node_ascii = 'if(self, if(current_working_copy, "@", "o"), ".")'
op_log_node = 'if(current_operation, "@", "◉")'
op_log_node_ascii = 'if(current_operation, "@", "o")'

[template-aliases]
builtin_log_oneline = '''
if(root,
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 @@ -1460,7 +1460,7 @@ fn test_log_with_custom_symbols() {
r###"
ui.log-synthetic-elided-nodes = true
ui.graph.style = 'ascii'
templates.log_node = 'if(self, if(current_working_copy, "$", if(root, "^", "*")), ":")'
templates.log_node_ascii = 'if(self, if(current_working_copy, "$", if(root, "^", "*")), ":")'
"###,
);
insta::assert_snapshot!(get_log("@ | @- | description(initial) | root()"), @r###"
Expand Down
57 changes: 57 additions & 0 deletions cli/tests/test_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,45 @@ fn test_op_log() {
"###);
}

#[test]
fn test_op_log_default_ascii_symbols() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "description 0"]);

let stdout = test_env.jj_cmd_success(
&repo_path,
&[
"op",
"log",
"--config-toml",
concat!(
"template-aliases.'format_time_range(x)' = 'x'\n",
"ui.graph.style = 'ascii'\n",
),
],
);
insta::assert_snapshot!(&stdout, @r###"
@ 52ac15d375ba [email protected] 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00
| describe commit 230dd059e1b059aefc0da06a2e5a7dbf22362f22
| args: jj describe -m 'description 0'
o b51416386f26 [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
| add workspace 'default'
o 9a7d829846af [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
| initialize repo
o 000000000000 root()
"###);
}

#[test]
fn test_op_log_with_custom_symbols() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "description 0"]);

// Custom symbols.
let stdout = test_env.jj_cmd_success(
&repo_path,
&[
Expand All @@ -122,6 +154,31 @@ fn test_op_log_with_custom_symbols() {
│ initialize repo
┴ 000000000000 root()
"###);

// Custom symbols, ascii.
let stdout = test_env.jj_cmd_success(
&repo_path,
&[
"op",
"log",
"--config-toml",
concat!(
"template-aliases.'format_time_range(x)' = 'x'\n",
"ui.graph.style = 'ascii'\n",
"templates.op_log_node_ascii = 'if(current_operation, \"v\", if(root, \"^\", \">\"))'",
),
],
);
insta::assert_snapshot!(&stdout, @r###"
v 52ac15d375ba [email protected] 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00
| describe commit 230dd059e1b059aefc0da06a2e5a7dbf22362f22
| args: jj describe -m 'description 0'
> b51416386f26 [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
| add workspace 'default'
> 9a7d829846af [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
| initialize repo
^ 000000000000 root()
"###);
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ ui.graph.style = "square"
The symbols used to represent commits or operations can be customized via
templates.

* `templates.log_node` for commits (with `Option<Commit>` keywords)
* `templates.op_log_node` for operations (with `Operation` keywords)
* `templates.log_node`/`templates.log_node_ascii` for commits (with `Option<Commit>` keywords)
* `templates.op_log_node`/`templates.op_log_node_ascii` for operations (with `Operation` keywords)

For example:
```toml
Expand Down
32 changes: 14 additions & 18 deletions lib/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,19 +243,23 @@ impl UserSettings {
}

pub fn commit_node_template(&self) -> String {
self.node_template_for_key(
"templates.log_node",
r#"if(self, if(current_working_copy, "@", "◉"), "◌")"#,
r#"if(self, if(current_working_copy, "@", "o"), ".")"#,
)
let key = match self.graph_style().as_str() {
"ascii" | "ascii-large" => "templates.log_node_ascii",
_ => "templates.log_node",
};
self.config
.get_string(key)
.expect("Node templates should exist in bundled config!")
}

pub fn op_node_template(&self) -> String {
self.node_template_for_key(
"templates.op_log_node",
r#"if(current_operation, "@", "◉")"#,
r#"if(current_operation, "@", "o")"#,
)
let key = match self.graph_style().as_str() {
"ascii" | "ascii-large" => "templates.op_log_node_ascii",
_ => "templates.op_log_node",
};
self.config
.get_string(key)
.expect("Node templates should exist in bundled config!")
}

pub fn max_new_file_size(&self) -> Result<u64, config::ConfigError> {
Expand All @@ -280,14 +284,6 @@ impl UserSettings {
pub fn sign_settings(&self) -> SignSettings {
SignSettings::from_settings(self)
}

fn node_template_for_key(&self, key: &str, fallback: &str, ascii_fallback: &str) -> String {
let symbol = self.config.get_string(key);
match self.graph_style().as_str() {
"ascii" | "ascii-large" => symbol.unwrap_or_else(|_| ascii_fallback.to_owned()),
_ => symbol.unwrap_or_else(|_| fallback.to_owned()),
}
}
}

/// This Rng uses interior mutability to allow generating random values using an
Expand Down

0 comments on commit f53c7d6

Please sign in to comment.