diff --git a/asymptome.lua b/asymptome.lua index 6af0ae6..17fc1e6 100755 --- a/asymptome.lua +++ b/asymptome.lua @@ -1,5 +1,7 @@ #!/usr/bin/env luajit +local filters = require "lib.filters" local json = require "lib.dkjson" +local sort_orders = require "lib.sort_orders" math.randomseed(os.time()) local default_file = "books.json" @@ -50,72 +52,6 @@ local get_random_order = function(array) return order end -local filters = { - all_books = function(book) - if book.hidden then return false end - return true - end, - unread = function(book) - if book.hidden then return false end - if book.progress == 0 then - return true - end - return false - end, - read = function(book) - if book.hidden then return false end - if book.progress == 1 then - return true - end - return false - end, - in_progress = function(book) - if book.hidden then return false end - if book.progress > 0 and book.progress < 1 then - return true - end - return false - end, - exclude_series = function(book) - if book.hidden then return false end - if book.series and not (book.title or book.author) then - return false - end - return true - end, -} - -local sort_orders -sort_orders = { - most_progress = function(A, B) - return A.progress > B.progress - end, - least_progress = function(A, B) - return A.progress < B.progress - end, - highest_priority = function(A, B) - return A.priority > B.priority - end, - lowest_priority = function(A, B) - return A.priority < B.priority - end, - most_pages_remaining = function(A, B, data) - if not (A.pages and B.pages) then -- if pages is unknown for both, fallback to percentage - return A.progress < B.progress - end - local average_page_count = data.total_pages / data.books_with_page_count - if not (average_page_count == average_page_count) then - average_page_count = 300 -- based on average novel length - end - local a = (A.pages or average_page_count) - (A.pages or average_page_count) * A.progress - local b = (B.pages or average_page_count) - (B.pages or average_page_count) * B.progress - return a > b - end, - fewest_pages_remaining = function(A, B, data) - return not sort_orders.most_pages_remaining(A, B, data) - end, -} - local get_display_name = function(book) local result = "" diff --git a/lib/filters.lua b/lib/filters.lua new file mode 100644 index 0000000..108cccb --- /dev/null +++ b/lib/filters.lua @@ -0,0 +1,36 @@ +local filters = { + all_books = function(book) + if book.hidden then return false end + return true + end, + unread = function(book) + if book.hidden then return false end + if book.progress == 0 then + return true + end + return false + end, + read = function(book) + if book.hidden then return false end + if book.progress == 1 then + return true + end + return false + end, + in_progress = function(book) + if book.hidden then return false end + if book.progress > 0 and book.progress < 1 then + return true + end + return false + end, + exclude_series = function(book) + if book.hidden then return false end + if book.series and not (book.title or book.author) then + return false + end + return true + end, +} + +return filters diff --git a/lib/sort_orders.lua b/lib/sort_orders.lua new file mode 100644 index 0000000..36fb273 --- /dev/null +++ b/lib/sort_orders.lua @@ -0,0 +1,32 @@ +local sort_orders +sort_orders = { + most_progress = function(A, B) + return A.progress > B.progress + end, + least_progress = function(A, B) + return A.progress < B.progress + end, + highest_priority = function(A, B) + return A.priority > B.priority + end, + lowest_priority = function(A, B) + return A.priority < B.priority + end, + most_pages_remaining = function(A, B, data) + if not (A.pages and B.pages) then -- if pages is unknown for both, fallback to percentage + return A.progress < B.progress + end + local average_page_count = data.total_pages / data.books_with_page_count + if not (average_page_count == average_page_count) then + average_page_count = 300 -- based on average novel length + end + local a = (A.pages or average_page_count) - (A.pages or average_page_count) * A.progress + local b = (B.pages or average_page_count) - (B.pages or average_page_count) * B.progress + return a > b + end, + fewest_pages_remaining = function(A, B, data) + return not sort_orders.most_pages_remaining(A, B, data) + end, +} + +return sort_orders