Skip to content

Commit

Permalink
Feature Addition: Reverse Option (#79)
Browse files Browse the repository at this point in the history
Hey mlange-42,

Just wanted to drop in a pull request for something you might find a bit
hacky, but I found myself needing a reverse option. The approach here is
a bit unconventional – instead of overhauling the code, I went for
reversing the text_lines and grid. To tackle the issue of characters
facing the wrong direction, I flipped them (so a 'left-up' becomes
'left-down', and so on).

It might be more ideal to adjust how the Character struct is accessed
with the reverse option in mind, but that would require a significant
refactor of the existing code. From what I've tested, it looks like it
works pretty well across different character sets. Would love to hear
your thoughts on this and if it's something you'd consider merging.

Cheers!
  • Loading branch information
djosh34 committed May 6, 2024
1 parent 9bd54eb commit f9f3c73
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,8 +683,8 @@ fn extract_branches(

/// Traces back branches by following 1st commit parent,
/// until a commit is reached that already has a trace.
fn trace_branch<'repo>(
repository: &'repo Repository,
fn trace_branch(
repository: &Repository,
commits: &mut [CommitInfo],
indices: &HashMap<Oid, usize>,
branches: &mut [BranchInfo],
Expand Down
17 changes: 17 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ fn from_args() -> Result<(), String> {
git-graph model -> Show repo's current branching models\n \
git-graph model <model> -> Permanently set model <model> for this repo",
)
.arg(
Arg::new("reverse")
.long("reverse")
.short('r')
.help("Reverse the order of commits.")
.required(false)
.num_args(0),
)
.arg(
Arg::new("path")
.long("path")
Expand Down Expand Up @@ -272,6 +280,8 @@ fn from_args() -> Result<(), String> {

let include_remote = !matches.get_flag("local");

let reverse_commit_order = matches.get_flag("reverse");

let svg = matches.get_flag("svg");
let pager = !matches.get_flag("no-pager");
let compact = !matches.get_flag("sparse");
Expand All @@ -281,6 +291,12 @@ fn from_args() -> Result<(), String> {
.map(|s| Characters::from_str(s))
.unwrap_or_else(|| Ok(Characters::thin()))?;

let style = if reverse_commit_order {
style.reverse()
} else {
style
};

let model = get_model(
&repository,
matches.get_one::<String>("model").map(|s| &s[..]),
Expand Down Expand Up @@ -364,6 +380,7 @@ fn from_args() -> Result<(), String> {
};

let settings = Settings {
reverse_commit_order,
debug,
colored,
compact,
Expand Down
9 changes: 9 additions & 0 deletions src/print/unicode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ pub fn print_unicode(graph: &GitGraph, settings: &Settings) -> Result<UnicodeGra
}
}

if settings.reverse_commit_order {
text_lines.reverse();
grid.reverse();
}

let lines = print_graph(&settings.characters, &grid, text_lines, settings.colored);

Ok((lines.0, lines.1, index_map))
Expand Down Expand Up @@ -682,6 +687,10 @@ impl Grid {
data: vec![initial; width * height],
}
}

pub fn reverse(&mut self) {
self.data.reverse();
}
pub fn index(&self, x: usize, y: usize) -> usize {
y * self.width + x
}
Expand Down
14 changes: 14 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub enum BranchOrder {

/// Top-level settings
pub struct Settings {
/// Reverse the order of commits
pub reverse_commit_order: bool,
/// Debug printing and drawing
pub debug: bool,
/// Compact text-based graph
Expand Down Expand Up @@ -336,4 +338,16 @@ impl Characters {
chars: " *o|-+'..'||++<>".chars().collect(),
}
}

pub fn reverse(self) -> Self {
let mut chars = self.chars;

chars.swap(6, 8);
chars.swap(7, 9);
chars.swap(10, 11);
chars.swap(12, 13);
chars.swap(14, 15);

Characters { chars }
}
}

0 comments on commit f9f3c73

Please sign in to comment.