1 |
slacapra |
1.1 |
#!/usr/bin/env python
|
2 |
|
|
|
3 |
|
|
import sys, re, time
|
4 |
|
|
|
5 |
|
|
class TerminalController:
|
6 |
|
|
"""
|
7 |
|
|
A class that can be used to portably generate formatted output to
|
8 |
|
|
a terminal.
|
9 |
|
|
|
10 |
|
|
`TerminalController` defines a set of instance variables whose
|
11 |
|
|
values are initialized to the control sequence necessary to
|
12 |
|
|
perform a given action. These can be simply included in normal
|
13 |
|
|
output to the terminal:
|
14 |
|
|
|
15 |
|
|
>>> term = TerminalController()
|
16 |
|
|
>>> print 'This is '+term.GREEN+'green'+term.NORMAL
|
17 |
|
|
|
18 |
|
|
Alternatively, the `render()` method can used, which replaces
|
19 |
|
|
'${action}' with the string required to perform 'action':
|
20 |
|
|
|
21 |
|
|
>>> term = TerminalController()
|
22 |
|
|
>>> print term.render('This is ${GREEN}green${NORMAL}')
|
23 |
|
|
|
24 |
|
|
If the terminal doesn't support a given action, then the value of
|
25 |
|
|
the corresponding instance variable will be set to ''. As a
|
26 |
|
|
result, the above code will still work on terminals that do not
|
27 |
|
|
support color, except that their output will not be colored.
|
28 |
|
|
Also, this means that you can test whether the terminal supports a
|
29 |
|
|
given action by simply testing the truth value of the
|
30 |
|
|
corresponding instance variable:
|
31 |
|
|
|
32 |
|
|
>>> term = TerminalController()
|
33 |
|
|
>>> if term.CLEAR_SCREEN:
|
34 |
|
|
... print 'This terminal supports clearning the screen.'
|
35 |
|
|
|
36 |
|
|
Finally, if the width and height of the terminal are known, then
|
37 |
|
|
they will be stored in the `COLS` and `LINES` attributes.
|
38 |
|
|
"""
|
39 |
|
|
# Cursor movement:
|
40 |
|
|
BOL = '' #: Move the cursor to the beginning of the line
|
41 |
|
|
UP = '' #: Move the cursor up one line
|
42 |
|
|
DOWN = '' #: Move the cursor down one line
|
43 |
|
|
LEFT = '' #: Move the cursor left one char
|
44 |
|
|
RIGHT = '' #: Move the cursor right one char
|
45 |
|
|
|
46 |
|
|
# Deletion:
|
47 |
|
|
CLEAR_SCREEN = '' #: Clear the screen and move to home position
|
48 |
|
|
CLEAR_EOL = '' #: Clear to the end of the line.
|
49 |
|
|
CLEAR_BOL = '' #: Clear to the beginning of the line.
|
50 |
|
|
CLEAR_EOS = '' #: Clear to the end of the screen
|
51 |
|
|
|
52 |
|
|
# Output modes:
|
53 |
|
|
BOLD = '' #: Turn on bold mode
|
54 |
|
|
BLINK = '' #: Turn on blink mode
|
55 |
|
|
DIM = '' #: Turn on half-bright mode
|
56 |
|
|
REVERSE = '' #: Turn on reverse-video mode
|
57 |
|
|
NORMAL = '' #: Turn off all modes
|
58 |
|
|
|
59 |
|
|
# Cursor display:
|
60 |
|
|
HIDE_CURSOR = '' #: Make the cursor invisible
|
61 |
|
|
SHOW_CURSOR = '' #: Make the cursor visible
|
62 |
|
|
|
63 |
|
|
# Terminal size:
|
64 |
|
|
COLS = None #: Width of the terminal (None for unknown)
|
65 |
|
|
LINES = None #: Height of the terminal (None for unknown)
|
66 |
|
|
|
67 |
|
|
# Foreground colors:
|
68 |
|
|
BLACK = BLUE = GREEN = CYAN = RED = MAGENTA = YELLOW = WHITE = ''
|
69 |
|
|
|
70 |
|
|
# Background colors:
|
71 |
|
|
BG_BLACK = BG_BLUE = BG_GREEN = BG_CYAN = ''
|
72 |
|
|
BG_RED = BG_MAGENTA = BG_YELLOW = BG_WHITE = ''
|
73 |
|
|
|
74 |
|
|
_STRING_CAPABILITIES = """
|
75 |
|
|
BOL=cr UP=cuu1 DOWN=cud1 LEFT=cub1 RIGHT=cuf1
|
76 |
|
|
CLEAR_SCREEN=clear CLEAR_EOL=el CLEAR_BOL=el1 CLEAR_EOS=ed BOLD=bold
|
77 |
|
|
BLINK=blink DIM=dim REVERSE=rev UNDERLINE=smul NORMAL=sgr0
|
78 |
|
|
HIDE_CURSOR=cinvis SHOW_CURSOR=cnorm""".split()
|
79 |
|
|
_COLORS = """BLACK BLUE GREEN CYAN RED MAGENTA YELLOW WHITE""".split()
|
80 |
|
|
_ANSICOLORS = "BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE".split()
|
81 |
|
|
|
82 |
|
|
def __init__(self, term_stream=sys.stdout):
|
83 |
|
|
"""
|
84 |
|
|
Create a `TerminalController` and initialize its attributes
|
85 |
|
|
with appropriate values for the current terminal.
|
86 |
|
|
`term_stream` is the stream that will be used for terminal
|
87 |
|
|
output; if this stream is not a tty, then the terminal is
|
88 |
|
|
assumed to be a dumb terminal (i.e., have no capabilities).
|
89 |
|
|
"""
|
90 |
|
|
# Curses isn't available on all platforms
|
91 |
|
|
try: import curses
|
92 |
|
|
except: return
|
93 |
|
|
|
94 |
|
|
# If the stream isn't a tty, then assume it has no capabilities.
|
95 |
|
|
if not term_stream.isatty(): return
|
96 |
|
|
|
97 |
|
|
# Check the terminal type. If we fail, then assume that the
|
98 |
|
|
# terminal has no capabilities.
|
99 |
|
|
try: curses.setupterm()
|
100 |
|
|
except: return
|
101 |
|
|
|
102 |
|
|
# Look up numeric capabilities.
|
103 |
|
|
self.COLS = curses.tigetnum('cols')
|
104 |
|
|
self.LINES = curses.tigetnum('lines')
|
105 |
|
|
|
106 |
|
|
# Look up string capabilities.
|
107 |
|
|
for capability in self._STRING_CAPABILITIES:
|
108 |
|
|
(attrib, cap_name) = capability.split('=')
|
109 |
|
|
setattr(self, attrib, self._tigetstr(cap_name) or '')
|
110 |
|
|
|
111 |
|
|
# Colors
|
112 |
|
|
set_fg = self._tigetstr('setf')
|
113 |
|
|
if set_fg:
|
114 |
|
|
for i,color in zip(range(len(self._COLORS)), self._COLORS):
|
115 |
|
|
setattr(self, color, curses.tparm(set_fg, i) or '')
|
116 |
|
|
set_fg_ansi = self._tigetstr('setaf')
|
117 |
|
|
if set_fg_ansi:
|
118 |
|
|
for i,color in zip(range(len(self._ANSICOLORS)), self._ANSICOLORS):
|
119 |
|
|
setattr(self, color, curses.tparm(set_fg_ansi, i) or '')
|
120 |
|
|
set_bg = self._tigetstr('setb')
|
121 |
|
|
if set_bg:
|
122 |
|
|
for i,color in zip(range(len(self._COLORS)), self._COLORS):
|
123 |
|
|
setattr(self, 'BG_'+color, curses.tparm(set_bg, i) or '')
|
124 |
|
|
set_bg_ansi = self._tigetstr('setab')
|
125 |
|
|
if set_bg_ansi:
|
126 |
|
|
for i,color in zip(range(len(self._ANSICOLORS)), self._ANSICOLORS):
|
127 |
|
|
setattr(self, 'BG_'+color, curses.tparm(set_bg_ansi, i) or '')
|
128 |
|
|
|
129 |
|
|
def _tigetstr(self, cap_name):
|
130 |
|
|
# String capabilities can include "delays" of the form "$<2>".
|
131 |
|
|
# For any modern terminal, we should be able to just ignore
|
132 |
|
|
# these, so strip them out.
|
133 |
|
|
import curses
|
134 |
|
|
cap = curses.tigetstr(cap_name) or ''
|
135 |
|
|
return re.sub(r'\$<\d+>[/*]?', '', cap)
|
136 |
|
|
|
137 |
|
|
def render(self, template):
|
138 |
|
|
"""
|
139 |
|
|
Replace each $-substitutions in the given template string with
|
140 |
|
|
the corresponding terminal control string (if it's defined) or
|
141 |
|
|
'' (if it's not).
|
142 |
|
|
"""
|
143 |
|
|
return re.sub(r'\$\$|\${\w+}', self._render_sub, template)
|
144 |
|
|
|
145 |
|
|
def _render_sub(self, match):
|
146 |
|
|
s = match.group()
|
147 |
|
|
if s == '$$': return s
|
148 |
|
|
else: return getattr(self, s[2:-1])
|
149 |
|
|
|
150 |
|
|
|
151 |
|
|
if __name__ == '__main__':
|
152 |
|
|
term = TerminalController()
|
153 |
|
|
print 'This is '+term.GREEN+term.BG_WHITE+'green'+term.NORMAL
|