From f9f3c7359f10830dff3d8534e5fbcf60575f6d9e Mon Sep 17 00:00:00 2001 From: djosh34 <35622390+djosh34@users.noreply.github.com> Date: Mon, 6 May 2024 11:33:25 +0200 Subject: [PATCH] Feature Addition: Reverse Option (#79) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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! --- src/graph.rs | 4 ++-- src/main.rs | 17 +++++++++++++++++ src/print/unicode.rs | 9 +++++++++ src/settings.rs | 14 ++++++++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/graph.rs b/src/graph.rs index ce0bbfe..c658756 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -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, branches: &mut [BranchInfo], diff --git a/src/main.rs b/src/main.rs index 90af35d..6c1fbfc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,6 +52,14 @@ fn from_args() -> Result<(), String> { git-graph model -> Show repo's current branching models\n \ git-graph model -> Permanently set 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") @@ -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"); @@ -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::("model").map(|s| &s[..]), @@ -364,6 +380,7 @@ fn from_args() -> Result<(), String> { }; let settings = Settings { + reverse_commit_order, debug, colored, compact, diff --git a/src/print/unicode.rs b/src/print/unicode.rs index b03e21b..1c14d53 100644 --- a/src/print/unicode.rs +++ b/src/print/unicode.rs @@ -189,6 +189,11 @@ pub fn print_unicode(graph: &GitGraph, settings: &Settings) -> Result usize { y * self.width + x } diff --git a/src/settings.rs b/src/settings.rs index 2d78f2b..28232ff 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -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 @@ -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 } + } }