Skip to content

Commit 1e53022

Browse files
committed
Add git-commit-plot example
1 parent 0228b7f commit 1e53022

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

examples/git-commit-plot.cr

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require "../src/unicode_plot"
2+
3+
include UnicodePlot
4+
5+
# Build as a Git plugin:
6+
# crystal build examples/time_axis.cr -o git-commit-plot
7+
# mv git-commit-plot ~/.local/bin/
8+
# Then run:
9+
# git commit-plot
10+
# git commit-plot /path/to/repo
11+
12+
directory = File.expand_path(ARGV[0]? || Dir.current)
13+
_, terminal_width = UnicodePlot.out_stream_size(STDOUT)
14+
plot_width = Math.max(20, terminal_width - 32)
15+
16+
def date_from_git(value : String) : Time
17+
year, month, day = value.split("-").map(&.to_i)
18+
Time.utc(year, month, day)
19+
end
20+
21+
stdout = IO::Memory.new
22+
stderr = IO::Memory.new
23+
status = Process.run(
24+
"git",
25+
["-C", directory, "log", "--date=short", "--format=%ad"],
26+
output: stdout,
27+
error: stderr
28+
)
29+
30+
unless status.success?
31+
abort "git log failed in #{directory}: #{stderr.to_s.strip}"
32+
end
33+
34+
counts = Hash(Time, Int32).new(0)
35+
stdout.to_s.each_line do |line|
36+
next if line.empty?
37+
counts[date_from_git(line)] += 1
38+
end
39+
40+
abort "no commits found in #{directory}" if counts.empty?
41+
42+
dates = counts.keys.sort!
43+
values = dates.map { |date| counts[date].to_f64 }
44+
45+
plot = stairs(
46+
dates,
47+
values,
48+
format: "%F",
49+
title: "Git commits per day",
50+
xlabel: "commit date",
51+
ylabel: "commits",
52+
name: "commits",
53+
color: :green,
54+
width: plot_width
55+
)
56+
57+
puts plot

0 commit comments

Comments
 (0)