1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (c) 2017 Petr Vorel <pvorel@suse.cz> 4 */ 5 6 #include <unistd.h> 7 #include <stdlib.h> 8 #include <string.h> 9 10 #include "tst_res_flags.h" 11 #include "tst_ansi_color.h" 12 tst_ttype2color(int ttype)13char* tst_ttype2color(int ttype) 14 { 15 switch (TTYPE_RESULT(ttype)) { 16 case TPASS: 17 return ANSI_COLOR_GREEN; 18 break; 19 case TFAIL: 20 return ANSI_COLOR_RED; 21 break; 22 case TBROK: 23 return ANSI_COLOR_RED; 24 break; 25 case TCONF: 26 return ANSI_COLOR_YELLOW; 27 break; 28 case TWARN: 29 return ANSI_COLOR_MAGENTA; 30 break; 31 case TINFO: 32 return ANSI_COLOR_BLUE; 33 break; 34 default: 35 return ""; 36 } 37 } 38 tst_color_enabled(int fd)39int tst_color_enabled(int fd) 40 { 41 static int color; 42 43 if (color) 44 return color - 1; 45 46 char *env = getenv("LTP_COLORIZE_OUTPUT"); 47 48 if (env) { 49 if (!strcmp(env, "n") || !strcmp(env, "0")) 50 color = 1; 51 52 if (!strcmp(env, "y") || !strcmp(env, "1")) 53 color = 2; 54 55 return color - 1; 56 } 57 58 if (isatty(fd) == 0) 59 color = 1; 60 else 61 color = 2; 62 63 return color - 1; 64 } 65