def flush_hunk(self): if self.hunk is None: # not initialized yet return hunk = [(ret[0].decode('utf-8'), ret[1]) for ret in self.hunk] new = [c[0] for c in hunk if c[1]['label'] == INSERT_NORM] old = [c[0] for c in hunk if c[1]['label'] == DELETE_NORM] write = super(colorui, self).write for string, style, highlighted in pprint_hunk(new, 0, len(new), old, 0, len(old)): if style == INSERTED: if highlighted: write(string.encode('utf-8'), label=INSERT_EMPH) else: write(string.encode('utf-8'), label=INSERT_NORM) elif style == DELETED: if highlighted: write(string.encode('utf-8'), label=DELETE_EMPH) else: write(string.encode('utf-8'), label=DELETE_NORM) else: write(string.encode('utf-8'), label='') self.hunk = []
def test_highlight_appended_text(self): # new, new_lo, new_hi, old, old_lo, old_hi): new = [ u("+Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor " "incididunt ut labore et commodo magna aliqua. Ut enim ad minim veniam, quis " "nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. " "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu " "fugiat nulla pariatur.") ] old = [ u("-Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor " "incididunt ut labore et dolore magna aliqua.") ] ret = pprint_hunk(new, 0, 1, old, 0, 1) pairs = list(ret) self.assertEqual(10, len(pairs)) self.assertEqual((u( "-Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " "tempor incididunt ut labore et "), DELETED, False), pairs[0]) self.assertEqual((u("dolore"), DELETED, True), pairs[1]) self.assertEqual((u(" magna aliqua."), DELETED, False), pairs[2]) self.assertEqual((u(""), DELETED, True), pairs[3]) self.assertEqual((u("\n"), NORMAL, False), pairs[4]) self.assertEqual((u( "+Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " "tempor incididunt ut labore et "), INSERTED, False), pairs[5]) self.assertEqual((u("commodo"), INSERTED, True), pairs[6]) self.assertEqual((u(" magna aliqua."), INSERTED, False), pairs[7]) self.assertEqual((u( " Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi " "ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit " "in voluptate velit esse cillum dolore eu fugiat nulla pariatur."), INSERTED, True), pairs[8]) self.assertEqual((u("\n"), NORMAL, False), pairs[9])
def test_highlight_indented_lines(self): # new, new_lo, new_hi, old, old_lo, old_hi): new = [ u("+class Foo(object):"), u("+ def hello(name):"), u("+ print 'Hello ', name") ] old = [u("-def hello(name):"), u("- print 'Hello ', name")] ret = pprint_hunk(new, 0, 3, old, 0, 2) pairs = list(ret) self.assertEqual(18, len(pairs)) self.assertEqual((u("+class Foo(object):"), INSERTED, False), pairs[0]) self.assertEqual((u("\n"), NORMAL, False), pairs[1]) self.assertEqual((u("-"), DELETED, False), pairs[2]) self.assertEqual((u(""), DELETED, True), pairs[3]) self.assertEqual((u("def hello(name):"), DELETED, False), pairs[4]) self.assertEqual((u("\n"), NORMAL, False), pairs[5]) self.assertEqual((u("-"), DELETED, False), pairs[6]) self.assertEqual((u(""), DELETED, True), pairs[7]) self.assertEqual((u(" print 'Hello ', name"), DELETED, False), pairs[8]) self.assertEqual((u("\n"), NORMAL, False), pairs[9]) self.assertEqual((u("+"), INSERTED, False), pairs[10]) self.assertEqual((u(" "), INSERTED, True), pairs[11]) self.assertEqual((u("def hello(name):"), INSERTED, False), pairs[12]) self.assertEqual((u("\n"), NORMAL, False), pairs[13]) self.assertEqual((u("+"), INSERTED, False), pairs[14]) self.assertEqual((u(" "), INSERTED, True), pairs[15]) self.assertEqual((u(" print 'Hello ', name"), INSERTED, False), pairs[16]) self.assertEqual((u("\n"), NORMAL, False), pairs[17])
def test_highlight_appended_text(self): # new, new_lo, new_hi, old, old_lo, old_hi): new = [u("+Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor " "incididunt ut labore et commodo magna aliqua. Ut enim ad minim veniam, quis " "nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. " "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu " "fugiat nulla pariatur.")] old = [u("-Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor " "incididunt ut labore et dolore magna aliqua.")] ret = pprint_hunk(new, 0, 1, old, 0, 1) pairs = list(ret) self.assertEqual(10, len(pairs)) self.assertEqual((u("-Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " "tempor incididunt ut labore et "), DELETED, False), pairs[0]) self.assertEqual((u("dolore"), DELETED, True), pairs[1]) self.assertEqual((u(" magna aliqua."), DELETED, False), pairs[2]) self.assertEqual((u(""), DELETED, True), pairs[3]) self.assertEqual((u("\n"), NORMAL, False), pairs[4]) self.assertEqual((u("+Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " "tempor incididunt ut labore et "), INSERTED, False), pairs[5]) self.assertEqual((u("commodo"), INSERTED, True), pairs[6]) self.assertEqual((u(" magna aliqua."), INSERTED, False), pairs[7]) self.assertEqual((u(" Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi " "ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit " "in voluptate velit esse cillum dolore eu fugiat nulla pariatur."), INSERTED, True), pairs[8]) self.assertEqual((u("\n"), NORMAL, False), pairs[9])
def test_highlight_indented_lines(self): # new, new_lo, new_hi, old, old_lo, old_hi): new = [u("+class Foo(object):"), u("+ def hello(name):"), u("+ print 'Hello ', name")] old = [u("-def hello(name):"), u("- print 'Hello ', name")] ret = pprint_hunk(new, 0, 3, old, 0, 2) pairs = list(ret) self.assertEqual(18, len(pairs)) self.assertEqual((u("+class Foo(object):"), INSERTED, False), pairs[0]) self.assertEqual((u("\n"), NORMAL, False), pairs[1]) self.assertEqual((u("-"), DELETED, False), pairs[2]) self.assertEqual((u(""), DELETED, True), pairs[3]) self.assertEqual((u("def hello(name):"), DELETED, False), pairs[4]) self.assertEqual((u("\n"), NORMAL, False), pairs[5]) self.assertEqual((u("-"), DELETED, False), pairs[6]) self.assertEqual((u(""), DELETED, True), pairs[7]) self.assertEqual((u(" print 'Hello ', name"), DELETED, False), pairs[8]) self.assertEqual((u("\n"), NORMAL, False), pairs[9]) self.assertEqual((u("+"), INSERTED, False), pairs[10]) self.assertEqual((u(" "), INSERTED, True), pairs[11]) self.assertEqual((u("def hello(name):"), INSERTED, False), pairs[12]) self.assertEqual((u("\n"), NORMAL, False), pairs[13]) self.assertEqual((u("+"), INSERTED, False), pairs[14]) self.assertEqual((u(" "), INSERTED, True), pairs[15]) self.assertEqual((u(" print 'Hello ', name"), INSERTED, False), pairs[16]) self.assertEqual((u("\n"), NORMAL, False), pairs[17])
def show_hunk(new, old): for string, style, highlighted, in pprint_hunk(new, 0, len(new), old, 0, len(old)): if style == INSERTED: if highlighted: write(string, 'green', True) else: write(string, 'green') elif style == DELETED: if highlighted: write(string, 'red', True) else: write(string, 'red') else: write(string)
def test_pprint_hunk(self): # new, new_lo, new_hi, old, old_lo, old_hi): new = [ u("+print 'hello', 'world'"), u("+"), u("+"), u("+print 'bye world'") ] old = [u("-print 'nice', 'boat'"), u("-print \"bye world\"")] ret = pprint_hunk(new, 0, 4, old, 0, 2) pairs = list(ret) self.assertEqual(26, len(pairs)) self.assertEqual((u("-print '"), DELETED, False), pairs[0]) self.assertEqual((u("nice"), DELETED, True), pairs[1]) self.assertEqual((u("', '"), DELETED, False), pairs[2]) self.assertEqual((u("boat"), DELETED, True), pairs[3]) self.assertEqual((u("'"), DELETED, False), pairs[4]) self.assertEqual((u("\n"), NORMAL, False), pairs[5]) self.assertEqual((u("+print '"), INSERTED, False), pairs[6]) self.assertEqual((u("hello"), INSERTED, True), pairs[7]) self.assertEqual((u("', '"), INSERTED, False), pairs[8]) self.assertEqual((u("world"), INSERTED, True), pairs[9]) self.assertEqual((u("'"), INSERTED, False), pairs[10]) self.assertEqual((u("\n"), NORMAL, False), pairs[11]) self.assertEqual((u("+"), INSERTED, False), pairs[12]) self.assertEqual((u("\n"), NORMAL, False), pairs[13]) self.assertEqual((u("+"), INSERTED, False), pairs[14]) self.assertEqual((u("\n"), NORMAL, False), pairs[15]) self.assertEqual((u("-print "), DELETED, False), pairs[16]) self.assertEqual((u("\""), DELETED, True), pairs[17]) self.assertEqual((u("bye world"), DELETED, False), pairs[18]) self.assertEqual((u("\""), DELETED, True), pairs[19]) self.assertEqual((u("\n"), NORMAL, False), pairs[20]) self.assertEqual((u("+print "), INSERTED, False), pairs[21]) self.assertEqual((u("'"), INSERTED, True), pairs[22]) self.assertEqual((u("bye world"), INSERTED, False), pairs[23]) self.assertEqual((u("'"), INSERTED, True), pairs[24]) self.assertEqual((u("\n"), NORMAL, False), pairs[25])
def test_pprint_hunk(self): # new, new_lo, new_hi, old, old_lo, old_hi): new = [u("+print 'hello', 'world'"), u("+"), u("+"), u("+print 'bye world'")] old = [u("-print 'nice', 'boat'"), u("-print \"bye world\"")] ret = pprint_hunk(new, 0, 4, old, 0, 2) pairs = list(ret) self.assertEqual(26, len(pairs)) self.assertEqual((u("-print '"), DELETED, False), pairs[0]) self.assertEqual((u("nice"), DELETED, True), pairs[1]) self.assertEqual((u("', '"), DELETED, False), pairs[2]) self.assertEqual((u("boat"), DELETED, True), pairs[3]) self.assertEqual((u("'"), DELETED, False), pairs[4]) self.assertEqual((u("\n"), NORMAL, False), pairs[5]) self.assertEqual((u("+print '"), INSERTED, False), pairs[6]) self.assertEqual((u("hello"), INSERTED, True), pairs[7]) self.assertEqual((u("', '"), INSERTED, False), pairs[8]) self.assertEqual((u("world"), INSERTED, True), pairs[9]) self.assertEqual((u("'"), INSERTED, False), pairs[10]) self.assertEqual((u("\n"), NORMAL, False), pairs[11]) self.assertEqual((u("+"), INSERTED, False), pairs[12]) self.assertEqual((u("\n"), NORMAL, False), pairs[13]) self.assertEqual((u("+"), INSERTED, False), pairs[14]) self.assertEqual((u("\n"), NORMAL, False), pairs[15]) self.assertEqual((u("-print "), DELETED, False), pairs[16]) self.assertEqual((u("\""), DELETED, True), pairs[17]) self.assertEqual((u("bye world"), DELETED, False), pairs[18]) self.assertEqual((u("\""), DELETED, True), pairs[19]) self.assertEqual((u("\n"), NORMAL, False), pairs[20]) self.assertEqual((u("+print "), INSERTED, False), pairs[21]) self.assertEqual((u("'"), INSERTED, True), pairs[22]) self.assertEqual((u("bye world"), INSERTED, False), pairs[23]) self.assertEqual((u("'"), INSERTED, True), pairs[24]) self.assertEqual((u("\n"), NORMAL, False), pairs[25])