Skip to content

Commit

Permalink
graphlog: refactor out node symbols from GraphLog
Browse files Browse the repository at this point in the history
Now as default and elided node symbols come from the config, the next logical
step is to use them directly bypassing GraphLog. Note that commands like `jj op
log` and `jj obslog` do not use the elided node symbol at all.
  • Loading branch information
zummenix committed Mar 11, 2024
1 parent 9c1d5d1 commit 50ff206
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 62 deletions.
4 changes: 2 additions & 2 deletions cli/src/commands/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ pub(crate) fn cmd_log(

if !args.no_graph {
let mut graph = get_graphlog(command.settings(), formatter.raw());
let default_node_symbol = graph.default_node_symbol().to_owned();
let elided_node_symbol = graph.elided_node_symbol().to_owned();
let default_node_symbol = command.settings().default_node_symbol();
let elided_node_symbol = command.settings().elided_node_symbol();
let forward_iter = TopoGroupedRevsetGraphIterator::new(revset.iter_graph());
let iter: Box<dyn Iterator<Item = _>> = if args.reversed {
Box::new(ReverseRevsetGraphIterator::new(forward_iter))
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/obslog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub(crate) fn cmd_obslog(
}
if !args.no_graph {
let mut graph = get_graphlog(command.settings(), formatter.raw());
let default_node_symbol = graph.default_node_symbol().to_owned();
let default_node_symbol = command.settings().default_node_symbol();
for commit in commits {
let mut edges = vec![];
for predecessor in &commit.predecessors() {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ fn cmd_op_log(
let iter = op_walk::walk_ancestors(&head_ops).take(args.limit.unwrap_or(usize::MAX));
if !args.no_graph {
let mut graph = get_graphlog(command.settings(), formatter.raw());
let default_node_symbol = graph.default_node_symbol().to_owned();
let default_node_symbol = command.settings().default_node_symbol();
for op in iter {
let op = op?;
let mut edges = vec![];
Expand Down
51 changes: 6 additions & 45 deletions cli/src/graphlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,12 @@ pub trait GraphLog<K: Clone + Eq + Hash> {
text: &str,
) -> io::Result<()>;

fn default_node_symbol(&self) -> &str;

fn elided_node_symbol(&self) -> &str;

fn width(&self, id: &K, edges: &[Edge<K>]) -> usize;
}

pub struct SaplingGraphLog<'writer, R> {
renderer: R,
writer: &'writer mut dyn Write,
default_node_symbol: String,
elided_node_symbol: String,
}

impl<K: Clone> From<&Edge<K>> for Ancestor<K> {
Expand Down Expand Up @@ -83,14 +77,6 @@ where
write!(self.writer, "{row}")
}

fn default_node_symbol(&self) -> &str {
&self.default_node_symbol
}

fn elided_node_symbol(&self) -> &str {
&self.elided_node_symbol
}

fn width(&self, id: &K, edges: &[Edge<K>]) -> usize {
let parents = edges.iter().map_into().collect();
let w: u64 = self.renderer.width(Some(id), Some(&parents));
Expand All @@ -102,8 +88,6 @@ impl<'writer, R> SaplingGraphLog<'writer, R> {
pub fn create<K>(
renderer: R,
formatter: &'writer mut dyn Write,
default_node_symbol: &str,
elided_node_symbol: &str,
) -> Box<dyn GraphLog<K> + 'writer>
where
K: Clone + Eq + Hash + 'writer,
Expand All @@ -112,8 +96,6 @@ impl<'writer, R> SaplingGraphLog<'writer, R> {
Box::new(SaplingGraphLog {
renderer,
writer: formatter,
default_node_symbol: default_node_symbol.to_owned(),
elided_node_symbol: elided_node_symbol.to_owned(),
})
}
}
Expand All @@ -123,34 +105,13 @@ pub fn get_graphlog<'a, K: Clone + Eq + Hash + 'a>(
formatter: &'a mut dyn Write,
) -> Box<dyn GraphLog<K> + 'a> {
let builder = GraphRowRenderer::new().output().with_min_row_height(0);

let (default_node_symbol, elided_node_symbol) = settings.node_symbols();

match settings.graph_style().as_str() {
"square" => SaplingGraphLog::create(
builder.build_box_drawing().with_square_glyphs(),
formatter,
&default_node_symbol,
&elided_node_symbol,
),
"ascii" => SaplingGraphLog::create(
builder.build_ascii(),
formatter,
&default_node_symbol,
&elided_node_symbol,
),
"ascii-large" => SaplingGraphLog::create(
builder.build_ascii_large(),
formatter,
&default_node_symbol,
&elided_node_symbol,
),
"square" => {
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,
&default_node_symbol,
&elided_node_symbol,
),
_ => SaplingGraphLog::create(builder.build_box_drawing(), formatter),
}
}
27 changes: 14 additions & 13 deletions lib/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,19 +242,12 @@ impl UserSettings {
.unwrap_or_else(|_| "curved".to_string())
}

pub fn node_symbols(&self) -> (String, String) {
let default_node_symbol = self.config.get_string("ui.graph.default_node");
let elided_node_symbol = self.config.get_string("ui.graph.elided_node");
match self.graph_style().as_str() {
"ascii" | "ascii-large" => (
default_node_symbol.unwrap_or("o".to_owned()),
elided_node_symbol.unwrap_or(".".to_owned()),
),
_ => (
default_node_symbol.unwrap_or("◉".to_owned()),
elided_node_symbol.unwrap_or("◌".to_owned()),
),
}
pub fn default_node_symbol(&self) -> String {
self.node_symbol_for_key("ui.graph.default_node", "◉", "o")
}

pub fn elided_node_symbol(&self) -> String {
self.node_symbol_for_key("ui.graph.elided_node", "◌", ".")
}

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

fn node_symbol_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 50ff206

Please sign in to comment.