color.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2024 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. color: color handling for the MMGen suite
  20. """
  21. _colors = {
  22. 'black': ( 232, (30,0) ),
  23. 'red': ( 210, (31,1) ),
  24. 'green': ( 121, (32,1) ),
  25. 'yellow': ( 229, (33,1) ),
  26. 'blue': ( 75, (34,1) ),
  27. 'magenta': ( 205, (35,1) ),
  28. 'cyan': ( 122, (36,1) ),
  29. 'gray': ( 246, (30,1) ),
  30. 'orange': ( 216, (31,1) ),
  31. 'purple': ( 141, (35,1) ),
  32. 'pink': ( 218, (35,1) ),
  33. 'melon': ( 222, (33,1) ),
  34. 'brown': ( 173, (33,0) ),
  35. 'grndim': ( 108, (32,0) ),
  36. 'redbg': ( (232,210), (30,101) ),
  37. 'grnbg': ( (232,121), (30,102) ),
  38. 'yelbg': ( (232,229), (30,103) ),
  39. 'blubg': ( (232,75), (30,104) ),
  40. }
  41. def nocolor(s):
  42. return s
  43. def set_vt100():
  44. 'hack to put term into VT100 mode under MSWin'
  45. import sys
  46. if sys.platform == 'win32':
  47. from subprocess import run
  48. run([],shell=True)
  49. def get_terminfo_colors(term=None):
  50. from subprocess import run,PIPE
  51. cmd = ['infocmp','-0']
  52. if term:
  53. cmd.append(term)
  54. try:
  55. cmdout = run(cmd,stdout=PIPE,check=True).stdout.decode()
  56. except:
  57. set_vt100()
  58. return None
  59. else:
  60. set_vt100()
  61. s = [e.split('#')[1] for e in cmdout.split(',') if e.startswith('colors')][0]
  62. from .util import is_hex_str
  63. if s.isdecimal():
  64. return int(s)
  65. elif s.startswith('0x') and is_hex_str(s[2:]):
  66. return int(s[2:],16)
  67. else:
  68. return None
  69. def init_color(num_colors='auto'):
  70. assert num_colors in ('auto',8,16,256,0)
  71. import sys
  72. self = sys.modules[__name__]
  73. if num_colors == 'auto':
  74. import os
  75. if sys.platform == 'win32':
  76. # Force 256-color for MSYS2: terminal supports it, however infocmp reports 8-color.
  77. # We also avoid spawning a subprocess, leading to a subsequent OSError 22 when testing
  78. # with pexpect spawn.
  79. num_colors = 256
  80. elif (t := os.getenv('TERM')) and t.endswith('256color'):
  81. num_colors = 256
  82. else:
  83. num_colors = get_terminfo_colors() or 16
  84. reset = '\033[0m'
  85. if num_colors == 0:
  86. ncc = (lambda s: s).__code__
  87. for c in _colors:
  88. getattr(self,c).__code__ = ncc
  89. elif num_colors == 256:
  90. for c,e in _colors.items():
  91. start = (
  92. '\033[38;5;{};1m'.format(e[0]) if type(e[0]) == int else
  93. '\033[38;5;{};48;5;{};1m'.format(*e[0]) )
  94. getattr(self,c).__code__ = eval(f'(lambda s: "{start}" + s + "{reset}").__code__')
  95. elif num_colors in (8,16):
  96. for c,e in _colors.items():
  97. start = (
  98. '\033[{}m'.format(e[1][0]) if e[1][1] == 0 else
  99. '\033[{};{}m'.format(*e[1]) )
  100. getattr(self,c).__code__ = eval(f'(lambda s: "{start}" + s + "{reset}").__code__')
  101. set_vt100()
  102. # Each color name must be bound to an independent stub function with its own
  103. # address in memory. The names themselves must never be redefined, since other
  104. # modules could import them before init_color() is run. Instead, the code
  105. # objects of their associated functions are manipulated by init_color() to
  106. # enable/disable color.
  107. black = lambda s: s
  108. red = lambda s: s
  109. green = lambda s: s
  110. yellow = lambda s: s
  111. blue = lambda s: s
  112. magenta = lambda s: s
  113. cyan = lambda s: s
  114. gray = lambda s: s
  115. orange = lambda s: s
  116. purple = lambda s: s
  117. pink = lambda s: s
  118. melon = lambda s: s
  119. brown = lambda s: s
  120. grndim = lambda s: s
  121. redbg = lambda s: s
  122. grnbg = lambda s: s
  123. yelbg = lambda s: s
  124. blubg = lambda s: s