diff options
-rw-r--r-- | activity.lua | 28 | ||||
-rw-r--r-- | activity/git.lua | 4 | ||||
-rw-r--r-- | generate.lua | 3 | ||||
-rw-r--r-- | spec/activity_spec.lua | 26 | ||||
-rw-r--r-- | spec/dates_spec.lua | 2 |
5 files changed, 46 insertions, 17 deletions
diff --git a/activity.lua b/activity.lua index 8bec2a0..9a602b5 100644 --- a/activity.lua +++ b/activity.lua @@ -3,13 +3,15 @@ local git = require "activity.git" local activity = {} -function activity.generate_table (year, repositories, format) +function activity.generate_table (year, lookup, format) 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? + local function day_of_year (first_weekday, week, weekday) + return week * 7 + weekday - first_weekday + 1 + end + + local function level (count) + if 0 < count and count <= 2 then return 1 end if 2 < count and count <= 4 then @@ -24,6 +26,10 @@ function activity.generate_table (year, repositories, format) return 0 end + local function day_level (first_weekday, week, weekday) + return level(lookup(year, day_of_year(first_weekday, week, weekday))) + end + local function cell (exists) if not exists then return format.spot @@ -31,15 +37,15 @@ function activity.generate_table (year, repositories, format) return format.cell end - local function row (weekday, index, offset, first, last) + local function row (weekday, index, start_from, first, last) first = cell(first) last = cell(last) local row = format.start_row() .. format.label(weekday) - row = row .. first(count(index, offset, 0)) - for week=2,52 do - row = row .. format.cell(count(index, offset, week)) + row = row .. first(day_level(start_from, 0, index)) + for week=1,51 do + row = row .. format.cell(day_level(start_from, week, index)) end - row = row .. last(count(index, offset, 53)) .. format.end_row() + row = row .. last(day_level(start_from, 52, index)) .. format.end_row() return row end @@ -48,7 +54,7 @@ function activity.generate_table (year, repositories, format) 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) + rows = rows .. row(weekday, index, start_from, index >= start_from, index <= end_at) end return format.start_document"Activity" .. format.start_table() .. rows .. format.end_table() .. format.end_document() diff --git a/activity/git.lua b/activity/git.lua index c1219ad..978dfec 100644 --- a/activity/git.lua +++ b/activity/git.lua @@ -33,7 +33,9 @@ function git.lookup (repositories) lookup[year] = lookup[year] or {} lookup[year][day] = (lookup[year][day] or 0) + 1 end - return lookup + return function (year, day) + return (lookup[year] or {})[day] or 0 + end end diff --git a/generate.lua b/generate.lua index 9bbfc09..a216808 100644 --- a/generate.lua +++ b/generate.lua @@ -6,9 +6,10 @@ Generates activity chart <repos...> (optional string) Paths to repositories with activity ]] local activity = require "activity" +local git = require "activity.git" local ok, maybe_format = pcall(require, "activity.formats." .. args.f) if not ok then io.stderr:write(("couldn't find specified format: %q\n"):format(args.f)) os.exit(1) end -io.write(activity.generate_table(args.y, args.repos, maybe_format)) +io.write(activity.generate_table(args.y, git.lookup(args.repos), maybe_format)) diff --git a/spec/activity_spec.lua b/spec/activity_spec.lua index 634a228..197b57f 100644 --- a/spec/activity_spec.lua +++ b/spec/activity_spec.lua @@ -1,4 +1,5 @@ local activity = require "activity" +local dates = require "activity.dates" local plain = require "activity.formats.plain" local Y2024 = [[ @@ -34,16 +35,35 @@ Fri 0000000000000000000000000000000000000000000000000000_ describe("Generator", function() describe("shall generate correct table for year", function() + local function lookup (year, day) return 0 end + it("2024", function() - assert.are.equal(Y2024, activity.generate_table(2024, {}, plain)) + assert.are.equal(Y2024, activity.generate_table(2024, lookup, plain)) end) it("2023", function() - assert.are.equal(Y2023, activity.generate_table(2023, {}, plain)) + assert.are.equal(Y2023, activity.generate_table(2023, lookup, plain)) end) it("2020", function() - assert.are.equal(Y2020, activity.generate_table(2020, {}, plain)) + assert.are.equal(Y2020, activity.generate_table(2020, lookup, plain)) + end) + end) + + describe("shall put activity into cells for", function() + local function lookup (year, day) + if day < 1 or dates.days_in(year) < day then + return 0 + end + return 1 + end + + it("regular years", function() + assert.are.equal(Y2023:gsub("0", "1"), activity.generate_table(2023, lookup, plain)) + end) + + it("leap years", function() + assert.are.equal(Y2024:gsub("0", "1"), activity.generate_table(2024, lookup, plain)) end) end) end) diff --git a/spec/dates_spec.lua b/spec/dates_spec.lua index b1d3594..a8b5a71 100644 --- a/spec/dates_spec.lua +++ b/spec/dates_spec.lua @@ -40,7 +40,7 @@ describe("First week day of", function() assert.are.equal(7, dates.first_week_day(2022)) end) - it("2020 is Saturday", function() + it("2020 is Wednesday", function() assert.are.equal(4, dates.first_week_day(2020)) end) end) |