terminal.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright (c) 2011 The Chromium OS Authors.
  2. #
  3. # See file CREDITS for list of people who contributed to this
  4. # project.
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License as
  8. # published by the Free Software Foundation; either version 2 of
  9. # the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. # MA 02111-1307 USA
  20. #
  21. """Terminal utilities
  22. This module handles terminal interaction including ANSI color codes.
  23. """
  24. class Color(object):
  25. """Conditionally wraps text in ANSI color escape sequences."""
  26. BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
  27. BOLD = -1
  28. COLOR_START = '\033[1;%dm'
  29. BOLD_START = '\033[1m'
  30. RESET = '\033[0m'
  31. def __init__(self, enabled=True):
  32. """Create a new Color object, optionally disabling color output.
  33. Args:
  34. enabled: True if color output should be enabled. If False then this
  35. class will not add color codes at all.
  36. """
  37. self._enabled = enabled
  38. def Start(self, color):
  39. """Returns a start color code.
  40. Args:
  41. color: Color to use, .e.g BLACK, RED, etc.
  42. Returns:
  43. If color is enabled, returns an ANSI sequence to start the given color,
  44. otherwise returns empty string
  45. """
  46. if self._enabled:
  47. return self.COLOR_START % (color + 30)
  48. return ''
  49. def Stop(self):
  50. """Retruns a stop color code.
  51. Returns:
  52. If color is enabled, returns an ANSI color reset sequence, otherwise
  53. returns empty string
  54. """
  55. if self._enabled:
  56. return self.RESET
  57. return ''
  58. def Color(self, color, text):
  59. """Returns text with conditionally added color escape sequences.
  60. Keyword arguments:
  61. color: Text color -- one of the color constants defined in this class.
  62. text: The text to color.
  63. Returns:
  64. If self._enabled is False, returns the original text. If it's True,
  65. returns text with color escape sequences based on the value of color.
  66. """
  67. if not self._enabled:
  68. return text
  69. if color == self.BOLD:
  70. start = self.BOLD_START
  71. else:
  72. start = self.COLOR_START % (color + 30)
  73. return start + text + self.RESET