]> arthur.barton.de Git - bup.git/blob - cmd/newliner-cmd.py
Merge remote branch 'origin/master' into meta
[bup.git] / cmd / newliner-cmd.py
1 #!/usr/bin/env python
2 import sys, os, re
3 from bup import options
4 from bup import _helpers   # fixes up sys.argv on import
5
6 optspec = """
7 bup newliner
8 """
9 o = options.Options(optspec)
10 (opt, flags, extra) = o.parse(sys.argv[1:])
11
12 if extra:
13     o.fatal("no arguments expected")
14
15 r = re.compile(r'([\r\n])')
16 lastlen = 0
17 all = ''
18 while 1:
19     l = r.split(all, 1)
20     if len(l) <= 1:
21         if len(all) >= 160:
22             sys.stdout.write('%s\n' % all[:78])
23             sys.stdout.flush()
24             all = all[78:]
25         try:
26             b = os.read(sys.stdin.fileno(), 4096)
27         except KeyboardInterrupt:
28             break
29         if not b:
30             break
31         all += b
32     else:
33         assert(len(l) == 3)
34         (line, splitchar, all) = l
35         sys.stdout.write('%-*s%s' % (lastlen, line, splitchar))
36         if splitchar == '\r':
37             lastlen = len(line)
38         else:
39             lastlen = 0
40         sys.stdout.flush()
41
42 if lastlen:
43     sys.stdout.write('%-*s\r' % (lastlen, ''))
44 if all:
45     sys.stdout.write('%s\n' % all)