Comparing version 1.2.0 to 1.3.0
# disparity changelog | ||
## v1.3.0 (2015/04/01) | ||
- add support for custom colors. | ||
- update unified color scheme. | ||
- simplify line splitting logic. | ||
- improve way strings are colorized (avoids `\n` and `\r` chars). | ||
## v1.2.0 (2015/04/01) | ||
@@ -4,0 +11,0 @@ |
@@ -5,3 +5,2 @@ 'use strict'; | ||
var ansi = require('ansi-styles'); | ||
var hasAnsi = require('has-ansi'); | ||
@@ -15,2 +14,10 @@ // --- | ||
exports.added = 'added'; | ||
exports.colors = { | ||
charsRemoved: ansi.bgRed, | ||
charsAdded: ansi.bgGreen, | ||
removed: ansi.red, | ||
added: ansi.green, | ||
header: ansi.yellow, | ||
section: ansi.magenta, | ||
}; | ||
@@ -35,3 +42,4 @@ // --- | ||
if (header == null) { | ||
header = bgRed(exports.removed) + ' ' + bgGreen(exports.added) + '\n\n'; | ||
header = colorize(exports.removed, 'charsRemoved') + ' ' + | ||
colorize(exports.added, 'charsAdded') + '\n\n'; | ||
} | ||
@@ -42,15 +50,10 @@ | ||
var val = replaceInvisibleChars(c.value); | ||
if (c.added) return bgGreen(val); | ||
if (c.removed) return bgRed(val); | ||
if (c.added) return colorize(val, 'charsAdded'); | ||
if (c.removed) return colorize(val, 'charsRemoved'); | ||
return val; | ||
}).join(''); | ||
var endsWithLineBreak = (/\n$/m).test(diff); | ||
var lines = diff.split('\n'); | ||
// this RegExp will include the '\n' char into the lines, easier to join() | ||
var lines = diff.split(/^/m); | ||
// if it ends with line break it would add an extra empty line at the end | ||
if (endsWithLineBreak) { | ||
lines.pop(); | ||
} | ||
// add line numbers | ||
@@ -64,34 +67,11 @@ var nChars = lines.length.toString().length; | ||
var eof = endsWithLineBreak ? '\n' : ''; | ||
return header + lines.join('\n') + eof; | ||
return header + lines.join(''); | ||
} | ||
function bgGreen(str) { | ||
return colorize(str, ansi.bgGreen); | ||
function colorize(str, colorId) { | ||
var color = exports.colors[colorId]; | ||
// avoid highlighting the "\n" (would highlight till the end of the line) | ||
return str.replace(/[^\n\r]+/g, color.open + '$&' + color.close); | ||
} | ||
function bgRed(str) { | ||
return colorize(str, ansi.bgRed); | ||
} | ||
function green(str) { | ||
return colorize(str, ansi.green); | ||
} | ||
function red(str) { | ||
return colorize(str, ansi.red); | ||
} | ||
function yellow(str) { | ||
return colorize(str, ansi.yellow); | ||
} | ||
function colorize(str, color) { | ||
// we need to split the lines to avoid highlighting the "\n" (would highlight | ||
// till the end of the line) | ||
return str.split('\n').map(function(s) { | ||
return color.open + s + color.close; | ||
}).join('\n'); | ||
} | ||
function replaceInvisibleChars(str) { | ||
@@ -113,3 +93,3 @@ return str | ||
function hasDiff(line, i) { | ||
if (diffMap[i] || hasAnsi(line)) { | ||
if (diffMap[i] || hasCharDiff(line)) { | ||
diffMap[i] = true; | ||
@@ -146,2 +126,11 @@ return true; | ||
function hasCharDiff(line) { | ||
return line.indexOf(exports.colors.charsAdded.open) !== -1 || | ||
line.indexOf(exports.colors.charsRemoved.open) !== -1; | ||
} | ||
function escapeRegExp(str) { | ||
return str.replace(/\W/g,'\\$&'); | ||
} | ||
function unified(oldStr, newStr, filePathOld, filePathNew) { | ||
@@ -152,8 +141,14 @@ if (newStr === oldStr) { | ||
var changes = unifiedNoColor(oldStr, newStr, filePathOld, filePathNew) | ||
.replace(/^\-.*/gm, red('$&')) | ||
.replace(/^\+.*/gm, green('$&')) | ||
.replace(/^@@.+/gm, yellow('$&')); | ||
var changes = unifiedNoColor(oldStr, newStr, filePathOld, filePathNew); | ||
// this RegExp will include all the `\n` chars into the lines, easier to join | ||
var lines = changes.split(/^/m); | ||
return changes; | ||
// we avoid colorizing the line breaks | ||
var start = colorize(lines.slice(0, 2).join(''), 'header'); | ||
var end = lines.slice(2).join('') | ||
.replace(/^\-.*/gm, colorize('$&', 'removed')) | ||
.replace(/^\+.*/gm, colorize('$&', 'added')) | ||
.replace(/^@@.+@@/gm, colorize('$&', 'section')); | ||
return start + end; | ||
} | ||
@@ -160,0 +155,0 @@ |
{ | ||
"name": "disparity", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Colorized string diff ideal for text/code that spans through multiple lines", | ||
@@ -34,4 +34,3 @@ "main": "disparity.js", | ||
"ansi-styles": "^2.0.1", | ||
"diff": "^1.3.2", | ||
"has-ansi": "^1.0.3" | ||
"diff": "^1.3.2" | ||
}, | ||
@@ -38,0 +37,0 @@ "jshintConfig": { |
@@ -100,2 +100,23 @@ # disparity | ||
### colors:Object | ||
Object containing references to all the colors used by disparity. | ||
If you want a different output than `ansi` you can replace the color values: | ||
```js | ||
// wrap blocks into custom tags | ||
disparity.colors = { | ||
// chars diff | ||
charsRemoved: { open: '<bggreen>', close: '</bggreen>' }, | ||
charsAdded: { open: '<bgred>', close: '</bgred>' }, | ||
// unified diff | ||
removed: { open: '<red>', close: '</red>' }, | ||
added: { open: '<green>', close: '</green>' }, | ||
header: { open: '<yellow>', close: '</yellow>' }, | ||
section: { open: '<magenta>', close: '</magenta>' } | ||
}; | ||
``` | ||
## CLI | ||
@@ -102,0 +123,0 @@ |
@@ -7,3 +7,3 @@ [41mremoved[49m [42madded[49m | ||
4 | function dolor(){[41m<LF>[49m | ||
5 | [41m[49m [41m [49mamet()[41m;[49m<LF> | ||
5 | [41m [49mamet()[41m;[49m<LF> | ||
6 | }<LF> | ||
@@ -10,0 +10,0 @@ 7 | <LF> |
@@ -17,8 +17,8 @@ 'use strict'; | ||
// not using assert because it is easier to understand what is wrong | ||
console.error('disparity.' + name + '() failure!'); | ||
console.error('=== expected result:'); | ||
console.error(expected); | ||
console.error('=== actual result:'); | ||
console.error(diff); | ||
process.exit(1); | ||
process.stderr.write('disparity.' + name + '() failure!\n'); | ||
process.stderr.write('=== expected result:\n'); | ||
process.stderr.write(expected); | ||
process.stderr.write('=== actual result:\n'); | ||
process.stderr.write(diff); | ||
throw new Error('assertion error'); | ||
} | ||
@@ -59,2 +59,29 @@ } | ||
// custom colors | ||
// ============= | ||
var _oldColors = disparity.colors; | ||
// wrap blocks into custom tags | ||
disparity.colors = { | ||
// chars diff | ||
charsRemoved: { open: '<bggreen>', close: '</bggreen>' }, | ||
charsAdded: { open: '<bgred>', close: '</bgred>' }, | ||
// unified diff | ||
removed: { open: '<red>', close: '</red>' }, | ||
added: { open: '<green>', close: '</green>' }, | ||
header: { open: '<yellow>', close: '</yellow>' }, | ||
section: { open: '<magenta>', close: '</magenta>' } | ||
}; | ||
diff = disparity.chars(file1, file2); | ||
expected = readFile('chars.html'); | ||
compare(diff, expected, 'chars.html'); | ||
diff = disparity.unified(file1, file2, 'test/file1.js', 'test/file2.js'); | ||
expected = readFile('unified.html'); | ||
compare(diff, expected, 'unified.html'); | ||
disparity.colors = _oldColors; | ||
// cli.parse | ||
@@ -61,0 +88,0 @@ // ========= |
@@ -1,4 +0,4 @@ | ||
[31m--- test/file1.js removed[39m | ||
[32m+++ test/file2.js added[39m | ||
[33m@@ -1,9 +1,8 @@[39m | ||
[33m--- test/file1.js removed[39m | ||
[33m+++ test/file2.js added[39m | ||
[35m@@ -1,9 +1,8 @@[39m | ||
var foo = "bar"; | ||
@@ -14,3 +14,3 @@ | ||
var a = function() { | ||
[33m@@ -15,7 +14,7 @@[39m | ||
[35m@@ -15,7 +14,7 @@[39m | ||
thisWontShowOnDiff(); | ||
@@ -17,0 +17,0 @@ thisShouldBeFirstLineOfLine19Diff(); |
@@ -1,4 +0,4 @@ | ||
[31m--- removed[39m | ||
[32m+++ added[39m | ||
[33m@@ -1,9 +1,8 @@[39m | ||
[33m--- removed[39m | ||
[33m+++ added[39m | ||
[35m@@ -1,9 +1,8 @@[39m | ||
var foo = "bar"; | ||
@@ -14,3 +14,3 @@ | ||
var a = function() { | ||
[33m@@ -15,7 +14,7 @@[39m | ||
[35m@@ -15,7 +14,7 @@[39m | ||
thisWontShowOnDiff(); | ||
@@ -17,0 +17,0 @@ thisShouldBeFirstLineOfLine19Diff(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
111610
2
20
396
142
- Removedhas-ansi@^1.0.3
- Removedansi-regex@1.1.1(transitive)
- Removedget-stdin@4.0.1(transitive)
- Removedhas-ansi@1.0.3(transitive)