-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoal-cli.js
More file actions
443 lines (370 loc) · 12.8 KB
/
Copy pathgoal-cli.js
File metadata and controls
443 lines (370 loc) · 12.8 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/usr/bin/env node
const DailyDashboard = require('./daily-dashboard');
const GoalManager = require('./goal-manager');
const readline = require('readline');
/**
* Goal CLI - Interactive interface for Goal Setting & Achievements
*
* Commands:
* - list: Show all goals
* - create: Create a new goal
* - progress: Update goal progress
* - stats: View goal statistics
* - achievements: Display achievements
* - delete <id>: Delete a goal
* - archive <id>: Archive a goal
* - help: Show help
*/
class GoalCLI {
constructor() {
this.dashboard = new DailyDashboard();
this.goalManager = new GoalManager(this.dashboard);
// Lazily create readline interface to avoid holding stdin open during tests
this._realRl = null;
this.rl = {
question: (q, cb) => {
if (!this._realRl) {
this._realRl = readline.createInterface({ input: process.stdin, output: process.stdout });
}
this._realRl.question(q, cb);
},
close: () => {
if (this._realRl) {
this._realRl.close();
this._realRl = null;
}
}
};
}
/**
* Show help message
*/
showHelp() {
console.log(`
╔════════════════════════════════════════════════════════════╗
║ Goal Setting & Achievement System - StepSyncAI ║
╚════════════════════════════════════════════════════════════╝
COMMANDS:
🎯 Managing Goals:
list Show all goals
create Create a new SMART goal
progress Update goal progress
delete <id> Delete a goal
archive <id> Archive a goal
📊 Statistics & Achievements:
stats View goal statistics
achievements Display unlocked achievements
leaderboard Show streak leaderboard
ℹ️ Help:
help Show this help message
GOAL TYPES:
😴 Sleep Goals - Target hours of sleep per night
🏃 Exercise Goals - Target minutes of exercise per day
😊 Mood Goals - Minimum mood rating to maintain
💊 Medication Goals - Medication compliance goals
🎯 Custom Goals - Any custom wellness metric
EXAMPLES:
# Create a sleep goal
node goal-cli.js create
# View all goals
node goal-cli.js list
# Update progress for today
node goal-cli.js progress
# View achievements
node goal-cli.js achievements
# Delete a goal
node goal-cli.js delete abc-123
═══════════════════════════════════════════════════════════
`);
}
/**
* Prompt user for input
*/
prompt(question) {
return new Promise(resolve => {
this.rl.question(question, answer => {
resolve(answer.trim());
});
});
}
/**
* Create a new goal interactively
*/
async createGoal() {
console.log('\n🎯 Create New SMART Goal\n');
console.log('═'.repeat(60));
console.log('\nGoal Types:');
console.log(' 1. 😴 Sleep Goal');
console.log(' 2. 🏃 Exercise Goal');
console.log(' 3. 😊 Mood Goal');
console.log(' 4. 💊 Medication Goal');
console.log(' 5. 🎯 Custom Goal');
const typeChoice = await this.prompt('\nSelect goal type (1-5): ');
const typeMap = {
'1': 'sleep',
'2': 'exercise',
'3': 'mood',
'4': 'medication',
'5': 'custom'
};
const type = typeMap[typeChoice];
if (!type) {
console.log('❌ Invalid goal type');
return;
}
const title = await this.prompt('\nGoal title (e.g., "Sleep 8 hours daily"): ');
if (!title) {
console.log('❌ Title is required');
return;
}
const description = await this.prompt('Description (optional): ');
let target;
switch (type) {
case 'sleep':
target = await this.prompt('Target hours per night (e.g., 8): ');
break;
case 'exercise':
target = await this.prompt('Target minutes per day (e.g., 30): ');
break;
case 'mood':
target = await this.prompt('Minimum mood rating (1-10): ');
break;
case 'medication':
target = 1; // Binary: compliance or not
break;
case 'custom':
target = await this.prompt('Target value: ');
break;
}
target = parseFloat(target);
if (isNaN(target)) {
console.log('❌ Invalid target value');
return;
}
const duration = await this.prompt('Duration in days (e.g., 21, 30): ');
const durationNum = parseInt(duration);
if (isNaN(durationNum) || durationNum < 1) {
console.log('❌ Invalid duration');
return;
}
const startDate = await this.prompt('Start date (YYYY-MM-DD, or press Enter for today): ');
try {
const goalData = {
type,
title,
description: description || title,
target,
duration: durationNum
};
if (startDate) {
goalData.startDate = startDate;
}
this.goalManager.createGoal(goalData);
} catch (error) {
console.log(`❌ Error: ${error.message}`);
}
}
/**
* List all goals
*/
listGoals() {
this.goalManager.displayGoals();
}
/**
* Update goal progress with today's data
*/
async updateProgress() {
const activeGoals = this.goalManager.getGoals({ status: 'active' });
if (activeGoals.length === 0) {
console.log('\n📭 No active goals to update.');
return;
}
console.log('\n📊 Update Goal Progress\n');
console.log('═'.repeat(60));
// Get today's data from dashboard
const today = new Date().toISOString().split('T')[0];
let todayData = this.dashboard.getEntry(today);
if (!todayData) {
console.log('\nNo data logged for today yet.');
const logNow = await this.prompt('Would you like to log today\'s data? (y/n): ');
if (logNow.toLowerCase() === 'y') {
todayData = await this.logTodayData();
} else {
return;
}
}
// Update all goals
console.log('\n🔄 Updating goal progress...\n');
const results = this.goalManager.updateAllGoals(todayData);
results.forEach(result => {
if (result.success) {
const goal = result.goal;
console.log(`${this.goalManager.getGoalEmoji(goal.type)} ${goal.title}`);
console.log(` Progress: ${goal.progress.percentage}% | Streak: ${goal.progress.streak} days 🔥`);
} else {
console.log(`❌ ${result.goal.title}: ${result.error}`);
}
});
}
/**
* Log today's data interactively
*/
async logTodayData() {
const data = {
date: new Date().toISOString().split('T')[0]
};
const mood = await this.prompt('\nMood (1-10): ');
if (mood) data.mood = parseInt(mood);
const sleep = await this.prompt('Sleep hours: ');
if (sleep) data.sleep_hours = parseFloat(sleep);
const exercise = await this.prompt('Exercise minutes: ');
if (exercise) data.exercise_minutes = parseInt(exercise);
// Add entry to dashboard
this.dashboard.addEntry(data);
console.log('\n✅ Today\'s data logged!');
return data;
}
/**
* Show goal statistics
*/
showStats() {
const stats = this.goalManager.getGoalStats();
console.log('\n📊 Goal Statistics\n');
console.log('═'.repeat(60));
console.log('\n📋 Overview:');
console.log(` Total Goals: ${stats.total}`);
console.log(` Active: ${stats.active}`);
console.log(` Completed: ${stats.completed}`);
console.log(` Archived: ${stats.archived}`);
console.log(` Average Completion: ${stats.averageCompletion}%`);
console.log('\n🔥 Streaks:');
console.log(` Longest Streak: ${stats.longestStreak} days`);
console.log(` Total Active Streaks: ${stats.totalStreak} days`);
if (Object.keys(stats.byType).length > 0) {
console.log('\n📊 By Type:');
Object.entries(stats.byType).forEach(([type, count]) => {
const emoji = this.goalManager.getGoalEmoji(type);
console.log(` ${emoji} ${type}: ${count} goal(s)`);
});
}
console.log('\n' + '═'.repeat(60));
}
/**
* Display achievements
*/
showAchievements() {
this.goalManager.displayAchievements();
}
/**
* Show streak leaderboard
*/
showLeaderboard() {
const goals = this.goalManager.getGoals();
if (goals.length === 0) {
console.log('\n🏆 No goals to display in leaderboard.');
return;
}
// Sort by max streak
const sorted = [...goals].sort((a, b) => b.progress.maxStreak - a.progress.maxStreak);
console.log('\n🏆 Streak Leaderboard\n');
console.log('═'.repeat(60));
sorted.slice(0, 10).forEach((goal, index) => {
const medal = index === 0 ? '🥇' : index === 1 ? '🥈' : index === 2 ? '🥉' : ' ';
console.log(`\n${medal} ${index + 1}. ${goal.title}`);
console.log(` Max Streak: ${goal.progress.maxStreak} days 🔥`);
console.log(` Current Streak: ${goal.progress.streak} days`);
console.log(` Status: ${goal.status}`);
});
console.log('\n' + '═'.repeat(60));
}
/**
* Delete a goal
*/
deleteGoal(id) {
try {
this.goalManager.deleteGoal(id);
} catch (error) {
console.log(`❌ Error: ${error.message}`);
}
}
/**
* Archive a goal
*/
archiveGoal(id) {
try {
this.goalManager.archiveGoal(id);
} catch (error) {
console.log(`❌ Error: ${error.message}`);
}
}
/**
* Run the CLI
*/
async run() {
const args = process.argv.slice(2);
const command = args[0] || 'help';
switch (command.toLowerCase()) {
case 'list':
this.listGoals();
this.rl.close();
break;
case 'create':
await this.createGoal();
this.rl.close();
break;
case 'progress':
case 'update':
await this.updateProgress();
this.rl.close();
break;
case 'stats':
case 'statistics':
this.showStats();
this.rl.close();
break;
case 'achievements':
case 'badges':
this.showAchievements();
this.rl.close();
break;
case 'leaderboard':
case 'streaks':
this.showLeaderboard();
this.rl.close();
break;
case 'delete':
case 'remove':
if (!args[1]) {
console.log('❌ Usage: delete <goal-id>');
} else {
this.deleteGoal(args[1]);
}
this.rl.close();
break;
case 'archive':
if (!args[1]) {
console.log('❌ Usage: archive <goal-id>');
} else {
this.archiveGoal(args[1]);
}
this.rl.close();
break;
case 'help':
case '--help':
case '-h':
default:
this.showHelp();
this.rl.close();
break;
}
}
}
// Run the CLI if executed directly
if (require.main === module) {
const cli = new GoalCLI();
cli.run().catch(error => {
console.error('❌ Error:', error.message);
process.exit(1);
});
}
module.exports = GoalCLI;