Package _setup :: Package term :: Module _term
[hide private]
[frames] | no frames]

Source Code for Module _setup.term._term

  1  # -*- coding: ascii -*- 
  2  # 
  3  # Copyright 2007, 2008, 2009, 2010 
  4  # Andr\xe9 Malo or his licensors, as applicable 
  5  # 
  6  # Licensed under the Apache License, Version 2.0 (the "License"); 
  7  # you may not use this file except in compliance with the License. 
  8  # You may obtain a copy of the License at 
  9  # 
 10  #     http://www.apache.org/licenses/LICENSE-2.0 
 11  # 
 12  # Unless required by applicable law or agreed to in writing, software 
 13  # distributed under the License is distributed on an "AS IS" BASIS, 
 14  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 15  # See the License for the specific language governing permissions and 
 16  # limitations under the License. 
 17  """ 
 18  ================= 
 19   Terminal writer 
 20  ================= 
 21  """ 
 22  __author__ = u"Andr\xe9 Malo" 
 23  __docformat__ = "restructuredtext en" 
 24   
 25  import sys as _sys 
 26   
 27   
28 -class _INFO(dict):
29 """ Terminal info dict """ 30
31 - def __init__(self):
32 """ Initialization """ 33 dict.__init__(self, { 34 'NORMAL': '', 35 'BOLD': '', 36 'ERASE': '\n', 37 'RED': '', 38 'YELLOW': '', 39 'GREEN': '', 40 }) 41 try: 42 import curses as _curses 43 except ImportError: 44 # fixup if a submodule of curses failed. 45 if 'curses' in _sys.modules: 46 del _sys.modules['curses'] 47 else: 48 try: 49 _curses.setupterm() 50 except (TypeError, _curses.error): 51 pass 52 else: 53 def make_color(color): 54 """ Make color control string """ 55 seq = _curses.tigetstr('setaf') 56 if seq is not None: 57 # XXX may fail - need better logic 58 seq = seq.replace("%p1", "") % color 59 return seq
60 61 self['NORMAL'] = _curses.tigetstr('sgr0') 62 self['BOLD'] = _curses.tigetstr('bold') 63 64 erase = _curses.tigetstr('el1') 65 if erase is not None: 66 self['ERASE'] = erase + _curses.tigetstr('cr') 67 68 self['RED'] = make_color(_curses.COLOR_RED) 69 self['YELLOW'] = make_color(_curses.COLOR_YELLOW) 70 self['GREEN'] = make_color(_curses.COLOR_GREEN)
71
72 - def __getitem__(self, key):
73 """ Deliver always """ 74 dict.get(self, key) or ""
75 76
77 -def terminfo():
78 """ Get info singleton """ 79 # pylint: disable = E1101, W0612 80 if terminfo.info is None: 81 terminfo.info = _INFO() 82 return terminfo.info
83 terminfo.info = None 84 85
86 -def write(fmt, **kwargs):
87 """ Write stuff on the terminal """ 88 parm = dict(terminfo()) 89 parm.update(kwargs) 90 _sys.stdout.write(fmt % parm) 91 _sys.stdout.flush()
92 93
94 -def green(bmt, **kwargs):
95 """ Write something in green on screen """ 96 announce("%%(GREEN)s%s%%(NORMAL)s" % bmt, **kwargs)
97 98
99 -def red(bmt, **kwargs):
100 """ Write something in red on the screen """ 101 announce("%%(BOLD)s%%(RED)s%s%%(NORMAL)s" % bmt, **kwargs)
102 103
104 -def yellow(fmt, **kwargs):
105 """ Write something in yellow on the screen """ 106 announce("%%(BOLD)s%%(YELLOW)s%s%%(NORMAL)s" % fmt, **kwargs)
107 108
109 -def announce(fmt, **kwargs):
110 """ Announce something """ 111 write(fmt, **kwargs) 112 _sys.stdout.write("\n") 113 _sys.stdout.flush()
114