Add a horizontal scale
[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
TF
18
19$opt{stack} = 1 unless $opt{cancel} or $opt{overlay};
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};
49 my $can = $add{$date} > $del{$date} ?
50 $add{$date} - $del{$date} :
51 $del{$date} - $add{$date};
52 $canmax = $can if $canmax < $can;
53}
54
55my $max = $opt{cancel} ? $canmax :
56 $opt{stack} ? $addmax + $delmax :
57 $addmax > $delmax ? $addmax : $delmax;
cce87900
TF
58my $scale = $width / $max;
59
37fadc81
TF
60my $today = int (time / 86400);
61
62for my $date ($today - $height .. $today) {
cce87900
TF
63 my $add;
64 my $del;
65 my $mod;
37fadc81
TF
66 $add{$date} //= 0;
67 $del{$date} //= 0;
cce87900
TF
68 if ($opt{cancel}) {
69 if ($add{$date} > $del{$date}) {
70 $add = $add{$date} - $del{$date};
71 $del = $mod = 0;
72 } else {
73 $del = $del{$date} - $add{$date};
74 $add = $mod = 0;
75 }
76 }
77 if ($opt{overlay}) {
78 if ($add{$date} > $del{$date}) {
79 $add = $add{$date} - $del{$date};
80 $del = 0;
81 $mod = $del{$date};
82 } else {
83 $del = $del{$date} - $add{$date};
84 $add = 0;
85 $mod = $add{$date};
86 }
87 }
88 if ($opt{stack}) {
89 $add = $add{$date};
90 $del = $del{$date};
91 $mod = 0;
92 }
37fadc81
TF
93 my $greg = strftime "%b %d %w", localtime $date * 86400;
94 $greg =~ s{(\d)$}{substr "sMTWTFs", $1, 1}e;
95 printf "$greg \e[33m%s\e[32m%s\e[31m%s\e[0m\n",
96 "#" x ($mod && 1 + $mod * $scale),
97 "+" x ($add && 1 + $add * $scale),
98 "-" x ($del && 1 + $del * $scale);
cce87900 99}
eed91edb
TF
100printf " \e[33m#\e[0m <- %-8d %s %8d ->\n",
101 1/$scale,
102 " " x ($width - 25),
103 $max;