3
__author__ = "Nadia Alramli"
4
__copyright__ = "Nadia Alramli"
5
__credits__ = ["Nadia Alramli"]
8
__maintainer__ = "Peter Maxwell"
9
__email__ = "pm67nz@gmail.com"
10
__status__ = "Production"
12
# Copyright: 2008 Nadia Alramli
21
COLORS = "BLUE GREEN CYAN RED MAGENTA YELLOW WHITE BLACK".split()
23
# List of terminal controls, you can add more to the list.
25
'BOL':'cr', 'UP':'cuu1', 'DOWN':'cud1', 'LEFT':'cub1', 'RIGHT':'cuf1',
26
'CLEAR_SCREEN':'clear', 'CLEAR_EOL':'el', 'CLEAR_BOL':'el1',
27
'CLEAR_EOS':'ed', 'BOLD':'bold', 'BLINK':'blink', 'DIM':'dim',
28
'REVERSE':'rev', 'UNDERLINE':'smul', 'NORMAL':'sgr0',
29
'HIDE_CURSOR':'cinvis', 'SHOW_CURSOR':'cnorm'
32
class TerminalUnavailableError(RuntimeError):
35
class CursesOutput(object):
37
if curses is None or not hasattr(sys.stdout, 'fileno'):
38
raise TerminalUnavailableError("No curses modules")
41
except curses.error, detail:
42
raise TerminalUnavailableError(detail)
45
return curses.tigetnum('cols')
48
return curses.tigetnum('lines')
51
# Get the color escape sequence template or '' if not supported
52
# setab and setaf are for ANSI escape sequences
53
bgColorSeq = curses.tigetstr('setab') or curses.tigetstr('setb') or ''
54
fgColorSeq = curses.tigetstr('setaf') or curses.tigetstr('setf') or ''
58
# Get the color index from curses
59
colorIndex = getattr(curses, 'COLOR_%s' % color)
60
# Set the color escape sequence after filling the template with index
61
codes[color] = curses.tparm(fgColorSeq, colorIndex)
62
# Set background escape sequence
63
codes['BG_%s' % color] = curses.tparm(bgColorSeq, colorIndex)
64
for control in _CONTROLS:
65
# Set the control escape sequence
66
codes[control] = curses.tigetstr(_CONTROLS[control]) or ''