colors.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. """
  2. .. versionadded:: 0.9.2
  3. Functions for wrapping strings in ANSI color codes.
  4. Each function within this module returns the input string ``text``, wrapped
  5. with ANSI color codes for the appropriate color.
  6. For example, to print some text as green on supporting terminals::
  7. from fabric.colors import green
  8. print(green("This text is green!"))
  9. Because these functions simply return modified strings, you can nest them::
  10. from fabric.colors import red, green
  11. print(red("This sentence is red, except for " + \
  12. green("these words, which are green") + "."))
  13. If ``bold`` is set to ``True``, the ANSI flag for bolding will be flipped on
  14. for that particular invocation, which usually shows up as a bold or brighter
  15. version of the original color on most terminals.
  16. .. versionchanged:: 1.11
  17. Added support for the shell env var ``FABRIC_DISABLE_COLORS``; if this
  18. variable is present and set to any non-empty value, all colorization driven
  19. by this module will be skipped/disabled.
  20. """
  21. import os
  22. def _wrap_with(code):
  23. def inner(text, bold=False):
  24. c = code
  25. if os.environ.get('FABRIC_DISABLE_COLORS'):
  26. return text
  27. if bold:
  28. c = "1;%s" % c
  29. return "\033[%sm%s\033[0m" % (c, text)
  30. return inner
  31. red = _wrap_with('31')
  32. green = _wrap_with('32')
  33. yellow = _wrap_with('33')
  34. blue = _wrap_with('34')
  35. magenta = _wrap_with('35')
  36. cyan = _wrap_with('36')
  37. white = _wrap_with('37')