Fossil SCM

Colorize the context diff. Add color to the line numbers so that they can be deemphasized.

drh 2012-02-04 19:55 UTC retro-sbsdiff
Commit b57b035654aea0ec6f7ac09b529758fae848233c
3 files changed +48 -26 +4 -2 +4
+48 -26
--- src/diff.c
+++ src/diff.c
@@ -149,21 +149,34 @@
149149
}
150150
151151
/*
152152
** Append a single line of "diff" output to pOut.
153153
*/
154
-static void appendDiffLine(Blob *pOut, char *zPrefix, DLine *pLine){
155
- blob_append(pOut, zPrefix, 1);
156
- blob_append(pOut, pLine->z, pLine->h & LENGTH_MASK);
154
+static void appendDiffLine(Blob *pOut, char cPrefix, DLine *pLine, int html){
155
+ blob_append(pOut, &cPrefix, 1);
156
+ if( html ){
157
+ if( cPrefix=='+' ){
158
+ blob_append(pOut, "<span class=\"diffadd\">", -1);
159
+ }else if( cPrefix=='-' ){
160
+ blob_append(pOut, "<span class=\"diffrm\">", -1);
161
+ }
162
+ blob_appendf(pOut, "%.*h", (pLine->h & LENGTH_MASK), pLine->z);
163
+ if( cPrefix!=' ' ){
164
+ blob_append(pOut, "</span>", -1);
165
+ }
166
+ }else{
167
+ blob_append(pOut, pLine->z, pLine->h & LENGTH_MASK);
168
+ }
157169
blob_append(pOut, "\n", 1);
158170
}
159171
160172
/*
161173
** Append line numbers to the context diff output. Zero or negative numbers
162174
** are blanks.
163175
*/
164
-static void appendDiffLineno(Blob *pOut, int lnA, int lnB){
176
+static void appendDiffLineno(Blob *pOut, int lnA, int lnB, int html){
177
+ if( html ) blob_append(pOut, "<span class=\"diffln\">", -1);
165178
if( lnA>0 ){
166179
blob_appendf(pOut, "%6d ", lnA);
167180
}else{
168181
blob_append(pOut, " ", 7);
169182
}
@@ -170,10 +183,11 @@
170183
if( lnB>0 ){
171184
blob_appendf(pOut, "%6d ", lnB);
172185
}else{
173186
blob_append(pOut, " ", 8);
174187
}
188
+ if( html ) blob_append(pOut, "</span>", -1);
175189
}
176190
177191
/*
178192
** Expand the size of aEdit[] array to hold nEdit elements.
179193
*/
@@ -218,11 +232,17 @@
218232
219233
/*
220234
** Given a diff context in which the aEdit[] array has been filled
221235
** in, compute a context diff into pOut.
222236
*/
223
-static void contextDiff(DContext *p, Blob *pOut, int nContext, int showLn){
237
+static void contextDiff(
238
+ DContext *p, /* The difference */
239
+ Blob *pOut, /* Output a context diff to here */
240
+ int nContext, /* Number of lines of context */
241
+ int showLn, /* Show line numbers */
242
+ int html /* Render as HTML */
243
+){
224244
DLine *A; /* Left side of the diff */
225245
DLine *B; /* Right side of the diff */
226246
int a = 0; /* Index of next line in A[] */
227247
int b = 0; /* Index of next line in B[] */
228248
int *R; /* Array of COPY/DELETE/INSERT triples */
@@ -282,35 +302,35 @@
282302
/* Show the initial common area */
283303
a += skip;
284304
b += skip;
285305
m = R[r] - skip;
286306
for(j=0; j<m; j++){
287
- if( showLn ) appendDiffLineno(pOut, a+j, b+j);
288
- appendDiffLine(pOut, " ", &A[a+j]);
307
+ if( showLn ) appendDiffLineno(pOut, a+j, b+j, html);
308
+ appendDiffLine(pOut, ' ', &A[a+j], html);
289309
}
290310
a += m;
291311
b += m;
292312
293313
/* Show the differences */
294314
for(i=0; i<nr; i++){
295315
m = R[r+i*3+1];
296316
for(j=0; j<m; j++){
297
- if( showLn ) appendDiffLineno(pOut, a+j, 0);
298
- appendDiffLine(pOut, "-", &A[a+j]);
317
+ if( showLn ) appendDiffLineno(pOut, a+j, 0, html);
318
+ appendDiffLine(pOut, '-', &A[a+j], html);
299319
}
300320
a += m;
301321
m = R[r+i*3+2];
302322
for(j=0; j<m; j++){
303
- if( showLn ) appendDiffLineno(pOut, 0, b+j);
304
- appendDiffLine(pOut, "+", &B[b+j]);
323
+ if( showLn ) appendDiffLineno(pOut, 0, b+j, html);
324
+ appendDiffLine(pOut, '+', &B[b+j], html);
305325
}
306326
b += m;
307327
if( i<nr-1 ){
308328
m = R[r+i*3+3];
309329
for(j=0; j<m; j++){
310
- if( showLn ) appendDiffLineno(pOut, a+j, b+j);
311
- appendDiffLine(pOut, " ", &B[b+j]);
330
+ if( showLn ) appendDiffLineno(pOut, a+j, b+j, html);
331
+ appendDiffLine(pOut, ' ', &B[b+j], html);
312332
}
313333
b += m;
314334
a += m;
315335
}
316336
}
@@ -318,12 +338,12 @@
318338
/* Show the final common area */
319339
assert( nr==i );
320340
m = R[r+nr*3];
321341
if( m>nContext ) m = nContext;
322342
for(j=0; j<m; j++){
323
- if( showLn ) appendDiffLineno(pOut, a+j, b+j);
324
- appendDiffLine(pOut, " ", &B[b+j]);
343
+ if( showLn ) appendDiffLineno(pOut, a+j, b+j, html);
344
+ appendDiffLine(pOut, ' ', &B[b+j], html);
325345
}
326346
}
327347
}
328348
329349
/*
@@ -335,19 +355,10 @@
335355
int n; /* Index of next unused slot in the zLine[] */
336356
int width; /* Maximum width of a column in the output */
337357
unsigned char escHtml; /* True to escape html characters */
338358
};
339359
340
-/*
341
-** Write a 6-digit line number followed by a single space onto the line.
342
-*/
343
-static void sbsWriteLineno(SbsLine *p, int ln){
344
- sqlite3_snprintf(7, &p->zLine[p->n], "%6d", ln+1);
345
- p->zLine[p->n+6] = ' ';
346
- p->n += 7;
347
-}
348
-
349360
/*
350361
** Flags for sbsWriteText()
351362
*/
352363
#define SBS_NEWLINE 0x0001 /* End with \n\000 */
353364
#define SBS_PAD 0x0002 /* Pad output to width spaces */
@@ -417,10 +428,21 @@
417428
** Append a string to the output only if we are rendering HTML.
418429
*/
419430
static void sbsWriteHtml(SbsLine *p, const char *zIn){
420431
if( p->escHtml ) sbsWrite(p, zIn, strlen(zIn));
421432
}
433
+
434
+/*
435
+** Write a 6-digit line number followed by a single space onto the line.
436
+*/
437
+static void sbsWriteLineno(SbsLine *p, int ln){
438
+ sbsWriteHtml(p, "<span class=\"diffln\">");
439
+ sqlite3_snprintf(7, &p->zLine[p->n], "%5d ", ln+1);
440
+ p->n += 6;
441
+ sbsWriteHtml(p, "</span>");
442
+ p->zLine[p->n++] = ' ';
443
+}
422444
423445
424446
/*
425447
** Given a diff context in which the aEdit[] array has been filled
426448
** in, compute a side-by-side diff into pOut.
@@ -887,17 +909,17 @@
887909
/* Compute the difference */
888910
diff_all(&c);
889911
890912
if( pOut ){
891913
/* Compute a context or side-by-side diff into pOut */
914
+ int escHtml = (diffFlags & DIFF_HTML)!=0;
892915
if( diffFlags & DIFF_SIDEBYSIDE ){
893916
int width = diff_width(diffFlags);
894
- int escHtml = (diffFlags & DIFF_HTML)!=0;
895917
sbsDiff(&c, pOut, nContext, width, escHtml);
896918
}else{
897919
int showLn = (diffFlags & DIFF_LINENO)!=0;
898
- contextDiff(&c, pOut, nContext, showLn);
920
+ contextDiff(&c, pOut, nContext, showLn, escHtml);
899921
}
900922
free(c.aFrom);
901923
free(c.aTo);
902924
free(c.aEdit);
903925
return 0;
904926
--- src/diff.c
+++ src/diff.c
@@ -149,21 +149,34 @@
149 }
150
151 /*
152 ** Append a single line of "diff" output to pOut.
153 */
154 static void appendDiffLine(Blob *pOut, char *zPrefix, DLine *pLine){
155 blob_append(pOut, zPrefix, 1);
156 blob_append(pOut, pLine->z, pLine->h & LENGTH_MASK);
 
 
 
 
 
 
 
 
 
 
 
 
157 blob_append(pOut, "\n", 1);
158 }
159
160 /*
161 ** Append line numbers to the context diff output. Zero or negative numbers
162 ** are blanks.
163 */
164 static void appendDiffLineno(Blob *pOut, int lnA, int lnB){
 
165 if( lnA>0 ){
166 blob_appendf(pOut, "%6d ", lnA);
167 }else{
168 blob_append(pOut, " ", 7);
169 }
@@ -170,10 +183,11 @@
170 if( lnB>0 ){
171 blob_appendf(pOut, "%6d ", lnB);
172 }else{
173 blob_append(pOut, " ", 8);
174 }
 
175 }
176
177 /*
178 ** Expand the size of aEdit[] array to hold nEdit elements.
179 */
@@ -218,11 +232,17 @@
218
219 /*
220 ** Given a diff context in which the aEdit[] array has been filled
221 ** in, compute a context diff into pOut.
222 */
223 static void contextDiff(DContext *p, Blob *pOut, int nContext, int showLn){
 
 
 
 
 
 
224 DLine *A; /* Left side of the diff */
225 DLine *B; /* Right side of the diff */
226 int a = 0; /* Index of next line in A[] */
227 int b = 0; /* Index of next line in B[] */
228 int *R; /* Array of COPY/DELETE/INSERT triples */
@@ -282,35 +302,35 @@
282 /* Show the initial common area */
283 a += skip;
284 b += skip;
285 m = R[r] - skip;
286 for(j=0; j<m; j++){
287 if( showLn ) appendDiffLineno(pOut, a+j, b+j);
288 appendDiffLine(pOut, " ", &A[a+j]);
289 }
290 a += m;
291 b += m;
292
293 /* Show the differences */
294 for(i=0; i<nr; i++){
295 m = R[r+i*3+1];
296 for(j=0; j<m; j++){
297 if( showLn ) appendDiffLineno(pOut, a+j, 0);
298 appendDiffLine(pOut, "-", &A[a+j]);
299 }
300 a += m;
301 m = R[r+i*3+2];
302 for(j=0; j<m; j++){
303 if( showLn ) appendDiffLineno(pOut, 0, b+j);
304 appendDiffLine(pOut, "+", &B[b+j]);
305 }
306 b += m;
307 if( i<nr-1 ){
308 m = R[r+i*3+3];
309 for(j=0; j<m; j++){
310 if( showLn ) appendDiffLineno(pOut, a+j, b+j);
311 appendDiffLine(pOut, " ", &B[b+j]);
312 }
313 b += m;
314 a += m;
315 }
316 }
@@ -318,12 +338,12 @@
318 /* Show the final common area */
319 assert( nr==i );
320 m = R[r+nr*3];
321 if( m>nContext ) m = nContext;
322 for(j=0; j<m; j++){
323 if( showLn ) appendDiffLineno(pOut, a+j, b+j);
324 appendDiffLine(pOut, " ", &B[b+j]);
325 }
326 }
327 }
328
329 /*
@@ -335,19 +355,10 @@
335 int n; /* Index of next unused slot in the zLine[] */
336 int width; /* Maximum width of a column in the output */
337 unsigned char escHtml; /* True to escape html characters */
338 };
339
340 /*
341 ** Write a 6-digit line number followed by a single space onto the line.
342 */
343 static void sbsWriteLineno(SbsLine *p, int ln){
344 sqlite3_snprintf(7, &p->zLine[p->n], "%6d", ln+1);
345 p->zLine[p->n+6] = ' ';
346 p->n += 7;
347 }
348
349 /*
350 ** Flags for sbsWriteText()
351 */
352 #define SBS_NEWLINE 0x0001 /* End with \n\000 */
353 #define SBS_PAD 0x0002 /* Pad output to width spaces */
@@ -417,10 +428,21 @@
417 ** Append a string to the output only if we are rendering HTML.
418 */
419 static void sbsWriteHtml(SbsLine *p, const char *zIn){
420 if( p->escHtml ) sbsWrite(p, zIn, strlen(zIn));
421 }
 
 
 
 
 
 
 
 
 
 
 
