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