]> arthur.barton.de Git - bup.git/blob - cmd/newliner-cmd.py
cmd/newliner: if input starts getting too long, print it out.
[bup.git] / cmd / newliner-cmd.py
1 #!/usr/bin/env python
2 import sys, os, re
3 from bup import options
4
5 optspec = """
6 bup newliner
7 """
8 o = options.Options('bup newliner', optspec)
9 (opt, flags, extra) = o.parse(sys.argv[1:])
10
11 if extra:
12     o.fatal("no arguments expected")
13
14 r = re.compile(r'([\r\n])')
15 lastlen = 0
16 all = ''
17 while 1:
18     l = r.split(all, 1)
19     if len(l) <= 1:
20         if len(all) >= 160:
21             sys.stdout.write('%s\n' % all[:78])
22             sys.stdout.flush()
23             all = all[78:]
24         try:
25             b = os.read(sys.stdin.fileno(), 4096)
26         except KeyboardInterrupt:
27             break
28         if not b:
29             break
30         all += b
31     else:
32         assert(len(l) == 3)
33         (line, splitchar, all) = l
34         #splitchar = '\n'
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 or all:
43     sys.stdout.write('%-*s\n' % (lastlen, all))