Compare commits

...
2 Commits
Author SHA1 Message Date
tangent 33e94e2ef0 close #1 recalculate_total_pages() added 2026-08-04 21:08:52 -06:00
tangent dfe74510ef close #6 total_pages is being used for sorting 2026-08-04 20:56:24 -06:00
+37 -11
View File
@@ -92,20 +92,20 @@ sort_orders = {
lowest_priority = function(A, B) lowest_priority = function(A, B)
return A.priority < B.priority return A.priority < B.priority
end, end,
most_pages_remaining = function(A, B) most_pages_remaining = function(A, B, data)
if not (A.pages and B.pages) then -- if pages is unknown for both, fallback to percentage if not (A.pages and B.pages) then -- if pages is unknown for both, fallback to percentage
return A.progress < B.progress return A.progress < B.progress
end end
-- TODO figure out an elegant way to pass the required values here local average_page_count = data.total_pages / data.books_with_page_count
-- local average_page_count = data.total_pages / #data.books if not (average_page_count == average_page_count) then
-- the previous default wasn't working for my desires, so I'm using a realistic default now average_page_count = 300 -- based on average novel length
local average_page_count = 300 end
local a = (A.pages or average_page_count) - (A.pages or average_page_count) * A.progress 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 local b = (B.pages or average_page_count) - (B.pages or average_page_count) * B.progress
return a > b return a > b
end, end,
fewest_pages_remaining = function(A, B) fewest_pages_remaining = function(A, B, data)
return not sort_orders.most_pages_remaining(A, B) return not sort_orders.most_pages_remaining(A, B, data)
end, end,
} }
@@ -269,6 +269,18 @@ local book_exists = function(data, book)
return false return false
end end
local recalculate_total_pages = function(data)
data.total_pages = 0
data.books_with_page_count = 0
for i = 1, #data.books do
local book = data.books[i]
if book.pages then
data.total_pages = data.total_pages + book.pages
data.books_with_page_count = data.books_with_page_count + 1
end
end
end
local import_json = function(data, file_name) local import_json = function(data, file_name)
import = load_json(file_name) import = load_json(file_name)
if import.books then if import.books then
@@ -322,11 +334,19 @@ local import_json = function(data, file_name)
end end
end end
recalculate_total_pages(data)
-- we must rely on parent function to save -- we must rely on parent function to save
end end
local prep_sort_function = function(data, sort_function)
-- this allows me to pass data as a third argument for functions that use it
return function(A, B)
return sort_function(A, B, data)
end
end
local get_books_by_preset = function(data, preset_name) local get_books_by_preset = function(data, preset_name)
local selected_books = {} local selected_books = {}
for i = 1, #data.books do for i = 1, #data.books do
@@ -335,7 +355,7 @@ local get_books_by_preset = function(data, preset_name)
selected_books[#selected_books + 1] = book selected_books[#selected_books + 1] = book
end end
end end
table.sort(selected_books, sort_orders[data.defaults[preset_name].sort]) table.sort(selected_books, prep_sort_function(sort_orders[data.defaults[preset_name].sort], data))
return selected_books return selected_books
end end
@@ -368,7 +388,7 @@ local print_list = function(data, options)
selected_books[#selected_books + 1] = book selected_books[#selected_books + 1] = book
end end
end end
table.sort(selected_books, sort_orders[options[2]]) table.sort(selected_books, prep_sort_function(sort_orders[options[2]], data))
local limit = 10 local limit = 10
if options[3] == "all" then if options[3] == "all" then
@@ -390,6 +410,9 @@ local launch = function(file_name)
if path_exists(file_name) then if path_exists(file_name) then
data = load_json(file_name) data = load_json(file_name)
-- TODO verify data structure -- TODO verify data structure
if not data.books_with_page_count then
recalculate_total_pages(data)
end
else else
data = { data = {
books = {}, books = {},
@@ -398,6 +421,7 @@ local launch = function(file_name)
maximum_change = 2.5, maximum_change = 2.5,
}, },
total_pages = 0, total_pages = 0,
books_with_page_count = 0,
defaults = { defaults = {
launch = { launch = {
filter = "in_progress", filter = "in_progress",
@@ -419,7 +443,7 @@ local main = function(data, selected_books)
print(" " .. (i == 10 and "0" or i) .. ". " .. get_display_name(book)) print(" " .. (i == 10 and "0" or i) .. ". " .. get_display_name(book))
end end
print("Commands: " .. (#selected_books > 0 and "[0-9] to modify a book's progress. " or "") .. "[i <file>] to import from a JSON") print("Commands: " .. (#selected_books > 0 and "[0-9] to modify a book's progress. " or "") .. "[i <file>] to import from a JSON")
print(" file. Enter nothing to exit.") print(" file. [recalculate pages] Enter nothing to exit.")
print(" Adding/Selecting: Title OR Title by Author OR \"Name (type)\" for other types.") print(" Adding/Selecting: Title OR Title by Author OR \"Name (type)\" for other types.")
print(" Listing: list [filter] [sort] (all)") print(" Listing: list [filter] [sort] (all)")
-- TODO implement elo ranking -- TODO implement elo ranking
@@ -436,7 +460,9 @@ local main = function(data, selected_books)
elseif input:find("list ") == 1 then elseif input:find("list ") == 1 then
print_list(data, input:sub(6)) print_list(data, input:sub(6))
return true return true
else -- assumed we are trying to add/select a book elseif input:find("recalculate pages") == 1 then
recalculate_total_pages(data)
else -- assume we are trying to add/select a book
get_book(data, input) get_book(data, input)
-- but that just returns a book, we need to DO something with it ?? -- but that just returns a book, we need to DO something with it ??
-- right now, it demands a progress update; -- right now, it demands a progress update;