summaryrefslogtreecommitdiff
path: root/activity.lua
blob: 5aefbdc337583f6ef8a510a9059b3e141b2ad161 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
local dates = require "activity.dates"
local git = require "activity.git"
local activity = {}


function activity.generate_table (year, repositories)
	year = year or dates.this_year()
	local lookup = git.lookup(repositories)

	local function count (weekday, offset, week)  -- TODO: filtering
		local count = (lookup[year] or {})[weekday + offset + week * 7] or 0
		if 0 < count and count <= 2 then  -- TODO: maybe relative to maximum?
			return 1
		end
		if 2 < count and count <= 4 then
			return 2
		end
		if 4 < count and count <= 6 then
			return 3
		end
		if 6 < count then
			return 4
		end
		return 0
	end

	local function spot ()
		return [[<td>]]
	end

	local function day (level)
		return ([[<td data-activity-level="%d">]]):format(level)
	end

	local function cell (exists)
		if not exists then
			return spot
		end
		return day
	end

	local function row (weekday, index, offset, first, last)
		first = cell(first)
		last = cell(last)
		local row = ([[<tr><td class="activity-chart-label"><span>%s</span>]]):format(weekday)
		row = row .. first(count(index, offset, 0))
		for week=2,52 do
			row = row .. day(count(index, offset, week))
		end
		row = row .. last(count(index, offset, 53)) .. "\n"
		return row
	end

	local rows = ""
	local weekdays = {"", "Mon", "", "Wed", "", "Fri", ""}
	local start_from = dates.first_week_day(year)
	local end_at = (start_from + dates.days_in(year) - 1) % 7
	for index, weekday in pairs(weekdays) do
		rows = rows .. row(weekday, index, 8 - start_from, index >= start_from, index <= end_at)
	end
	return [[<table class="activity-chart">]] .. "\n" .. rows .. "</table>"
end


return activity