]> Tony Duckles's Git Repositories (git.nynim.org) - svn2svn.git/blob - svn2svn/ui.py
Continue-mode support w/ rev_map
[svn2svn.git] / svn2svn / ui.py
1 """ User interface functions """
2
3 import os
4 import sys
5
6 def termwidth():
7 if 'COLUMNS' in os.environ:
8 try:
9 return int(os.environ['COLUMNS'])
10 except ValueError:
11 pass
12 try:
13 import termios, array, fcntl
14 for dev in (sys.stdout, sys.stdin):
15 try:
16 fd = dev.fileno()
17 if not os.isatty(fd):
18 continue
19 arri = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * 8)
20 return array.array('h', arri)[1]
21 except ValueError:
22 pass
23 except ImportError:
24 pass
25 return 80
26
27
28 # Log levels
29 ERROR = 0
30 DEFAULT = 10
31 VERBOSE = 20
32 DEBUG = 30
33
34
35 # SGR foreground color codes
36 _colors = {
37 'BLACK': '30', 'BLACK_B': '90',
38 'RED': '31', 'RED_B': '91',
39 'GREEN': '32', 'GREEN_B': '92',
40 'YELLOW': '33', 'YELLOW_B': '93',
41 'BLUE': '34', 'BLUE_B': '94',
42 'MAGENTA': '35', 'MAGENTA_B': '95',
43 'CYAN': '36', 'CYAN_B': '96',
44 'WHITE': '37', 'WHITE_B': '97' }
45
46
47 # Configuration
48 _level = DEFAULT
49
50
51 def status(msg, *args, **kwargs):
52 """Write a status message.
53
54 args are treated as substitutions for msg.
55
56 The following keyword arguments are allowed:
57 level : One of DEFAULT, VERBOSE or DEBUG.
58 linebreak: If True a new line is appended to msg (default: True).
59 truncate : Truncate output if larger then term width (default: True).
60 """
61 global _level
62 level = kwargs.get('level', DEFAULT)
63 if level > _level:
64 return
65 width = termwidth()
66 if args:
67 msg = msg % args
68 if kwargs.get('linebreak', True):
69 msg = '%s%s' % (msg, os.linesep)
70 if level == ERROR:
71 stream = sys.stderr
72 else:
73 stream = sys.stdout
74 if kwargs.get('truncate', False) and level != ERROR:
75 add_newline = msg.endswith('\n')
76 msglines = msg.splitlines()
77 for no, line in enumerate(msglines):
78 if len(line) > width:
79 msglines[no] = line[:width-3]+"..."
80 msg = os.linesep.join(msglines)
81 if add_newline:
82 msg = '%s%s' % (msg, os.linesep)
83 if isinstance(msg, unicode):
84 msg = msg.encode('utf-8')
85 color = kwargs.get('color', None)
86 bold = kwargs.get('bold', None)
87 if color in _colors:
88 msg = '%s%s%s' % ("\x1b["+_colors[color]+(";1" if bold else "")+"m", msg, "\x1b[0m")
89 stream.write(msg)
90 stream.flush()
91
92
93 def update_config(options):
94 """Update UI configuration."""
95 global _level
96 _level = options.verbosity