422
423
424 /*
425 ** Given a diff context in which the aEdit[] array has been filled
426 ** in, compute a side-by-side diff into pOut.
@@ -887,17 +909,17 @@
887 /* Compute the difference */
888 diff_all(&c);
889
890 if( pOut ){
891 /* Compute a context or side-by-side diff into pOut */
 
892 if( diffFlags & DIFF_SIDEBYSIDE ){
893 int width = diff_width(diffFlags);
894 int escHtml = (diffFlags & DIFF_HTML)!=0;
895 sbsDiff(&c, pOut, nContext, width, escHtml);
896 }else{
897 int showLn = (diffFlags & DIFF_LINENO)!=0;
898 contextDiff(&c, pOut, nContext, showLn);
899 }
900 free(c.aFrom);
901 free(c.aTo);
902 free(c.aEdit);
903 return 0;
904
--- src/diff.c
+++ src/diff.c
@@ -149,21 +149,34 @@
149 }
150
151 /*
152 ** Append a single line of "diff" output to pOut.
153 */
154 static void appendDiffLine(Blob *pOut, char cPrefix, DLine *pLine, int html){
155 blob_append(pOut, &cPrefix, 1);
156 if( html ){
157 if( cPrefix=='+' ){
158 blob_append(pOut, "<span class=\"diffadd\">", -1);
159 }else if( cPrefix=='-' ){
160 blob_append(pOut, "<span class=\"diffrm\">", -1);
161 }
162 blob_appendf(pOut, "%.*h", (pLine->h & LENGTH_MASK), pLine->z);
163 if( cPrefix!=' ' ){
164 blob_append(pOut, "</span>", -1);
165 }
166 }else{
167 blob_append(pOut, pLine->z, pLine->h & LENGTH_MASK);
168 }
169 blob_append(pOut, "\n", 1);
170 }
171
172 /*
173 ** Append line numbers to the context diff output. Zero or negative numbers
174 ** are blanks.
175 */
176 static void appendDiffLineno(Blob *pOut, int lnA, int lnB, int html){
177 if( html ) blob_append(pOut, "<span class=\"diffln\">", -1);
178 if( lnA>0 ){
179 blob_appendf(pOut, "%6d ", lnA);
180 }else{
181 blob_append(pOut, " ", 7);
182 }
@@ -170,10 +183,11 @@
183 if( lnB>0 ){
184 blob_appendf(pOut, "%6d ", lnB);
185 }else{
186 blob_append(pOut, " ", 8);
187 }
188 if( html ) blob_append(pOut, "</span>", -1);
189 }
190
191 /*
192 ** Expand the size of aEdit[] array to hold nEdit elements.
193 */
@@ -218,11 +232,17 @@
232
233 /*
234 ** Given a diff context in which the aEdit[] array has been filled
235 ** in, compute a context diff into pOut.
236 */
237 static void contextDiff(
238 DContext *p, /* The difference */
239 Blob *pOut, /* Output a context diff to here */
240 int nContext, /* Number of lines of context */
241 int showLn, /* Show line numbers */
242 int html /* Render as HTML */
243 ){
244 DLine *A; /* Left side of the diff */
245 DLine *B; /* Right side of the diff */
246 int a = 0; /* Index of next line in A[] */
247 int b = 0; /* Index of next line in B[] */
248 int *R; /* Array of COPY/DELETE/INSERT triples */
@@ -282,35 +302,35 @@
302 /* Show the initial common area */
303 a += skip;
304 b += skip;
305 m = R[r] - skip;
306 for(j=0; j<m; j++){
307 if( showLn ) appendDiffLineno(pOut, a+j, b+j, html);
308 appendDiffLine(pOut, ' ', &A[a+j], html);
309 }
310 a += m;
311 b += m;
312
313 /* Show the differences */
314 for(i=0; i<nr; i++){
315 m = R[r+i*3+1];
316 for(j=0; j<m; j++){
317 if( showLn ) appendDiffLineno(pOut, a+j, 0, html);
318 appendDiffLine(pOut, '-', &A[a+j], html);
319 }
320 a += m;
321 m = R[r+i*3+2];
322 for(j=0; j<m; j++){
323 if( showLn ) appendDiffLineno(pOut, 0, b+j, html);
324 appendDiffLine(pOut, '+', &B[b+j], html);
325 }
326 b += m;
327 if( i<nr-1 ){
328 m = R[r+i*3+3];
329 for(j=0; j<m; j++){
330 if( showLn ) appendDiffLineno(pOut, a+j, b+j, html);
331 appendDiffLine(pOut, ' ', &B[b+j], html);
332 }
333 b += m;
334 a += m;
335 }
336 }
@@ -318,12 +338,12 @@
338 /* Show the final common area */
339 assert( nr==i );
340 m = R[r+nr*3];
341 if( m>nContext ) m = nContext;
342 for(j=0; j<m; j++){
343 if( showLn ) appendDiffLineno(pOut, a+j, b+j, html);
344 appendDiffLine(pOut, ' ', &B[b+j], html);
345 }
346 }
347 }
348
349 /*
@@ -335,19 +355,10 @@
355 int n; /* Index of next unused slot in the zLine[] */
356 int width; /* Maximum width of a column in the output */
357 unsigned char escHtml; /* True to escape html characters */
358 };
359
 
 
 
 
 
 
 
 
 
360 /*
361 ** Flags for sbsWriteText()
362 */
363 #define SBS_NEWLINE 0x0001 /* End with \n\000 */
364 #define SBS_PAD 0x0002 /* Pad output to width spaces */
@@ -417,10 +428,21 @@
428 ** Append a string to the output only if we are rendering HTML.
429 */
430 static void sbsWriteHtml(SbsLine *p, const char *zIn){
431 if( p->escHtml ) sbsWrite(p, zIn, strlen(zIn));
432 }
433
434 /*
435 ** Write a 6-digit line number followed by a single space onto the line.
436 */
437 static void sbsWriteLineno(SbsLine *p, int ln){
438 sbsWriteHtml(p, "<span class=\"diffln\">");
439 sqlite3_snprintf(7, &p->zLine[p->n], "%5d ", ln+1);
440 p->n += 6;
441 sbsWriteHtml(p, "</span>");
442 p->zLine[p->n++] = ' ';
443 }
444
445
446 /*
447 ** Given a diff context in which the aEdit[] array has been filled
448 ** in, compute a side-by-side diff into pOut.
@@ -887,17 +909,17 @@
909 /* Compute the difference */
910 diff_all(&c);
911
912 if( pOut ){
913 /* Compute a context or side-by-side diff into pOut */
914 int escHtml = (diffFlags & DIFF_HTML)!=0;
915 if( diffFlags & DIFF_SIDEBYSIDE ){
916 int width = diff_width(diffFlags);
 
917 sbsDiff(&c, pOut, nContext, width, escHtml);
918 }else{
919 int showLn = (diffFlags & DIFF_LINENO)!=0;
920 contextDiff(&c, pOut, nContext, showLn, escHtml);
921 }
922 free(c.aFrom);
923 free(c.aTo);
924 free(c.aEdit);
925 return 0;
926
+4 -2
--- src/info.c
+++ src/info.c
@@ -274,12 +274,14 @@
274274
text_diff(&from, &to, &out, diffFlags | DIFF_HTML);
275275
@ <div class="sbsdiff">
276276
@ %s(blob_str(&out))
277277
@ </div>
278278
}else{
279
- text_diff(&from, &to, &out, diffFlags | DIFF_LINENO);
280
- @ %h(blob_str(&out))
279
+ text_diff(&from, &to, &out, diffFlags | DIFF_LINENO | DIFF_HTML);
280
+ @ <div class="udiff">
281
+ @ %s(blob_str(&out))
282
+ @ </div>
281283
}
282284
blob_reset(&from);
283285
blob_reset(&to);
284286
blob_reset(&out);
285287
}
286288
--- src/info.c
+++ src/info.c
@@ -274,12 +274,14 @@
274 text_diff(&from, &to, &out, diffFlags | DIFF_HTML);
275 @ <div class="sbsdiff">
276 @ %s(blob_str(&out))
277 @ </div>
278 }else{
279 text_diff(&from, &to, &out, diffFlags | DIFF_LINENO);
280 @ %h(blob_str(&out))
 
 
281 }
282 blob_reset(&from);
283 blob_reset(&to);
284 blob_reset(&out);
285 }
286
--- src/info.c
+++ src/info.c
@@ -274,12 +274,14 @@
274 text_diff(&from, &to, &out, diffFlags | DIFF_HTML);
275 @ <div class="sbsdiff">
276 @ %s(blob_str(&out))
277 @ </div>
278 }else{
279 text_diff(&from, &to, &out, diffFlags | DIFF_LINENO | DIFF_HTML);
280 @ <div class="udiff">
281 @ %s(blob_str(&out))
282 @ </div>
283 }
284 blob_reset(&from);
285 blob_reset(&to);
286 blob_reset(&out);
287 }
288
--- src/style.c
+++ src/style.c
@@ -777,10 +777,14 @@
777777
},
778778
{ "span.diffhr",
779779
"suppressed lines in a diff",
780780
@ color: #0000ff;
781781
},
782
+ { "span.diffln",
783
+ "line nubmers in a diff",
784
+ @ color: #a0a0a0;
785
+ },
782786
{ 0,
783787
0,
784788
0
785789
}
786790
};
787791
--- src/style.c
+++ src/style.c
@@ -777,10 +777,14 @@
777 },
778 { "span.diffhr",
779 "suppressed lines in a diff",
780 @ color: #0000ff;
781 },
 
 
 
 
782 { 0,
783 0,
784 0
785 }
786 };
787
--- src/style.c
+++ src/style.c
@@ -777,10 +777,14 @@
777 },
778 { "span.diffhr",
779 "suppressed lines in a diff",
780 @ color: #0000ff;
781 },
782 { "span.diffln",
783 "line nubmers in a diff",
784 @ color: #a0a0a0;
785 },
786 { 0,
787 0,
788 0
789 }
790 };
791

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button