summaryrefslogtreecommitdiff
path: root/bin/linediff
blob: c0b522cf271ce4fc40b744b3f8d68f0f9848f126 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/python3

# script for line-by-line diffing in vim
# TODO: prettify/test

import sys

if len(sys.argv) < 3:
    sys.stderr.write('Usage: %s file1 file2\n' % sys.argv[0])
    sys.exit(1)

f1 = open(sys.argv[1], 'r')
f2 = open(sys.argv[2], 'r')

buf_start = -1
buf1 = []
buf2 = []

def flush_buf():
    global buf_start, buf1, buf2

    if buf_start == -1:
        return

    if (len(buf1) > 1):
        sys.stdout.write('%d,%dc%d,%d\n' % (buf_start, buf_start + len(buf1) - 1, buf_start, buf_start + len(buf1) - 1))
    else:
        sys.stdout.write('%dc%d\n' % (buf_start, buf_start))

    for line in buf1:
        sys.stdout.write('< %s' % line)
    sys.stdout.write('---\n')
    for line in buf2:
        sys.stdout.write('> %s' % line)

    buf_start = -1
    buf1 = []
    buf2 = []

i = 1
while True:
    line1 = f1.readline()
    line2 = f2.readline()

    if (line1 == '') != (line2 == ''):
        flush_buf()
        line = line1 if line1 else line2
        f    = f1    if line1 else f2
        buf = [line]
        buf.extend(map(lambda x: x.rstrip('\n'), f.readlines()))
        sys.stdout.write('%d,%d' % (i, i + len(buf) - 1))
        sys.stdout.write('d' if line1 else 'c')
        sys.stdout.write('\n')
        for line in buf:
            sys.stdout.write('%s %s\n' % ('>' if line1 else '<', line))
        break

    if line1 == '':
        flush_buf()
        break

    if line1 == line2:
        flush_buf()
    else:
        if buf_start == -1:
            buf_start = i
        buf1.append(line1)
        buf2.append(line2)

    i += 1

f1.close()
f2.close()