tst_ansi_color.sh 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. tst_flag2color()
  6. {
  7. # NOTE: these colors should match colors defined in include/tst_ansi_color.h
  8. local ansi_color_blue='\033[1;34m'
  9. local ansi_color_green='\033[1;32m'
  10. local ansi_color_magenta='\033[1;35m'
  11. local ansi_color_red='\033[1;31m'
  12. local ansi_color_yellow='\033[1;33m'
  13. case "$1" in
  14. TPASS) printf $ansi_color_green;;
  15. TFAIL) printf $ansi_color_red;;
  16. TBROK) printf $ansi_color_red;;
  17. TWARN) printf $ansi_color_magenta;;
  18. TINFO) printf $ansi_color_blue;;
  19. TCONF) printf $ansi_color_yellow;;
  20. esac
  21. }
  22. tst_color_enabled()
  23. {
  24. [ "$LTP_COLORIZE_OUTPUT" = "n" ] || [ "$LTP_COLORIZE_OUTPUT" = "0" ] && return 0
  25. [ "$LTP_COLORIZE_OUTPUT" = "y" ] || [ "$LTP_COLORIZE_OUTPUT" = "1" ] && return 1
  26. [ -t 1 ] || return 0
  27. return 1
  28. }
  29. tst_print_colored()
  30. {
  31. tst_color_enabled
  32. local color=$?
  33. [ "$color" = "1" ] && tst_flag2color "$1"
  34. printf "$2"
  35. [ "$color" = "1" ] && printf '\033[0m'
  36. }