Skip to content

Commit 6629ad9

Browse files
committed
output styling improvements and cleanup
1 parent fe5b000 commit 6629ad9

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

‎index.js‎

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ module.exports = {
6161
let left = [{ line: '', number: ++leftLineNumber }]
6262
let right = [{ line: '', number: ++rightLineNumber }]
6363

64-
const splitLines = str => str.match(/\n([^\n]*)/g)
64+
const splitLines = str => str.match(/\n([^\n]*)/g) || []
6565

6666
diff[diffType](one, two).forEach(e => {
6767
if (!e.added && !e.removed) {
@@ -94,15 +94,6 @@ module.exports = {
9494
left.push({ line, number: ++leftLineNumber })
9595
}
9696

97-
/* if(numBreaks(e.value) > 0) {
98-
for(let line of e.value.split('\n')) {
99-
left.push({ line: chalk.bold.red(line), number: ++leftLineNumber })
100-
}
101-
}
102-
else {
103-
let lastLine = left[left.length-1]
104-
lastLine.line = (lastLine.line ? lastLine.line : '') + e.value
105-
}*/
10697
}
10798
if (e.added) {
10899
for(let chars of e.value.match(/^([^\n]*)/g)) {
@@ -126,15 +117,31 @@ module.exports = {
126117
let widestLineNumberRight = String(rightLineNumber).length
127118
let backgrounds = [
128119
chalk.bgRgb(25, 25, 25),
129-
chalk.bgRgb(30, 30, 30)
120+
chalk.bgRgb(30, 30, 30),
121+
chalk.bgRgb(15, 15, 15),
130122
]
131123

132124
for (let i = 0; i < left.length; i++) {
133-
output += backgrounds[i%2](
134-
fit((left[i].number && left[i].number > 0 ? chalk.grey(leftPad(String(left[i].number), widestLineNumberLeft)) : '') + ' ' + (left[i].line ? left[i].line : ''), colwidth) +
135-
` ` +
136-
fit((right[i].number && right[i].number > 0 ? chalk.grey(leftPad(String(right[i].number), widestLineNumberRight)) : '') + ' ' + (right[i].line ? right[i].line : ''), colwidth) + `\n`
137-
)
125+
let leftPadding = !(left[i].number && left[i].number > 0)
126+
let rightPadding = !(right[i].number && right[i].number > 0)
127+
128+
let leftString, rightString
129+
130+
if(leftPadding) {
131+
leftString = backgrounds[2](fit(left[i].line ? left[i].line : '', colwidth))
132+
} else {
133+
let leftLineNumber = chalk.grey(leftPad(String(left[i].number), widestLineNumberLeft))
134+
leftString = backgrounds[i%2](fit(leftLineNumber + ' ' + left[i].line, colwidth))
135+
}
136+
137+
if(rightPadding) {
138+
rightString = backgrounds[2](fit(right[i].line ? right[i].line : '', colwidth))
139+
} else {
140+
let rightLineNumber = chalk.grey(leftPad(String(right[i].number), widestLineNumberRight))
141+
rightString = backgrounds[i%2](fit(rightLineNumber + ' ' + right[i].line, colwidth))
142+
}
143+
144+
output += leftString + ' ' + rightString + '\n'
138145
}
139146

140147
return output

0 commit comments

Comments
 (0)