forked from sundial-org/awesome-openclaw-skills
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.js
More file actions
213 lines (176 loc) · 5.54 KB
/
Copy pathtimer.js
File metadata and controls
213 lines (176 loc) · 5.54 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env node
/**
* Timer script for Clawdbot
*
* Usage: node timer.js <duration> [label]
*
* Duration formats:
* 30s - 30 seconds
* 5m - 5 minutes
* 1h - 1 hour
* 5 - 5 minutes (default unit)
* 5:30 - 5 minutes 30 seconds
* 1:30:00 - 1 hour 30 minutes
*/
const { spawn } = require('child_process');
const path = require('path');
// Parse duration string to milliseconds
function parseDuration(input) {
if (!input) {
throw new Error('Duration is required');
}
const str = input.trim().toLowerCase();
// HH:MM:SS or MM:SS format
if (str.includes(':')) {
const parts = str.split(':').map(p => parseInt(p, 10));
if (parts.some(isNaN)) {
throw new Error(`Invalid time format: ${input}`);
}
if (parts.length === 2) {
// MM:SS
const [minutes, seconds] = parts;
return (minutes * 60 + seconds) * 1000;
} else if (parts.length === 3) {
// HH:MM:SS
const [hours, minutes, seconds] = parts;
return (hours * 3600 + minutes * 60 + seconds) * 1000;
}
throw new Error(`Invalid time format: ${input}`);
}
// Ns, Nm, Nh format
const match = str.match(/^(\d+(?:\.\d+)?)\s*(s|sec|seconds?|m|min|minutes?|h|hr|hours?)?$/i);
if (!match) {
throw new Error(`Invalid duration format: ${input}`);
}
const value = parseFloat(match[1]);
const unit = (match[2] || 'm').toLowerCase();
let multiplier;
if (unit.startsWith('s')) {
multiplier = 1000;
} else if (unit.startsWith('m')) {
multiplier = 60 * 1000;
} else if (unit.startsWith('h')) {
multiplier = 60 * 60 * 1000;
} else {
multiplier = 60 * 1000; // default to minutes
}
return Math.round(value * multiplier);
}
// Format milliseconds to human-readable string
function formatDuration(ms) {
const totalSeconds = Math.round(ms / 1000);
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60);
const seconds = totalSeconds % 60;
const parts = [];
if (hours > 0) parts.push(`${hours}h`);
if (minutes > 0) parts.push(`${minutes}m`);
if (seconds > 0 || parts.length === 0) parts.push(`${seconds}s`);
return parts.join(' ');
}
// Format remaining time for display
function formatRemaining(ms) {
const totalSeconds = Math.ceil(ms / 1000);
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60);
const seconds = totalSeconds % 60;
if (hours > 0) {
return `${hours}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
}
return `${minutes}:${String(seconds).padStart(2, '0')}`;
}
// Play sound notification (macOS)
function playNotificationSound() {
try {
// Try macOS system sound
const sound = spawn('afplay', ['/System/Library/Sounds/Glass.aiff'], {
stdio: 'ignore',
detached: true
});
sound.unref();
} catch (e) {
// Sound not available, continue silently
}
}
// Main timer function
async function runTimer(durationMs, label) {
const startTime = Date.now();
const endTime = startTime + durationMs;
console.log(`⏱️ Timer started: ${formatDuration(durationMs)}`);
if (label) {
console.log(`📝 Label: ${label}`);
}
console.log(`⏰ Will complete at: ${new Date(endTime).toLocaleTimeString()}`);
console.log('');
// Progress update interval (every 10 seconds for timers > 1 minute, else every second)
const updateInterval = durationMs > 60000 ? 10000 : 1000;
let lastUpdate = startTime;
return new Promise((resolve) => {
const checkTimer = () => {
const now = Date.now();
const remaining = endTime - now;
if (remaining <= 0) {
// Timer complete!
console.log('');
console.log('═'.repeat(50));
if (label) {
console.log(`⏰ Timer complete! ${label}`);
} else {
console.log('⏰ Timer complete!');
}
console.log('═'.repeat(50));
// Play sound
playNotificationSound();
resolve();
return;
}
// Show progress update
if (now - lastUpdate >= updateInterval) {
console.log(`⏳ Remaining: ${formatRemaining(remaining)}`);
lastUpdate = now;
}
// Check again in 100ms for accuracy
setTimeout(checkTimer, 100);
};
checkTimer();
});
}
// Main execution
async function main() {
const args = process.argv.slice(2);
if (args.length === 0 || args[0] === '--help' || args[0] === '-h') {
console.log(`
Timer - Set a countdown timer with notification
Usage: node timer.js <duration> [label]
Duration formats:
30s 30 seconds
5m 5 minutes
1h 1 hour
5 5 minutes (default unit)
5:30 5 minutes 30 seconds
1:30:00 1 hour 30 minutes
Examples:
node timer.js 5m # 5 minute timer
node timer.js 30s "Check email" # 30 second timer with label
node timer.js 1h "Meeting time" # 1 hour timer with label
node timer.js 2:30 # 2 minutes 30 seconds
`);
process.exit(0);
}
try {
const durationMs = parseDuration(args[0]);
const label = args.slice(1).join(' ') || null;
if (durationMs <= 0) {
throw new Error('Duration must be positive');
}
if (durationMs > 24 * 60 * 60 * 1000) {
throw new Error('Duration cannot exceed 24 hours');
}
await runTimer(durationMs, label);
process.exit(0);
} catch (error) {
console.error(`❌ Error: ${error.message}`);
process.exit(1);
}
}
main();