From ae2793c44adebc344242de94e39d9e442a2c0f6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20=C3=84lgmyr?= Date: Sun, 31 Mar 2024 19:43:54 +0200 Subject: [PATCH] cli: Split out ascii template versions and migrate config to toml This should hopefully unblock #3381. --- CHANGELOG.md | 2 ++ cli/src/config/templates.toml | 5 +++ cli/tests/test_log_command.rs | 2 +- cli/tests/test_operations.rs | 58 +++++++++++++++++++++++++++++++++++ docs/config.md | 4 +-- lib/src/settings.rs | 32 +++++++++---------- 6 files changed, 82 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b7f3aea80..a4dfef842b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/cli/src/config/templates.toml b/cli/src/config/templates.toml index 80b0f9aa69..11e9c28002 100644 --- a/cli/src/config/templates.toml +++ b/cli/src/config/templates.toml @@ -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, diff --git a/cli/tests/test_log_command.rs b/cli/tests/test_log_command.rs index 0782d6ba3a..ca839b2a9c 100644 --- a/cli/tests/test_log_command.rs +++ b/cli/tests/test_log_command.rs @@ -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###" diff --git a/cli/tests/test_operations.rs b/cli/tests/test_operations.rs index 4e05eb55af..17d90f8242 100644 --- a/cli/tests/test_operations.rs +++ b/cli/tests/test_operations.rs @@ -93,6 +93,37 @@ 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 test-username@host.example.com 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 test-username@host.example.com 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 + | add workspace 'default' + o 9a7d829846af test-username@host.example.com 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(); @@ -100,6 +131,7 @@ fn test_op_log_with_custom_symbols() { 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, &[ @@ -122,6 +154,32 @@ 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 test-username@host.example.com 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 test-username@host.example.com 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 + | add workspace 'default' + > 9a7d829846af test-username@host.example.com 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 + | initialize repo + ^ 000000000000 root() + "###); } #[test] diff --git a/docs/config.md b/docs/config.md index 6ff3b99add..a3f73c2dde 100644 --- a/docs/config.md +++ b/docs/config.md @@ -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` keywords) - * `templates.op_log_node` for operations (with `Operation` keywords) + * `templates.log_node`/`templates.log_node_ascii` for commits (with `Option` keywords) + * `templates.op_log_node`/`templates.op_log_node_ascii` for operations (with `Operation` keywords) For example: ```toml diff --git a/lib/src/settings.rs b/lib/src/settings.rs index 1eb572469b..d5c3682526 100644 --- a/lib/src/settings.rs +++ b/lib/src/settings.rs @@ -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 { @@ -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