]> Tony Duckles's Git Repositories (git.nynim.org) - svn2svn.git/blob - svn2svn/ui.py
Migrate to run/svn2svn.py
[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 # Configuration
36 _level = DEFAULT
37
38
39 def status(msg, *args, **kwargs):
40 """Write a status message.
41
42 args are treated as substitutions for msg.
43
44 The following keyword arguments are allowed:
45 level : One of DEFAULT, VERBOSE or DEBUG.
46 linebreak: If True a new line is appended to msg (default: True).
47 truncate : Truncate output if larger then term width (default: True).
48 """
49 global _level
50 level = kwargs.get('level', DEFAULT)
51 if level > _level:
52 return
53 width = termwidth()
54 if args:
55 msg = msg % args
56 if kwargs.get('linebreak', True):
57 msg = '%s%s' % (msg, os.linesep)
58 if level == ERROR:
59 stream = sys.stderr
60 else:
61 stream = sys.stdout
62 if kwargs.get('truncate', True) and level != ERROR:
63 add_newline = msg.endswith('\n')
64 msglines = msg.splitlines()
65 for no, line in enumerate(msglines):
66 if len(line) > width:
67 msglines[no] = line[:width-3]+"..."
68 msg = os.linesep.join(msglines)
69 if add_newline:
70 msg = '%s%s' % (msg, os.linesep)
71 if isinstance(msg, unicode):
72 msg = msg.encode('utf-8')
73 stream.write(msg)
74 stream.flush()
75
76
77 def update_config(options):
78 """Update UI configuration."""
79 global _level
80 _level = options.verbosity