Do not use curses on Windows
[FeedLint.git] / display.py
blob92bc153a6e82f643650c8c5d6a3ea504c600d405
1 import sys
2 import os
4 if os.name == 'nt':
5 n_cols = 80
6 set_fg = None
7 normal = None
8 else:
9 import curses
10 curses.setupterm()
12 n_cols = curses.tigetnum('cols') or 80
13 set_fg = curses.tigetstr('setf') or None
14 normal = curses.tigetstr('sgr0') or None
16 cursor_pos = 0
18 COLOURS = {
19 'BLACK' : 0,
20 'BLUE' : 1,
21 'GREEN' : 2,
22 'CYAN' : 3,
23 'RED' : 4,
24 'MAGENTA' : 5,
25 'YELLOW' : 6,
26 'WHITE' : 7,
29 def checking(msg, indent = 2):
30 global cursor_pos
31 if cursor_pos:
32 result('!', 'RED')
33 msg = (' ' * indent) + msg
34 cursor_pos = len(msg)
35 sys.stdout.write(msg)
36 sys.stdout.flush()
38 def result(msg, colour = 'GREEN'):
39 global cursor_pos
40 result_col = n_cols - max(15, len(msg) + 5)
41 if cursor_pos > result_col:
42 print
43 cursor_pos = 0
44 if colour:
45 msg = highlight(msg, colour)
46 print " " * (result_col - cursor_pos), "[ %s ]" % msg
47 cursor_pos = 0
49 def error(msg):
50 result(msg, 'RED')
52 def error_new_line(msg, colour = 'RED'):
53 if cursor_pos:
54 error('ERROR')
55 print highlight(msg, colour)
57 def highlight(msg, colour):
58 if set_fg and normal:
59 return curses.tparm(set_fg, COLOURS[colour]) + msg + curses.tparm(normal)
60 else:
61 return msg