Skip to content

Commit

Permalink
Add Timer Session Support with :TimerSession Command (#29)
Browse files Browse the repository at this point in the history
* Initial commit

* Added :TimerSession command for managing Pomodoro like sessions

* clean up readme

* Add autocomplete to command

---------

Co-authored-by: epwalsh <[email protected]>
  • Loading branch information
jmartinn and epwalsh authored Jul 26, 2024
1 parent ed72657 commit a926b5e
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## Unreleased

### Added
- Added `:TimerSession <session_name>` command to create and manage Pomodoro like sessions.

### Added

- Added Windows support for System notifications.
Expand Down
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ In **pomo.nvim**, most of the functionality is surfaced through the [`Notifier`]

- `:TimerResume [TIMERID]` the opposite of `:TimerPause`.

- `:TimerSession [SESSION_NAME]` to start a predefined Pomodoro session.

Example session configuration in your setup:

```lua
require("pomo").setup({
sessions = {
pomodoro = {
{ name = "Work", duration = "25m" },
{ name = "Short Break", duration = "5m" },
{ name = "Work", duration = "25m" },
{ name = "Short Break", duration = "5m" },
{ name = "Work", duration = "25m" },
{ name = "Long Break", duration = "15m" },
},
},
})
```

To start the above session, use: `:TimerSession pomodoro`.

<br>

**💡 Tip:** You can pass `-1` as the `TIMERID` to apply the command to all active timers.
Expand Down Expand Up @@ -134,6 +155,18 @@ This is a complete list of all of the options that can be passed to `require("po
{ name = "System" },
},
},
-- You can optionally define custom timer sessions.
sessions = {
-- Example session configuration for a session called "pomodoro".
pomodoro = {
{ name = "Work", duration = "25m" },
{ name = "Short Break", duration = "5m" },
{ name = "Work", duration = "25m" },
{ name = "Short Break", duration = "5m" },
{ name = "Work", duration = "25m" },
{ name = "Long Break", duration = "15m" },
},
},
}
```

Expand Down
13 changes: 13 additions & 0 deletions lua/pomo/commands/init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local pomo = require "pomo"

local command_lookups = {
TimerStart = "pomo.commands.timer_start",
TimerStop = "pomo.commands.timer_stop",
Expand All @@ -6,6 +8,7 @@ local command_lookups = {
TimerShow = "pomo.commands.timer_show",
TimerPause = "pomo.commands.timer_pause",
TimerResume = "pomo.commands.timer_resume",
TimerSession = "pomo.commands.timer_session",
}

local M = setmetatable({}, {
Expand Down Expand Up @@ -50,6 +53,16 @@ M.register_all = function()
vim.api.nvim_create_user_command("TimerResume", function(data)
return M.TimerResume(data)
end, { nargs = "?" })

vim.api.nvim_create_user_command("TimerSession", function(data)
return M.TimerSession(data)
end, {
nargs = "?",
complete = function()
local config = pomo.get_config()
return vim.tbl_keys(config.sessions or {})
end,
})
end

return M
42 changes: 42 additions & 0 deletions lua/pomo/commands/timer_session.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
local log = require "pomo.log"
local pomo = require "pomo"

return function(data)
local config = pomo.get_config()

---@type string
local session_name
if data.args and string.len(data.args) > 0 then
session_name = data.args
elseif config.sessions and #vim.tbl_keys(config.sessions) == 1 then
session_name = vim.tbl_keys(config.sessions)[1]
else
return log.error "Please provide a session name.\nUsage: TimerSession <session_name>"
end

local session = config.sessions[session_name]
if not session then
return log.error("Session '%s' not found", session_name)
end

local function start_session(current_session, index)
if index > #current_session then
log.info("Session '%s' completed", session_name)
return
end

local timer_config = current_session[index]
local time_limit = require("pomo.util").parse_time(timer_config.duration)
if not time_limit then
log.error("Invalid time duration '%s' in session '%s'", timer_config.duration, session_name)
return
end

local timer = pomo.start_timer(time_limit, timer_config.name)
timer:start(function()
start_session(current_session, index + 1)
end)
end

start_session(session, 1)
end
6 changes: 6 additions & 0 deletions lua/pomo/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ local Config = {}
---@field update_interval integer
---@field notifiers pomo.NotifierConfig[]
---@field timers table<string, pomo.NotifierConfig[]>
---@field sessions table<string, pomo.SessionConfig[]> -- Add sessions field

---@class pomo.NotifierConfig
---@field name pomo.NotifierType|?
---@field init function|? function(timer_id, time_limit, name, opts)
---@field opts table|?

---@class pomo.SessionConfig -- Define session config
---@field name string
---@field duration string

---@return pomo.Config
Config.default = function()
return {
Expand All @@ -20,6 +25,7 @@ Config.default = function()
{ name = NotifierType.Default },
},
timers = {},
sessions = {}, -- Initialize sessions
}
end

Expand Down

0 comments on commit a926b5e

Please sign in to comment.