Skip to content

Commit

Permalink
fix(report): refactor ui
Browse files Browse the repository at this point in the history
  • Loading branch information
FDutina authored and The-Sirius-Black committed Jun 28, 2024
1 parent 5ac389d commit bfb7fb7
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 234 deletions.
25 changes: 12 additions & 13 deletions lua/hardtime/report.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ end

function M.report()
local file_path = vim.api.nvim_call_function("stdpath", { "log" })
.. "/hardtime.nvim.log"
.. "/hardtime.nvim.log"

local file = io.open(file_path, "r")

Expand All @@ -41,7 +41,7 @@ function M.report()
all_hints[hint] = all_hints[hint] and all_hints[hint] + 1 or 1

local day, month, year =
string.match(time_info, "(%d+).(%d+).(%d+) (%d+):(%d+):(%d+)")
string.match(time_info, "(%d+).(%d+).(%d+) (%d+):(%d+):(%d+)")

if day ~= nil and month ~= nil and year ~= nil then
local date = os.time({
Expand All @@ -56,13 +56,13 @@ function M.report()

if util.is_this_week(date) then
weekly_hints[hint] = weekly_hints[hint] and weekly_hints[hint] + 1
or 1
or 1
end

if util.is_this_month(date) then
monthly_hints[hint] = monthly_hints[hint]
and monthly_hints[hint] + 1
or 1
and monthly_hints[hint] + 1
or 1
end
end
end
Expand All @@ -71,16 +71,15 @@ function M.report()

local ReportModel = require("hardtime.ui.report_model")

local content = {
ReportModel.new("All Time (A)", sort_hints(all_hints), "A"),
ReportModel.new("Daily (D)", sort_hints(daily_hints), "D"),
ReportModel.new("Weekly (W)", sort_hints(weekly_hints), "W"),
ReportModel.new("Monthly (M)", sort_hints(monthly_hints), "M"),
local reports = {
ReportModel.new(" All Time (A) ", sort_hints(all_hints), "A"),
ReportModel.new(" Daily (D) ", sort_hints(daily_hints), "D"),
ReportModel.new(" Weekly (W) ", sort_hints(weekly_hints), "W"),
ReportModel.new(" Monthly (M) ", sort_hints(monthly_hints), "M"),
}

local inital_tab = 1

report.open(content, inital_tab)
local initial_index = 1
report.open(reports, initial_index)
end

return M
42 changes: 0 additions & 42 deletions lua/hardtime/ui/content_renderer.lua

This file was deleted.

40 changes: 40 additions & 0 deletions lua/hardtime/ui/highlights.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
local M = {}

M.report_tab_focused = "ReportTabFocused"
M.report_tab_unfocused = "ReportTabUnfocused"
M.title_highlight = "TitleHighlight"

local hl_groups = {
report_tab_unfocused = {
name = M.report_tab_unfocused,
bg = "#888888",
fg = "#222222",
default = true,
},
report_tab_focused = {
name = M.report_tab_focused,
bg = "#ABE9B3",
fg = "#222222",
default = true,
},
title_highlight = {
name = M.title_highlight,
bg = "#6C8EBF",
fg = "#222222",
default = true,
},
}

function M.init()
for _, hl in pairs(hl_groups) do
local command = "highlight "
.. hl.name
.. " guifg="
.. hl.fg
.. " guibg="
.. hl.bg
vim.api.nvim_command(command)
end
end

return M
89 changes: 89 additions & 0 deletions lua/hardtime/ui/renderer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
local highlights = require("hardtime.ui.highlights")

local M = {}

function M.render_title(title, bufnr)
local buf_width = vim.api.nvim_win_get_width(0)

local indent_length = math.floor((buf_width - #title) / 2)

local indent = string.rep(" ", indent_length)

vim.api.nvim_buf_set_lines(bufnr, 0, 0, false, { indent .. title })

vim.api.nvim_buf_add_highlight(
bufnr,
0,
highlights.title_highlight,
0,
indent_length,
indent_length + #title
)
end

function M.render_report(reports, bufnr)
local hints = {}

for index, report in ipairs(reports) do
local repetition = " (" .. report[2] .. ")"
local hint = index .. ". " .. report[1] .. repetition
table.insert(hints, hint)
end

vim.api.nvim_buf_set_lines(bufnr, 6, -1, false, hints)
end

local function find_highlighted_indexes(tabs)
local result = {}

local start = 0

for i = 1, #tabs do
local ending = start + #tabs[i]
table.insert(result, { start, ending })
start = ending + 1
end

return result
end

function M.render_tabs(tabs, picked_tab_idx, bufnr)
highlights.init()

vim.api.nvim_buf_set_lines(
bufnr,
2,
2,
false,
{ table.concat(tabs, " ") }
)

local indexes = find_highlighted_indexes(tabs)

for i, indices in ipairs(indexes) do
local start_index = indices[1]
local end_index = indices[2]

if i == picked_tab_idx then
vim.api.nvim_buf_add_highlight(
bufnr,
2,
highlights.report_tab_focused,
2,
start_index,
end_index
)
else
vim.api.nvim_buf_add_highlight(
bufnr,
2,
highlights.report_tab_unfocused,
2,
start_index,
end_index
)
end
end
end

return M
17 changes: 7 additions & 10 deletions lua/hardtime/ui/report_model.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
---@class ReportModel
---@field tab string
---@field content table<string , string>
---@field keybind string
--- @class ReportModel
--- @field tab string
--- @field report table<string , string>
--- @field keybind string
local ReportModel = {
tab = "",
content = {},
report = {},
keybind = "",
}

--- @param tab string?
--- @param content table<string, string>
--- @param keybind string
function ReportModel.new(tab, content, keybind)
function ReportModel.new(tab, report, keybind)
local self = setmetatable({}, ReportModel)

self.tab = tab
self.content = content
self.report = report
self.keybind = keybind

return self
Expand Down
70 changes: 36 additions & 34 deletions lua/hardtime/ui/state.lua
Original file line number Diff line number Diff line change
@@ -1,41 +1,33 @@
local _ = require("hardtime.ui.report_model")
local tabs_renderer = require("hardtime.ui.tabs_renderer")
local content_renderer = require("hardtime.ui.content_renderer")
local renderer = require("hardtime.ui.renderer")

local M = {}

--- @param content table<ReportModel>
--- @param picked_tab integer
local function render_content(content, picked_tab, bufnr)
local function render_content(reports, picked_tab, bufnr)
vim.api.nvim_buf_set_option(bufnr, "modifiable", true)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, {})

local tabs = {}

for _, tab in ipairs(content) do
table.insert(tabs, tab.tab)
for _, report in ipairs(reports) do
table.insert(tabs, report.tab)
end

content_renderer.render_title("Hardtime Report", bufnr)
tabs_renderer.render_tabs(tabs, picked_tab, bufnr)
content_renderer.spacer(bufnr)
content_renderer.render_hints(content[picked_tab].content, bufnr)
renderer.render_title(" Hardtime Report ", bufnr)

renderer.render_tabs(tabs, picked_tab, bufnr)

vim.api.nvim_buf_set_lines(bufnr, 5, 5, false, { "" })

renderer.render_report(reports[picked_tab].report, bufnr)

vim.api.nvim_buf_set_option(bufnr, "modifiable", false)
end

---@param content table<ReportModel>
local function add_keybinds(content, bufnr)
vim.keymap.set("n", "q", function()
vim.api.nvim_buf_delete(bufnr, { force = true })
end, {
buffer = bufnr,
nowait = true,
silent = true,
})

for i, con in ipairs(content) do
vim.keymap.set("n", con.keybind, function()
render_content(content, i, bufnr)
local function add_keybinds(reports, bufnr)
for index, report in ipairs(reports) do
vim.keymap.set("n", report.keybind, function()
render_content(reports, index, bufnr)
end, {
buffer = bufnr,
nowait = true,
Expand All @@ -46,16 +38,12 @@ end

local is_open = false

--- @param content table<ReportModel>
--- @param initial_tab integer
function M.open(content, initial_tab)
function M.open(reports, initial_tab)
if is_open then
print("Report window is already open")
return
end

local Popup = require("nui.popup")
local event = require("nui.utils.autocmd").event

local popup = Popup({
enter = true,
Expand All @@ -65,21 +53,35 @@ function M.open(content, initial_tab)
width = "60%",
height = "70%",
},
border = {
padding = {
left = 2,
right = 2,
},
},
relative = "editor",
})

popup:mount()
is_open = true

add_keybinds(content, popup.bufnr)
render_content(content, initial_tab, popup.bufnr)
vim.keymap.set("n", "q", function()
is_open = false
popup:unmount()
end, {
buffer = popup.bufnr,
nowait = true,
silent = true,
})

vim.api.nvim_buf_set_option(popup.bufnr, "modifiable", false)
add_keybinds(reports, popup.bufnr)

popup:on(event.BufLeave, function()
popup:unmount()
popup:on("BufWinLeave", function()
is_open = false
end)

render_content(reports, initial_tab, popup.bufnr)
vim.api.nvim_buf_set_option(popup.bufnr, "modifiable", false)
end

return M
Loading

0 comments on commit bfb7fb7

Please sign in to comment.