-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterval-timer.psm1
110 lines (97 loc) · 3.23 KB
/
interval-timer.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
Add-Type -AssemblyName presentationCore
function playAudio {
param(
[Parameter(Mandatory = $true, Position = 0)]
[string]$AudioPath
)
$mediaPlayer = New-Object system.windows.media.mediaplayer
$mediaPlayer.open((join-path $PSScriptRoot $AudioPath))
$mediaPlayer.Play()
}
function runTimer {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 1)]
[double[]]$Intervals
)
$stopWatch = New-Object -TypeName System.Diagnostics.Stopwatch
$len = $Intervals.Count
$i = 0
do {
$timer = [System.TimeSpan]::FromMinutes($Intervals[$i])
$b = [system.TimeSpan]::FromSeconds(1)
$timer = $timer+$b
$stopWatch.Start()
while (1) {
$remaining = $timer - $stopWatch.Elapsed
if ($remaining.TotalSeconds -le 1) {
break
}
$s = "{0:d2}h:{1:d2}m:{2:d2}s" -f $remaining.hours, $remaining.minutes, $remaining.seconds
$host.ui.RawUI.WindowTitle = $s
write-host -NoNewline "`r$s"
Start-Sleep -s 1
}
$i++
$a = "interval.mp3"
if ($i -eq $len) {
$a = "set.mp3"
}
playAudio -AudioPath $a
$stopWatch.Reset()
}while ($i -lt $len)
}
<#
.SYNOPSIS
timer -- a pomodoro/interval timer based on https://github.com/rlue/timer/ for windows powershell
.DESCRIPTION
timer is implemented as a function, run this script in your pofile.ps1 file to have it available always.
Make sure the bell mp3 files are available in same path as script.
- Interval values are in minutes, can be entered as decimal. e.g. 0.5 for 30 sec
- Delay values are in seconds
- Repeat value is an integer, for a negative number the timer repeats intervals indefinitely
.NOTES
This function is not supported only in windows
.EXAMPLE
timer -Intervals 25,5,30,4 -Delay 6 -Repeat 2
This will create a timer which will bell at specied intervals of 25 min, 5 min, 30 min and 4 min.
The timer will start after a delay of 6 seconds, and whole intervals will be reapeated 2 times.
.Example
timer 25,5,30,4 -d 6 -r 2
Same as above
#>
function timer {
[CmdletBinding(DefaultParameterSetName = "Run")]
param (
[Parameter(Mandatory = $true, Position = 0)]
[double[]]$Intervals,
# Repeat timer
[Parameter()]
[Alias('r')]
[int]$Repeat = 1,
# Delay timer start by seconds
[Parameter()]
[Alias('d')]
[int]$Delay = 0
)
process {
$title = $host.UI.RawUI.WindowTitle
$hasFinished = $false
try {
Start-Sleep $Delay
$i = $Repeat
while ($i -ne 0) {
runTimer -Intervals $Intervals
$i--
}
$hasFinished = $true
write-host "`r$((Get-Date).ToString("[HH:mm]")) Timer Elapsed"
}
finally {
$host.UI.RawUI.WindowTitle = $title
if (-not $hasFinished) {
write-host "`rTimer cancelled"
}
}
}
}