Skip to content

[FIX] Functionality Issues #2

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions .stylua.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
column_width = 120
line_endings = "Unix"
indent_type = "Spaces"
indent_width = 4
quote_style = "AutoPreferDouble"
call_parentheses = "Always"
collapse_simple_statement = "Never"

[sort_requires]
enabled = false
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Install the plugin with your preferred package manager.
### [lazy.nvim](https://github.com/folke/lazy.nvim)
```lua
require("lazy").setup({
"orumin/pomodoro.nvim",
"JamesTeague/pomodoro.nvim",
lazy = true
dependencies = "MunifTanjim/nui.nvim",
cmd = { "PomodoroStart", "PomodoroStatus", "PomodoroStop" },
Expand Down
4 changes: 2 additions & 2 deletions doc/pomodoro.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ Using lazy.nvim <https://github.com/folke/lazy.nvim> in lua
{
-- amongst your other plugins
{
"orumin/pomodoro.nvim",
"JamesTeague/pomodoro.nvim",
lazy = true,
cmd = { "PomodoroStart", "PomodoroStatus", "PomodoroStop" },
dependencies = "MunifTanjim/nui.nvim",
config = true
},
-- or
{
"orumin/pomodoro.nvim",
"JamesTeague/pomodoro.nvim",
lazy = true,
cmd = { "PomodoroStart", "PomodoroStatus", "PomodoroStop" },
dependencies = "MunifTanjim/nui.nvim",
Expand Down
150 changes: 88 additions & 62 deletions lua/pomodoro.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ local config = require("pomodoro.config")
local ui = require("pomodoro.ui")

---@class Pomodoro
---@field private opts pomodoroOpts
---@field opts pomodoroOpts
---@field private state "stopped" | "started" | "break"
---@field private timer uv_timer_t?
---@field private timers_completed integer
---@field private work_started_at integer
---@field private break_started_at integer
---@field private time_break fun(self: Pomodoro):integer
---@field private start_pomodoro fun(self: Pomodoro)
---@field private start_break fun(self: Pomodoro)
---@field start_pomodoro fun(self: Pomodoro)
---@field start_break fun(self: Pomodoro)
---@field private statusline_ fun(self: Pomodoro):string
---@field private start_ fun(self: Pomodoro)
---@field private stop_ fun(self: Pomodoro)
Expand All @@ -20,13 +20,13 @@ local ui = require("pomodoro.ui")
---@field status fun()
---@field setup fun(pomodoroOpts)
local Pomodoro = {
opts = config,
ui = ui,
state = "stopped",
timer = nil,
timers_completed = 0,
work_started_at = 0,
break_started_at = 0,
opts = config,
ui = ui,
state = "stopped",
timer = nil,
timers_completed = 0,
work_started_at = 0,
break_started_at = 0,
}

local uv = vim.uv or vim.loop
Expand All @@ -35,83 +35,109 @@ local uv = vim.uv or vim.loop
---@param start integer
---@return string|osdate
local function calc_time_remaining(duration, start)
local seconds = duration * 60 - os.difftime(os.time(), start)
if math.floor(seconds / 60) >= 60 then
return os.date('!%0H:%0M:%0S', seconds)
else
return os.date('!%0M:%0S', seconds)
end
local seconds = duration * 60 - os.difftime(os.time(), start)
if math.floor(seconds / 60) >= 60 then
return os.date("!%H:%M:%S", seconds)
else
return os.date("!%M:%S", seconds)
end
end

function Pomodoro:time_break()
if self.timers_completed == 0 then
return self.opts.time_break_long
else
return self.opts.time_break_short
end
if self.timers_completed == self.opts.timers_to_long_break then
return self.opts.time_break_long
else
return self.opts.time_break_short
end
end

function Pomodoro:start_pomodoro()
if self.state ~= "started" then
local work_milliseconds = self.opts.time_work * 60 * 1000
self.timer:start(work_milliseconds, 0, vim.schedule_wrap(function() self.ui.pomodoro_completed_menu(self.opts.ui) end))
self.work_started_at = os.time()
self.state = "started"
end
if self.state ~= "started" then
local work_milliseconds = self.opts.time_work * 60 * 1000
self.timer:start(
work_milliseconds,
0,
vim.schedule_wrap(function()
self.ui.pomodoro_completed_menu(self)
end)
)
self.work_started_at = os.time()
self.state = "started"
end
end

function Pomodoro:start_break()
if self.state == "started" then
self.timers_completed = (self.timers_completed + 1) % self.opts.timers_to_long_break
local break_milliseconds = self:time_break() * 60 * 1000
self.timer:start(break_milliseconds, 0, vim.schedule_wrap(function() self.ui.break_completed_menu(self.opts.ui) end))
self.break_started_at = os.time()
self.state = "break"
end
if self.state == "started" then
self.timers_completed = (self.timers_completed + 1) % self.opts.timers_to_long_break
local break_milliseconds = self:time_break() * 60 * 1000
self.timer:start(
break_milliseconds,
0,
vim.schedule_wrap(function()
self.ui.break_completed_menu(self)
end)
)
self.break_started_at = os.time()
self.state = "break"
end
end

function Pomodoro:start_()
if self.state == "stopped" then
self.timer = uv.new_timer()
self:start_pomodoro()
end
if self.state == "stopped" then
self.timer = uv.new_timer()
self:start_pomodoro()
end
end

function Pomodoro:statusline_()
if self.state == "stopped" then
return self.opts.icons.stopped .. " (inactive)"
elseif self.state == "started" then
return self.opts.icons.started .. " " .. calc_time_remaining(self.opts.time_work, self.work_started_at)
else
local break_minutes = self:time_break()
return self.opts.icons.breaking .. " " .. calc_time_remaining(break_minutes, self.break_started_at)
end
if self.state == "stopped" then
return self.opts.icons.stopped .. " (inactive)"
elseif self.state == "started" then
return self.opts.icons.started .. " " .. calc_time_remaining(self.opts.time_work, self.work_started_at)
else
local break_minutes = self:time_break()
return self.opts.icons.breaking .. " " .. calc_time_remaining(break_minutes, self.break_started_at)
end
end

function Pomodoro:stop_()
if self.state ~= "stopped" then
self.timer:stop()
self.timer:close()
self.state = "stopped"
end
if self.state ~= "stopped" then
self.timer:stop()
self.timer:close()
self.state = "stopped"
end
end

Pomodoro.start = function () Pomodoro.start_(Pomodoro) end
Pomodoro.stop = function () Pomodoro.stop_(Pomodoro) end
Pomodoro.statusline = function () return Pomodoro.statusline_(Pomodoro) end
Pomodoro.status = function() vim.notify(Pomodoro.statusline(), vim.log.levels.INFO, {title = "pomodoro.nvim"}) end
Pomodoro.start = function()
Pomodoro.start_(Pomodoro)
end
Pomodoro.stop = function()
Pomodoro.stop_(Pomodoro)
end
Pomodoro.statusline = function()
return Pomodoro.statusline_(Pomodoro)
end
Pomodoro.status = function()
vim.notify(Pomodoro.statusline(), vim.log.levels.INFO, { title = "pomodoro.nvim" })
end

local function setup_commands()
local command_opts = {bang = false, bar = false, complete = nil}
vim.api.nvim_create_user_command("PomodoroStart", function() Pomodoro:start() end, command_opts)
vim.api.nvim_create_user_command("PomodoroStatus", function() Pomodoro:status() end, command_opts)
vim.api.nvim_create_user_command("PomodoroStop", function() Pomodoro:stop() end, command_opts)
local command_opts = { bang = false, bar = false, complete = nil }
vim.api.nvim_create_user_command("PomodoroStart", function()
Pomodoro:start()
end, command_opts)
vim.api.nvim_create_user_command("PomodoroStatus", function()
Pomodoro:status()
end, command_opts)
vim.api.nvim_create_user_command("PomodoroStop", function()
Pomodoro:stop()
end, command_opts)
end

function Pomodoro.setup(opts)
opts = opts or {}
vim.tbl_deep_extend("force", Pomodoro.opts, opts)
setup_commands()
opts = opts or {}
Pomodoro.opts = vim.tbl_deep_extend("force", Pomodoro.opts, opts)
setup_commands()
end

return Pomodoro
54 changes: 27 additions & 27 deletions lua/pomodoro/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@
---@field time_break_long integer
---@field timers_to_long_break integer
local opts = {
time_work = 25,
time_break_short = 5,
time_break_long = 20,
timers_to_long_break = 4,
icons = {
stopped = "󰚭",
started = "󰔟",
breaking = "󰞌",
},
ui = {
border = {
style = "rounded",
text = {
top_align = "left",
},
padding = { 1, 3 },
time_work = 25,
time_break_short = 5,
time_break_long = 20,
timers_to_long_break = 4,
icons = {
stopped = "󰚭",
started = "󰔟",
breaking = "󰞌",
},
position = "50%",
size = {
width = "25%",
ui = {
border = {
style = "rounded",
text = {
top_align = "left",
},
padding = { 1, 3 },
},
position = "50%",
size = {
width = "25%",
},
opacity = 1,
},
keymap = {
focus_next = { "j", "<Down>", "<Tab>" },
focus_prev = { "k", "<Up>", "<S-Tab>" },
close = { "<Esc>", "<C-c>" },
submit = { "<CR>", "<Space>" },
},
opacity = 1,
},
keymap = {
focus_next = { "j", "<Down>", "<Tab>" },
focus_prev = { "k", "<Up>", "<S-Tab>" },
close = { "<Esc>", "<C-c>" },
submit = { "<CR>", "<Space>" },
}
}

return opts
Loading