-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinclock.cpp
More file actions
588 lines (551 loc) · 13.9 KB
/
binclock.cpp
File metadata and controls
588 lines (551 loc) · 13.9 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
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
#include <ctime>
#include <iostream>
#include <string>
#include <cstring> // strcmp
#include <cstdlib> // atoi and exit
#include <cmath> // for floor
#include <unistd.h> // for sleep
using namespace std;
/**
* Binary, console clock program, with hexadecimal time for real nerds.
*
* It's my very first "project" in c++, so it can be piece of shit. You running this on you own risk!
* Only standard libs inside. You need only g++ to compile it. Oh and Linux... Sorry Windows users... but to be honest i don't know is there anybody who would like to use console clock in Windows. Give me sign if is any, only for information, I don't want install Windows to upgrade this program :(.
* You can use this program to show clock in Conky.
* Have fun.
*
* @version 1.0 [19.08.2015]
* @author krzmig
* @licence GNU GPL v2.0
*
* @todo short console params like -d
*/
class binaryClock
{
protected:
string true_char; // string for true bits
string false_char; // string for false bits
string null_char; // string for free space
bool show_descriptions;
bool run_live;
bool show_decimal;
bool vertical;
bool show_seconds;
bool twelve_hours;
bool hex_time;
string ch_time; // hhmmss for decimal clock
int fulltime[4]; // h, m, s (h,m,m,s for hex)
bool bitTime[6][6]; // array for binary presentation of time
int bitTimeNulls[6]; // array to ignore first bits in any line
string bitTimeDesc; // string with description like: H H M M S S
string description; // string with description of bits value, ex: 8 4 2 1
int lines_counter; // line counter for live clock
public:
binaryClock();
void run();
void help();
bool checkParam( char *, char * );
protected:
void setCurrentTime();
void setOptions();
void drawDescription();
void drawClock();
void drawVerticalClock();
void drawNewLine();
void clearConsole();
void generateBitArray();
void genNullChar();
void intToBits( int, bool [], int );
void drawBits( bool [], int, int );
string fillSpaces( string, int );
string fillSpaces( char, int );
int utf8_strlen( const string& );
};
/**
* Constructor to setup thing which I can't do in declaration...
*
* @version 1.0 [19.08.2015]
* @author krzmig
*/
binaryClock::binaryClock()
{
this -> true_char = "◉ ";
this -> false_char = "○ ";
this -> genNullChar();
this -> vertical = false;
this -> show_decimal = false;
this -> run_live = false;
this -> show_seconds = true;
this -> show_descriptions = false;
this -> twelve_hours = false;
this -> hex_time = false;
this-> lines_counter = 0;
}
/**
* Method to display help command
*
* @version 1.0 [19.08.2015]
* @author krzmig
*/
void binaryClock::help()
{
cout << "binclock [OPTIONS]\n\n" <<
" --description - Showing description for clock (G is 16, Z is 32).\n" <<
" --truechar <char> - String to show for 1 bit, default: \"◉ \".\n" <<
" --falsechar <char> - String to show for 0 bit, default: \"○ \".\n" <<
" --live - Showing live clock (refresh every second). For hex time there is some delay,\n" <<
" because one hexadecimal seconds it's equal to 1.318359375 standard seconds,\n" <<
" and refresh interval is one standard second.\n" <<
" --vertical - Showing clock in vertical mode.\n" <<
" --decimal - Use decimal clock mode (not working with --hex option).\n" <<
" --bcd - Alias for --decimal.\n" <<
" --noseconds - Don't show seconds.\n" <<
" --12hours - Show clock in 12h mode, default is 24h mode (not working with --hex option).\n" <<
" --hex - Show clock for Hexadecimal time (16h, 256m, 16s).\n" <<
" See: https://en.wikipedia.org/wiki/Hexadecimal_time\n" <<
" --help - Show this help.\n";
exit(0);
}
/**
* Main method of class. This one starts others methods.
*
* @version 1.0 [19.08.2015]
* @author krzmig
*/
void binaryClock::run()
{
this -> setOptions();
do
{
this -> setCurrentTime();
this -> generateBitArray();
if( this -> show_descriptions )
this -> drawDescription();
if( this -> vertical )
this -> drawVerticalClock();
else
this -> drawClock();
if( this -> run_live )
this -> clearConsole();
}
while( this -> run_live );
}
/**
* Method sets options depend on params posted by user.
*
* @version 1.0 [19.08.2015]
* @author krzmig
*/
void binaryClock::setOptions()
{
if( this -> hex_time )
{
this -> bitTimeDesc = "HMMS";
}
else if( this -> show_decimal )
{
this -> bitTimeNulls[0] = 2;
this -> bitTimeNulls[2] = 1;
this -> bitTimeNulls[4] = 1;
this -> bitTimeDesc = "HHMMSS";
}
else
{
this -> bitTimeNulls[0] = 1;
this -> bitTimeDesc = "HMS";
}
this -> description = "1248GZ";
}
/**
* This method gets current time and convert them to version that will be used by others methods.
*
* @version 1.0 [19.08.2015]
* @author krzmig
*/
void binaryClock::setCurrentTime()
{
time_t timestamp = time(NULL);
char hh[3];
char mm[3];
char ss[3];
if(( this -> twelve_hours ) && ( !this -> hex_time ))
strftime( hh, 3, "%I", localtime( ×tamp ));
else
strftime( hh, 3, "%H", localtime( ×tamp ));
strftime( mm, 3, "%M", localtime( ×tamp ));
strftime( ss, 3, "%S", localtime( ×tamp ));
this -> fulltime[0] = atoi( hh );
this -> fulltime[1] = atoi( mm );
this -> fulltime[2] = atoi( ss );
this -> ch_time = hh;
this -> ch_time += mm;
this -> ch_time += ss;
if( this -> hex_time )
{
int today_timestamp = floor(( this -> fulltime[0] * 3600 + this -> fulltime[1] * 60 + this -> fulltime[2]) / 1.318359375 );
this -> fulltime[0] = floor( today_timestamp / 4096 );
today_timestamp = today_timestamp % 4096;
this -> fulltime[1] = floor( today_timestamp / 256 );
today_timestamp = today_timestamp % 256;
this -> fulltime[2] = floor( today_timestamp / 16 );
today_timestamp = today_timestamp % 16;
this -> fulltime[3] = today_timestamp;
}
}
/**
* Method generate bits array for all needed units, like hours, seconds etc.
*
* @version 1.0 [19.08.2015]
* @author krzmig
*/
void binaryClock::generateBitArray()
{
if( this -> hex_time )
{
for( int i = 0; i < 4; i++ )
{
this -> intToBits( this -> fulltime[i], this -> bitTime[i], 4 );
}
}
else if( this -> show_decimal )
{
for( int i = 0; i < 6; i++ )
{
this -> intToBits( this -> ch_time[i], this -> bitTime[i], 4 );
}
}
else
{
for( int i = 0; i < 3; i++ )
{
this -> intToBits( this -> fulltime[i], this -> bitTime[i], 6 );
}
}
}
/**
* This method gets int value, convert it to binary and put results bits to given array.
*
* @param int number - Int value which will be converted to binary.
* @param bool[] array - It's array to which method should puts results bits.
* @param int size - This is length. For example if we want convert seconds, we set size 6 (6 bits are needed to make 60), but for 12 hours size 4 it's enough.
*
* @version 1.0 [19.08.2015]
* @author krzmig
*/
void binaryClock::intToBits( int number, bool array[], int size )
{
for( int i = size - 1; i >= 0; i-- )
{
if( number > 0 )
{
if( number % 2 == 1 )
array[i] = true;
else
array[i] = false;
number = floor( number / 2 );
}
else
array[i] = false;
}
}
/**
* Method draw one line of chars on screen depends on generated earlier bits arrays.
*
* @param bool[] array - It's array from which chars are drawn.
* @param int size - This Is length. Max number of bits to show maximum possible value for this unit.
* @param int nullsCount - Int value which defined how much first chars should be ignored. If hours take less place than minutes, it's nice to show free space than false bit.
*
* @version 1.0 [19.08.2015]
* @author krzmig
*/
void binaryClock::drawBits( bool array[], int size, int nullsCount = 0 )
{
for( int i = 0; i < size; i++ )
{
if( i < nullsCount )
cout << this -> null_char;
else if( array[i] )
cout << this -> true_char;
else
cout << this -> false_char;
}
this -> drawNewLine();
}
/**
* Method to draw horizontal descriptions.
*
* @version 1.0 [19.08.2015]
* @author krzmig
*/
void binaryClock::drawDescription()
{
int size = this -> null_char.length();
cout << this -> fillSpaces( " ", size );
if( this -> vertical )
{
int limit = 3;
if( this -> hex_time )
limit = 4;
else if( this -> show_decimal )
limit = 6;
for( int i = 0; i < limit; i++ )
{
cout << this -> fillSpaces( this -> bitTimeDesc[i], size );
}
this -> drawNewLine();
}
else
{
int limit = 5;
if( this -> hex_time )
limit = 3;
else if( this -> show_decimal )
limit = 3;
for( int i = limit; i >= 0; i-- )
{
cout << this -> fillSpaces( this -> description[i], size );
}
this -> drawNewLine();
}
}
/**
* Method to draw horizontal version of clock, with vertical descriptions if they are on.
*
* @version 1.0 [19.08.2015]
* @author krzmig
*/
void binaryClock::drawClock()
{
int length = 6;
int limit = 2;
if( this -> show_seconds )
limit = 3;
int size = this -> null_char.length();
if( this -> hex_time )
{
limit += 1;
length = 4;
}
else if( this -> show_decimal )
{
limit += limit;
length = 4;
}
for( int i = 0; i < limit; i++ )
{
if( this -> show_descriptions )
cout << this -> fillSpaces( this -> bitTimeDesc[i], size );
this -> drawBits( this -> bitTime[i], length, this -> bitTimeNulls[i] );
}
}
/**
* Method to draw vertical version of clock with vertical descriptions if they are on.
*
* @version 1.0 [19.08.2015]
* @author krzmig
*/
void binaryClock::drawVerticalClock()
{
int size = this -> null_char.length();
int init_i = 5;
int limit_j = 3;
if( this -> hex_time )
{
init_i = 3;
limit_j = 4;
}
else if( this -> show_decimal )
{
init_i = 3;
limit_j = 6;
}
for( int i = init_i, k = 0; i >= 0; i--, k++ )
{
if( this -> show_descriptions )
cout << this -> fillSpaces( this -> description[i], size );
for( int j = 0; j < limit_j; j++ )
{
if( this -> bitTimeNulls[j] > k )
cout << this -> null_char;
else if( bitTime[j][k] )
cout << this -> true_char;
else
cout << this -> false_char;
}
this -> drawNewLine();
}
}
/**
* Method draw new line to console, and count number of lines to display live clock in the same place as old clock.
*
* @version 1.0 [19.08.2015]
* @author krzmig
*/
void binaryClock::drawNewLine()
{
cout << "\n";
this -> lines_counter++;
}
/**
* Back drawing cursor up for numbers of lines counted in drawNewLine method and to begin of line.
*
* @version 1.0 [19.08.2015]
* @author krzmig
*/
void binaryClock::clearConsole()
{
sleep(1);
for( int i = 0; i < this -> lines_counter; i++ )
{
cout << "\033[1A\033[K";
}
this -> lines_counter = 0;
}
/**
* This method fill spaces after given text to given size. ("1", 3) will return "1 "
*
* @param string/char content - string to expand
* @param int length - needed length of content
* @return string/char
*
* @version 1.0 [19.08.2015]
* @author krzmig
*/
string binaryClock::fillSpaces( string content, int length )
{
for( int i = content.size(); i < length; i++ )
{
content += ' ';
}
return content;
}
string binaryClock::fillSpaces( char ch, int length )
{
string content = "";
content += ch;
for( int i = 1; i < length; i++ )
{
content += ' ';
}
return content;
}
/**
* Generate string for null value with the same length as true string.
*
* @version 1.0 [19.08.2015]
* @author krzmig
*/
void binaryClock::genNullChar()
{
for( int i = 0; i < this -> utf8_strlen( this -> true_char ); i++ )
{
if( i == 0 )
this -> null_char = ' ';
else
this -> null_char += ' ';
}
}
/**
* Method to check console params and setup values depend on this params.
*
* @param char* param - Param name to set.
* @param char* next_param - Next param, example needed for setting true/false string.
* @return bool - true if next param is used
*
* @version 1.0 [19.08.2015]
* @author krzmig
*/
bool binaryClock::checkParam( char *param, char *next_param )
{
if( strcmp( param, "--help" ) == 0 )
this -> help();
else if( strcmp( param, "--description" ) == 0 )
this -> show_descriptions = true;
else if( strcmp( param, "--live" ) == 0 )
this -> run_live = true;
else if( strcmp( param, "--vertical" ) == 0 )
this -> vertical = true;
else if(( strcmp( param, "--decimal" ) == 0 ) || ( strcmp( param, "--bcd" ) == 0 ))
this -> show_decimal = true;
else if( strcmp( param, "--noseconds" ) == 0 )
this -> show_seconds = false;
else if( strcmp( param, "--hex" ) == 0 )
this -> hex_time = true;
else if( strcmp( param, "--12hours" ) == 0 )
{
this -> twelve_hours = true;
this -> bitTimeNulls[0] = 2;
}
else if( strcmp( param, "--truechar" ) == 0 )
{
this -> true_char = next_param;
this -> genNullChar();
return true;
}
else if( strcmp( param, "--falsechar" ) == 0 )
{
this -> false_char = next_param;
this -> genNullChar();
return true;
}
else
{
cout << "Unknow parametr " << param << "\n\n";
this -> help();
}
return false;
}
/**
* Method to count number of chars in string. There is problem in pure c++ to count utf-8 char as one char.
*
* @param string str - String content to count.
* @return int
*
* @version 1.0 [19.08.2015]
* @author krzmig
*/
int binaryClock::utf8_strlen( const string& str )
{
int size = 0;
for( int i = 0; i < str.length(); i++, size++)
{
int charnr = (unsigned char) str[i];
if(( charnr >= 0 ) && ( charnr <= 127 ))
i += 0;
else if(( charnr & 0xE0 ) == 0xC0 )
i++;
else if(( charnr & 0xF0 ) == 0xE0 )
i += 2;
else if(( charnr & 0xF8 ) == 0xF0 )
i += 3;
else if(( charnr & 0xFC ) == 0xF8 )
i += 4;
else if(( charnr & 0xFE ) == 0xFC )
i += 5;
else
return 0;
}
return size;
}
/**
* Main function of program.
*
* @version 1.0 [19.08.2015]
* @author krzmig
*/
int main( int argc, char* argv[] )
{
binaryClock* bc = new binaryClock;
for( int i = 1; i < argc; i++ )
{
if( i + 1 < argc )
{
if( bc -> checkParam( argv[i], argv[i+1] ) )
i = i + 1;
}
else
bc -> checkParam( argv[i], (char *)"" );
}
bc -> run();
delete bc;
}