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