From a49ed8dd36739d4edd72b9bf249c683681fa94a3 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Fri, 12 Sep 2014 20:22:03 +0400 Subject: [PATCH] Autodetect color support, allow override through arguments --- src/2048.c | 37 ++++++++++++++++++++++--------------- src/2048.h | 3 ++- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/2048.c b/src/2048.c index 3176a61..b784f59 100644 --- a/src/2048.c +++ b/src/2048.c @@ -231,23 +231,15 @@ int main(int argc, char **argv) /* parse options */ int c; - while ((c = getopt(argc, argv, "rchs:b:")) != -1) { + int enable_color = has_colors(); + while ((c = getopt(argc, argv, "rcChs:b:")) != -1) { switch (c) { /* Color support */ case 'c': - if (has_colors()) { - start_color(); - init_pair(0, 1, 0); - init_pair(1, 2, 0); - init_pair(2, 3, 0); - init_pair(3, 4, 0); - init_pair(4, 5, 0); - init_pair(5, 6, 0); - init_pair(6, 7, 0); - } - else { - fprintf(stderr, "Terminal does not support color\n"); - } + enable_color = 1; + break; + case 'C': + enable_color = 0; break; // different board sizes case 's':; @@ -275,7 +267,22 @@ int main(int argc, char **argv) exit(EXIT_SUCCESS); } } - + + if (enable_color) { + if (!has_colors()) { + fprintf(stderr, "Terminal does not support color\n"); + } else { + start_color(); + init_pair(0, 1, 0); + init_pair(1, 2, 0); + init_pair(2, 3, 0); + init_pair(3, 4, 0); + init_pair(4, 5, 0); + init_pair(5, 6, 0); + init_pair(6, 7, 0); + } + } + /* Allocate memory once we actually know amount */ CALLOC2D(grid, grid_size); diff --git a/src/2048.h b/src/2048.h index d1f5b3e..26177b7 100644 --- a/src/2048.h +++ b/src/2048.h @@ -50,6 +50,7 @@ typedef enum { "Options:\n"\ " -s Set the grid border length\n"\ " -b Set the block spawn rate\n"\ - " -c Enables color support (ncurses version only)\n" + " -c Enables color support (ncurses version only)\n"\ + " -C Disabled color support (ncurses version only)\n" #endif