UNPKG

14.6 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3/**
4 * A clock using blessed
5 * Copyright (c) 2013, Christopher Jeffrey (MIT License).
6 * https://github.com/chjj/blessed
7 */
8
9process.title = 'time.js';
10
11var argv = process.argv;
12
13if (~argv.indexOf('-h') || ~argv.indexOf('--help')) {
14 console.log('Options:');
15 console.log('-s - Show seconds.');
16 console.log('-n - No leading zero on hours.');
17 console.log('-d - Show date box.');
18 console.log('--skinny - Skinny text.');
19 return process.exit(0);
20}
21
22var blessed = require('blessed');
23
24var screen = blessed.screen({
25 autoPadding: true
26});
27
28var lastTime;
29
30var positions = {};
31
32var container = blessed.box({
33 parent: screen,
34 top: 'center',
35 left: 'center',
36 width: 'shrink',
37 height: 9,
38 //padding: 2,
39 //border: {
40 // type: 'line',
41 // fg: 'black'
42 //}
43});
44
45// Workaround for centering shrunken box.
46container.on('prerender', function() {
47 var lpos = container._getCoords(true);
48 if (lpos) {
49 container.rleft = (screen.width - (lpos.xl - lpos.xi)) / 2 | 0;
50 }
51});
52
53var date = blessed.box({
54 parent: screen,
55 top: 1,
56 left: 1,
57 //top: '80%',
58 //left: 'center',
59 width: 'shrink',
60 height: 'shrink',
61 border: {
62 type: 'line',
63 fg: 'black'
64 }
65});
66
67date.hide();
68
69var wid = ~argv.indexOf('--skinny') ? 1 : 2;
70// var bch = ' ';
71var bch = '│';
72var inverse = true;
73
74// var bch = '*';
75// var bch = '·';
76// var bch = '│';
77// var bch = '◆';
78// var bch = '▪';
79// var inverse = false;
80
81// TODO: Potentially make height of each char 9 instead
82// of 8 so we can vertically center horizontal lines
83// in 4, 8, etc.
84
85for (var i = 0; i < 10; i++) {
86 var symbols = positions[i] = {};
87
88 /**
89 * Zero
90 */
91
92 symbols[0] = blessed.box({
93 parent: container,
94 top: 0,
95 left: 0,
96 width: 10,
97 height: 9
98 });
99
100 blessed.box({
101 parent: symbols[0],
102 top: 0,
103 left: 0,
104 right: 0,
105 height: 1,
106 ch: bch,
107 style: {
108 fg: 'default',
109 inverse: inverse
110 }
111 });
112
113 blessed.box({
114 parent: symbols[0],
115 top: 0,
116 left: 0,
117 bottom: 0,
118 width: wid,
119 ch: bch,
120 style: {
121 fg: 'default',
122 inverse: inverse
123 }
124 });
125
126 blessed.box({
127 parent: symbols[0],
128 top: 0,
129 right: 0,
130 bottom: 0,
131 width: wid,
132 ch: bch,
133 style: {
134 fg: 'default',
135 inverse: inverse
136 }
137 });
138
139 blessed.box({
140 parent: symbols[0],
141 top: 8,
142 left: 0,
143 right: 0,
144 height: 1,
145 ch: bch,
146 style: {
147 fg: 'default',
148 inverse: inverse
149 }
150 });
151
152 symbols[0].hide();
153
154 /**
155 * One
156 */
157
158 symbols[1] = blessed.box({
159 parent: container,
160 top: 0,
161 width: 10,
162 height: 9
163 });
164
165 blessed.box({
166 parent: symbols[1],
167 top: 0,
168 left: 'center',
169 width: 2,
170 ch: bch,
171 style: {
172 fg: 'default',
173 inverse: inverse
174 }
175 });
176
177 symbols[1].hide();
178
179 /**
180 * Two
181 */
182
183 symbols[2] = blessed.box({
184 parent: container,
185 top: 0,
186 left: 0,
187 width: 10,
188 height: 9
189 });
190
191 blessed.box({
192 parent: symbols[2],
193 top: 0,
194 left: 0,
195 right: 0,
196 height: 1,
197 ch: bch,
198 style: {
199 fg: 'default',
200 inverse: inverse
201 }
202 });
203
204 blessed.box({
205 parent: symbols[2],
206 top: 0,
207 right: 0,
208 height: 4,
209 width: wid,
210 ch: bch,
211 style: {
212 fg: 'default',
213 inverse: inverse
214 }
215 });
216
217 blessed.box({
218 parent: symbols[2],
219 top: 4,
220 left: 0,
221 right: 0,
222 height: 1,
223 ch: bch,
224 style: {
225 fg: 'default',
226 inverse: inverse
227 }
228 });
229
230 blessed.box({
231 parent: symbols[2],
232 top: 4,
233 left: 0,
234 height: 4,
235 width: wid,
236 ch: bch,
237 style: {
238 fg: 'default',
239 inverse: inverse
240 }
241 });
242
243 blessed.box({
244 parent: symbols[2],
245 top: 8,
246 left: 0,
247 right: 0,
248 height: 1,
249 ch: bch,
250 style: {
251 fg: 'default',
252 inverse: inverse
253 }
254 });
255
256 symbols[2].hide();
257
258 /**
259 * Three
260 */
261
262 symbols[3] = blessed.box({
263 parent: container,
264 top: 0,
265 left: 0,
266 width: 10,
267 height: 9
268 });
269
270 blessed.box({
271 parent: symbols[3],
272 top: 0,
273 bottom: 0,
274 right: 0,
275 width: wid,
276 height: 9,
277 ch: bch,
278 style: {
279 fg: 'default',
280 inverse: inverse
281 }
282 });
283
284 blessed.box({
285 parent: symbols[3],
286 top: 0,
287 right: 0,
288 left: 0,
289 height: 1,
290 ch: bch,
291 style: {
292 fg: 'default',
293 inverse: inverse
294 }
295 });
296
297 blessed.box({
298 parent: symbols[3],
299 top: 4,
300 right: 0,
301 left: 0,
302 height: 1,
303 ch: bch,
304 style: {
305 fg: 'default',
306 inverse: inverse
307 }
308 });
309
310 blessed.box({
311 parent: symbols[3],
312 top: 8,
313 right: 0,
314 left: 0,
315 height: 1,
316 ch: bch,
317 style: {
318 fg: 'default',
319 inverse: inverse
320 }
321 });
322
323 symbols[3].hide();
324
325 /**
326 * Four
327 */
328
329 symbols[4] = blessed.box({
330 parent: container,
331 top: 0,
332 left: 0,
333 width: 10,
334 height: 9
335 });
336
337 blessed.box({
338 parent: symbols[4],
339 top: 0,
340 bottom: 0,
341 right: 0,
342 width: wid,
343 height: 9,
344 ch: bch,
345 style: {
346 fg: 'default',
347 inverse: inverse
348 }
349 });
350
351 blessed.box({
352 parent: symbols[4],
353 top: 4,
354 right: 0,
355 left: 0,
356 height: 1,
357 ch: bch,
358 style: {
359 fg: 'default',
360 inverse: inverse
361 }
362 });
363
364 blessed.box({
365 parent: symbols[4],
366 top: 0,
367 left: 0,
368 width: wid,
369 height: 4,
370 ch: bch,
371 style: {
372 fg: 'default',
373 inverse: inverse
374 }
375 });
376
377 symbols[4].hide();
378
379 /**
380 * Five
381 */
382
383 symbols[5] = blessed.box({
384 parent: container,
385 top: 0,
386 left: 0,
387 width: 10,
388 height: 9
389 });
390
391 blessed.box({
392 parent: symbols[5],
393 top: 0,
394 left: 0,
395 right: 0,
396 height: 1,
397 ch: bch,
398 style: {
399 fg: 'default',
400 inverse: inverse
401 }
402 });
403
404 blessed.box({
405 parent: symbols[5],
406 top: 0,
407 left: 0,
408 height: 4,
409 width: wid,
410 ch: bch,
411 style: {
412 fg: 'default',
413 inverse: inverse
414 }
415 });
416
417 blessed.box({
418 parent: symbols[5],
419 top: 4,
420 left: 0,
421 right: 0,
422 height: 1,
423 ch: bch,
424 style: {
425 fg: 'default',
426 inverse: inverse
427 }
428 });
429
430 blessed.box({
431 parent: symbols[5],
432 top: 4,
433 right: 0,
434 height: 4,
435 width: wid,
436 ch: bch,
437 style: {
438 fg: 'default',
439 inverse: inverse
440 }
441 });
442
443 blessed.box({
444 parent: symbols[5],
445 top: 8,
446 left: 0,
447 right: 0,
448 height: 1,
449 ch: bch,
450 style: {
451 fg: 'default',
452 inverse: inverse
453 }
454 });
455
456 symbols[5].hide();
457
458 /**
459 * Six
460 */
461
462 symbols[6] = blessed.box({
463 parent: container,
464 top: 0,
465 left: 0,
466 width: 10,
467 height: 9
468 });
469
470 blessed.box({
471 parent: symbols[6],
472 top: 0,
473 left: 0,
474 right: 0,
475 height: 1,
476 ch: bch,
477 style: {
478 fg: 'default',
479 inverse: inverse
480 }
481 });
482
483 blessed.box({
484 parent: symbols[6],
485 top: 0,
486 left: 0,
487 bottom: 0,
488 width: wid,
489 ch: bch,
490 style: {
491 fg: 'default',
492 inverse: inverse
493 }
494 });
495
496 blessed.box({
497 parent: symbols[6],
498 top: 4,
499 left: 0,
500 right: 0,
501 height: 1,
502 ch: bch,
503 style: {
504 fg: 'default',
505 inverse: inverse
506 }
507 });
508
509 blessed.box({
510 parent: symbols[6],
511 top: 4,
512 right: 0,
513 height: 4,
514 width: wid,
515 ch: bch,
516 style: {
517 fg: 'default',
518 inverse: inverse
519 }
520 });
521
522 blessed.box({
523 parent: symbols[6],
524 top: 8,
525 left: 0,
526 right: 0,
527 height: 1,
528 ch: bch,
529 style: {
530 fg: 'default',
531 inverse: inverse
532 }
533 });
534
535 symbols[6].hide();
536
537 /**
538 * Seven
539 */
540
541 symbols[7] = blessed.box({
542 parent: container,
543 top: 0,
544 left: 0,
545 width: 10,
546 height: 9
547 });
548
549 blessed.box({
550 parent: symbols[7],
551 top: 0,
552 bottom: 0,
553 right: 0,
554 width: wid,
555 height: 9,
556 ch: bch,
557 style: {
558 fg: 'default',
559 inverse: inverse
560 }
561 });
562
563 blessed.box({
564 parent: symbols[7],
565 top: 0,
566 right: 0,
567 left: 0,
568 height: 1,
569 ch: bch,
570 style: {
571 fg: 'default',
572 inverse: inverse
573 }
574 });
575
576 symbols[7].hide();
577
578 /**
579 * Eight
580 */
581
582 symbols[8] = blessed.box({
583 parent: container,
584 top: 0,
585 left: 0,
586 width: 10,
587 height: 9
588 });
589
590 blessed.box({
591 parent: symbols[8],
592 top: 0,
593 left: 0,
594 right: 0,
595 height: 1,
596 ch: bch,
597 style: {
598 fg: 'default',
599 inverse: inverse
600 }
601 });
602
603 blessed.box({
604 parent: symbols[8],
605 top: 0,
606 left: 0,
607 bottom: 0,
608 width: wid,
609 ch: bch,
610 style: {
611 fg: 'default',
612 inverse: inverse
613 }
614 });
615
616 blessed.box({
617 parent: symbols[8],
618 top: 4,
619 left: 0,
620 right: 0,
621 height: 1,
622 ch: bch,
623 style: {
624 fg: 'default',
625 inverse: inverse
626 }
627 });
628
629 blessed.box({
630 parent: symbols[8],
631 top: 0,
632 right: 0,
633 bottom: 0,
634 width: wid,
635 ch: bch,
636 style: {
637 fg: 'default',
638 inverse: inverse
639 }
640 });
641
642 blessed.box({
643 parent: symbols[8],
644 top: 8,
645 left: 0,
646 right: 0,
647 height: 1,
648 ch: bch,
649 style: {
650 fg: 'default',
651 inverse: inverse
652 }
653 });
654
655 symbols[8].hide();
656
657 /**
658 * Nine
659 */
660
661 symbols[9] = blessed.box({
662 parent: container,
663 top: 0,
664 left: 0,
665 width: 10,
666 height: 9
667 });
668
669 blessed.box({
670 parent: symbols[9],
671 top: 0,
672 left: 0,
673 right: 0,
674 height: 1,
675 ch: bch,
676 style: {
677 fg: 'default',
678 inverse: inverse
679 }
680 });
681
682 blessed.box({
683 parent: symbols[9],
684 top: 0,
685 left: 0,
686 height: 4,
687 width: wid,
688 ch: bch,
689 style: {
690 fg: 'default',
691 inverse: inverse
692 }
693 });
694
695 blessed.box({
696 parent: symbols[9],
697 top: 4,
698 left: 0,
699 right: 0,
700 height: 1,
701 ch: bch,
702 style: {
703 fg: 'default',
704 inverse: inverse
705 }
706 });
707
708 blessed.box({
709 parent: symbols[9],
710 top: 0,
711 right: 0,
712 bottom: 0,
713 width: wid,
714 ch: bch,
715 style: {
716 fg: 'default',
717 inverse: inverse
718 }
719 });
720
721 blessed.box({
722 parent: symbols[9],
723 top: 8,
724 left: 0,
725 right: 0,
726 height: 1,
727 ch: bch,
728 style: {
729 fg: 'default',
730 inverse: inverse
731 }
732 });
733
734 symbols[9].hide();
735
736 /**
737 * Colon
738 */
739
740 symbols[':'] = blessed.box({
741 parent: container,
742 top: 0,
743 left: 0,
744 width: 5,
745 height: 9
746 });
747
748 blessed.box({
749 parent: symbols[':'],
750 top: 3,
751 left: 'center',
752 width: 2,
753 height: 1,
754 ch: bch,
755 style: {
756 fg: 'black',
757 inverse: inverse
758 }
759 });
760
761 blessed.box({
762 parent: symbols[':'],
763 top: 6,
764 left: 'center',
765 width: 2,
766 height: 1,
767 ch: bch,
768 style: {
769 fg: 'black',
770 inverse: inverse
771 }
772 });
773
774 symbols[':'].hide();
775
776 /**
777 * A
778 */
779
780 symbols['a'] = blessed.box({
781 parent: container,
782 top: 2,
783 left: 0,
784 width: 10,
785 height: 7
786 });
787
788 blessed.box({
789 parent: symbols['a'],
790 top: 0,
791 left: 0,
792 right: 0,
793 height: 1,
794 ch: bch,
795 style: {
796 fg: 'blue',
797 inverse: inverse
798 }
799 });
800
801 blessed.box({
802 parent: symbols['a'],
803 top: 0,
804 left: 0,
805 bottom: 0,
806 width: wid,
807 ch: bch,
808 style: {
809 fg: 'blue',
810 inverse: inverse
811 }
812 });
813
814 blessed.box({
815 parent: symbols['a'],
816 top: 3,
817 left: 0,
818 right: 0,
819 height: 1,
820 ch: bch,
821 style: {
822 fg: 'blue',
823 inverse: inverse
824 }
825 });
826
827 blessed.box({
828 parent: symbols['a'],
829 top: 0,
830 right: 0,
831 bottom: 0,
832 width: wid,
833 ch: bch,
834 style: {
835 fg: 'blue',
836 inverse: inverse
837 }
838 });
839
840 symbols['a'].hide();
841
842 /**
843 * P
844 */
845
846 symbols['p'] = blessed.box({
847 parent: container,
848 top: 2,
849 left: 0,
850 width: 10,
851 height: 7
852 });
853
854 blessed.box({
855 parent: symbols['p'],
856 top: 0,
857 left: 0,
858 right: 0,
859 height: 1,
860 ch: bch,
861 style: {
862 fg: 'blue',
863 inverse: inverse
864 }
865 });
866
867 blessed.box({
868 parent: symbols['p'],
869 top: 0,
870 right: 0,
871 height: 4,
872 width: wid,
873 ch: bch,
874 style: {
875 fg: 'blue',
876 inverse: inverse
877 }
878 });
879
880 blessed.box({
881 parent: symbols['p'],
882 top: 0,
883 left: 0,
884 bottom: 0,
885 width: wid,
886 ch: bch,
887 style: {
888 fg: 'blue',
889 inverse: inverse
890 }
891 });
892
893 blessed.box({
894 parent: symbols['p'],
895 top: 3,
896 left: 0,
897 right: 0,
898 height: 1,
899 ch: bch,
900 style: {
901 fg: 'blue',
902 inverse: inverse
903 }
904 });
905
906 symbols['p'].hide();
907
908 /**
909 * M
910 */
911
912 symbols['m'] = blessed.box({
913 parent: container,
914 top: 2,
915 left: 0,
916 width: 10,
917 height: 7
918 });
919
920 blessed.box({
921 parent: symbols['m'],
922 top: 0,
923 left: 0,
924 right: 0,
925 height: 1,
926 ch: bch,
927 style: {
928 fg: 'black',
929 inverse: inverse
930 }
931 });
932
933 blessed.box({
934 parent: symbols['m'],
935 top: 0,
936 left: 0,
937 bottom: 0,
938 width: wid,
939 ch: bch,
940 style: {
941 fg: 'black',
942 inverse: inverse
943 }
944 });
945
946 blessed.box({
947 parent: symbols['m'],
948 top: 0,
949 right: 0,
950 bottom: 0,
951 width: wid,
952 ch: bch,
953 style: {
954 fg: 'black',
955 inverse: inverse
956 }
957 });
958
959 blessed.box({
960 parent: symbols['m'],
961 top: 0,
962 bottom: 0,
963 left: 'center',
964 width: wid,
965 ch: bch,
966 style: {
967 fg: 'black',
968 inverse: inverse
969 }
970 });
971
972 symbols['m'].hide();
973}
974
975function updateTime() {
976 var pos = 0
977 , d = new Date
978 , im = 'am'
979 , time
980 , h
981 , m
982 , s;
983
984 h = d.getHours();
985 if (h >= 12) {
986 im = 'pm';
987 }
988 if (h > 12) {
989 h -= 12;
990 }
991 if (h === 0) h = 12;
992 if (h < 10) {
993 h = '0' + h;
994 }
995
996 m = d.getMinutes();
997 if (m < 10) {
998 m = '0' + m;
999 }
1000
1001 s = d.getSeconds();
1002 if (s < 10) {
1003 s = '0' + s;
1004 }
1005
1006 time = ~argv.indexOf('-s')
1007 ? h + ':' + m + ':' + s + im
1008 : h + ':' + m + im;
1009
1010 if (time === lastTime) return;
1011 lastTime = time;
1012
1013 time = time.split('');
1014
1015 if (~argv.indexOf('-n')) {
1016 if (time[0] === '0') time[0] = ' ';
1017 }
1018
1019 Object.keys(positions).forEach(function(key) {
1020 var symbols = positions[key];
1021 Object.keys(symbols).forEach(function(key) {
1022 symbols[key].hide();
1023 });
1024 });
1025
1026 time.forEach(function(ch, i) {
1027 var symbols = positions[i]
1028 , symbol = symbols[ch];
1029
1030 if (!symbol) return;
1031
1032 symbol.rleft = pos;
1033 pos += symbol.width + 2;
1034
1035 symbol.show();
1036 });
1037
1038 if (~argv.indexOf('-d')) {
1039 date.show();
1040 date.setContent(d.toISOString().replace(/\.\d+/, ''));
1041 }
1042
1043 screen.render();
1044}
1045
1046setInterval(updateTime, ~argv.indexOf('-s') ? 100 : 950);
1047
1048updateTime();
1049
1050screen.key('q', function() {
1051 process.exit(0);
1052});