text.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # -*- test-case-name: twisted.conch.test.test_text -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Character attribute manipulation API.
  6. This module provides a domain-specific language (using Python syntax)
  7. for the creation of text with additional display attributes associated
  8. with it. It is intended as an alternative to manually building up
  9. strings containing ECMA 48 character attribute control codes. It
  10. currently supports foreground and background colors (black, red,
  11. green, yellow, blue, magenta, cyan, and white), intensity selection,
  12. underlining, blinking and reverse video. Character set selection
  13. support is planned.
  14. Character attributes are specified by using two Python operations:
  15. attribute lookup and indexing. For example, the string \"Hello
  16. world\" with red foreground and all other attributes set to their
  17. defaults, assuming the name twisted.conch.insults.text.attributes has
  18. been imported and bound to the name \"A\" (with the statement C{from
  19. twisted.conch.insults.text import attributes as A}, for example) one
  20. uses this expression::
  21. A.fg.red[\"Hello world\"]
  22. Other foreground colors are set by substituting their name for
  23. \"red\". To set both a foreground and a background color, this
  24. expression is used::
  25. A.fg.red[A.bg.green[\"Hello world\"]]
  26. Note that either A.bg.green can be nested within A.fg.red or vice
  27. versa. Also note that multiple items can be nested within a single
  28. index operation by separating them with commas::
  29. A.bg.green[A.fg.red[\"Hello\"], " ", A.fg.blue[\"world\"]]
  30. Other character attributes are set in a similar fashion. To specify a
  31. blinking version of the previous expression::
  32. A.blink[A.bg.green[A.fg.red[\"Hello\"], " ", A.fg.blue[\"world\"]]]
  33. C{A.reverseVideo}, C{A.underline}, and C{A.bold} are also valid.
  34. A third operation is actually supported: unary negation. This turns
  35. off an attribute when an enclosing expression would otherwise have
  36. caused it to be on. For example::
  37. A.underline[A.fg.red[\"Hello\", -A.underline[\" world\"]]]
  38. A formatting structure can then be serialized into a string containing the
  39. necessary VT102 control codes with L{assembleFormattedText}.
  40. @see: L{twisted.conch.insults.text._CharacterAttributes}
  41. @author: Jp Calderone
  42. """
  43. from incremental import Version
  44. from twisted.conch.insults import helper, insults
  45. from twisted.python import _textattributes
  46. from twisted.python.deprecate import deprecatedModuleAttribute
  47. flatten = _textattributes.flatten
  48. deprecatedModuleAttribute(
  49. Version('Twisted', 13, 1, 0),
  50. 'Use twisted.conch.insults.text.assembleFormattedText instead.',
  51. 'twisted.conch.insults.text',
  52. 'flatten')
  53. _TEXT_COLORS = {
  54. 'black': helper.BLACK,
  55. 'red': helper.RED,
  56. 'green': helper.GREEN,
  57. 'yellow': helper.YELLOW,
  58. 'blue': helper.BLUE,
  59. 'magenta': helper.MAGENTA,
  60. 'cyan': helper.CYAN,
  61. 'white': helper.WHITE}
  62. class _CharacterAttributes(_textattributes.CharacterAttributesMixin):
  63. """
  64. Factory for character attributes, including foreground and background color
  65. and non-color attributes such as bold, reverse video and underline.
  66. Character attributes are applied to actual text by using object
  67. indexing-syntax (C{obj['abc']}) after accessing a factory attribute, for
  68. example::
  69. attributes.bold['Some text']
  70. These can be nested to mix attributes::
  71. attributes.bold[attributes.underline['Some text']]
  72. And multiple values can be passed::
  73. attributes.normal[attributes.bold['Some'], ' text']
  74. Non-color attributes can be accessed by attribute name, available
  75. attributes are:
  76. - bold
  77. - blink
  78. - reverseVideo
  79. - underline
  80. Available colors are:
  81. 0. black
  82. 1. red
  83. 2. green
  84. 3. yellow
  85. 4. blue
  86. 5. magenta
  87. 6. cyan
  88. 7. white
  89. @ivar fg: Foreground colors accessed by attribute name, see above
  90. for possible names.
  91. @ivar bg: Background colors accessed by attribute name, see above
  92. for possible names.
  93. """
  94. fg = _textattributes._ColorAttribute(
  95. _textattributes._ForegroundColorAttr, _TEXT_COLORS)
  96. bg = _textattributes._ColorAttribute(
  97. _textattributes._BackgroundColorAttr, _TEXT_COLORS)
  98. attrs = {
  99. 'bold': insults.BOLD,
  100. 'blink': insults.BLINK,
  101. 'underline': insults.UNDERLINE,
  102. 'reverseVideo': insults.REVERSE_VIDEO}
  103. def assembleFormattedText(formatted):
  104. """
  105. Assemble formatted text from structured information.
  106. Currently handled formatting includes: bold, blink, reverse, underline and
  107. color codes.
  108. For example::
  109. from twisted.conch.insults.text import attributes as A
  110. assembleFormattedText(
  111. A.normal[A.bold['Time: '], A.fg.lightRed['Now!']])
  112. Would produce "Time: " in bold formatting, followed by "Now!" with a
  113. foreground color of light red and without any additional formatting.
  114. @param formatted: Structured text and attributes.
  115. @rtype: L{str}
  116. @return: String containing VT102 control sequences that mimic those
  117. specified by C{formatted}.
  118. @see: L{twisted.conch.insults.text._CharacterAttributes}
  119. @since: 13.1
  120. """
  121. return _textattributes.flatten(
  122. formatted, helper._FormattingState(), 'toVT102')
  123. attributes = _CharacterAttributes()
  124. __all__ = ['attributes', 'flatten']