Skip to content

Commit

Permalink
feat: allow sorting by other columns
Browse files Browse the repository at this point in the history
This patch adds the `--sort` option, to determine which column is used
to determine the ordering of the rows.

We also add `--reverse` in case someone wants to reverse the order.

Closes: #36
  • Loading branch information
lukehsiao committed Apr 26, 2024
1 parent 960f18e commit 627d3f0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ Arguments:
[revision-range] Show only commits in the specified revision range [default: HEAD]
Options:
-e, --email Show the email address of each author
-r, --reviews Show who reviewed/tested commits based on `Acked-by`, `Tested-by`, and `Reviewed-by` git trailers
-h, --help Print help (see more with '--help')
-V, --version Print version
```
-e, --email Show the email address of each author
-r, --reviews Show who reviewed/tested commits based on `Acked-by`, `Tested-by`, and `Reviewed-by` git trailers
-s, --sort <SORT> What column to sort by [default: commits] [possible values: author, commits, files, insertions, deletions, net]
--reverse Whether to reverse the sorting from descending to ascending
-h, --help Print help (see more with '--help')
-V, --version Print version
```
36 changes: 35 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::str::FromStr;

use anyhow::{bail, Result};
use clap::Parser;
use clap::{Parser, ValueEnum};
use rayon::prelude::*;
use tabled::{
settings::{
Expand Down Expand Up @@ -33,6 +33,28 @@ struct Cli {
/// Show who reviewed/tested commits based on `Acked-by`, `Tested-by`, and
/// `Reviewed-by` git trailers.
reviews: bool,
/// What column to sort by
#[arg(short, long, value_enum, default_value_t = SortBy::Commits)]
sort: SortBy,
/// Whether to reverse the sorting from descending to ascending
#[arg(long)]
reverse: bool,
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
enum SortBy {
/// Sort by author alphabetic order
Author,
/// Sort by number of commits
Commits,
/// Sort by number of files touched
Files,
/// Sort by number of insertions
Insertions,
/// Sort by number of deletions
Deletions,
/// Sort by net lines of change
Net,
}

#[derive(Tabled)]
Expand Down Expand Up @@ -149,6 +171,18 @@ fn main() -> Result<()> {
.filter_map(|r| r.ok())
.collect::<_>();

match cli.sort {
SortBy::Author => stats.sort_unstable_by(|a, b| b.author.cmp(&a.author)),
SortBy::Commits => stats.sort_unstable_by(|a, b| b.commits.cmp(&a.commits)),
SortBy::Files => stats.sort_unstable_by(|a, b| b.num_files.cmp(&a.num_files)),
SortBy::Insertions => stats.sort_unstable_by(|a, b| b.insertions.cmp(&a.insertions)),
SortBy::Deletions => stats.sort_unstable_by(|a, b| b.deletions.cmp(&a.deletions)),
SortBy::Net => stats.sort_unstable_by(|a, b| b.net.cmp(&a.net)),
}
if cli.reverse {
stats.reverse()
}

// Collect totals
let totals = stats.iter().fold(
Stat {
Expand Down

0 comments on commit 627d3f0

Please sign in to comment.