Trim more redundant code
[u/fanf2/git-graph.git] / git-graph
CommitLineData
cce87900
TF
1#!/usr/bin/perl
2
3use warnings;
4use strict;
5
6use Getopt::Long qw{:config gnu_getopt posix_default};
37fadc81 7use POSIX;
cce87900
TF
8use Term::ReadKey;
9
10$0 =~ s{^.*/([^/]+)$}{$1};
11
cce87900
TF
12my %opt;
13GetOptions(\%opt, qw{
37fadc81
TF
14 cancel|c
15 overlay|o
16 stack|s
c562dc48 17}) or exit 1;
cce87900 18
22e04a63 19$opt{overlay} = 1 unless $opt{cancel} or $opt{stack};
cce87900 20
37fadc81 21my @terminal = GetTerminalSize;
c562dc48 22my $width = $terminal[0] - 11;
eed91edb 23my $height = $terminal[1] - 2;
37fadc81 24
cce87900
TF
25my %add;
26my %del;
27
28my $addmax = 0;
29my $delmax = 0;
30my $canmax = 0;
31
c562dc48
TF
32my $date;
33
34for (qx{git log --pretty=format:%ct --shortstat --since='$height days ago'}) {
35 next if m{^\s*$};
36 if (m{^(\d{10})\n$}) {
37 $date = int ($1 / 86400);
38 next;
39 }
40 m{^\ \d+\ files?\ changed
41 (?:,\ (\d+)\ insertion[s()+]+)?
42 (?:,\ (\d+)\ deletion[s()-]+)?$}x
43 or die "$0: bad stats $_";
cce87900
TF
44 my ($add,$del) = ($1 || 0, $2 || 0);
45 $add{$date} += $add;
46 $del{$date} += $del;
47 $addmax = $add{$date} if $addmax < $add{$date};
48 $delmax = $del{$date} if $delmax < $del{$date};
bceb4717 49 my $can = abs $add{$date} - $del{$date};
cce87900
TF
50 $canmax = $can if $canmax < $can;
51}
52
53my $max = $opt{cancel} ? $canmax :
54 $opt{stack} ? $addmax + $delmax :
55 $addmax > $delmax ? $addmax : $delmax;
cce87900
TF
56my $scale = $width / $max;
57
37fadc81
TF
58my $today = int (time / 86400);
59
60for my $date ($today - $height .. $today) {
d419a729
TF
61 my $add = 0;
62 my $del = 0;
63 my $mod = 0;
37fadc81
TF
64 $add{$date} //= 0;
65 $del{$date} //= 0;
d419a729 66 if ($opt{cancel} or $opt{overlay}) {
cce87900
TF
67 if ($add{$date} > $del{$date}) {
68 $add = $add{$date} - $del{$date};
cce87900
TF
69 } else {
70 $del = $del{$date} - $add{$date};
cce87900
TF
71 }
72 }
73 if ($opt{overlay}) {
d419a729
TF
74 $mod = $add{$date} < $del{$date} ?
75 $add{$date} : $del{$date};
cce87900
TF
76 }
77 if ($opt{stack}) {
78 $add = $add{$date};
79 $del = $del{$date};
cce87900 80 }
37fadc81
TF
81 my $greg = strftime "%b %d %w", localtime $date * 86400;
82 $greg =~ s{(\d)$}{substr "sMTWTFs", $1, 1}e;
83 printf "$greg \e[33m%s\e[32m%s\e[31m%s\e[0m\n",
84 "#" x ($mod && 1 + $mod * $scale),
85 "+" x ($add && 1 + $add * $scale),
86 "-" x ($del && 1 + $del * $scale);
cce87900 87}
cb4ff77a 88printf " \e[33m#\e[0m <- %-8.1f %s %8d ->\n",
bceb4717 89 1/$scale, " " x ($width - 25), $max;