From 3ad54ad38db9c6df7fe0c67eab6918c24791f7da Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Tue, 3 Sep 2024 16:37:25 +0900 Subject: [PATCH] settings: move graph node helpers to CLI I'm going to add enum GraphStyle, and it's specific to the CLI. node_template_for_key() will be inlined later. --- cli/src/commands/evolog.rs | 3 ++- cli/src/commands/log.rs | 13 ++++++++++++- cli/src/commands/operation/log.rs | 13 ++++++++++++- cli/src/graphlog.rs | 22 +++++++++++++++++++++- lib/src/settings.rs | 30 ------------------------------ 5 files changed, 47 insertions(+), 34 deletions(-) diff --git a/cli/src/commands/evolog.rs b/cli/src/commands/evolog.rs index a5e3e7908..bd2747733 100644 --- a/cli/src/commands/evolog.rs +++ b/cli/src/commands/evolog.rs @@ -20,6 +20,7 @@ use jj_lib::repo::Repo; use jj_lib::rewrite::rebase_to_dest_parent; use tracing::instrument; +use super::log::get_node_template; use crate::cli_util::format_template; use crate::cli_util::CommandHelper; use crate::cli_util::LogContentFormat; @@ -103,7 +104,7 @@ pub(crate) fn cmd_evolog( node_template = workspace_command .parse_template( &language, - &command.settings().commit_node_template(), + &get_node_template(command.settings()), CommitTemplateLanguage::wrap_commit_opt, )? .labeled("node"); diff --git a/cli/src/commands/log.rs b/cli/src/commands/log.rs index 735a198ba..26cf497d2 100644 --- a/cli/src/commands/log.rs +++ b/cli/src/commands/log.rs @@ -20,6 +20,7 @@ use jj_lib::repo::Repo; use jj_lib::revset::RevsetExpression; use jj_lib::revset::RevsetFilterPredicate; use jj_lib::revset::RevsetIteratorExt; +use jj_lib::settings::UserSettings; use tracing::instrument; use crate::cli_util::format_template; @@ -30,6 +31,7 @@ use crate::command_error::CommandError; use crate::commit_templater::CommitTemplateLanguage; use crate::diff_util::DiffFormatArgs; use crate::graphlog::get_graphlog; +use crate::graphlog::node_template_for_key; use crate::graphlog::Edge; use crate::ui::Ui; @@ -142,7 +144,7 @@ pub(crate) fn cmd_log( node_template = workspace_command .parse_template( &language, - &command.settings().commit_node_template(), + &get_node_template(command.settings()), CommitTemplateLanguage::wrap_commit_opt, )? .labeled("node"); @@ -292,3 +294,12 @@ pub(crate) fn cmd_log( Ok(()) } + +pub fn get_node_template(settings: &UserSettings) -> String { + node_template_for_key( + settings, + "templates.log_node", + "builtin_log_node", + "builtin_log_node_ascii", + ) +} diff --git a/cli/src/commands/operation/log.rs b/cli/src/commands/operation/log.rs index 183af0398..6520aa3be 100644 --- a/cli/src/commands/operation/log.rs +++ b/cli/src/commands/operation/log.rs @@ -15,12 +15,14 @@ use std::slice; use jj_lib::op_walk; +use jj_lib::settings::UserSettings; use crate::cli_util::format_template; use crate::cli_util::CommandHelper; use crate::cli_util::LogContentFormat; use crate::command_error::CommandError; use crate::graphlog::get_graphlog; +use crate::graphlog::node_template_for_key; use crate::graphlog::Edge; use crate::operation_templater::OperationTemplateLanguage; use crate::ui::Ui; @@ -96,7 +98,7 @@ pub fn cmd_op_log( .parse_template( ui, &language, - &command.settings().op_node_template(), + &get_node_template(command.settings()), OperationTemplateLanguage::wrap_operation, )? .labeled("node"); @@ -147,3 +149,12 @@ pub fn cmd_op_log( Ok(()) } + +fn get_node_template(settings: &UserSettings) -> String { + node_template_for_key( + settings, + "templates.op_log_node", + "builtin_op_log_node", + "builtin_op_log_node_ascii", + ) +} diff --git a/cli/src/graphlog.rs b/cli/src/graphlog.rs index a5b92b91f..445d3eea1 100644 --- a/cli/src/graphlog.rs +++ b/cli/src/graphlog.rs @@ -102,12 +102,32 @@ impl<'writer, R> SaplingGraphLog<'writer, R> { } } +pub fn graph_style(settings: &UserSettings) -> String { + settings + .config() + .get_string("ui.graph.style") + .unwrap_or_else(|_| "curved".to_string()) +} + +pub fn node_template_for_key( + settings: &UserSettings, + key: &str, + fallback: &str, + ascii_fallback: &str, +) -> String { + let symbol = settings.config().get_string(key); + match graph_style(settings).as_str() { + "ascii" | "ascii-large" => symbol.unwrap_or_else(|_| ascii_fallback.to_owned()), + _ => symbol.unwrap_or_else(|_| fallback.to_owned()), + } +} + pub fn get_graphlog<'a, K: Clone + Eq + Hash + 'a>( settings: &UserSettings, formatter: &'a mut dyn Write, ) -> Box + 'a> { let builder = GraphRowRenderer::new().output().with_min_row_height(0); - match settings.graph_style().as_str() { + match graph_style(settings).as_str() { "square" => { SaplingGraphLog::create(builder.build_box_drawing().with_square_glyphs(), formatter) } diff --git a/lib/src/settings.rs b/lib/src/settings.rs index 7433a93ff..58e074884 100644 --- a/lib/src/settings.rs +++ b/lib/src/settings.rs @@ -229,28 +229,6 @@ impl UserSettings { GitSettings::from_config(&self.config) } - pub fn graph_style(&self) -> String { - self.config - .get_string("ui.graph.style") - .unwrap_or_else(|_| "curved".to_string()) - } - - pub fn commit_node_template(&self) -> String { - self.node_template_for_key( - "templates.log_node", - "builtin_log_node", - "builtin_log_node_ascii", - ) - } - - pub fn op_node_template(&self) -> String { - self.node_template_for_key( - "templates.op_log_node", - "builtin_op_log_node", - "builtin_op_log_node_ascii", - ) - } - pub fn max_new_file_size(&self) -> Result { let cfg = self .config @@ -273,14 +251,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