forked from paulfitz/daff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTerminalDiffRender.hx
207 lines (196 loc) · 6.28 KB
/
TerminalDiffRender.hx
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
#if !TOPLEVEL
package coopy;
#end
/**
*
* Decorate a diff being displayed on a console. Colors, glyphs, any
* other eye-candy we like.
*
*/
@:expose
class TerminalDiffRender {
private var codes: Map<String,String>;
private var t: Table;
private var csv: Csv;
private var v: View;
private var align_columns : Bool;
private var wide_columns : Bool;
private var use_glyphs : Bool;
private var flags : CompareFlags;
private var delim : String;
private var diff : Bool;
public function new(flags: CompareFlags = null, delim: String = null, diff : Bool = true) {
align_columns = true;
wide_columns = false;
use_glyphs = true;
this.flags = flags;
if (flags!=null) {
if (flags.padding_strategy == "dense") {
align_columns = false;
}
if (flags.padding_strategy == "sparse") {
wide_columns = true;
}
use_glyphs = flags.use_glyphs;
}
this.delim = (delim!=null) ? delim : ",";
this.diff = diff;
}
/**
*
* @param enable choose whether columns should be aligned by padding
*
*/
public function alignColumns(enable: Bool) {
align_columns = enable;
}
/**
*
* Generate a string with appropriate ANSI colors for a given diff.
*
* @param t a tabular diff (perhaps generated by `TableDiff.hilite`)
* @return the diff in text form, with inserted color codes
*
*/
public function render(t: Table) : String {
csv = new Csv();
var result: String = "";
var w : Int = t.width;
var h : Int = t.height;
this.t = t;
v = t.getCellView();
codes = new Map<String,String>();
codes.set("header","\x1b[0;1m");
codes.set("minor","\x1b[33m");
codes.set("done","\x1b[0m");
codes.set("meta","\x1b[0;1m");
codes.set("spec","\x1b[35;1m");
codes.set("add","\x1b[32;1m");
codes.set("conflict","\x1b[33;1m");
codes.set("modify","\x1b[34;1m");
codes.set("remove","\x1b[31;1m");
var sizes = null;
if (align_columns) sizes = pickSizes(t);
var txts = new Array<String>();
for (y in 0...h) {
var target = 0;
var at = 0;
for (x in 0...w) {
if (sizes!=null) {
var spaces = target-at;
for (i in 0...spaces) {
txts.push(" ");
at++;
}
}
if (x>0) {
txts.push(codes["minor"]);
txts.push(delim);
txts.push(codes["done"]);
}
txts.push(getText(x,y,true));
if (sizes!=null) {
var bit = getText(x,y,false);
at += bit.length;
target += sizes[x];
}
}
txts.push("\r\n");
}
this.t = null;
v = null;
csv = null;
codes = null;
return txts.join("");
}
private function getText(x: Int, y: Int, color: Bool) : String {
var val : Dynamic = t.getCell(x,y);
var cell = DiffRender.renderCell(t,v,x,y);
if (color && diff) {
var code = null;
if (cell.category!=null) {
code = codes[cell.category];
}
if (cell.category_given_tr!=null) {
var code_tr = codes[cell.category_given_tr];
if (code_tr!=null) code = code_tr;
}
if (code!=null) {
var separator = use_glyphs ? cell.pretty_separator : cell.separator;
if (cell.rvalue!=null) {
val = codes["remove"] + cell.lvalue + codes["modify"] + separator + codes["add"] + cell.rvalue + codes["done"];
if (cell.pvalue!=null) {
val = codes["conflict"] + cell.pvalue + codes["modify"] + separator + val;
}
} else {
val = use_glyphs ? cell.pretty_value : cell.value;
val = code + val + codes["done"];
}
}
} else if (color && !diff) {
if (y==0) {
val = codes["header"] + val + codes["done"];
}
} else {
val = use_glyphs ? cell.pretty_value : cell.value;
}
return csv.renderCell(v,val);
}
private function pickSizes(t: Table) {
var w : Int = t.width;
var h : Int = t.height;
var v : View = t.getCellView();
var csv = new Csv();
var sizes = new Array<Int>();
var row = -1;
var total = w-1; // account for commas
for (x in 0...w) {
var m : Float = 0;
var m2 : Float = 0;
var mmax : Int = 0;
var mmostmax : Int = 0;
var mmin : Int = -1;
for (y in 0...h) {
var txt = getText(x,y,false);
if (txt=="@@"&&row==-1&&diff) {
row = y;
}
if (row==-1&&!diff) {
row = y;
}
var len = txt.length;
if (y==row) {
mmin = len;
}
m += len;
m2 += len*len;
if (len>mmax) mmax = len;
}
var mean = m/h;
var stddev = Math.sqrt((m2/h)-mean*mean);
var most = Std.int(mean+stddev*2+0.5);
for (y in 0...h) {
var txt = getText(x,y,false);
var len = txt.length;
if (len<=most) {
if (len>mmostmax) mmostmax = len;
}
}
var full = mmax;
most = mmostmax;
if (mmin!=-1) {
if (most<mmin) most = mmin;
}
if (wide_columns) {
most = full;
}
sizes.push(most);
total += most;
}
if (total>130&&!wide_columns) { // arbitrary wide terminal size
return null;
}
return sizes;
}
}