1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) Linux Test Project, 2017
4# Written by Petr Vorel <pvorel@suse.cz>
5
6tst_flag2color()
7{
8	# NOTE: these colors should match colors defined in include/tst_ansi_color.h
9	local ansi_color_blue='\033[1;34m'
10	local ansi_color_green='\033[1;32m'
11	local ansi_color_magenta='\033[1;35m'
12	local ansi_color_red='\033[1;31m'
13	local ansi_color_yellow='\033[1;33m'
14
15	case "$1" in
16	TPASS) printf $ansi_color_green;;
17	TFAIL) printf $ansi_color_red;;
18	TBROK) printf $ansi_color_red;;
19	TWARN) printf $ansi_color_magenta;;
20	TINFO) printf $ansi_color_blue;;
21	TCONF) printf $ansi_color_yellow;;
22	esac
23}
24
25tst_color_enabled()
26{
27	[ "$LTP_COLORIZE_OUTPUT" = "n" ] || [ "$LTP_COLORIZE_OUTPUT" = "0" ] && return 0
28	[ "$LTP_COLORIZE_OUTPUT" = "y" ] || [ "$LTP_COLORIZE_OUTPUT" = "1" ] && return 1
29	[ -t 1 ] || return 0
30	return 1
31}
32
33tst_print_colored()
34{
35	tst_color_enabled
36	local color=$?
37
38	[ "$color" = "1" ] && tst_flag2color "$1"
39	printf "$2"
40	[ "$color" = "1" ] && printf '\033[0m'
41}
42