From 9b1f5d2b5554c0ff9f355866d4669b3ffcaa3686 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 9 Feb 2024 21:56:33 +0100 Subject: Moved the standalone activity.lua into activity.generators --- Makefile | 11 ++--- activity.lua | 97 ------------------------------------------ activity/generators.lua | 97 ++++++++++++++++++++++++++++++++++++++++++ generate.lua | 7 +-- spec/activity_spec.lua | 108 ----------------------------------------------- spec/generators_spec.lua | 108 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 213 insertions(+), 215 deletions(-) delete mode 100644 activity.lua create mode 100644 activity/generators.lua mode change 100644 => 100755 generate.lua delete mode 100644 spec/activity_spec.lua create mode 100644 spec/generators_spec.lua diff --git a/Makefile b/Makefile index 4ea4d71..236b04d 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,6 @@ -LUA_VERSION?=5.4 -PREFIX?=/usr/local +LUA_VERSION=5.4 +PREFIX=/usr/local BINDIR=$(PREFIX)/bin -LIBDIR=$(PREFIX)/lib DATADIR=$(PREFIX)/share LUA_LMOD=$(DATADIR)/lua/$(LUA_VERSION) @@ -9,15 +8,13 @@ all: @echo Nothing to be built install: - install -m644 -Dt $(DESTDIR)$(LUA_LMOD) activity.lua install -m644 -Dt $(DESTDIR)$(LUA_LMOD)/activity activity/*.lua install -m644 -Dt $(DESTDIR)$(LUA_LMOD)/activity/formats activity/formats/*.lua - install -m755 -Dt $(DESTDIR)$(BINDIR) generate.lua + install -m755 -DT generate.lua $(DESTDIR)$(BINDIR)/activity uninstall: - rm -f $(DESTDIR)$(LUA_LMOD)/activity.lua rm -rf $(DESTDIR)$(LUA_LMOD)/activity - rm -f $(DESTIR)$(BINDIR)/generate.lua + rm -f $(DESTIR)$(BINDIR)/activity test: busted diff --git a/activity.lua b/activity.lua deleted file mode 100644 index 55cb061..0000000 --- a/activity.lua +++ /dev/null @@ -1,97 +0,0 @@ -local dates = require "activity.dates" -local git = require "activity.git" -local activity = {} - - -local -function level (count) - if 0 < count and count <= 2 then - 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 generate_rolling (lookup, format, date) - date = date or os.date"*t" - local rows = "" - local weekdays = {"", "Mon", "", "Wed", "", "Fri", ""} - - local function year_and_day (week, weekday) - local days = week * 7 + date.wday - weekday - local at = dates.before(date, days) - return at.year, dates.day_of_year(at) - end - - local head = format.start_document"Activity" .. format.start_table() - for index, weekday in pairs(weekdays) do - rows = rows .. format.start_row() .. format.label(weekday) - for week=0,51 do - rows = rows .. format.cell(level(lookup(year_and_day(52 - week, index)))) - end - if date.wday >= index then - rows = rows .. format.cell(level(lookup(year_and_day(0, index)))) - else - rows = rows .. format.spot() - end - rows = rows .. format.end_row() - end - return head .. rows .. format.end_table() .. format.end_document() -end - - -function activity.generate_table (year, lookup, format, ...) - if year == "rolling" then - return generate_rolling(lookup, format, ...) - end - year = year or dates.this_year() - - local function day_of_year (first_weekday, week, weekday) - return week * 7 + weekday - first_weekday + 1 - 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 - end - return format.cell - end - - 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(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(day_level(start_from, 52, index)) .. format.end_row() - 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, start_from, index >= start_from, index <= end_at) - end - return format.start_document"Activity" .. format.start_table() .. rows .. format.end_table() .. format.end_document() -end - - -return activity diff --git a/activity/generators.lua b/activity/generators.lua new file mode 100644 index 0000000..ff7f0a9 --- /dev/null +++ b/activity/generators.lua @@ -0,0 +1,97 @@ +local dates = require "activity.dates" +local git = require "activity.git" +local generators = {} + + +local +function level (count) + if 0 < count and count <= 2 then + 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 generate_rolling (lookup, format, date) + date = date or os.date"*t" + local rows = "" + local weekdays = {"", "Mon", "", "Wed", "", "Fri", ""} + + local function year_and_day (week, weekday) + local days = week * 7 + date.wday - weekday + local at = dates.before(date, days) + return at.year, dates.day_of_year(at) + end + + local head = format.start_document"Activity" .. format.start_table() + for index, weekday in pairs(weekdays) do + rows = rows .. format.start_row() .. format.label(weekday) + for week=0,51 do + rows = rows .. format.cell(level(lookup(year_and_day(52 - week, index)))) + end + if date.wday >= index then + rows = rows .. format.cell(level(lookup(year_and_day(0, index)))) + else + rows = rows .. format.spot() + end + rows = rows .. format.end_row() + end + return head .. rows .. format.end_table() .. format.end_document() +end + + +function generators.generate_table (year, lookup, format, ...) + if year == "rolling" then + return generate_rolling(lookup, format, ...) + end + year = year or dates.this_year() + + local function day_of_year (first_weekday, week, weekday) + return week * 7 + weekday - first_weekday + 1 + 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 + end + return format.cell + end + + 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(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(day_level(start_from, 52, index)) .. format.end_row() + 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, start_from, index >= start_from, index <= end_at) + end + return format.start_document"Activity" .. format.start_table() .. rows .. format.end_table() .. format.end_document() +end + + +return generators diff --git a/generate.lua b/generate.lua old mode 100644 new mode 100755 index cea6a87..478bc9d --- a/generate.lua +++ b/generate.lua @@ -6,15 +6,16 @@ Generates activity chart -R Generate rolling chart (optional string) Paths to repositories with activity ]] -local activity = require "activity" +local generators = require "activity.generators" 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)) + io.stderr:write(("could not find specified format: %q\n\n"):format(args.f)) + io.stderr:write(maybe_format, "\n") os.exit(1) end local maybe_year = args.y if args.R then maybe_year = "rolling" end -io.write(activity.generate_table(maybe_year, git.lookup(args.repos), maybe_format)) +io.write(generators.generate_table(maybe_year, git.lookup(args.repos), maybe_format)) diff --git a/spec/activity_spec.lua b/spec/activity_spec.lua deleted file mode 100644 index 38338e8..0000000 --- a/spec/activity_spec.lua +++ /dev/null @@ -1,108 +0,0 @@ -local activity = require "activity" -local dates = require "activity.dates" -local plain = require "activity.formats.plain" - -local Y2024 = [[ - _0000000000000000000000000000000000000000000000000000 -Mon 00000000000000000000000000000000000000000000000000000 - 00000000000000000000000000000000000000000000000000000 -Wed 0000000000000000000000000000000000000000000000000000_ - 0000000000000000000000000000000000000000000000000000_ -Fri 0000000000000000000000000000000000000000000000000000_ - 0000000000000000000000000000000000000000000000000000_ -]] - -local Y2023 = [[ - 00000000000000000000000000000000000000000000000000000 -Mon 0000000000000000000000000000000000000000000000000000_ - 0000000000000000000000000000000000000000000000000000_ -Wed 0000000000000000000000000000000000000000000000000000_ - 0000000000000000000000000000000000000000000000000000_ -Fri 0000000000000000000000000000000000000000000000000000_ - 0000000000000000000000000000000000000000000000000000_ -]] - -local Y2020 = [[ - _0000000000000000000000000000000000000000000000000000 -Mon _0000000000000000000000000000000000000000000000000000 - _0000000000000000000000000000000000000000000000000000 -Wed 00000000000000000000000000000000000000000000000000000 - 00000000000000000000000000000000000000000000000000000 -Fri 0000000000000000000000000000000000000000000000000000_ - 0000000000000000000000000000000000000000000000000000_ -]] - -local ROLL = [[ - 00000000000000000000000000000000000000000000000000000 -Mon 00000000000000000000000000000000000000000000000000000 - 00000000000000000000000000000000000000000000000000000 -Wed 00000000000000000000000000000000000000000000000000000 - 00000000000000000000000000000000000000000000000000000 -Fri 0000000000000000000000000000000000000000000000000000_ - 0000000000000000000000000000000000000000000000000000_ -]] - - -local -function always_zero (...) - return 0 -end - - -describe("Yearly chart generator", function() - describe("shall generate empty table for year", function() - it("2024", function() - assert.are.equal(Y2024, activity.generate_table(2024, always_zero, plain)) - end) - - it("2023", function() - assert.are.equal(Y2023, activity.generate_table(2023, always_zero, plain)) - end) - - it("2020", function() - assert.are.equal(Y2020, activity.generate_table(2020, always_zero, 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) - - -describe("Rolling chart generator", function() - it("shall generate empty table", function() - local date = os.date("*t", os.time{year=2024, month=2, day=8}) - assert.are.equal(ROLL, activity.generate_table("rolling", always_zero, plain, date)) - end) - - it("shall put activity into cells", function() - local date = os.date("*t", os.time{year=2024, month=2, day=8}) - - local function lookup (year, day) - if year == 2023 and day >= 36 then - return 1 - end - if year == 2024 and day <= 39 then - return 1 - end - return 0 - end - - assert.are.equal(ROLL:gsub("0", "1"), activity.generate_table("rolling", lookup, plain, date)) - end) -end) diff --git a/spec/generators_spec.lua b/spec/generators_spec.lua new file mode 100644 index 0000000..a6c1e9a --- /dev/null +++ b/spec/generators_spec.lua @@ -0,0 +1,108 @@ +local dates = require "activity.dates" +local generators = require "activity.generators" +local plain = require "activity.formats.plain" + +local Y2024 = [[ + _0000000000000000000000000000000000000000000000000000 +Mon 00000000000000000000000000000000000000000000000000000 + 00000000000000000000000000000000000000000000000000000 +Wed 0000000000000000000000000000000000000000000000000000_ + 0000000000000000000000000000000000000000000000000000_ +Fri 0000000000000000000000000000000000000000000000000000_ + 0000000000000000000000000000000000000000000000000000_ +]] + +local Y2023 = [[ + 00000000000000000000000000000000000000000000000000000 +Mon 0000000000000000000000000000000000000000000000000000_ + 0000000000000000000000000000000000000000000000000000_ +Wed 0000000000000000000000000000000000000000000000000000_ + 0000000000000000000000000000000000000000000000000000_ +Fri 0000000000000000000000000000000000000000000000000000_ + 0000000000000000000000000000000000000000000000000000_ +]] + +local Y2020 = [[ + _0000000000000000000000000000000000000000000000000000 +Mon _0000000000000000000000000000000000000000000000000000 + _0000000000000000000000000000000000000000000000000000 +Wed 00000000000000000000000000000000000000000000000000000 + 00000000000000000000000000000000000000000000000000000 +Fri 0000000000000000000000000000000000000000000000000000_ + 0000000000000000000000000000000000000000000000000000_ +]] + +local ROLL = [[ + 00000000000000000000000000000000000000000000000000000 +Mon 00000000000000000000000000000000000000000000000000000 + 00000000000000000000000000000000000000000000000000000 +Wed 00000000000000000000000000000000000000000000000000000 + 00000000000000000000000000000000000000000000000000000 +Fri 0000000000000000000000000000000000000000000000000000_ + 0000000000000000000000000000000000000000000000000000_ +]] + + +local +function always_zero (...) + return 0 +end + + +describe("Yearly chart generator", function() + describe("shall generate empty table for year", function() + it("2024", function() + assert.are.equal(Y2024, generators.generate_table(2024, always_zero, plain)) + end) + + it("2023", function() + assert.are.equal(Y2023, generators.generate_table(2023, always_zero, plain)) + end) + + it("2020", function() + assert.are.equal(Y2020, generators.generate_table(2020, always_zero, 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"), generators.generate_table(2023, lookup, plain)) + end) + + it("leap years", function() + assert.are.equal(Y2024:gsub("0", "1"), generators.generate_table(2024, lookup, plain)) + end) + end) +end) + + +describe("Rolling chart generator", function() + it("shall generate empty table", function() + local date = os.date("*t", os.time{year=2024, month=2, day=8}) + assert.are.equal(ROLL, generators.generate_table("rolling", always_zero, plain, date)) + end) + + it("shall put activity into cells", function() + local date = os.date("*t", os.time{year=2024, month=2, day=8}) + + local function lookup (year, day) + if year == 2023 and day >= 36 then + return 1 + end + if year == 2024 and day <= 39 then + return 1 + end + return 0 + end + + assert.are.equal(ROLL:gsub("0", "1"), generators.generate_table("rolling", lookup, plain, date)) + end) +end) -- cgit v1.1