import sys, os, pwd, subprocess, errno, socket, select, mmap, stat, re
from bup import _version
+# This function should really be in helpers, not in bup.options. But we
+# want options.py to be standalone so people can include it in other projects.
+from bup.options import _tty_width
+tty_width = _tty_width
+
def atoi(s):
"""Convert the string 's' to an integer. Return 0 if s is not a number."""
return ""
l = l[:]
clen = max(len(s) for s in l)
- ncols = (78 - len(prefix)) / (clen + 2)
+ ncols = (tty_width() - len(prefix)) / (clen + 2)
if ncols <= 1:
ncols = 1
clen = 0
"""Command-line options parser.
With the help of an options spec string, easily parse command-line options.
"""
-import sys
-import textwrap
-import getopt
-import re
+import sys, os, textwrap, getopt, re, struct
class OptDict:
def __init__(self):
return v
+def _atoi(v):
+ try:
+ return int(v or 0)
+ except ValueError:
+ return 0
+
+
def _remove_negative_kv(k, v):
if k.startswith('no-') or k.startswith('no_'):
return k[3:], not v
return _remove_negative_kv(k, None)[0]
+def _tty_width():
+ s = struct.pack("HHHH", 0, 0, 0, 0)
+ try:
+ import fcntl, termios
+ s = fcntl.ioctl(sys.stderr.fileno(), termios.TIOCGWINSZ, s)
+ except (IOError, ImportError):
+ return _atoi(os.environ.get('WIDTH')) or 70
+ (ysize,xsize,ypix,xpix) = struct.unpack('HHHH', s)
+ return xsize
+
+
class Options:
"""Option parser.
When constructed, two strings are mandatory. The first one is the command
if has_parm:
flags_nice += ' ...'
prefix = ' %-20s ' % flags_nice
- argtext = '\n'.join(textwrap.wrap(extra, width=70,
+ argtext = '\n'.join(textwrap.wrap(extra, width=_tty_width(),
initial_indent=prefix,
subsequent_indent=' '*28))
out.append(argtext + '\n')
from bup import helpers
from bup.helpers import *
+# after running 'bup newliner', the tty_width() ioctl won't work anymore
+os.environ['WIDTH'] = str(tty_width())
def usage():
log('Usage: bup [-?|--help] [-d BUP_DIR] [--debug] '