Skip to content

Commit 6ce59e9

Browse files
Onur Berk Töreabishekvashok
authored andcommitted
Adds rainbow mode and fixes screen resize bug
Fixes window resize problem, earlier when the terminal window was resized to a size lesser than 3, a null pointer exception was invoked. Rainbow mode support gives users the choice of displaying the matrix in different colors using the -r option. Signed-off-by: Abishek V Ashok <abishekvashok@gmail.com>
1 parent 6525abe commit 6ce59e9

1 file changed

Lines changed: 54 additions & 1 deletion

File tree

cmatrix.c

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ void usage(void) {
140140
printf(" -V: Print version information and exit\n");
141141
printf(" -u delay (0 - 10, default 4): Screen update delay\n");
142142
printf(" -C [color]: Use this color for matrix (default green)\n");
143+
printf(" -r: rainbow mode\n");
143144
}
144145

145146
void version(void) {
@@ -235,6 +236,13 @@ void handle_sigwinch(int s) {
235236
COLS = win.ws_col;
236237
LINES = win.ws_row;
237238

239+
if(LINES <10){
240+
LINES = 10;
241+
}
242+
if(COLS <10){
243+
COLS = 10;
244+
}
245+
238246
#ifdef HAVE_RESIZETERM
239247
resizeterm(LINES, COLS);
240248
#ifdef HAVE_WRESIZE
@@ -264,16 +272,21 @@ int main(int argc, char *argv[]) {
264272
int update = 4;
265273
int highnum = 0;
266274
int mcolor = COLOR_GREEN;
275+
int rainbow = 0;
267276
int randnum = 0;
268277
int randmin = 0;
269278
int pause = 0;
270279

271280
char *oldtermname;
272281
char *syscmd = NULL;
273282

283+
time_t t;
284+
srand((unsigned) time(&t));
285+
286+
274287
/* Many thanks to morph- (morph@jmss.com) for this getopt patch */
275288
opterr = 0;
276-
while ((optchr = getopt(argc, argv, "abBfhlnosxVu:C:")) != EOF) {
289+
while ((optchr = getopt(argc, argv, "abBfhlnrosxVu:C:")) != EOF) {
277290
switch (optchr) {
278291
case 's':
279292
screensaver = 1;
@@ -340,6 +353,9 @@ int main(int argc, char *argv[]) {
340353
case 'V':
341354
version();
342355
exit(0);
356+
case 'r':
357+
rainbow = 1;
358+
break;
343359
}
344360
}
345361

@@ -424,6 +440,7 @@ if (console) {
424440
var_init();
425441

426442
while (1) {
443+
427444
count++;
428445
if (count > 4) {
429446
count = 1;
@@ -463,29 +480,40 @@ if (console) {
463480
break;
464481
case '!':
465482
mcolor = COLOR_RED;
483+
rainbow = 0;
466484
break;
467485
case '@':
468486
mcolor = COLOR_GREEN;
487+
rainbow = 0;
469488
break;
470489
case '#':
471490
mcolor = COLOR_YELLOW;
491+
rainbow = 0;
472492
break;
473493
case '$':
474494
mcolor = COLOR_BLUE;
495+
rainbow = 0;
475496
break;
476497
case '%':
477498
mcolor = COLOR_MAGENTA;
499+
rainbow = 0;
478500
break;
501+
case 'r':
502+
rainbow = 1;
503+
break;
479504
case '^':
480505
mcolor = COLOR_CYAN;
506+
rainbow = 0;
481507
break;
482508
case '&':
483509
mcolor = COLOR_WHITE;
510+
rainbow = 0;
484511
break;
485512
case 'p':
486513
case 'P':
487514
pause = (pause == 0)?1:0;
488515
break;
516+
489517
}
490518
}
491519
}
@@ -628,6 +656,31 @@ if (console) {
628656
attroff(A_ALTCHARSET);
629657
}
630658
} else {
659+
660+
if(rainbow){
661+
int randomColor = rand() % 6;
662+
663+
switch(randomColor){
664+
case 0:
665+
mcolor = COLOR_GREEN;
666+
break;
667+
case 1:
668+
mcolor = COLOR_BLUE;
669+
break;
670+
case 2:
671+
mcolor = COLOR_BLACK;
672+
break;
673+
case 3:
674+
mcolor = COLOR_YELLOW;
675+
break;
676+
case 4:
677+
mcolor = COLOR_CYAN;
678+
break;
679+
case 5:
680+
mcolor = COLOR_MAGENTA;
681+
break;
682+
}
683+
}
631684
attron(COLOR_PAIR(mcolor));
632685
if (matrix[i][j].val == 1) {
633686
if (bold) {

0 commit comments

Comments
 (0)