Skip to content

Commit

Permalink
Consider maximum span of runtime argument names
Browse files Browse the repository at this point in the history
This pre-aligns the table to the length of the longest runtime argument
name.
  • Loading branch information
nvzqz committed Jan 21, 2024
1 parent 75b958c commit 0fce2c4
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/entry/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,29 @@ impl<'a> EntryTree<'a> {
pub fn max_name_span(tree: &[Self], depth: usize) -> usize {
tree.iter()
.map(|node| {
let node_prefix_len = depth * 3;
let node_name_len = node.display_name().chars().count();
let node_name_span = node_prefix_len + node_name_len;

let children_max = Self::max_name_span(node.children(), depth + 1);
let node_name_span = {
let prefix_len = depth * 3;
let name_len = node.display_name().chars().count();
prefix_len + name_len
};

node_name_span.max(children_max)
// The maximum span of any descendent.
let children_max_span = Self::max_name_span(node.children(), depth + 1);

// The maximum span of any runtime argument.
let args_max_span = node
.arg_names()
.unwrap_or_default()
.iter()
.map(|arg| {
let prefix_len = (depth + 1) * 3;
let name_len = arg.chars().count();
prefix_len + name_len
})
.max()
.unwrap_or_default();

node_name_span.max(children_max_span).max(args_max_span)
})
.max()
.unwrap_or_default()
Expand Down Expand Up @@ -378,4 +394,11 @@ impl<'a> EntryTree<'a> {
Self::Parent { children, .. } => children,
}
}

fn arg_names(&self) -> Option<&[&'static &'static str]> {
match self {
Self::Leaf { args, .. } => args.as_deref(),
Self::Parent { .. } => None,
}
}
}

0 comments on commit 0fce2c4

Please sign in to comment.