-
Notifications
You must be signed in to change notification settings - Fork 0
/
stim_cli.cc
480 lines (427 loc) · 14.3 KB
/
stim_cli.cc
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#include "stim_cli.hh"
#include "stim.hh"
using std::string;
using std::vector;
const char g_szUsage[] =
"stim - Simple Task Information Manager\n"
"(c) Copyright 2003-2017 Drew Leske.\n\n"
"Usage: stim start <task>\n"
" stim stop\n"
" stim log <message>\n"
" stim status [--raw]\n"
" stim report <daterange> [taskpath...]\n";
/*
** Helper functions
*/
// interpret "32m20s" as a number of seconds
// This is a pretty dorky implementation
int interpret_relative_timespec(const char* timespec)
{
int num[3];
char ch[3];
int seconds;
seconds = 0;
int items = sscanf(timespec, "%d%c%d%c%d%c", &num[0], &ch[0], &num[1], &ch[1], &num[2], &ch[2]);
for (int i = 0; i < items / 2; i++)
{
switch(ch[i])
{
case 'h':
seconds += num[i] * 3600;
break;
case 'm':
seconds += num[i] * 60;
break;
case 's':
seconds += num[i];
break;
default:
throw "Invalid relative time specifications";
}
}
return seconds;
}
time_t interpret_absolute_timespec(time_t now, const char* timespec)
{
int year, month, day, hours, minutes, seconds;
// initialize struct with current time as defaults
struct tm* pTm = localtime(&now);
// get pointer to timespec so we can move it along
char* bufptr = (char*) timespec;
int items = sscanf(timespec, "%04d%02d%02d ", &year, &month, &day);
if (items == 3)
{
// move buffer along nine characters in order to interpret time
bufptr += sizeof(char) * 9;
// record date in struct
pTm->tm_mday = day;
pTm->tm_mon = (month - 1);
pTm->tm_year = (year - 1900);
}
items = sscanf(bufptr, "%d:%02d:%02d", &hours, &minutes, &seconds);
if (items == 3)
{
pTm->tm_hour = hours;
pTm->tm_min = minutes;
pTm->tm_sec = seconds;
}
else
{ // guess they didn't specify seconds
items = sscanf(bufptr, "%d:%02d", &hours, &minutes);
if (items == 2)
{
pTm->tm_hour = hours;
pTm->tm_min = minutes;
}
else
throw "Invalid absolute time specification";
}
// convert struct into timestamp
return(mktime(pTm));
}
// interpret time spec, if provided, for logging operations; if not provided
// use current time
time_t interpret_timespec(time_t tNow, string sWhen)
{
time_t tWhen;
if (sWhen.empty())
tWhen = tNow;
else
{
if (sWhen.c_str()[0] == '-')
{ // relative time
// TODO: fill this out
// -30s means 30 sconds ago
// -1h means 1 hour ago
// -1m30s means one minute, 30 seconds ago
time_t tBackWhen = interpret_relative_timespec(
sWhen.c_str() + sizeof(char));
// calculate back from now
tWhen = tNow - tBackWhen;
}
else
{ // absolute time
time_t tWhen = interpret_absolute_timespec(tNow, sWhen.c_str());
}
}
return tWhen;
}
int main(int argc, char** argv)
{
int iStatus;
int iLoop;
string sCommand;
vector<string> vArgs;
map<string, string> vOptions;
string sStimDirectory;
// assume the best
iStatus = STIM_CLI_RETURN_SUCCESS;
// check if we're using fake now for debugging or testing
time_t tNow;
const char* szFakeNow = getenv(STIM_ENV_FAKENOW);
if (szFakeNow == NULL)
tNow = time(0);
else
tNow = (time_t) atoi(szFakeNow);
// set operating parameters from environment or from defaults
// timestamp format
const char* szTimestampFormat = getenv(STIM_ENV_TIMESTAMP_FORMAT);
if (szTimestampFormat == NULL)
szTimestampFormat = STIM_DEFAULT_TIMESTAMP_FORMAT;
// reporting format
const char* szReportFormat = getenv(STIM_ENV_REPORT_FORMAT);
if (szReportFormat == NULL)
szReportFormat = STIM_DEFAULT_REPORT_FORMAT;
// log reporting format
const char* szLogFormat = getenv(STIM_ENV_LOG_FORMAT);
if (szLogFormat == NULL)
szLogFormat = STIM_DEFAULT_LOG_FORMAT;
// get parameters into vector of strings
if (argc > 1)
{
sCommand = argv[1];
for (iLoop = 2; iLoop < argc; iLoop++)
{
if (strncmp(argv[iLoop],"--", 2) == 0)
{
char* szValue = strstr(argv[iLoop], "=");
if (szValue == NULL)
vOptions[argv[iLoop]+2] = "yes";
else
{
szValue++;
char szOption[255];
size_t iLen = strlen(argv[iLoop]) - strlen(szValue) - 3;
strncpy(szOption, argv[iLoop]+2, iLen);
szOption[iLen] = 0;
vOptions[szOption] = szValue;
}
}
else
vArgs.push_back(argv[iLoop]);
}
}
// determine stim directory
const char* szStimHome = getenv(STIM_ENV_HOME);
if (szStimHome)
{
sStimDirectory = szStimHome;
}
else
{
sStimDirectory = getenv("HOME");
sStimDirectory += "/.stim";
}
// determine contract
const char* szContract = getenv(STIM_ENV_CONTRACT);
string sContract;
if (szContract)
{
sContract = szContract;
}
else
{
sContract = "general";
}
// attempt to parse command
try
{
if (sCommand == "" || sCommand == "help")
{
std::cout << g_szUsage << std::endl;
exit(0);
}
else if (sCommand == "init")
{
// initialise Stim environment
Stim cStim(sStimDirectory.c_str(), sContract.c_str());
cStim.Initialise();
}
else
{
// ensure Stim environment
Stim cStim(sStimDirectory.c_str(), sContract.c_str());
// handle Stim command
if (sCommand == "start")
{
// syntax: start <task>
if (vArgs.size() != 1)
throw "Usage: start <task>";
string sTask = vArgs[0];
// specified time?
time_t tWhen = interpret_timespec(tNow, vOptions["when"]);
// now start the task
cStim.StartTask(tWhen, sTask);
}
else if (sCommand == "stop")
{
// syntax: stop
if (vArgs.size() > 0)
throw "Usage: stop";
// specified time?
time_t tWhen = interpret_timespec(tNow, vOptions["when"]);
// now stop the current task
cStim.StopTask(tWhen);
}
else if (sCommand == "log")
{
// syntax: log <message>
if (vArgs.size() != 1)
throw "Usage: log <message>";
string sMessage = vArgs[0];
// specified time?
time_t tWhen = interpret_timespec(tNow, vOptions["when"]);
// now log the message
cStim.LogTask(tWhen, sMessage);
}
else if (sCommand == "status")
{
// syntax: status [--raw]
if (vArgs.size() > 0)
{
throw "Usage: status";
}
// raw or readable?
bool bRaw = false;
if (!vOptions["raw"].empty())
bRaw = true;
// get status
TSessionStatus tSession;
bool bHaveResults = cStim.Status(tNow, tSession);
// report
if (bRaw)
{
if (!bHaveResults)
{
cout << "0 0 -1 stopped Nothing" << endl;
iStatus = STIM_CLI_RETURN_NO_RESULTS;
}
else
{
cout
<< tSession.aSessionTime << " "
<< tSession.aTaskTime << " "
<< tSession.aTransitionTime
<< (tSession.bRunning ? " running " : " stopped ")
<< tSession.sCurrentTask << endl;
}
}
else
{
if (!bHaveResults)
{
cout << "Nothing today." << endl;
iStatus = STIM_CLI_RETURN_NO_RESULTS;
}
else
{
// unlike the "raw" output, the human-readable output
// calculates the session time with the current task's time
// included
time_t aElapsedSinceTransition = tNow - tSession.aTransitionTime;
// make session and task times readable
string sSessionTime;
string sTaskTime;
string sTimerStatus;
if (tSession.bRunning)
{
SecondsToHms(tSession.aSessionTime + aElapsedSinceTransition,
sSessionTime);
SecondsToHms(tSession.aTaskTime + aElapsedSinceTransition,
sTaskTime);
sTimerStatus = "running";
}
else
{
SecondsToHms(tSession.aSessionTime, sSessionTime);
SecondsToHms(tSession.aTaskTime, sTaskTime);
SecondsToHms(aElapsedSinceTransition, sTimerStatus);
sTimerStatus.insert(0, "stopped for ");
}
// echo session and task times, timer state and current or last task
cout
<< "Session time: " << sSessionTime << std::endl
<< "Task time: " << sTaskTime << std::endl
<< "Task: " << tSession.sCurrentTask << std::endl
<< "Timer is " << sTimerStatus
<< endl;
}
}
}
else if (sCommand == "report")
{
// syntax: report <daterange> [taskpath...]
if (vArgs.size() == 0)
throw "Usage: report <daterange> [taskpath...]";
string sDateRange = vArgs[0];
// get optional task paths
vector<string> vTaskPaths;
if (vArgs.size() > 1)
{
// task path vector = vArgs[1]..vArgs[N]
vTaskPaths.assign(vArgs.begin() + 1, vArgs.end());
}
// get time spent
TTimeSpent vTimeSpent;
if (!cStim.ReportTime(tNow, sDateRange, vTaskPaths, vTimeSpent))
{
std::cerr << "Nothing to report." << std::endl;
iStatus = STIM_CLI_RETURN_NO_RESULTS;
}
// iterate through results
TTimeSpent::iterator it3;
map<string, string>::iterator it4;
vector<TLogEntry>::iterator it5;
char szStartTimestamp[255];
char szStopTimestamp[255];
char szLogTimestamp[255];
string sElapsed;
time_t aElapsed;
string sSeparator;
map<string, time_t> vPeriodTime;
for (it3 = vTimeSpent.begin(); it3 != vTimeSpent.end(); it3++)
{
strftime(szStartTimestamp, 255,
szTimestampFormat, localtime(&it3->aStartTime));
strftime(szStopTimestamp, 255,
szTimestampFormat, localtime(&it3->aStopTime));
// calculate elapsed time
aElapsed = it3->aStopTime - it3->aStartTime;
// format elapsed time as readable string
SecondsToHms(aElapsed, sElapsed);
// add to period totals
AddToTaskTotals(vPeriodTime, it3->sTaskPath, aElapsed);
string sLogMessages = "";
if (!it3->vLogMessages.empty())
{
it5 = it3->vLogMessages.begin();
while (1) // only do comparison (below) once
{
strftime(szLogTimestamp, 255,
szTimestampFormat, localtime(&it5->aLogTime));
// TODO: this should be generalized; create a dictionary
// and send it off
string sLogFormat = szLogFormat;
string::size_type pos;
pos = sLogFormat.find("%WHEN%");
if (pos != string::npos)
sLogFormat.replace(pos, 6, szLogTimestamp);
pos = sLogFormat.find("%LOG%");
if (pos != string::npos)
sLogFormat.replace(pos, 5, it5->sLogMessage);
sLogMessages += sLogFormat;
if (++it5 == it3->vLogMessages.end())
break;
}
}
// TODO: this should be generalized; create a dictionary
// and send it off
string sFormat = szReportFormat;
string::size_type pos;
pos = sFormat.find("%BEGIN%");
if (pos != string::npos)
sFormat.replace(pos, 7, szStartTimestamp);
pos = sFormat.find("%END%");
if (pos != string::npos)
sFormat.replace(pos, 5, szStopTimestamp);
pos = sFormat.find("%DETAIL%");
if (pos != string::npos)
sFormat.replace(pos, 8, it3->sTaskPath.c_str());
pos = sFormat.find("%ELAPSED%");
if (pos != string::npos)
sFormat.replace(pos, 9, sElapsed.c_str());
pos = sFormat.find("%LOG%");
if (pos != string::npos)
sFormat.replace(pos, 5, sLogMessages.c_str());
pos = sFormat.find("\\n");
if (pos != string::npos)
sFormat.replace(pos, 2, "\n");
printf("%s", sFormat.c_str());
//printf("%s\n", sFormat.c_str());
}
if (!vPeriodTime.empty() && vOptions["no-summary"].empty())
{
cout << endl;
PrintOutTotals(sDateRange, vPeriodTime);
}
}
else
{
throw ("No such command: " + sCommand).c_str();
}
}
}
catch (const string sError)
{
std::cerr << sError << std::endl << std::endl;
std::cerr << g_szUsage << std::endl;
iStatus = STIM_CLI_RETURN_USAGE_ERROR;
}
catch (const char* szError)
{
std::cerr << szError << std::endl << std::endl;
std::cerr << g_szUsage << std::endl;
iStatus = STIM_CLI_RETURN_USAGE_ERROR;
}
exit(iStatus);
}