ijontichy

ansicodes.py

Mar 9th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.99 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. ANSISTART = "\033["
  4. ANSIEND   = "m"
  5.  
  6. RESET               = ANSISTART + "0"  + ANSIEND
  7.  
  8. BOLDON              = ANSISTART + "1"  + ANSIEND
  9. BOLDOFF             = ANSISTART + "22" + ANSIEND
  10.  
  11. DIMON               = ANSISTART + "2"  + ANSIEND
  12. DIMOFF              = ANSISTART + "21" + ANSIEND
  13.  
  14. ITALICSON           = ANSISTART + "3"  + ANSIEND
  15. ITALICSOFF          = ANSISTART + "23" + ANSIEND
  16.  
  17. UNDERLINEON         = ANSISTART + "4"  + ANSIEND
  18. UNDERLINEOFF        = ANSISTART + "24" + ANSIEND
  19.  
  20. BLINKON             = ANSISTART + "5"  + ANSIEND
  21. BLINKONFAST         = ANSISTART + "6"  + ANSIEND   # not widely supported
  22. BLINKOFF            = ANSISTART + "25" + ANSIEND
  23.  
  24. INVERSEON           = ANSISTART + "7"  + ANSIEND
  25. INVERSEOFF          = ANSISTART + "27" + ANSIEND
  26.  
  27. STRIKETHROUGHON     = ANSISTART + "7"  + ANSIEND
  28. STRIKETHROUGHOFF    = ANSISTART + "27" + ANSIEND
  29.  
  30. BLACKF              = ANSISTART + "30" + ANSIEND
  31. REDF                = ANSISTART + "31" + ANSIEND
  32. GREENF              = ANSISTART + "32" + ANSIEND
  33. YELLOWF             = ANSISTART + "33" + ANSIEND
  34. BLUEF               = ANSISTART + "34" + ANSIEND
  35. MAGENTAF            = ANSISTART + "35" + ANSIEND
  36. CYANF               = ANSISTART + "36" + ANSIEND
  37. WHITEF              = ANSISTART + "37" + ANSIEND
  38. DEFAULTF            = ANSISTART + "39" + ANSIEND
  39.  
  40. BLACKB              = ANSISTART + "40" + ANSIEND
  41. REDB                = ANSISTART + "41" + ANSIEND
  42. GREENB              = ANSISTART + "42" + ANSIEND
  43. YELLOWB             = ANSISTART + "43" + ANSIEND
  44. BLUEB               = ANSISTART + "44" + ANSIEND
  45. MAGENTAB            = ANSISTART + "45" + ANSIEND
  46. CYANB               = ANSISTART + "46" + ANSIEND
  47. WHITEB              = ANSISTART + "47" + ANSIEND
  48. DEFAULTB            = ANSISTART + "49" + ANSIEND
  49.  
  50. fgColors = {"0": BOLDOFF + DIMOFF + BLACKF, "1": BOLDOFF + DIMOFF + REDF, "2": BOLDOFF + DIMOFF + GREENF, "3": BOLDOFF + DIMOFF + YELLOWF,
  51.             "4": BOLDOFF + DIMOFF + BLUEF, "5": BOLDOFF + DIMOFF + MAGENTAF, "6": BOLDOFF + DIMOFF + CYANF, "7": BOLDOFF + DIMOFF + WHITEF,
  52.  
  53.             "A": DIMOFF + BOLDON + BLACKF, "B": DIMOFF + BOLDON + REDF, "C": DIMOFF + BOLDON + GREENF, "D": DIMOFF + BOLDON + YELLOWF,
  54.             "E": DIMOFF + BOLDON + BLUEF, "F": DIMOFF + BOLDON + MAGENTAF, "G": DIMOFF + BOLDON + CYANF, "H": DIMOFF + BOLDON + WHITEF,
  55.  
  56.             "a": BOLDOFF + DIMON + BLACKF, "b": BOLDOFF + DIMON + REDF, "c": BOLDOFF + DIMON + GREENF, "d": BOLDOFF + DIMON + YELLOWF,
  57.             "e": BOLDOFF + DIMON + BLUEF, "f": BOLDOFF + DIMON + MAGENTAF, "g": BOLDOFF + DIMON + CYANF, "h": BOLDOFF + DIMON + WHITEF}
  58.  
  59.  
  60. bgColors = {"0": BLACKB, "1": REDB, "2": GREENB, "3": YELLOWB,
  61.             "4": BLUEB, "5": MAGENTAB, "6": CYANB, "7": WHITEB}
  62.  
  63. def combineCodes(*ansiCodes):
  64.     """Combines multiple ANSI excape codes
  65.  
  66. >>> combineCodes(BOLDON, REDF)
  67. '\\x1b[1;31m'
  68. >>> combineCodes(BOLDON, REDF, BLUEB)
  69. '\\x1b[1;31;44m'
  70. >>> combineCodes(BOLDON, REDF, BLUEB, "bacon")
  71. Traceback (most recent call last):
  72.  ...
  73. ValueError: 'bacon' is not an ANSI code"""
  74.  
  75.     ret = ANSISTART + "{}" + ANSIEND
  76.  
  77.     retCodes = []
  78.  
  79.     for i in ansiCodes:
  80.  
  81.         if not (i.startswith(ANSISTART) and i.endswith(ANSIEND) ):
  82.             raise ValueError("{!r} is not an ANSI code".format(i) )
  83.  
  84.         j = i.lstrip(ANSISTART)
  85.         j = j.rstrip(ANSIEND)
  86.  
  87.         retCodes.append(j)
  88.  
  89.     retStr = ";".join(retCodes)
  90.  
  91.     return ret.format(retStr)
  92.  
  93. def stripCodes(message):
  94.  
  95.     ret = []
  96.     removing = False
  97.  
  98.     for n, c in enumerate(message):
  99.  
  100.         if not removing:
  101.             for n2, c2 in enumerate(ANSISTART):
  102.                 cut = n + n2
  103.  
  104.                 if message[cut:cut+1] != c2:
  105.                     break
  106.  
  107.             else:
  108.                 removing = True
  109.  
  110.             if not removing:
  111.                 ret.append(c)
  112.  
  113.         else:
  114.  
  115.             for n2, c2 in enumerate(ANSIEND):
  116.                 cut = n - n2
  117.  
  118.                 if cut < 0:
  119.                     break
  120.  
  121.                 if message[cut:cut+1] != ANSIEND[-n2 - 1]:
  122.                     break
  123.  
  124.                 removing = False
  125.  
  126.     return "".join(ret)
  127.  
  128. def mapColors(strn, fgMap, bgMap=None, *, fCols=None, bCols=None):
  129.     """Maps ANSI color codes to a string or list of strings
  130.  
  131. >>> mapColors("Potato", "AA11AA", "------")
  132. '\\x1b[1m\\x1b[30mPo\\x1b[22m\\x1b[31mta\\x1b[1m\\x1b[30mto\\x1b[0m'
  133. >>> print(_)
  134. Potato
  135.  
  136.  
  137. By default, the colors are:
  138.  
  139. 0 - Black   fore/background
  140. 1 - Red     fore/background
  141. 2 - Green   fore/background
  142. 3 - Yellow  fore/background
  143. 4 - Blue    fore/background
  144. 5 - Magenta fore/background
  145. 6 - Cyan    fore/background
  146. 7 - White   fore/background
  147.  
  148. A - Bold black foreground
  149. B - Bold red foreground
  150. C - Bold green foreground
  151. D - Bold yellow foreground
  152. E - Bold blue foreground
  153. F - Bold magenta foreground
  154. G - Bold cyan foreground
  155. H - Bold white foreground
  156.  
  157. a - Dim black foreground
  158. b - Dim red foreground
  159. c - Dim green foreground
  160. d - Dim yellow foreground
  161. e - Dim blue foreground
  162. f - Dim magenta foreground
  163. g - Dim cyan foreground
  164. h - Dim white foreground"""
  165.  
  166.     if fCols is None:
  167.         fCols = fgColors
  168.  
  169.     if bCols is None:
  170.         bCols = bgColors
  171.  
  172.     currentColorF = ""
  173.     currentColorB = ""
  174.  
  175.     resetF = combineCodes(DEFAULTF, BOLDOFF, DIMOFF)
  176.     resetB = DEFAULTB
  177.  
  178.     ret = []
  179.  
  180.     if bgMap is None:
  181.         bgMap = "-" * len(fgMap)
  182.  
  183.     if not (len(strn) == len(fgMap) == len(bgMap) ):
  184.         raise AssertionError("string and maps not equal lengths")
  185.  
  186.  
  187.     for pos, char in enumerate(strn):
  188.         fChar = fgMap[pos]
  189.         bChar = bgMap[pos]
  190.  
  191.         if fChar in fCols and fChar != "-":
  192.             if currentColorF != fChar:
  193.                 ret.append(fCols[fChar] )
  194.                 currentColorF = fChar
  195.  
  196.         else:
  197.             if currentColorF != "":
  198.                 ret.append(resetF)
  199.             currentColorF = ""
  200.  
  201.         if bChar in bCols and bChar != "-":
  202.             if currentColorB != bChar:
  203.                 ret.append(bCols[bChar] )
  204.                 currentColorB = bChar
  205.  
  206.         else:
  207.             if currentColorB != "":
  208.                 ret.append(resetB)
  209.             currentColorB = ""
  210.  
  211.  
  212.         ret.append(char)
  213.  
  214.     ret.append(RESET)
  215.  
  216.     return "".join(ret)
Advertisement
Add Comment
Please, Sign In to add comment