color.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2021 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.py: 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. 'pink': ( 218, (35,1) ),
  30. 'orange': ( 216, (31,1) ),
  31. 'gray': ( 246, (30,1) ),
  32. 'purple': ( 141, (35,1) ),
  33. 'brown': ( 208, (33,0) ),
  34. 'grndim': ( 108, (32,0) ),
  35. 'redbg': ( (232,210), (30,101) ),
  36. 'grnbg': ( (232,121), (30,102) ),
  37. 'blubg': ( (232,75), (30,104) ),
  38. 'yelbg': ( (232,229), (30,103) ),
  39. }
  40. _reset = ''
  41. for _c in _colors:
  42. _e = _colors[_c]
  43. globals()['_256_'+_c] = (
  44. '\033[38;5;{};1m'.format(_e[0]) if type(_e[0]) == int else
  45. '\033[38;5;{};48;5;{};1m'.format(*_e[0])
  46. )
  47. globals()['_16_'+_c] = (
  48. '\033[{}m'.format(_e[1][0]) if _e[1][1] == 0 else
  49. '\033[{};{}m'.format(*_e[1])
  50. )
  51. globals()['_clr_'+_c] = ''
  52. exec(f'{_c} = lambda s: _clr_{_c}+s+_reset')
  53. def nocolor(s):
  54. return s
  55. def set_vt100():
  56. 'hack to put term into VT100 mode under MSWin'
  57. from .globalvars import g
  58. if g.platform == 'win':
  59. from subprocess import run
  60. run([],shell=True)
  61. def get_terminfo_colors(term=None):
  62. from subprocess import run,PIPE
  63. cmd = ['infocmp','-0']
  64. if term:
  65. cmd.append(term)
  66. def is_hex_str(s):
  67. from string import hexdigits
  68. return set(list(s)) <= set(list(hexdigits))
  69. try:
  70. cmdout = run(cmd,stdout=PIPE,check=True).stdout.decode()
  71. except:
  72. return None
  73. else:
  74. s = [e.split('#')[1] for e in cmdout.split(',') if e.startswith('colors')][0]
  75. if s.isdecimal():
  76. return int(s)
  77. elif s.startswith('0x') and is_hex_str(s[2:]):
  78. return int(s[2:],16)
  79. else:
  80. return None
  81. def init_color(num_colors='auto'):
  82. assert num_colors in ('auto',8,16,256,0)
  83. globals()['_reset'] = '\033[0m' if num_colors else ''
  84. if num_colors in (0,8,16):
  85. pfx = '_16_'
  86. else:
  87. import os
  88. t = os.getenv('TERM')
  89. if num_colors == 256 or (t and t.endswith('256color')) or get_terminfo_colors() == 256:
  90. pfx = '_256_'
  91. else:
  92. pfx = '_16_'
  93. for c in _colors:
  94. if num_colors == 0:
  95. globals()['_clr_'+c] = ''
  96. else:
  97. globals()['_clr_'+c] = globals()[pfx+c]
  98. set_vt100()