Skip to content
This repository was archived by the owner on May 18, 2024. It is now read-only.

Commit a49ed8d

Browse files
committed
Autodetect color support, allow override through arguments
1 parent 37c1d36 commit a49ed8d

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

src/2048.c

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -231,23 +231,15 @@ int main(int argc, char **argv)
231231

232232
/* parse options */
233233
int c;
234-
while ((c = getopt(argc, argv, "rchs:b:")) != -1) {
234+
int enable_color = has_colors();
235+
while ((c = getopt(argc, argv, "rcChs:b:")) != -1) {
235236
switch (c) {
236237
/* Color support */
237238
case 'c':
238-
if (has_colors()) {
239-
start_color();
240-
init_pair(0, 1, 0);
241-
init_pair(1, 2, 0);
242-
init_pair(2, 3, 0);
243-
init_pair(3, 4, 0);
244-
init_pair(4, 5, 0);
245-
init_pair(5, 6, 0);
246-
init_pair(6, 7, 0);
247-
}
248-
else {
249-
fprintf(stderr, "Terminal does not support color\n");
250-
}
239+
enable_color = 1;
240+
break;
241+
case 'C':
242+
enable_color = 0;
251243
break;
252244
// different board sizes
253245
case 's':;
@@ -275,7 +267,22 @@ int main(int argc, char **argv)
275267
exit(EXIT_SUCCESS);
276268
}
277269
}
278-
270+
271+
if (enable_color) {
272+
if (!has_colors()) {
273+
fprintf(stderr, "Terminal does not support color\n");
274+
} else {
275+
start_color();
276+
init_pair(0, 1, 0);
277+
init_pair(1, 2, 0);
278+
init_pair(2, 3, 0);
279+
init_pair(3, 4, 0);
280+
init_pair(4, 5, 0);
281+
init_pair(5, 6, 0);
282+
init_pair(6, 7, 0);
283+
}
284+
}
285+
279286
/* Allocate memory once we actually know amount */
280287
CALLOC2D(grid, grid_size);
281288

src/2048.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ typedef enum {
5050
"Options:\n"\
5151
" -s <size> Set the grid border length\n"\
5252
" -b <rate> Set the block spawn rate\n"\
53-
" -c Enables color support (ncurses version only)\n"
53+
" -c Enables color support (ncurses version only)\n"\
54+
" -C Disabled color support (ncurses version only)\n"
5455

5556
#endif

0 commit comments

Comments
 (0)