m2m模型翻译
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.

191 lines
7.2 KiB

7 months ago
  1. # -*- coding: ISO-8859-1 -*-
  2. from __future__ import print_function, unicode_literals, absolute_import
  3. import re, sys, os
  4. terminal_escape = re.compile('(\001?\033\\[[0-9;]*m\002?)')
  5. escape_parts = re.compile('\001?\033\\[([0-9;]*)m\002?')
  6. class AnsiState(object):
  7. def __init__(self, bold=False, inverse=False, color="white", background="black", backgroundbold=False):
  8. self.bold = bold
  9. self.inverse = inverse
  10. self.color = color
  11. self.background = background
  12. self.backgroundbold = backgroundbold
  13. trtable = {"black":0, "red":4, "green":2, "yellow":6,
  14. "blue":1, "magenta":5, "cyan":3, "white":7}
  15. revtable = dict(zip(trtable.values(),trtable.keys()))
  16. def get_winattr(self):
  17. attr = 0
  18. if self.bold:
  19. attr |= 0x0008
  20. if self.backgroundbold:
  21. attr |= 0x0080
  22. if self.inverse:
  23. attr |= 0x4000
  24. attr |= self.trtable[self.color]
  25. attr |= (self.trtable[self.background] << 4)
  26. return attr
  27. def set_winattr(self, attr):
  28. self.bold = bool(attr & 0x0008)
  29. self.backgroundbold = bool(attr & 0x0080)
  30. self.inverse = bool(attr & 0x4000)
  31. self.color = self.revtable[attr & 0x0007]
  32. self.background = self.revtable[(attr & 0x0070) >> 4]
  33. winattr = property(get_winattr, set_winattr)
  34. def __repr__(self):
  35. return 'AnsiState(bold=%s,inverse=%s,color=%9s,' \
  36. 'background=%9s,backgroundbold=%s)# 0x%x'% \
  37. (self.bold, self.inverse, '"%s"'%self.color,
  38. '"%s"'%self.background, self.backgroundbold,
  39. self.winattr)
  40. def copy(self):
  41. x = AnsiState()
  42. x.bold = self.bold
  43. x.inverse = self.inverse
  44. x.color = self.color
  45. x.background = self.background
  46. x.backgroundbold = self.backgroundbold
  47. return x
  48. defaultstate = AnsiState(False, False, "white")
  49. trtable = {0:"black", 1:"red", 2:"green", 3:"yellow",
  50. 4:"blue", 5:"magenta", 6:"cyan", 7:"white"}
  51. class AnsiWriter(object):
  52. def __init__(self, default=defaultstate):
  53. if isinstance(defaultstate, AnsiState):
  54. self.defaultstate = default
  55. else:
  56. self.defaultstate=AnsiState()
  57. self.defaultstate.winattr = defaultstate
  58. def write_color(self,text, attr=None):
  59. '''write text at current cursor position and interpret color escapes.
  60. return the number of characters written.
  61. '''
  62. if isinstance(attr,AnsiState):
  63. defaultstate = attr
  64. elif attr is None: #use attribute form initial console
  65. attr = self.defaultstate.copy()
  66. else:
  67. defaultstate = AnsiState()
  68. defaultstate.winattr = attr
  69. attr = defaultstate
  70. chunks = terminal_escape.split(text)
  71. n = 0 # count the characters we actually write, omitting the escapes
  72. res=[]
  73. for chunk in chunks:
  74. m = escape_parts.match(chunk)
  75. if m:
  76. parts = m.group(1).split(";")
  77. if len(parts) == 1 and parts[0] == "0":
  78. attr = self.defaultstate.copy()
  79. continue
  80. for part in parts:
  81. if part == "0": # No text attribute
  82. attr = self.defaultstate.copy()
  83. attr.bold=False
  84. elif part == "7": # switch on reverse
  85. attr.inverse=True
  86. elif part == "1": # switch on bold (i.e. intensify foreground color)
  87. attr.bold=True
  88. elif len(part) == 2 and "30" <= part <= "37": # set foreground color
  89. attr.color = trtable[int(part) - 30]
  90. elif len(part) == 2 and "40" <= part <= "47": # set background color
  91. attr.backgroundcolor = trtable[int(part) - 40]
  92. continue
  93. n += len(chunk)
  94. if True:
  95. res.append((attr.copy(), chunk))
  96. return n,res
  97. def parse_color(self,text, attr=None):
  98. n, res = self.write_color(text, attr)
  99. return n, [attr.winattr for attr, text in res]
  100. def write_color(text, attr=None):
  101. a = AnsiWriter(defaultstate)
  102. return a.write_color(text, attr)
  103. def write_color_old( text, attr=None):
  104. '''write text at current cursor position and interpret color escapes.
  105. return the number of characters written.
  106. '''
  107. res = []
  108. chunks = terminal_escape.split(text)
  109. n = 0 # count the characters we actually write, omitting the escapes
  110. if attr is None:#use attribute from initial console
  111. attr = 15
  112. for chunk in chunks:
  113. m = escape_parts.match(chunk)
  114. if m:
  115. for part in m.group(1).split(";"):
  116. if part == "0": # No text attribute
  117. attr = 0
  118. elif part == "7": # switch on reverse
  119. attr |= 0x4000
  120. if part == "1": # switch on bold (i.e. intensify foreground color)
  121. attr |= 0x08
  122. elif len(part) == 2 and "30" <= part <= "37": # set foreground color
  123. part = int(part)-30
  124. # we have to mirror bits
  125. attr = (attr & ~0x07) | ((part & 0x1) << 2) | (part & 0x2) | ((part & 0x4) >> 2)
  126. elif len(part) == 2 and "40" <= part <= "47": # set background color
  127. part = int(part) - 40
  128. # we have to mirror bits
  129. attr = (attr & ~0x70) | ((part & 0x1) << 6) | ((part & 0x2) << 4) | ((part & 0x4) << 2)
  130. # ignore blink, underline and anything we don't understand
  131. continue
  132. n += len(chunk)
  133. if chunk:
  134. res.append(("0x%x"%attr, chunk))
  135. return res
  136. #trtable={0:"black",1:"red",2:"green",3:"yellow",4:"blue",5:"magenta",6:"cyan",7:"white"}
  137. if __name__ == "__main__x":
  138. import pprint
  139. pprint = pprint.pprint
  140. s = "\033[0;31mred\033[0;32mgreen\033[0;33myellow\033[0;34mblue\033[0;35mmagenta\033[0;36mcyan\033[0;37mwhite\033[0m"
  141. pprint (write_color(s))
  142. pprint (write_color_old(s))
  143. s = "\033[1;31mred\033[1;32mgreen\033[1;33myellow\033[1;34mblue\033[1;35mmagenta\033[1;36mcyan\033[1;37mwhite\033[0m"
  144. pprint (write_color(s))
  145. pprint (write_color_old(s))
  146. s = "\033[0;7;31mred\033[0;7;32mgreen\033[0;7;33myellow\033[0;7;34mblue\033[0;7;35mmagenta\033[0;7;36mcyan\033[0;7;37mwhite\033[0m"
  147. pprint (write_color(s))
  148. pprint (write_color_old(s))
  149. s = "\033[1;7;31mred\033[1;7;32mgreen\033[1;7;33myellow\033[1;7;34mblue\033[1;7;35mmagenta\033[1;7;36mcyan\033[1;7;37mwhite\033[0m"
  150. pprint (write_color(s))
  151. pprint (write_color_old(s))
  152. if __name__ == "__main__":
  153. import console
  154. import pprint
  155. pprint = pprint.pprint
  156. c = console.Console()
  157. c.write_color("dhsjdhs")
  158. c.write_color("\033[0;32mIn [\033[1;32m1\033[0;32m]:")
  159. print
  160. pprint (write_color("\033[0;32mIn [\033[1;32m1\033[0;32m]:"))
  161. if __name__ == "__main__x":
  162. import pprint
  163. pprint = pprint.pprint
  164. s = "\033[0;31mred\033[0;32mgreen\033[0;33myellow\033[0;34mblue\033[0;35mmagenta\033[0;36mcyan\033[0;37mwhite\033[0m"
  165. pprint (write_color(s))