Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: add progress bar and spaces between table #3

Merged
merged 4 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lua/pomodoro/pomodoro.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ function pomodoro.startTimer(time, fn)
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()
Expand All @@ -53,6 +51,7 @@ function pomodoro.displayPomodoroUI()
end
end
end

function pomodoro.closePomodoroUi()
UI.close()
end
Expand All @@ -63,6 +62,7 @@ function pomodoro.startBreak()
vim.schedule(pomodoro.displayPomodoroUI)
pomodoro.startTimer(pomodoro.break_duration, pomodoro.endBreak)
end

function pomodoro.endBreak()
vim.schedule(pomodoro.closePomodoroUi)
pomodoro.phase = Phases.RUNNING
Expand Down
76 changes: 50 additions & 26 deletions lua/pomodoro/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ local function set_command_key(buffer, mode, key, command)
{ noremap = true, silent = true }
)
end

-- Apply custom keymaps to a buffer
---@param buffer integer --
local function apply_buffer_keymaps(buffer)
Expand All @@ -44,16 +45,18 @@ local function apply_buffer_keymaps(buffer)
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
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 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)

Expand All @@ -64,7 +67,7 @@ local function createBufferOpts()
height = win_height,
row = row,
col = col,
border = "single",
border = "rounded",
}
return opts
end
Expand All @@ -81,46 +84,65 @@ 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
---@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
Abizrh marked this conversation as resolved.
Show resolved Hide resolved
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 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, 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")
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)
end

-- update the UI time and in which phase we are
---@param pomodoro Pomodoro
Abizrh marked this conversation as resolved.
Show resolved Hide resolved
function UI.updateUi(pomodoro)
Expand All @@ -136,13 +158,15 @@ function UI.updateUi(pomodoro)
end
end)
end

---@param pomodoro Pomodoro
Abizrh marked this conversation as resolved.
Show resolved Hide resolved
function UI.startRenderingTimer(pomodoro)
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)
Expand Down
Loading