wdiff-colours.awk (2059B)
1 # awk program to clean up wdiff output 2 # prints one line of before and after context 3 # turns deletes red 4 # turns inserts green 5 # http://www.sable.mcgill.ca/~cpicke/swd/README 6 7 BEGIN { 8 printing = 0; # true if we are in printing mode 9 printed_last = 0; # true if we printed the last line and it was not just context 10 first_match = 1; # true if we are at the first match 11 12 color_context = "\033[0m\033[37m"; 13 color_insert = "\033[1m\033[32m"; 14 color_delete = "\033[1m\033[31m" 15 color_off = "\033[0m\033[00m"; 16 17 printf "%s", color_context; 18 } 19 20 END { 21 printf "%s", color_off; 22 } 23 24 { 25 should_print = 0; # true if we need to print the current line and it is not context 26 delete_start = gsub (/\[\-/, color_delete, $0); 27 delete_end = gsub (/\-\]/, color_context, $0); 28 insert_start = gsub (/{\+/, color_insert, $0); 29 insert_end = gsub (/\+}/, color_context, $0); 30 31 # if we have any special tokens we need to print the current line 32 if (delete_start > 0 || 33 delete_end > 0 || 34 insert_start > 0 || 35 insert_end > 0) 36 { 37 should_print = 1; 38 } 39 # if there are more starts than ends we want to turn printing on 40 if (delete_start + insert_start > delete_end + insert_end) 41 { 42 printing = 1; 43 } 44 # if there are less starts than ends we want to turn printing off 45 if (delete_start + insert_start < delete_end + insert_end) 46 { 47 printing = 0; 48 } 49 if (printing) 50 { 51 should_print = 1; 52 } 53 if (should_print) 54 { 55 if (printed_last == 0) 56 { 57 # print before context unless it was already after context 58 if (last_NR != context_NR) 59 { 60 # print separators from the beginning of the second match 61 if (!first_match) 62 { 63 print "---"; 64 } 65 print last; 66 } 67 } 68 print; 69 first_match = 0; 70 printed_last = 1; 71 } 72 else 73 { 74 # print after context 75 if (printed_last == 1) 76 { 77 context_NR = NR; 78 print; 79 printed_last = 0; 80 } 81 } 82 last = $0; 83 last_NR = NR; 84 }