xref: /aosp_15_r20/external/ltp/testcases/lib/tst_ansi_color.sh (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) Linux Test Project, 2017
4# Written by Petr Vorel <[email protected]>
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	if [ "$LTP_COLORIZE_OUTPUT" = "n" -o "$LTP_COLORIZE_OUTPUT" = "0" ]; then
28		return 0
29	fi
30
31	if [ "$LTP_COLORIZE_OUTPUT" = "y" -o "$LTP_COLORIZE_OUTPUT" = "1" ]; then
32		return 1
33	fi
34
35	[ -t 1 ] || return 0
36
37	return 1
38}
39
40tst_print_colored()
41{
42	local color=0
43
44	tst_color_enabled || color=$?
45
46	[ "$color" != 1 ] || tst_flag2color "$1"
47	printf "$2"
48	[ "$color" != 1 ] || printf '\033[0m'
49}
50