ok/jj
1
0
Fork 0
forked from mirrors/jj

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.
This commit is contained in:
Yuya Nishihara 2024-09-03 16:37:25 +09:00
parent 42dee7d08c
commit 3ad54ad38d
5 changed files with 47 additions and 34 deletions

View file

@ -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");

View file

@ -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",
)
}

View file

@ -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",
)
}

View file

@ -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<dyn GraphLog<K> + '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)
}

View file

@ -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<u64, config::ConfigError> {
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