Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

text.py 5.3KB

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. )
  54. _TEXT_COLORS = {
  55. "black": helper.BLACK,
  56. "red": helper.RED,
  57. "green": helper.GREEN,
  58. "yellow": helper.YELLOW,
  59. "blue": helper.BLUE,
  60. "magenta": helper.MAGENTA,
  61. "cyan": helper.CYAN,
  62. "white": helper.WHITE,
  63. }
  64. class _CharacterAttributes(_textattributes.CharacterAttributesMixin):
  65. """
  66. Factory for character attributes, including foreground and background color
  67. and non-color attributes such as bold, reverse video and underline.
  68. Character attributes are applied to actual text by using object
  69. indexing-syntax (C{obj['abc']}) after accessing a factory attribute, for
  70. example::
  71. attributes.bold['Some text']
  72. These can be nested to mix attributes::
  73. attributes.bold[attributes.underline['Some text']]
  74. And multiple values can be passed::
  75. attributes.normal[attributes.bold['Some'], ' text']
  76. Non-color attributes can be accessed by attribute name, available
  77. attributes are:
  78. - bold
  79. - blink
  80. - reverseVideo
  81. - underline
  82. Available colors are:
  83. 0. black
  84. 1. red
  85. 2. green
  86. 3. yellow
  87. 4. blue
  88. 5. magenta
  89. 6. cyan
  90. 7. white
  91. @ivar fg: Foreground colors accessed by attribute name, see above
  92. for possible names.
  93. @ivar bg: Background colors accessed by attribute name, see above
  94. for possible names.
  95. """
  96. fg = _textattributes._ColorAttribute(
  97. _textattributes._ForegroundColorAttr, _TEXT_COLORS
  98. )
  99. bg = _textattributes._ColorAttribute(
  100. _textattributes._BackgroundColorAttr, _TEXT_COLORS
  101. )
  102. attrs = {
  103. "bold": insults.BOLD,
  104. "blink": insults.BLINK,
  105. "underline": insults.UNDERLINE,
  106. "reverseVideo": insults.REVERSE_VIDEO,
  107. }
  108. def assembleFormattedText(formatted):
  109. """
  110. Assemble formatted text from structured information.
  111. Currently handled formatting includes: bold, blink, reverse, underline and
  112. color codes.
  113. For example::
  114. from twisted.conch.insults.text import attributes as A
  115. assembleFormattedText(
  116. A.normal[A.bold['Time: '], A.fg.lightRed['Now!']])
  117. Would produce "Time: " in bold formatting, followed by "Now!" with a
  118. foreground color of light red and without any additional formatting.
  119. @param formatted: Structured text and attributes.
  120. @rtype: L{str}
  121. @return: String containing VT102 control sequences that mimic those
  122. specified by C{formatted}.
  123. @see: L{twisted.conch.insults.text._CharacterAttributes}
  124. @since: 13.1
  125. """
  126. return _textattributes.flatten(formatted, helper._FormattingState(), "toVT102")
  127. attributes = _CharacterAttributes()
  128. __all__ = ["attributes", "flatten"]