From e907e5729f1d54947180c6a44725b78a0b0e46b2 Mon Sep 17 00:00:00 2001 From: Abizrh Date: Wed, 25 Sep 2024 14:24:30 +0700 Subject: [PATCH 1/3] enhance: add progress bar and spaces between table --- lua/pomodoro/pomodoro.lua | 150 ++++++++++++------------- lua/pomodoro/ui.lua | 224 ++++++++++++++++++-------------------- 2 files changed, 183 insertions(+), 191 deletions(-) diff --git a/lua/pomodoro/pomodoro.lua b/lua/pomodoro/pomodoro.lua index a6337dd..18a6c50 100644 --- a/lua/pomodoro/pomodoro.lua +++ b/lua/pomodoro/pomodoro.lua @@ -30,88 +30,88 @@ pomodoro.phase = Phases.NOT_RUNNING ---@param time number ---@param fn function function pomodoro.startTimer(time, fn) - pomodoro.timer:stop() - pomodoro.timer_duration = time - pomodoro.started_timer_time = uv.now() - pomodoro.timer:start(time, 0, fn) + pomodoro.timer:stop() + pomodoro.timer_duration = time + pomodoro.started_timer_time = uv.now() + pomodoro.timer:start(time, 0, fn) end ----@return boolean - function pomodoro.displayPomodoroUI() - if pomodoro.phase == Phases.NOT_RUNNING or pomodoro.phase == nil then - pomodoro.start() + if pomodoro.phase == Phases.NOT_RUNNING or pomodoro.phase == nil then + pomodoro.start() + end + if UI.isWinOpen() ~= true then + if UI.ui_update_timer:is_active() ~= true then + UI.updateUi(pomodoro) end - if UI.isWinOpen() ~= true then - if UI.ui_update_timer:is_active() ~= true then - UI.updateUi(pomodoro) - end - UI.win = vim.api.nvim_open_win(UI.buffer, true, UI.buffer_opts) - else - if pomodoro.phase ~= Phases.BREAK then - UI.close() - end + UI.win = vim.api.nvim_open_win(UI.buffer, true, UI.buffer_opts) + else + if pomodoro.phase ~= Phases.BREAK then + UI.close() end + end end + function pomodoro.closePomodoroUi() - UI.close() + UI.close() end function pomodoro.startBreak() - info("Break of " .. pomodoro.break_duration / MIN_IN_MS .. "m started!") - pomodoro.phase = Phases.BREAK - vim.schedule(pomodoro.displayPomodoroUI) - pomodoro.startTimer(pomodoro.break_duration, pomodoro.endBreak) + info("Break of " .. pomodoro.break_duration / MIN_IN_MS .. "m started!") + pomodoro.phase = Phases.BREAK + vim.schedule(pomodoro.displayPomodoroUI) + pomodoro.startTimer(pomodoro.break_duration, pomodoro.endBreak) end + function pomodoro.endBreak() - vim.schedule(pomodoro.closePomodoroUi) - pomodoro.phase = Phases.RUNNING - pomodoro.start() + vim.schedule(pomodoro.closePomodoroUi) + pomodoro.phase = Phases.RUNNING + pomodoro.start() end function pomodoro.start() - info( - "Work session of " .. pomodoro.work_duration / MIN_IN_MS .. "m started!" - ) - pomodoro.phase = Phases.RUNNING - pomodoro.startTimer(pomodoro.work_duration, pomodoro.startBreak) + info( + "Work session of " .. pomodoro.work_duration / MIN_IN_MS .. "m started!" + ) + pomodoro.phase = Phases.RUNNING + pomodoro.startTimer(pomodoro.work_duration, pomodoro.startBreak) end function pomodoro.delayBreak() - if pomodoro.phase == Phases.BREAK then - pomodoro.phase = Phases.RUNNING - pomodoro.closePomodoroUi() - pomodoro.startTimer(MIN_IN_MS, pomodoro.startBreak) - end + if pomodoro.phase == Phases.BREAK then + pomodoro.phase = Phases.RUNNING + pomodoro.closePomodoroUi() + pomodoro.startTimer(MIN_IN_MS, pomodoro.startBreak) + end end function pomodoro.stop() - pomodoro.phase = Phases.NOT_RUNNING - pomodoro.timer:stop() - UI.ui_update_timer:stop() - pomodoro.closePomodoroUi() - info("Stopped") + pomodoro.phase = Phases.NOT_RUNNING + pomodoro.timer:stop() + UI.ui_update_timer:stop() + pomodoro.closePomodoroUi() + info("Stopped") end function pomodoro.registerCmds() - vim.api.nvim_create_user_command( - "PomodoroForceBreak", - pomodoro.startBreak, - {} - ) - vim.api.nvim_create_user_command("PomodoroSkipBreak", pomodoro.endBreak, {}) - vim.api.nvim_create_user_command("PomodoroStart", pomodoro.start, {}) - vim.api.nvim_create_user_command("PomodoroStop", pomodoro.stop, {}) - vim.api.nvim_create_user_command( - "PomodoroDelayBreak", - pomodoro.delayBreak, - {} - ) - vim.api.nvim_create_user_command( - "PomodoroUI", - pomodoro.displayPomodoroUI, - {} - ) + vim.api.nvim_create_user_command( + "PomodoroForceBreak", + pomodoro.startBreak, + {} + ) + vim.api.nvim_create_user_command("PomodoroSkipBreak", pomodoro.endBreak, {}) + vim.api.nvim_create_user_command("PomodoroStart", pomodoro.start, {}) + vim.api.nvim_create_user_command("PomodoroStop", pomodoro.stop, {}) + vim.api.nvim_create_user_command( + "PomodoroDelayBreak", + pomodoro.delayBreak, + {} + ) + vim.api.nvim_create_user_command( + "PomodoroUI", + pomodoro.displayPomodoroUI, + {} + ) end ---@class PomodoroOpts @@ -122,26 +122,26 @@ end ---@param opts PomodoroOpts function pomodoro.setup(opts) - if opts then - if opts.work_duration ~= nil then - pomodoro.work_duration = opts.work_duration * MIN_IN_MS - end - if opts.break_duration ~= nil then - pomodoro.break_duration = opts.break_duration * MIN_IN_MS - end - if opts.delay_duration ~= nil then - pomodoro.delay_duration = opts.delay_duration * MIN_IN_MS - end - if opts.start_at_launch ~= nil then - pomodoro.start_at_launch = opts.start_at_launch - end + if opts then + if opts.work_duration ~= nil then + pomodoro.work_duration = opts.work_duration * MIN_IN_MS + end + if opts.break_duration ~= nil then + pomodoro.break_duration = opts.break_duration * MIN_IN_MS end + if opts.delay_duration ~= nil then + pomodoro.delay_duration = opts.delay_duration * MIN_IN_MS + end + if opts.start_at_launch ~= nil then + pomodoro.start_at_launch = opts.start_at_launch + end + end - pomodoro.registerCmds() + pomodoro.registerCmds() - if pomodoro.start_at_launch then - pomodoro.start() - end + if pomodoro.start_at_launch then + pomodoro.start() + end end return pomodoro diff --git a/lua/pomodoro/ui.lua b/lua/pomodoro/ui.lua index 5cd866b..ed0e2de 100644 --- a/lua/pomodoro/ui.lua +++ b/lua/pomodoro/ui.lua @@ -3,152 +3,144 @@ local uv = require("pomodoro.uv") local MIN_IN_MS = constants.MIN_IN_MS local Phases = constants.Phases --- Disable a keymap for a given buffer ----@param buffer integer -- ----@param mode string -- ----@param key string -- local function disable_key(buffer, mode, key) - vim.api.nvim_buf_set_keymap( - buffer, - mode, - key, - "", - { noremap = true, silent = true } - ) + vim.api.nvim_buf_set_keymap( + buffer, + mode, + key, + "", + { noremap = true, silent = true } + ) end --- Set an exit key for a buffer ----@param buffer integer -- ----@param mode string -- ----@param key string -- local function set_command_key(buffer, mode, key, command) - vim.api.nvim_buf_set_keymap( - buffer, - mode, - key, - string.format(":%s", command), - { noremap = true, silent = true } - ) + vim.api.nvim_buf_set_keymap( + buffer, + mode, + key, + string.format(":%s", command), + { noremap = true, silent = true } + ) end --- Apply custom keymaps to a buffer ----@param buffer integer -- + local function apply_buffer_keymaps(buffer) - disable_key(buffer, "n", "d") - disable_key(buffer, "n", "i") - disable_key(buffer, "n", "v") - disable_key(buffer, "n", "r") - disable_key(buffer, "n", "u") - disable_key(buffer, "n", "") - set_command_key(buffer, "n", "Q", "PomodoroStop") - set_command_key(buffer, "n", "W", "PomodoroSkipBreak") - set_command_key(buffer, "n", "B", "PomodoroForceBreak") - set_command_key(buffer, "n", "D", "PomodoroDelayBreak") + disable_key(buffer, "n", "d") + disable_key(buffer, "n", "i") + disable_key(buffer, "n", "v") + disable_key(buffer, "n", "r") + disable_key(buffer, "n", "u") + disable_key(buffer, "n", "") + set_command_key(buffer, "n", "Q", "PomodoroStop") + set_command_key(buffer, "n", "W", "PomodoroSkipBreak") + set_command_key(buffer, "n", "B", "PomodoroForceBreak") + set_command_key(buffer, "n", "D", "PomodoroDelayBreak") end ----@return integer + local function createBufferUi() - local buffer = vim.api.nvim_create_buf(false, true) -- Create a new buffer, not listed, scratch buffer - apply_buffer_keymaps(buffer) - return buffer + local buffer = vim.api.nvim_create_buf(false, true) + apply_buffer_keymaps(buffer) + return buffer end ----@return table + local function createBufferOpts() - local win_width = 25 - local win_height = 5 - local row = math.floor((vim.o.lines - win_height) / 2) - local col = math.floor((vim.o.columns - win_width) / 2) - - local opts = { - style = "minimal", - relative = "editor", - width = win_width, - height = win_height, - row = row, - col = col, - border = "single", - } - return opts + local win_width = 40 + local win_height = 8 + local row = math.floor((vim.o.lines - win_height) / 2) + local col = math.floor((vim.o.columns - win_width) / 2) + + local opts = { + style = "minimal", + relative = "editor", + width = win_width, + height = win_height, + row = row, + col = col, + border = "rounded", + } + return opts end ----@class PomodoroUI ----@field ui_update_timer uv_timer_t ----@field buffer integer ----@field buffer_opts table ----@field win? integer local UI = {} UI.buffer = createBufferUi() UI.buffer_opts = createBufferOpts() UI.ui_update_timer = uv.new_timer() UI.win = nil ----@return string -local function spaces(nb) - local s = " " - for _ = 0, nb do - s = s .. " " - end +local function center(str, width) + local padding = width - #str + return string.rep(" ", math.floor(padding / 2)) .. str +end - return s +local function progress_bar(current, total, width) + local filled = math.floor(current / total * width) + return "[" .. string.rep("=", filled) .. string.rep(" ", width - filled) .. "]" end ----@param pomodoro Pomodoro function UI.get_buffer_data(pomodoro) - local time_pass = uv.now() - pomodoro.started_timer_time - local time_left = pomodoro.timer_duration - time_pass - local time_left_string - time_left_string = math.floor(time_left / MIN_IN_MS) - .. "min" - .. math.floor(time_left % MIN_IN_MS / 1000) - .. "sec" - local data = {} - if pomodoro.phase == Phases.RUNNING then - table.insert(data, spaces(7) .. "[WORK]") - table.insert(data, spaces(4) .. "Time to work !") - table.insert(data, spaces(5) .. time_left_string) - table.insert(data, spaces(2) .. "[B] Break [Q] Stop") - end - if pomodoro.phase == Phases.BREAK then - table.insert(data, spaces(7) .. "[BREAK]") - table.insert(data, spaces(1) .. "Time to take a break !") - table.insert(data, spaces(5) .. time_left_string) - table.insert(data, "[W] Work [Q] Stop") - table.insert(data, "[D] DelayBreak") - end - return data + local time_pass = uv.now() - pomodoro.started_timer_time + local time_left = pomodoro.timer_duration - time_pass + local minutes = math.floor(time_left / MIN_IN_MS) + local seconds = math.floor((time_left % MIN_IN_MS) / 1000) + local time_left_string = string.format("%02d:%02d", minutes, seconds) + + local data = {} + local width = UI.buffer_opts.width - 2 -- Account for borders + + if pomodoro.phase == Phases.RUNNING then + table.insert(data, center("🍅 POMODORO", width)) + table.insert(data, center(time_left_string, width)) + table.insert(data, progress_bar(time_pass, pomodoro.timer_duration, width)) + table.insert(data, "") + table.insert(data, center("Time to work!", width)) + table.insert(data, "") + table.insert(data, center("[B]reak [Q]uit", width)) + elseif pomodoro.phase == Phases.BREAK then + table.insert(data, center("☕ BREAK TIME", width)) + table.insert(data, center(time_left_string, width)) + table.insert(data, progress_bar(time_pass, pomodoro.timer_duration, width)) + table.insert(data, "") + table.insert(data, center("Time to relax!", width)) + table.insert(data, "") + table.insert(data, center("[W]ork [Q]uit [D]elay", width)) + end + + return data end + function UI.isWinOpen() - if UI.win == nil then - return false - end - return vim.api.nvim_win_is_valid(UI.win) + if UI.win == nil then + return false + end + return vim.api.nvim_win_is_valid(UI.win) end --- update the UI time and in which phase we are ----@param pomodoro Pomodoro + function UI.updateUi(pomodoro) - vim.schedule(function() - if UI.isWinOpen() then - local data = UI.get_buffer_data(pomodoro) - vim.api.nvim_buf_set_lines(UI.buffer, 0, -1, false, data) - UI.startRenderingTimer(pomodoro) - else - if pomodoro.phase == Phases.BREAK then - pomodoro.delayBreak() - end - end - end) + vim.schedule(function() + if UI.isWinOpen() then + local data = UI.get_buffer_data(pomodoro) + vim.api.nvim_buf_set_lines(UI.buffer, 0, -1, false, data) + UI.startRenderingTimer(pomodoro) + else + if pomodoro.phase == Phases.BREAK then + pomodoro.delayBreak() + end + end + end) end ----@param pomodoro Pomodoro + function UI.startRenderingTimer(pomodoro) - UI.ui_update_timer:stop() - UI.ui_update_timer:start(1000, 0, function() - UI.updateUi(pomodoro) - end) + UI.ui_update_timer:stop() + UI.ui_update_timer:start(1000, 0, function() + UI.updateUi(pomodoro) + end) end + function UI.close() - if UI.isWinOpen() then - vim.api.nvim_win_close(UI.win, true) - UI.win = nil - end - UI.ui_update_timer:stop() + if UI.isWinOpen() then + vim.api.nvim_win_close(UI.win, true) + UI.win = nil + end + UI.ui_update_timer:stop() end return UI From d07d27f14db200d3ad2699166e118294784109f9 Mon Sep 17 00:00:00 2001 From: quentingruber Date: Wed, 25 Sep 2024 11:27:45 +0200 Subject: [PATCH 2/3] fmt --- lua/pomodoro/pomodoro.lua | 146 +++++++++++++-------------- lua/pomodoro/ui.lua | 205 ++++++++++++++++++++------------------ 2 files changed, 180 insertions(+), 171 deletions(-) diff --git a/lua/pomodoro/pomodoro.lua b/lua/pomodoro/pomodoro.lua index 18a6c50..2c6b924 100644 --- a/lua/pomodoro/pomodoro.lua +++ b/lua/pomodoro/pomodoro.lua @@ -30,88 +30,88 @@ pomodoro.phase = Phases.NOT_RUNNING ---@param time number ---@param fn function function pomodoro.startTimer(time, fn) - pomodoro.timer:stop() - pomodoro.timer_duration = time - pomodoro.started_timer_time = uv.now() - pomodoro.timer:start(time, 0, fn) + pomodoro.timer:stop() + pomodoro.timer_duration = time + pomodoro.started_timer_time = uv.now() + pomodoro.timer:start(time, 0, fn) end function pomodoro.displayPomodoroUI() - if pomodoro.phase == Phases.NOT_RUNNING or pomodoro.phase == nil then - pomodoro.start() - end - if UI.isWinOpen() ~= true then - if UI.ui_update_timer:is_active() ~= true then - UI.updateUi(pomodoro) + if pomodoro.phase == Phases.NOT_RUNNING or pomodoro.phase == nil then + pomodoro.start() end - UI.win = vim.api.nvim_open_win(UI.buffer, true, UI.buffer_opts) - else - if pomodoro.phase ~= Phases.BREAK then - UI.close() + if UI.isWinOpen() ~= true then + if UI.ui_update_timer:is_active() ~= true then + UI.updateUi(pomodoro) + end + UI.win = vim.api.nvim_open_win(UI.buffer, true, UI.buffer_opts) + else + if pomodoro.phase ~= Phases.BREAK then + UI.close() + end end - end end function pomodoro.closePomodoroUi() - UI.close() + UI.close() end function pomodoro.startBreak() - info("Break of " .. pomodoro.break_duration / MIN_IN_MS .. "m started!") - pomodoro.phase = Phases.BREAK - vim.schedule(pomodoro.displayPomodoroUI) - pomodoro.startTimer(pomodoro.break_duration, pomodoro.endBreak) + info("Break of " .. pomodoro.break_duration / MIN_IN_MS .. "m started!") + pomodoro.phase = Phases.BREAK + vim.schedule(pomodoro.displayPomodoroUI) + pomodoro.startTimer(pomodoro.break_duration, pomodoro.endBreak) end function pomodoro.endBreak() - vim.schedule(pomodoro.closePomodoroUi) - pomodoro.phase = Phases.RUNNING - pomodoro.start() + vim.schedule(pomodoro.closePomodoroUi) + pomodoro.phase = Phases.RUNNING + pomodoro.start() end function pomodoro.start() - info( - "Work session of " .. pomodoro.work_duration / MIN_IN_MS .. "m started!" - ) - pomodoro.phase = Phases.RUNNING - pomodoro.startTimer(pomodoro.work_duration, pomodoro.startBreak) + info( + "Work session of " .. pomodoro.work_duration / MIN_IN_MS .. "m started!" + ) + pomodoro.phase = Phases.RUNNING + pomodoro.startTimer(pomodoro.work_duration, pomodoro.startBreak) end function pomodoro.delayBreak() - if pomodoro.phase == Phases.BREAK then - pomodoro.phase = Phases.RUNNING - pomodoro.closePomodoroUi() - pomodoro.startTimer(MIN_IN_MS, pomodoro.startBreak) - end + if pomodoro.phase == Phases.BREAK then + pomodoro.phase = Phases.RUNNING + pomodoro.closePomodoroUi() + pomodoro.startTimer(MIN_IN_MS, pomodoro.startBreak) + end end function pomodoro.stop() - pomodoro.phase = Phases.NOT_RUNNING - pomodoro.timer:stop() - UI.ui_update_timer:stop() - pomodoro.closePomodoroUi() - info("Stopped") + pomodoro.phase = Phases.NOT_RUNNING + pomodoro.timer:stop() + UI.ui_update_timer:stop() + pomodoro.closePomodoroUi() + info("Stopped") end function pomodoro.registerCmds() - vim.api.nvim_create_user_command( - "PomodoroForceBreak", - pomodoro.startBreak, - {} - ) - vim.api.nvim_create_user_command("PomodoroSkipBreak", pomodoro.endBreak, {}) - vim.api.nvim_create_user_command("PomodoroStart", pomodoro.start, {}) - vim.api.nvim_create_user_command("PomodoroStop", pomodoro.stop, {}) - vim.api.nvim_create_user_command( - "PomodoroDelayBreak", - pomodoro.delayBreak, - {} - ) - vim.api.nvim_create_user_command( - "PomodoroUI", - pomodoro.displayPomodoroUI, - {} - ) + vim.api.nvim_create_user_command( + "PomodoroForceBreak", + pomodoro.startBreak, + {} + ) + vim.api.nvim_create_user_command("PomodoroSkipBreak", pomodoro.endBreak, {}) + vim.api.nvim_create_user_command("PomodoroStart", pomodoro.start, {}) + vim.api.nvim_create_user_command("PomodoroStop", pomodoro.stop, {}) + vim.api.nvim_create_user_command( + "PomodoroDelayBreak", + pomodoro.delayBreak, + {} + ) + vim.api.nvim_create_user_command( + "PomodoroUI", + pomodoro.displayPomodoroUI, + {} + ) end ---@class PomodoroOpts @@ -122,26 +122,26 @@ end ---@param opts PomodoroOpts function pomodoro.setup(opts) - if opts then - if opts.work_duration ~= nil then - pomodoro.work_duration = opts.work_duration * MIN_IN_MS - end - if opts.break_duration ~= nil then - pomodoro.break_duration = opts.break_duration * MIN_IN_MS - end - if opts.delay_duration ~= nil then - pomodoro.delay_duration = opts.delay_duration * MIN_IN_MS + if opts then + if opts.work_duration ~= nil then + pomodoro.work_duration = opts.work_duration * MIN_IN_MS + end + if opts.break_duration ~= nil then + pomodoro.break_duration = opts.break_duration * MIN_IN_MS + end + if opts.delay_duration ~= nil then + pomodoro.delay_duration = opts.delay_duration * MIN_IN_MS + end + if opts.start_at_launch ~= nil then + pomodoro.start_at_launch = opts.start_at_launch + end end - if opts.start_at_launch ~= nil then - pomodoro.start_at_launch = opts.start_at_launch - end - end - pomodoro.registerCmds() + pomodoro.registerCmds() - if pomodoro.start_at_launch then - pomodoro.start() - end + if pomodoro.start_at_launch then + pomodoro.start() + end end return pomodoro diff --git a/lua/pomodoro/ui.lua b/lua/pomodoro/ui.lua index ed0e2de..9b53c2a 100644 --- a/lua/pomodoro/ui.lua +++ b/lua/pomodoro/ui.lua @@ -4,60 +4,60 @@ local MIN_IN_MS = constants.MIN_IN_MS local Phases = constants.Phases local function disable_key(buffer, mode, key) - vim.api.nvim_buf_set_keymap( - buffer, - mode, - key, - "", - { noremap = true, silent = true } - ) + vim.api.nvim_buf_set_keymap( + buffer, + mode, + key, + "", + { noremap = true, silent = true } + ) end local function set_command_key(buffer, mode, key, command) - vim.api.nvim_buf_set_keymap( - buffer, - mode, - key, - string.format(":%s", command), - { noremap = true, silent = true } - ) + vim.api.nvim_buf_set_keymap( + buffer, + mode, + key, + string.format(":%s", command), + { noremap = true, silent = true } + ) end local function apply_buffer_keymaps(buffer) - disable_key(buffer, "n", "d") - disable_key(buffer, "n", "i") - disable_key(buffer, "n", "v") - disable_key(buffer, "n", "r") - disable_key(buffer, "n", "u") - disable_key(buffer, "n", "") - set_command_key(buffer, "n", "Q", "PomodoroStop") - set_command_key(buffer, "n", "W", "PomodoroSkipBreak") - set_command_key(buffer, "n", "B", "PomodoroForceBreak") - set_command_key(buffer, "n", "D", "PomodoroDelayBreak") + disable_key(buffer, "n", "d") + disable_key(buffer, "n", "i") + disable_key(buffer, "n", "v") + disable_key(buffer, "n", "r") + disable_key(buffer, "n", "u") + disable_key(buffer, "n", "") + set_command_key(buffer, "n", "Q", "PomodoroStop") + set_command_key(buffer, "n", "W", "PomodoroSkipBreak") + set_command_key(buffer, "n", "B", "PomodoroForceBreak") + set_command_key(buffer, "n", "D", "PomodoroDelayBreak") end local function createBufferUi() - local buffer = vim.api.nvim_create_buf(false, true) - apply_buffer_keymaps(buffer) - return buffer + local buffer = vim.api.nvim_create_buf(false, true) + apply_buffer_keymaps(buffer) + return buffer end local function createBufferOpts() - local win_width = 40 - local win_height = 8 - local row = math.floor((vim.o.lines - win_height) / 2) - local col = math.floor((vim.o.columns - win_width) / 2) - - local opts = { - style = "minimal", - relative = "editor", - width = win_width, - height = win_height, - row = row, - col = col, - border = "rounded", - } - return opts + local win_width = 40 + local win_height = 8 + local row = math.floor((vim.o.lines - win_height) / 2) + local col = math.floor((vim.o.columns - win_width) / 2) + + local opts = { + style = "minimal", + relative = "editor", + width = win_width, + height = win_height, + row = row, + col = col, + border = "rounded", + } + return opts end local UI = {} @@ -67,80 +67,89 @@ UI.ui_update_timer = uv.new_timer() UI.win = nil local function center(str, width) - local padding = width - #str - return string.rep(" ", math.floor(padding / 2)) .. str + local padding = width - #str + return string.rep(" ", math.floor(padding / 2)) .. str end local function progress_bar(current, total, width) - local filled = math.floor(current / total * width) - return "[" .. string.rep("=", filled) .. string.rep(" ", width - filled) .. "]" + local filled = math.floor(current / total * width) + return "[" + .. string.rep("=", filled) + .. string.rep(" ", width - filled) + .. "]" end function UI.get_buffer_data(pomodoro) - local time_pass = uv.now() - pomodoro.started_timer_time - local time_left = pomodoro.timer_duration - time_pass - local minutes = math.floor(time_left / MIN_IN_MS) - local seconds = math.floor((time_left % MIN_IN_MS) / 1000) - local time_left_string = string.format("%02d:%02d", minutes, seconds) - - local data = {} - local width = UI.buffer_opts.width - 2 -- Account for borders - - if pomodoro.phase == Phases.RUNNING then - table.insert(data, center("🍅 POMODORO", width)) - table.insert(data, center(time_left_string, width)) - table.insert(data, progress_bar(time_pass, pomodoro.timer_duration, width)) - table.insert(data, "") - table.insert(data, center("Time to work!", width)) - table.insert(data, "") - table.insert(data, center("[B]reak [Q]uit", width)) - elseif pomodoro.phase == Phases.BREAK then - table.insert(data, center("☕ BREAK TIME", width)) - table.insert(data, center(time_left_string, width)) - table.insert(data, progress_bar(time_pass, pomodoro.timer_duration, width)) - table.insert(data, "") - table.insert(data, center("Time to relax!", width)) - table.insert(data, "") - table.insert(data, center("[W]ork [Q]uit [D]elay", width)) - end - - return data + local time_pass = uv.now() - pomodoro.started_timer_time + local time_left = pomodoro.timer_duration - time_pass + local minutes = math.floor(time_left / MIN_IN_MS) + local seconds = math.floor((time_left % MIN_IN_MS) / 1000) + local time_left_string = string.format("%02d:%02d", minutes, seconds) + + local data = {} + local width = UI.buffer_opts.width - 2 -- Account for borders + + if pomodoro.phase == Phases.RUNNING then + table.insert(data, center("🍅 POMODORO", width)) + table.insert(data, center(time_left_string, width)) + table.insert( + data, + progress_bar(time_pass, pomodoro.timer_duration, width) + ) + table.insert(data, "") + table.insert(data, center("Time to work!", width)) + table.insert(data, "") + table.insert(data, center("[B]reak [Q]uit", width)) + elseif pomodoro.phase == Phases.BREAK then + table.insert(data, center("☕ BREAK TIME", width)) + table.insert(data, center(time_left_string, width)) + table.insert( + data, + progress_bar(time_pass, pomodoro.timer_duration, width) + ) + table.insert(data, "") + table.insert(data, center("Time to relax!", width)) + table.insert(data, "") + table.insert(data, center("[W]ork [Q]uit [D]elay", width)) + end + + return data end function UI.isWinOpen() - if UI.win == nil then - return false - end - return vim.api.nvim_win_is_valid(UI.win) + if UI.win == nil then + return false + end + return vim.api.nvim_win_is_valid(UI.win) end function UI.updateUi(pomodoro) - vim.schedule(function() - if UI.isWinOpen() then - local data = UI.get_buffer_data(pomodoro) - vim.api.nvim_buf_set_lines(UI.buffer, 0, -1, false, data) - UI.startRenderingTimer(pomodoro) - else - if pomodoro.phase == Phases.BREAK then - pomodoro.delayBreak() - end - end - end) + vim.schedule(function() + if UI.isWinOpen() then + local data = UI.get_buffer_data(pomodoro) + vim.api.nvim_buf_set_lines(UI.buffer, 0, -1, false, data) + UI.startRenderingTimer(pomodoro) + else + if pomodoro.phase == Phases.BREAK then + pomodoro.delayBreak() + end + end + end) end function UI.startRenderingTimer(pomodoro) - UI.ui_update_timer:stop() - UI.ui_update_timer:start(1000, 0, function() - UI.updateUi(pomodoro) - end) + UI.ui_update_timer:stop() + UI.ui_update_timer:start(1000, 0, function() + UI.updateUi(pomodoro) + end) end function UI.close() - if UI.isWinOpen() then - vim.api.nvim_win_close(UI.win, true) - UI.win = nil - end - UI.ui_update_timer:stop() + if UI.isWinOpen() then + vim.api.nvim_win_close(UI.win, true) + UI.win = nil + end + UI.ui_update_timer:stop() end return UI From 900d546721706eda52e38e7a7ea72289a7eb8e30 Mon Sep 17 00:00:00 2001 From: Abizrh Date: Wed, 25 Sep 2024 17:27:36 +0700 Subject: [PATCH 3/3] fix: add mising comments --- lua/pomodoro/ui.lua | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lua/pomodoro/ui.lua b/lua/pomodoro/ui.lua index ed0e2de..3703ecb 100644 --- a/lua/pomodoro/ui.lua +++ b/lua/pomodoro/ui.lua @@ -3,6 +3,10 @@ local uv = require("pomodoro.uv") local MIN_IN_MS = constants.MIN_IN_MS local Phases = constants.Phases +-- Disable a keymap for a given buffer +---@param buffer integer -- +---@param mode string -- +---@param key string -- local function disable_key(buffer, mode, key) vim.api.nvim_buf_set_keymap( buffer, @@ -13,6 +17,10 @@ local function disable_key(buffer, mode, key) ) end +-- Set an exit key for a buffer +---@param buffer integer -- +---@param mode string -- +---@param key string -- local function set_command_key(buffer, mode, key, command) vim.api.nvim_buf_set_keymap( buffer, @@ -23,6 +31,8 @@ local function set_command_key(buffer, mode, key, command) ) end +-- Apply custom keymaps to a buffer +---@param buffer integer -- local function apply_buffer_keymaps(buffer) disable_key(buffer, "n", "d") disable_key(buffer, "n", "i") @@ -36,12 +46,14 @@ local function apply_buffer_keymaps(buffer) set_command_key(buffer, "n", "D", "PomodoroDelayBreak") end +---@return integer local function createBufferUi() local buffer = vim.api.nvim_create_buf(false, true) apply_buffer_keymaps(buffer) return buffer end +---@return table local function createBufferOpts() local win_width = 40 local win_height = 8 @@ -60,22 +72,30 @@ local function createBufferOpts() return opts end +---@class PomodoroUI +---@field ui_update_timer uv_timer_t +---@field buffer integer +---@field buffer_opts table +---@field win? integer local UI = {} UI.buffer = createBufferUi() UI.buffer_opts = createBufferOpts() UI.ui_update_timer = uv.new_timer() UI.win = nil +---@return string local function center(str, width) local padding = width - #str return string.rep(" ", math.floor(padding / 2)) .. str end +---@return string local function progress_bar(current, total, width) local filled = math.floor(current / total * width) return "[" .. string.rep("=", filled) .. string.rep(" ", width - filled) .. "]" end +---@param pomodoro Pomodoro function UI.get_buffer_data(pomodoro) local time_pass = uv.now() - pomodoro.started_timer_time local time_left = pomodoro.timer_duration - time_pass @@ -114,6 +134,8 @@ function UI.isWinOpen() return vim.api.nvim_win_is_valid(UI.win) end +-- update the UI time and in which phase we are +---@param pomodoro Pomodoro function UI.updateUi(pomodoro) vim.schedule(function() if UI.isWinOpen() then @@ -128,6 +150,7 @@ function UI.updateUi(pomodoro) end) end +---@param pomodoro Pomodoro function UI.startRenderingTimer(pomodoro) UI.ui_update_timer:stop() UI.ui_update_timer:start(1000, 0, function()