terminal.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # -*- coding: utf-8 -*-
  2. # <sure - utility belt for automated testing in python>
  3. # Copyright (C) <2010-2017> Gabriel Falcão <gabriel@nacaolivre.org>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. from __future__ import unicode_literals
  18. import os
  19. import sys
  20. import platform
  21. SUPPORTS_ANSI = False
  22. for handle in [sys.stdout, sys.stderr]:
  23. if (hasattr(handle, "isatty") and handle.isatty()) or \
  24. ('TERM' in os.environ and os.environ['TERM'] == 'ANSI'):
  25. if platform.system() == 'Windows' and not (
  26. 'TERM' in os.environ and os.environ['TERM'] == 'ANSI'):
  27. SUPPORTS_ANSI = False
  28. else:
  29. SUPPORTS_ANSI = True
  30. if os.getenv('SURE_NO_COLORS'):
  31. SUPPORTS_ANSI = False
  32. SUPPORTS_ANSI = False
  33. def red(string):
  34. if not SUPPORTS_ANSI:
  35. return string
  36. return r"\033[1;31m{0}\033[0m".format(string)
  37. def green(string):
  38. if not SUPPORTS_ANSI:
  39. return string
  40. return r"\033[1;32m{0}\033[0m".format(string)
  41. def yellow(string):
  42. if not SUPPORTS_ANSI:
  43. return string
  44. return r"\033[1;33m{0}\033[0m".format(string)
  45. def white(string):
  46. if not SUPPORTS_ANSI:
  47. return string
  48. return r"\033[1;37m{0}\033[0m".format(string)