From a926b5ea8898b2e5057a030f68d8ed00a0a1b6f1 Mon Sep 17 00:00:00 2001 From: Juan Pedro Martin Date: Fri, 26 Jul 2024 19:10:18 +0200 Subject: [PATCH] Add Timer Session Support with `:TimerSession` Command (#29) * Initial commit * Added :TimerSession command for managing Pomodoro like sessions * clean up readme * Add autocomplete to command --------- Co-authored-by: epwalsh --- CHANGELOG.md | 3 +++ README.md | 33 +++++++++++++++++++++++ lua/pomo/commands/init.lua | 13 +++++++++ lua/pomo/commands/timer_session.lua | 42 +++++++++++++++++++++++++++++ lua/pomo/config.lua | 6 +++++ 5 files changed, 97 insertions(+) create mode 100644 lua/pomo/commands/timer_session.lua diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bced8f..c47f098 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## Unreleased +### Added +- Added `:TimerSession ` command to create and manage Pomodoro like sessions. + ### Added - Added Windows support for System notifications. diff --git a/README.md b/README.md index 8726b47..e62ac4f 100644 --- a/README.md +++ b/README.md @@ -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`. +
**💡 Tip:** You can pass `-1` as the `TIMERID` to apply the command to all active timers. @@ -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" }, + }, + }, } ``` diff --git a/lua/pomo/commands/init.lua b/lua/pomo/commands/init.lua index ce6c5bf..3bf88aa 100644 --- a/lua/pomo/commands/init.lua +++ b/lua/pomo/commands/init.lua @@ -1,3 +1,5 @@ +local pomo = require "pomo" + local command_lookups = { TimerStart = "pomo.commands.timer_start", TimerStop = "pomo.commands.timer_stop", @@ -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({}, { @@ -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 diff --git a/lua/pomo/commands/timer_session.lua b/lua/pomo/commands/timer_session.lua new file mode 100644 index 0000000..f97fa50 --- /dev/null +++ b/lua/pomo/commands/timer_session.lua @@ -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 " + 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 diff --git a/lua/pomo/config.lua b/lua/pomo/config.lua index 277d2d5..cc4edff 100644 --- a/lua/pomo/config.lua +++ b/lua/pomo/config.lua @@ -6,12 +6,17 @@ local Config = {} ---@field update_interval integer ---@field notifiers pomo.NotifierConfig[] ---@field timers table +---@field sessions table -- 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 { @@ -20,6 +25,7 @@ Config.default = function() { name = NotifierType.Default }, }, timers = {}, + sessions = {}, -- Initialize sessions } end