Skip to content

Commit

Permalink
refactor(agenda): Attach sort function to exported module
Browse files Browse the repository at this point in the history
  • Loading branch information
kristijanhusak committed Jan 6, 2025
1 parent 630f997 commit ad88620
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 46 deletions.
71 changes: 36 additions & 35 deletions lua/orgmode/agenda/views/agenda.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,6 @@ local function sort_by_date_or_priority_or_category(a, b)
return a.index < b.index
end

---@param agenda_items OrgAgendaItem[]
---@return OrgAgendaItem[]
local function sort_agenda_items(agenda_items)
table.sort(agenda_items, function(a, b)
if a.is_same_day and b.is_same_day then
if a.real_date:has_time() and not b.real_date:has_time() then
return true
end
if b.real_date:has_time() and not a.real_date:has_time() then
return false
end
if a.real_date:has_time() and b.real_date:has_time() then
return a.real_date:is_before(b.real_date)
end
return sort_by_date_or_priority_or_category(a, b)
end

if a.is_same_day and not b.is_same_day then
if a.real_date:has_time() or (b.real_date:is_none() and not a.real_date:is_none()) then
return true
end
end

if not a.is_same_day and b.is_same_day then
if b.real_date:has_time() or (a.real_date:is_none() and not b.real_date:is_none()) then
return false
end
end

return sort_by_date_or_priority_or_category(a, b)
end)
return agenda_items
end

---@class OrgAgendaView
---@field span string|number
---@field from OrgDate
Expand Down Expand Up @@ -157,7 +123,7 @@ function AgendaView:_build_items()
end
end

date.agenda_items = sort_agenda_items(date.agenda_items)
date.agenda_items = self._sort(date.agenda_items)

table.insert(agenda_days, date)
end
Expand Down Expand Up @@ -348,4 +314,39 @@ function AgendaView:_format_day(day)
return string.format('%-10s %s', day:format('%A'), day:format('%d %B %Y'))
end

---@private
---@param agenda_items OrgAgendaItem[]
---@return OrgAgendaItem[]
function AgendaView._sort(agenda_items)
table.sort(agenda_items, function(a, b)
if a.is_same_day and b.is_same_day then
if a.real_date:has_time() and not b.real_date:has_time() then
return true
end
if b.real_date:has_time() and not a.real_date:has_time() then
return false
end
if a.real_date:has_time() and b.real_date:has_time() then
return a.real_date:is_before(b.real_date)
end
return sort_by_date_or_priority_or_category(a, b)
end

if a.is_same_day and not b.is_same_day then
if a.real_date:has_time() or (b.real_date:is_none() and not a.real_date:is_none()) then
return true
end
end

if not a.is_same_day and b.is_same_day then
if b.real_date:has_time() or (a.real_date:is_none() and not b.real_date:is_none()) then
return false
end
end

return sort_by_date_or_priority_or_category(a, b)
end)
return agenda_items
end

return AgendaView
25 changes: 14 additions & 11 deletions lua/orgmode/agenda/views/todos.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ local utils = require('orgmode.utils')
local agenda_highlights = require('orgmode.colors.highlights')
local hl_map = agenda_highlights.get_agenda_hl_map()

local function sort_todos(todos)
table.sort(todos, function(a, b)
if a:get_priority_sort_value() ~= b:get_priority_sort_value() then
return a:get_priority_sort_value() > b:get_priority_sort_value()
end
return a:get_category() < b:get_category()
end)
return todos
end

---@class OrgAgendaTodosView
---@field items table[]
---@field content table[]
Expand Down Expand Up @@ -59,7 +49,7 @@ function AgendaTodosView:build()
end

function AgendaTodosView.generate_view(items, content, filters)
items = sort_todos(items)
items = AgendaTodosView._sort(items)
local offset = #content
local longest_category = utils.reduce(items, function(acc, todo)
return math.max(acc, vim.api.nvim_strwidth(todo:get_category()))
Expand Down Expand Up @@ -142,4 +132,17 @@ function AgendaTodosView.generate_todo_item(headline, longest_category, line_nr)
}
end

---@private
---@param todos OrgHeadline[]
---@return OrgHeadline[]
function AgendaTodosView._sort(todos)
table.sort(todos, function(a, b)
if a:get_priority_sort_value() ~= b:get_priority_sort_value() then
return a:get_priority_sort_value() > b:get_priority_sort_value()
end
return a:get_category() < b:get_category()
end)
return todos
end

return AgendaTodosView

0 comments on commit ad88620

Please sign in to comment.