Home | History | Annotate | Download | only in lib
      1 /*
      2  * Copyright (c) 2017 Petr Vorel <pvorel (at) suse.cz>
      3  *
      4  * This program is free software: you can redistribute it and/or modify
      5  * it under the terms of the GNU General Public License as published by
      6  * the Free Software Foundation, either version 2 of the License, or
      7  * (at your option) any later version.
      8  *
      9  * This program is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12  * GNU General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU General Public License
     15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
     16  */
     17 
     18 #include <unistd.h>
     19 #include <stdlib.h>
     20 #include <string.h>
     21 
     22 #include "tst_res_flags.h"
     23 #include "tst_ansi_color.h"
     24 
     25 char* tst_ttype2color(int ttype)
     26 {
     27 	switch (TTYPE_RESULT(ttype)) {
     28 	case TPASS:
     29 		return ANSI_COLOR_GREEN;
     30 	break;
     31 	case TFAIL:
     32 		return ANSI_COLOR_RED;
     33 	break;
     34 	case TBROK:
     35 		return ANSI_COLOR_RED;
     36 	break;
     37 	case TCONF:
     38 		return ANSI_COLOR_YELLOW;
     39 	break;
     40 	case TWARN:
     41 		return ANSI_COLOR_MAGENTA;
     42 	break;
     43 	case TINFO:
     44 		return ANSI_COLOR_BLUE;
     45 	break;
     46 	default:
     47 		return "";
     48 	}
     49 }
     50 
     51 int tst_color_enabled(int fd)
     52 {
     53 	static int color;
     54 
     55 	if (color)
     56 		return color - 1;
     57 
     58 	char *env = getenv("LTP_COLORIZE_OUTPUT");
     59 
     60 	if (env) {
     61 		if (!strcmp(env, "n") || !strcmp(env, "0"))
     62 			color = 1;
     63 
     64 		if (!strcmp(env, "y") || !strcmp(env, "1"))
     65 			color = 2;
     66 
     67 		return color - 1;
     68 	}
     69 
     70 	if (isatty(fd) == 0)
     71 		color = 1;
     72 	else
     73 		color = 2;
     74 
     75 	return color - 1;
     76 }
     77