-
-
Save txitxo0/5028a3060cd19d347387e23cc862ae91 to your computer and use it in GitHub Desktop.
CLI Pomodoro for Powershell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Start-Pomodoro { | |
param( | |
[ValidateSet("work", "rest")] | |
[string]$Mode = "work", | |
[int]$Minutes = $(if ($Mode -eq "work") { 45 } else { 10 }), | |
[System.Drawing.Color]$startColor = [System.Drawing.Color]::Aqua, | |
[System.Drawing.Color]$endColor = [System.Drawing.Color]::MintCream | |
) | |
$startTime = Get-Date -Format "hh:mm" | |
$end = (Get-Date).AddMinutes($Minutes) | |
$barLength = 30 | |
$totalSeconds = $Minutes * 60 | |
$blockChar = "$([char]0x2588)" | |
$emptyChar = "$([char]0x2591)" | |
$initialLeft = [Console]::CursorLeft | |
$initialTop = [Console]::CursorTop | |
while ((Get-Date) -lt $end) { | |
$remaining = $end - (Get-Date) | |
$elapsed = $totalSeconds - [int]$remaining.TotalSeconds | |
$percent = [math]::Round(($elapsed / $totalSeconds) * 100) | |
$filled = [int](($percent / 100) * $barLength) | |
$bar = "" | |
for ($i = 0; $i -lt $barLength; $i++) { | |
if ($i -lt $filled) { | |
$ratio = $i / $barLength | |
$r = [int]($startColor.R + ($endColor.R - $startColor.R) * $ratio) | |
$g = [int]($startColor.G + ($endColor.G - $startColor.G) * $ratio) | |
$b = [int]($startColor.B + ($endColor.B - $startColor.B) * $ratio) | |
$bar += "$([char]0x1b)[38;2;${r};${g};${b}m$blockChar$([char]0x1b)[0m" | |
} else { | |
$bar += "$([char]0x1b)[38;5;240m$emptyChar$([char]0x1b)[0m" | |
} | |
} | |
$timeLeft = ("{0:mm\:ss}" -f $remaining) | |
$modeText = if ($Mode -eq "work") { | |
"We are working" | |
} else { | |
"We are resting" | |
} | |
[Console]::SetCursorPosition($initialLeft, $initialTop) | |
[Console]::Write("$bar") | |
[Console]::Write(" {0}% - {1} [{2} since $startTime]", $percent, $timeLeft, $modeText) | |
Start-Sleep -Seconds 1 | |
} | |
if (-not (Get-Module -ListAvailable -Name BurntToast)) { | |
Install-Module -Name BurntToast -Scope CurrentUser -Force | |
Import-Module BurntToast -ErrorAction SilentlyContinue | |
} | |
$toastText = if ($Mode -eq "work") { | |
"Pomodoro done. Let's take a break!" | |
} else { | |
"Break done, get back to work!" | |
} | |
$logoPath = Join-Path $PSScriptRoot "logo.jpg" | |
if (Test-Path -Path $logoPath) { | |
New-BurntToastNotification -Text "Pomodoro", $toastText -AppLogo $logoPath | |
} else { | |
New-BurntToastNotification -Text "Pomodoro", $toastText | |
} | |
} | |
function work { Start-Pomodoro -Mode "work" -startColor MediumPurple -endColor HotPink } | |
function rest { Start-Pomodoro -Mode "rest" -startColor LightGreen} | |
Set-Alias -Name work -Value work | |
Set-Alias -Name rest -Value rest | |
Export-ModuleMember -Function Start-Pomodoro, work, rest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on the work of the Pomodoro CLI from bashbunni and, taking in account I use to work in Windows and I don't like to use subsystems like WSL (but I prefer no Windows SO), I dev a simply Pomodoro CLI like for windowsusers.
Pomodoro Timer Script for PowerShell
Description
This PowerShell script implements a Pomodoro timer, a time management method that alternates work periods with short breaks. It displays a progress bar in the console and can optionally show a notification using the BurntToast module when a work or break session is complete.
Features
work
andrest
) for starting work and break sessions.Prerequisites
How to Use
Save the Script: Save the PowerShell script to a file (e.g.,
Pomodoro.ps1
).Open PowerShell: Open a PowerShell console.
Navigate to the Script's Directory: Use the
cd
command to navigate to the directory where you saved the script. For example:Import the Module: Import the script as a module:
Import-Module .\Pomodoro.ps1
Run the Timer: Use the
Start-Pomodoro
function or thework
andrest
aliases to start the timer.To start a work session with the default settings (45 minutes, Aqua to MintCream):
To start a rest session with the default settings (10 minutes, LightGreen):
To start a work session with custom settings:
Functions and Parameters
Start-Pomodoro
FunctionStarts the Pomodoro timer.
Parameters:
Mode
(String, Optional): The mode of the session. Valid values are"work"
or"rest"
. Default is"work"
.Minutes
(Int, Optional): The duration of the session in minutes. Default is 45 for work and 10 for rest.startColor
(System.Drawing.Color, Optional): The starting color of the progress bar. Default isAqua
.endColor
(System.Drawing.Color, Optional): The ending color of the progress bar. Default isMintCream
.work
AliasStarts a work session with default settings (45 minutes, Aqua to MintCream). Equivalent to:
Start a 15-minute rest session with a blue to white progress bar
Use the aliases
OutputThe script displays a progress bar in the PowerShell console, along with the remaining time and the current mode (work or rest). When a session is complete, a Windows notification is shown (if BurntToast is installed).NotesThe script uses Start-Sleep to pause execution, which may affect other PowerShell operations.The progress bar updates every second.The script attempts to install the BurntToast module if it's not found, but this may require administrator privileges and an internet connection.The [char]0x2588 character is a solid block, and [char]0x2591 is a light shade character.
I am currently using the following image as logo:
