Fossil SCM

Revised default color scheme. Add line-numbers to context diff.

drh 2012-02-04 19:34 UTC retro-sbsdiff
Commit 6a6697694cadd11cc244e930ddf68aaf5d68b5dd
3 files changed +31 -3 +1 -1 +4 -4
+31 -3
--- src/diff.c
+++ src/diff.c
@@ -32,10 +32,11 @@
3232
#define DIFF_IGNORE_EOLWS 0x01000000 /* Ignore end-of-line whitespace */
3333
#define DIFF_SIDEBYSIDE 0x02000000 /* Generate a side-by-side diff */
3434
#define DIFF_NEWFILE 0x04000000 /* Missing files are as empty files */
3535
#define DIFF_INLINE 0x08000000 /* Inline (not side-by-side) diff */
3636
#define DIFF_HTML 0x10000000 /* Render for HTML */
37
+#define DIFF_LINENO 0x20000000 /* Show line numbers in context diff */
3738
3839
#endif /* INTERFACE */
3940
4041
/*
4142
** Maximum length of a line in a text file. (8192)
@@ -153,10 +154,27 @@
153154
static void appendDiffLine(Blob *pOut, char *zPrefix, DLine *pLine){
154155
blob_append(pOut, zPrefix, 1);
155156
blob_append(pOut, pLine->z, pLine->h & LENGTH_MASK);
156157
blob_append(pOut, "\n", 1);
157158
}
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
+ if( lnB>0 ){
171
+ blob_appendf(pOut, "%6d ", lnB);
172
+ }else{
173
+ blob_append(pOut, " ", 8);
174
+ }
175
+}
158176
159177
/*
160178
** Expand the size of aEdit[] array to hold nEdit elements.
161179
*/
162180
static void expandEdit(DContext *p, int nEdit){
@@ -200,11 +218,11 @@
200218
201219
/*
202220
** Given a diff context in which the aEdit[] array has been filled
203221
** in, compute a context diff into pOut.
204222
*/
205
-static void contextDiff(DContext *p, Blob *pOut, int nContext){
223
+static void contextDiff(DContext *p, Blob *pOut, int nContext, int showLn){
206224
DLine *A; /* Left side of the diff */
207225
DLine *B; /* Right side of the diff */
208226
int a = 0; /* Index of next line in A[] */
209227
int b = 0; /* Index of next line in B[] */
210228
int *R; /* Array of COPY/DELETE/INSERT triples */
@@ -254,39 +272,44 @@
254272
/*
255273
* If the patch changes an empty file or results in an empty file,
256274
* the block header must use 0,0 as position indicator and not 1,0.
257275
* Otherwise, patch would be confused and may reject the diff.
258276
*/
277
+ if( showLn ) blob_appendf(pOut, "%*s", 15, "");
259278
blob_appendf(pOut,"@@ -%d,%d +%d,%d @@\n",
260279
na ? a+skip+1 : 0, na,
261280
nb ? b+skip+1 : 0, nb);
262281
263282
/* Show the initial common area */
264283
a += skip;
265284
b += skip;
266285
m = R[r] - skip;
267286
for(j=0; j<m; j++){
287
+ if( showLn ) appendDiffLineno(pOut, a+j, b+j);
268288
appendDiffLine(pOut, " ", &A[a+j]);
269289
}
270290
a += m;
271291
b += m;
272292
273293
/* Show the differences */
274294
for(i=0; i<nr; i++){
275295
m = R[r+i*3+1];
276296
for(j=0; j<m; j++){
297
+ if( showLn ) appendDiffLineno(pOut, a+j, 0);
277298
appendDiffLine(pOut, "-", &A[a+j]);
278299
}
279300
a += m;
280301
m = R[r+i*3+2];
281302
for(j=0; j<m; j++){
303
+ if( showLn ) appendDiffLineno(pOut, 0, b+j);
282304
appendDiffLine(pOut, "+", &B[b+j]);
283305
}
284306
b += m;
285307
if( i<nr-1 ){
286308
m = R[r+i*3+3];
287309
for(j=0; j<m; j++){
310
+ if( showLn ) appendDiffLineno(pOut, a+j, b+j);
288311
appendDiffLine(pOut, " ", &B[b+j]);
289312
}
290313
b += m;
291314
a += m;
292315
}
@@ -295,10 +318,11 @@
295318
/* Show the final common area */
296319
assert( nr==i );
297320
m = R[r+nr*3];
298321
if( m>nContext ) m = nContext;
299322
for(j=0; j<m; j++){
323
+ if( showLn ) appendDiffLineno(pOut, a+j, b+j);
300324
appendDiffLine(pOut, " ", &B[b+j]);
301325
}
302326
}
303327
}
304328
@@ -334,11 +358,11 @@
334358
** spaces. Add a newline if SBS_NEWLINE is set. Translate HTML characters
335359
** if SBS_HTML is set. Pad the rendering out width bytes if SBS_PAD is set.
336360
*/
337361
static void sbsWriteText(SbsLine *p, DLine *pLine, unsigned flags){
338362
int n = pLine->h & LENGTH_MASK;
339
- int i, j, k;
363
+ int i, j;
340364
const char *zIn = pLine->z;
341365
char *z = &p->zLine[p->n];
342366
int w = p->width;
343367
if( n>w ) n = w;
344368
for(i=j=0; i<n; i++){
@@ -868,11 +892,12 @@
868892
if( diffFlags & DIFF_SIDEBYSIDE ){
869893
int width = diff_width(diffFlags);
870894
int escHtml = (diffFlags & DIFF_HTML)!=0;
871895
sbsDiff(&c, pOut, nContext, width, escHtml);
872896
}else{
873
- contextDiff(&c, pOut, nContext);
897
+ int showLn = (diffFlags & DIFF_LINENO)!=0;
898
+ contextDiff(&c, pOut, nContext, showLn);
874899
}
875900
free(c.aFrom);
876901
free(c.aTo);
877902
free(c.aEdit);
878903
return 0;
@@ -913,10 +938,12 @@
913938
** "diffFlags" integer.
914939
**
915940
** --side-by-side|-y Side-by-side diff. DIFF_SIDEBYSIDE
916941
** --context|-c N N lines of context. DIFF_CONTEXT_MASK
917942
** --width|-W N N character lines. DIFF_WIDTH_MASK
943
+** --html Format for HTML DIFF_HTML
944
+** --linenum|-n Show line numbers DIFF_LINENO
918945
*/
919946
int diff_options(void){
920947
int diffFlags = 0;
921948
const char *z;
922949
int f;
@@ -929,10 +956,11 @@
929956
f *= DIFF_CONTEXT_MASK+1;
930957
if( f > DIFF_WIDTH_MASK ) f = DIFF_CONTEXT_MASK;
931958
diffFlags |= f;
932959
}
933960
if( find_option("html",0,0)!=0 ) diffFlags |= DIFF_HTML;
961
+ if( find_option("linenum","n",0)!=0 ) diffFlags |= DIFF_LINENO;
934962
return diffFlags;
935963
}
936964
937965
/*
938966
** COMMAND: test-udiff
939967
--- src/diff.c
+++ src/diff.c
@@ -32,10 +32,11 @@
32 #define DIFF_IGNORE_EOLWS 0x01000000 /* Ignore end-of-line whitespace */
33 #define DIFF_SIDEBYSIDE 0x02000000 /* Generate a side-by-side diff */
34 #define DIFF_NEWFILE 0x04000000 /* Missing files are as empty files */
35 #define DIFF_INLINE 0x08000000 /* Inline (not side-by-side) diff */
36 #define DIFF_HTML 0x10000000 /* Render for HTML */
 
37
38 #endif /* INTERFACE */
39
40 /*
41 ** Maximum length of a line in a text file. (8192)
@@ -153,10 +154,27 @@
153 static void appendDiffLine(Blob *pOut, char *zPrefix, DLine *pLine){
154 blob_append(pOut, zPrefix, 1);
155 blob_append(pOut, pLine->z, pLine->h & LENGTH_MASK);
156 blob_append(pOut, "\n", 1);
157 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
159 /*
160 ** Expand the size of aEdit[] array to hold nEdit elements.
161 */
162 static void expandEdit(DContext *p, int nEdit){
@@ -200,11 +218,11 @@
200
201 /*
202 ** Given a diff context in which the aEdit[] array has been filled
203 ** in, compute a context diff into pOut.
204 */
205 static void contextDiff(DContext *p, Blob *pOut, int nContext){
206 DLine *A; /* Left side of the diff */
207 DLine *B; /* Right side of the diff */
208 int a = 0; /* Index of next line in A[] */
209 int b = 0; /* Index of next line in B[] */
210 int *R; /* Array of COPY/DELETE/INSERT triples */
@@ -254,39 +272,44 @@
254 /*
255 * If the patch changes an empty file or results in an empty file,
256 * the block header must use 0,0 as position indicator and not 1,0.
257 * Otherwise, patch would be confused and may reject the diff.
258 */
 
259 blob_appendf(pOut,"@@ -%d,%d +%d,%d @@\n",
260 na ? a+skip+1 : 0, na,
261 nb ? b+skip+1 : 0, nb);
262
263 /* Show the initial common area */
264 a += skip;
265 b += skip;
266 m = R[r] - skip;
267 for(j=0; j<m; j++){
 
268 appendDiffLine(pOut, " ", &A[a+j]);
269 }
270 a += m;
271 b += m;
272
273 /* Show the differences */
274 for(i=0; i<nr; i++){
275 m = R[r+i*3+1];
276 for(j=0; j<m; j++){
 
277 appendDiffLine(pOut, "-", &A[a+j]);
278 }
279 a += m;
280 m = R[r+i*3+2];
281 for(j=0; j<m; j++){
 
282 appendDiffLine(pOut, "+", &B[b+j]);
283 }
284 b += m;
285 if( i<nr-1 ){
286 m = R[r+i*3+3];
287 for(j=0; j<m; j++){
 
288 appendDiffLine(pOut, " ", &B[b+j]);
289 }
290 b += m;
291 a += m;
292 }
@@ -295,10 +318,11 @@
295 /* Show the final common area */
296 assert( nr==i );
297 m = R[r+nr*3];
298 if( m>nContext ) m = nContext;
299 for(j=0; j<m; j++){
 
300 appendDiffLine(pOut, " ", &B[b+j]);
301 }
302 }
303 }
304
@@ -334,11 +358,11 @@
334 ** spaces. Add a newline if SBS_NEWLINE is set. Translate HTML characters
335 ** if SBS_HTML is set. Pad the rendering out width bytes if SBS_PAD is set.
336 */
337 static void sbsWriteText(SbsLine *p, DLine *pLine, unsigned flags){
338 int n = pLine->h & LENGTH_MASK;
339 int i, j, k;
340 const char *zIn = pLine->z;
341 char *z = &p->zLine[p->n];
342 int w = p->width;
343 if( n>w ) n = w;
344 for(i=j=0; i<n; i++){
@@ -868,11 +892,12 @@
868 if( diffFlags & DIFF_SIDEBYSIDE ){
869 int width = diff_width(diffFlags);
870 int escHtml = (diffFlags & DIFF_HTML)!=0;
871 sbsDiff(&c, pOut, nContext, width, escHtml);
872 }else{
873 contextDiff(&c, pOut, nContext);
 
874 }
875 free(c.aFrom);
876 free(c.aTo);
877 free(c.aEdit);
878 return 0;
@@ -913,10 +938,12 @@
913 ** "diffFlags" integer.
914 **
915 ** --side-by-side|-y Side-by-side diff. DIFF_SIDEBYSIDE
916 ** --context|-c N N lines of context. DIFF_CONTEXT_MASK
917 ** --width|-W N N character lines. DIFF_WIDTH_MASK
 
 
918 */
919 int diff_options(void){
920 int diffFlags = 0;
921 const char *z;
922 int f;
@@ -929,10 +956,11 @@
929 f *= DIFF_CONTEXT_MASK+1;
930 if( f > DIFF_WIDTH_MASK ) f = DIFF_CONTEXT_MASK;
931 diffFlags |= f;
932 }
933 if( find_option("html",0,0)!=0 ) diffFlags |= DIFF_HTML;
 
934 return diffFlags;
935 }
936
937 /*
938 ** COMMAND: test-udiff
939
--- src/diff.c
+++ src/diff.c
@@ -32,10 +32,11 @@
32 #define DIFF_IGNORE_EOLWS 0x01000000 /* Ignore end-of-line whitespace */
33 #define DIFF_SIDEBYSIDE 0x02000000 /* Generate a side-by-side diff */
34 #define DIFF_NEWFILE 0x04000000 /* Missing files are as empty files */
35 #define DIFF_INLINE 0x08000000 /* Inline (not side-by-side) diff */
36 #define DIFF_HTML 0x10000000 /* Render for HTML */
37 #define DIFF_LINENO 0x20000000 /* Show line numbers in context diff */
38
39 #endif /* INTERFACE */
40
41 /*
42 ** Maximum length of a line in a text file. (8192)
@@ -153,10 +154,27 @@
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 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 */
180 static void expandEdit(DContext *p, int nEdit){
@@ -200,11 +218,11 @@
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 */
@@ -254,39 +272,44 @@
272 /*
273 * If the patch changes an empty file or results in an empty file,
274 * the block header must use 0,0 as position indicator and not 1,0.
275 * Otherwise, patch would be confused and may reject the diff.
276 */
277 if( showLn ) blob_appendf(pOut, "%*s", 15, "");
278 blob_appendf(pOut,"@@ -%d,%d +%d,%d @@\n",
279 na ? a+skip+1 : 0, na,
280 nb ? b+skip+1 : 0, nb);
281
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 }
@@ -295,10 +318,11 @@
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
@@ -334,11 +358,11 @@
358 ** spaces. Add a newline if SBS_NEWLINE is set. Translate HTML characters
359 ** if SBS_HTML is set. Pad the rendering out width bytes if SBS_PAD is set.
360 */
361 static void sbsWriteText(SbsLine *p, DLine *pLine, unsigned flags){
362 int n = pLine->h & LENGTH_MASK;
363 int i, j;
364 const char *zIn = pLine->z;
365 char *z = &p->zLine[p->n];
366 int w = p->width;
367 if( n>w ) n = w;
368 for(i=j=0; i<n; i++){
@@ -868,11 +892,12 @@
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;
@@ -913,10 +938,12 @@
938 ** "diffFlags" integer.
939 **
940 ** --side-by-side|-y Side-by-side diff. DIFF_SIDEBYSIDE
941 ** --context|-c N N lines of context. DIFF_CONTEXT_MASK
942 ** --width|-W N N character lines. DIFF_WIDTH_MASK
943 ** --html Format for HTML DIFF_HTML
944 ** --linenum|-n Show line numbers DIFF_LINENO
945 */
946 int diff_options(void){
947 int diffFlags = 0;
948 const char *z;
949 int f;
@@ -929,10 +956,11 @@
956 f *= DIFF_CONTEXT_MASK+1;
957 if( f > DIFF_WIDTH_MASK ) f = DIFF_CONTEXT_MASK;
958 diffFlags |= f;
959 }
960 if( find_option("html",0,0)!=0 ) diffFlags |= DIFF_HTML;
961 if( find_option("linenum","n",0)!=0 ) diffFlags |= DIFF_LINENO;
962 return diffFlags;
963 }
964
965 /*
966 ** COMMAND: test-udiff
967
+1 -1
--- src/info.c
+++ src/info.c
@@ -274,11 +274,11 @@
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);
279
+ text_diff(&from, &to, &out, diffFlags | DIFF_LINENO);
280280
@ %h(blob_str(&out))
281281
}
282282
blob_reset(&from);
283283
blob_reset(&to);
284284
blob_reset(&out);
285285
--- src/info.c
+++ src/info.c
@@ -274,11 +274,11 @@
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);
280 @ %h(blob_str(&out))
281 }
282 blob_reset(&from);
283 blob_reset(&to);
284 blob_reset(&out);
285
--- src/info.c
+++ src/info.c
@@ -274,11 +274,11 @@
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
+4 -4
--- src/style.c
+++ src/style.c
@@ -757,25 +757,25 @@
757757
"side-by-side diff display",
758758
@ font-family: monospace;
759759
@ white-space: pre;
760760
},
761761
{ "div.udiff",
762
- "unified diff display",
762
+ "context diff display",
763763
@ font-family: monospace;
764764
@ white-space: pre;
765765
},
766766
{ "span.diffchng",
767767
"changes in a diff",
768
- @ background-color: #d8d8d8;
768
+ @ background-color: #ffffc8;
769769
},
770770
{ "span.diffadd",
771771
"added code in a diff",
772
- @ background-color: #d8ffd8;
772
+ @ background-color: #e0ffe0;
773773
},
774774
{ "span.diffrm",
775775
"deleted in a diff",
776
- @ background-color: #ffd8d8;
776
+ @ background-color: #ffe0e0;
777777
},
778778
{ "span.diffhr",
779779
"suppressed lines in a diff",
780780
@ color: #0000ff;
781781
},
782782
--- src/style.c
+++ src/style.c
@@ -757,25 +757,25 @@
757 "side-by-side diff display",
758 @ font-family: monospace;
759 @ white-space: pre;
760 },
761 { "div.udiff",
762 "unified diff display",
763 @ font-family: monospace;
764 @ white-space: pre;
765 },
766 { "span.diffchng",
767 "changes in a diff",
768 @ background-color: #d8d8d8;
769 },
770 { "span.diffadd",
771 "added code in a diff",
772 @ background-color: #d8ffd8;
773 },
774 { "span.diffrm",
775 "deleted in a diff",
776 @ background-color: #ffd8d8;
777 },
778 { "span.diffhr",
779 "suppressed lines in a diff",
780 @ color: #0000ff;
781 },
782
--- src/style.c
+++ src/style.c
@@ -757,25 +757,25 @@
757 "side-by-side diff display",
758 @ font-family: monospace;
759 @ white-space: pre;
760 },
761 { "div.udiff",
762 "context diff display",
763 @ font-family: monospace;
764 @ white-space: pre;
765 },
766 { "span.diffchng",
767 "changes in a diff",
768 @ background-color: #ffffc8;
769 },
770 { "span.diffadd",
771 "added code in a diff",
772 @ background-color: #e0ffe0;
773 },
774 { "span.diffrm",
775 "deleted in a diff",
776 @ background-color: #ffe0e0;
777 },
778 { "span.diffhr",
779 "suppressed lines in a diff",
780 @ color: #0000ff;
781 },
782

Keyboard Shortcuts

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