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

graphlog: add enum describing graph style

This commit is contained in:
Yuya Nishihara 2024-09-03 16:51:06 +09:00
parent 3ad54ad38d
commit bd7cbaaffb

View file

@ -102,11 +102,29 @@ impl<'writer, R> SaplingGraphLog<'writer, R> {
} }
} }
pub fn graph_style(settings: &UserSettings) -> String { #[derive(Clone, Copy, Debug, Eq, PartialEq, serde::Deserialize)]
settings #[serde(rename_all(deserialize = "kebab-case"))]
.config() pub enum GraphStyle {
.get_string("ui.graph.style") Ascii,
.unwrap_or_else(|_| "curved".to_string()) AsciiLarge,
Curved,
Square,
}
impl GraphStyle {
pub fn from_settings(settings: &UserSettings) -> Self {
settings
.config()
.get("ui.graph.style")
.unwrap_or(GraphStyle::Curved)
}
pub fn is_ascii(self) -> bool {
match self {
GraphStyle::Ascii | GraphStyle::AsciiLarge => true,
GraphStyle::Curved | GraphStyle::Square => false,
}
}
} }
pub fn node_template_for_key( pub fn node_template_for_key(
@ -116,9 +134,10 @@ pub fn node_template_for_key(
ascii_fallback: &str, ascii_fallback: &str,
) -> String { ) -> String {
let symbol = settings.config().get_string(key); let symbol = settings.config().get_string(key);
match graph_style(settings).as_str() { if GraphStyle::from_settings(settings).is_ascii() {
"ascii" | "ascii-large" => symbol.unwrap_or_else(|_| ascii_fallback.to_owned()), symbol.unwrap_or_else(|_| ascii_fallback.to_owned())
_ => symbol.unwrap_or_else(|_| fallback.to_owned()), } else {
symbol.unwrap_or_else(|_| fallback.to_owned())
} }
} }
@ -127,13 +146,12 @@ pub fn get_graphlog<'a, K: Clone + Eq + Hash + 'a>(
formatter: &'a mut dyn Write, formatter: &'a mut dyn Write,
) -> Box<dyn GraphLog<K> + 'a> { ) -> Box<dyn GraphLog<K> + 'a> {
let builder = GraphRowRenderer::new().output().with_min_row_height(0); let builder = GraphRowRenderer::new().output().with_min_row_height(0);
match graph_style(settings).as_str() { match GraphStyle::from_settings(settings) {
"square" => { GraphStyle::Ascii => SaplingGraphLog::create(builder.build_ascii(), formatter),
GraphStyle::AsciiLarge => SaplingGraphLog::create(builder.build_ascii_large(), formatter),
GraphStyle::Curved => SaplingGraphLog::create(builder.build_box_drawing(), formatter),
GraphStyle::Square => {
SaplingGraphLog::create(builder.build_box_drawing().with_square_glyphs(), formatter) SaplingGraphLog::create(builder.build_box_drawing().with_square_glyphs(), formatter)
} }
"ascii" => SaplingGraphLog::create(builder.build_ascii(), formatter),
"ascii-large" => SaplingGraphLog::create(builder.build_ascii_large(), formatter),
// "curved"
_ => SaplingGraphLog::create(builder.build_box_drawing(), formatter),
} }
} }