color.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2016 Philemon <mmgen-py@yandex.com>
  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 routines for the MMGen suite
  20. """
  21. import os
  22. # If 88- or 256-color support is compiled, the following apply.
  23. # P s = 3 8 ; 5 ; P s -> Set foreground color to the second P s .
  24. # P s = 4 8 ; 5 ; P s -> Set background color to the second P s .
  25. if os.environ['TERM'][-8:] == '256color':
  26. _blk,_red,_grn,_yel,_blu,_mag,_cya,_bright,_dim,_ybright,_ydim,_pnk,_orng,_gry = [
  27. '\033[38;5;%s;1m' % c for c in 232,210,121,229,75,90,122,231,245,187,243,218,215,246]
  28. _redbg = '\033[38;5;232;48;5;210;1m'
  29. _grnbg = '\033[38;5;232;48;5;121;1m'
  30. _grybg = '\033[38;5;231;48;5;240;1m'
  31. _reset = '\033[0m'
  32. else:
  33. _blk,_red,_grn,_yel,_blu,_mag,_cya,_reset,_grnbg = \
  34. ['\033[%sm' % c for c in '30;1','31;1','32;1','33;1','34;1','35;1','36;1','0','30;102']
  35. _gry=_orng=_pnk=_redbg=_ybright=_ydim=_bright=_dim=_grybg=_mag # TODO
  36. clr_red=clr_grn=clr_grnbg=clr_yel=clr_cya=clr_blu=clr_pnk=clr_orng=clr_gry=clr_mag=clr_reset=''
  37. def nocolor(s): return s
  38. def red(s): return clr_red+s+clr_reset
  39. def green(s): return clr_grn+s+clr_reset
  40. def grnbg(s): return clr_grnbg+s+clr_reset
  41. def yellow(s): return clr_yel+s+clr_reset
  42. def cyan(s): return clr_cya+s+clr_reset
  43. def blue(s): return clr_blu+s+clr_reset
  44. def pink(s): return clr_pnk+s+clr_reset
  45. def orange(s): return clr_orng+s+clr_reset
  46. def gray(s): return clr_gry+s+clr_reset
  47. def magenta(s): return clr_mag+s+clr_reset
  48. def init_color(enable_color=True):
  49. global clr_red,clr_grn,clr_grnbg,clr_yel,clr_cya,clr_blu,clr_pnk,clr_orng,clr_gry,clr_mag,clr_reset
  50. if enable_color:
  51. clr_red = _red
  52. clr_grn = _grn
  53. clr_grnbg = _grnbg
  54. clr_yel = _yel
  55. clr_cya = _cya
  56. clr_blu = _blu
  57. clr_pnk = _pnk
  58. clr_orng = _orng
  59. clr_gry = _gry
  60. clr_mag = _mag
  61. clr_reset = _reset