Skip to content

Commit cfbe267

Browse files
committed
Merge pull request #9 from rtfpessoa/improve-code-style
Fix some JS style issues and update CSS
2 parents 307cf0c + cf484ea commit cfbe267

File tree

4 files changed

+131
-28
lines changed

4 files changed

+131
-28
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Diff to Html CLI by [rtfpessoa](https://github.com/rtfpessoa)
22

3+
[![Codacy Badge](https://api.codacy.com/project/badge/grade/e6139937d72f40ed8b3920d53c74298a)](https://www.codacy.com/app/Codacy/diff2html-cli)
4+
35
Diff to Html generates pretty HTML diffs from git diff output in your terminal
46

57
## Features

dist/diff2html.css

Lines changed: 95 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,31 @@
3333
}
3434

3535
.d2h-lines-added {
36+
text-align: right;
37+
}
38+
39+
.d2h-lines-added > * {
3640
background-color: #ceffce;
3741
border: 1px solid #b4e2b4;
3842
color: #399839;
3943
border-radius: 5px 0 0 5px;
4044
padding: 2px;
41-
width: 25px;
4245
}
4346

4447
.d2h-lines-deleted {
48+
text-align: left;
49+
}
50+
51+
.d2h-lines-deleted > * {
4552
background-color: #f7c8c8;
4653
border: 1px solid #e9aeae;
4754
color: #c33;
4855
border-radius: 0 5px 5px 0;
4956
padding: 2px;
50-
width: 25px;
5157
}
5258

5359
.d2h-file-name {
5460
display: inline;
55-
height: 33px;
5661
line-height: 33px;
5762
max-width: 80%;
5863
white-space: nowrap;
@@ -144,28 +149,30 @@
144149
}
145150

146151
.line-num1 {
147-
display: inline-block;
152+
box-sizing: border-box;
148153
float: left;
149-
width: 30px;
154+
width: 32px;
150155
overflow: hidden;
151156
text-overflow: ellipsis;
157+
padding-left: 3px;
152158
}
153159

154160
.line-num2 {
155-
display: inline-block;
161+
box-sizing: border-box;
156162
float: right;
157-
width: 30px;
163+
width: 32px;
158164
overflow: hidden;
159165
text-overflow: ellipsis;
166+
padding-left: 3px;
160167
}
161168

162169
.d2h-code-linenumber {
170+
box-sizing: border-box;
163171
position: absolute;
164-
width: 2%;
165-
min-width: 65px;
166-
padding-left: 10px;
167-
padding-right: 10px;
172+
width: 82px;
168173
height: 18px;
174+
padding-left: 2px;
175+
padding-right: 2px;
169176
line-height: 18px;
170177
background-color: #fff;
171178
color: rgba(0, 0, 0, 0.3);
@@ -176,8 +183,9 @@
176183
}
177184

178185
.d2h-code-side-linenumber {
186+
box-sizing: border-box;
179187
position: absolute;
180-
width: 35px;
188+
width: 52px;
181189
padding-left: 10px;
182190
padding-right: 10px;
183191
height: 18px;
@@ -207,3 +215,78 @@
207215
color: rgba(0, 0, 0, 0.3);
208216
border-color: #d5e4f2;
209217
}
218+
219+
.d2h-file-list-wrapper {
220+
margin-bottom: 10px;
221+
padding: 0 10px;
222+
}
223+
224+
.d2h-file-list-wrapper a {
225+
text-decoration: none;
226+
color: #3572b0;
227+
}
228+
229+
.d2h-file-list-wrapper a:visited {
230+
color: #3572b0;
231+
}
232+
233+
.d2h-file-list-header {
234+
font-weight: bold;
235+
float: left;
236+
}
237+
238+
.d2h-file-list-line {
239+
text-align: left;
240+
font: 13px Helvetica, arial, freesans, clean, sans-serif, "Segoe UI Emoji", "Segoe UI Symbol";
241+
}
242+
243+
.d2h-file-list-line .d2h-file-name {
244+
line-height: 21px;
245+
}
246+
247+
.d2h-file-list {
248+
display: none;
249+
}
250+
251+
.d2h-clear {
252+
display: block;
253+
clear: both;
254+
}
255+
256+
.d2h-del.d2h-change, .d2h-ins.d2h-change {
257+
background-color: #ffc;
258+
}
259+
260+
ins.d2h-change, del.d2h-change {
261+
background-color: #fad771;
262+
}
263+
264+
.d2h-file-diff .d2h-del.d2h-change {
265+
background-color: #fae1af;
266+
}
267+
268+
.d2h-file-diff .d2h-ins.d2h-change {
269+
background-color: #ded;
270+
}
271+
272+
/* CSS only show/hide */
273+
.d2h-show {
274+
display: none;
275+
float: left;
276+
}
277+
278+
.d2h-hide {
279+
float: left;
280+
}
281+
282+
.d2h-hide:target + .d2h-show {
283+
display: inline;
284+
}
285+
286+
.d2h-hide:target {
287+
display: none;
288+
}
289+
290+
.d2h-hide:target ~ .d2h-file-list {
291+
display: block;
292+
}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "diff2html-cli",
3-
"version": "1.2.1",
3+
"version": "1.3.0",
44
"homepage": "https://www.github.com/rtfpessoa/diff2html-cli",
55
"description": "Fast Diff to colorized HTML",
66
"keywords": [
@@ -43,9 +43,9 @@
4343
},
4444
"main": "./src/main.js",
4545
"dependencies": {
46-
"yargs": "3.27.*",
46+
"yargs": "3.31.*",
4747
"extend": "3.0.*",
48-
"request": "2.65.*",
48+
"request": "2.67.*",
4949
"diff2html": "*"
5050
},
5151
"devDependencies": {

src/main.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env node
2+
23
/*
34
*
45
* Diff to HTML CLI (main.js)
@@ -115,7 +116,7 @@ function runGitDiff(callback) {
115116
if (argv._.length && argv._[0]) {
116117
gitArgs = argv._.join(' ');
117118
} else {
118-
gitArgs = '-M HEAD~1'
119+
gitArgs = '-M HEAD~1';
119120
}
120121

121122
var diffCommand = 'git diff ' + gitArgs;
@@ -135,10 +136,11 @@ function getOutput(input) {
135136
htmlContent = diff2Html.getPrettyHtmlFromDiff(input, config);
136137
}
137138
return prepareHTML(htmlContent);
138-
} else {
139-
var jsonContent = diff2Html.getJsonFromDiff(input, config);
140-
return prepareJSON(jsonContent);
141139
}
140+
141+
var jsonContent = diff2Html.getJsonFromDiff(input, config);
142+
return prepareJSON(jsonContent);
143+
142144
}
143145

144146
function preview(content) {
@@ -148,10 +150,16 @@ function preview(content) {
148150
}
149151

150152
function prepareHTML(content) {
151-
var template = readFileSync(__dirname + '/../dist/template.html', 'utf8');
152-
var css = readFileSync(__dirname + '/../dist/diff2html.css', 'utf8');
153+
var template = readFileSync(__dirname + '/../dist/template.html');
154+
155+
var cssFile = __dirname + '/../node_modules/diff2html/css/diff2html.css';
156+
var cssFallbackFile = __dirname + '/../dist/diff2html.css';
157+
if (existsSync(cssFile)) cssFile = cssFallbackFile;
158+
159+
var cssContent = readFileSync(cssFile);
160+
153161
return template
154-
.replace('<!--css-->', '<style>\n' + css + '\n</style>')
162+
.replace('<!--css-->', '<style>\n' + cssContent + '\n</style>')
155163
.replace('<!--diff-->', content);
156164
}
157165

@@ -164,8 +172,8 @@ function postToDiffy(diff, postType) {
164172
return;
165173
}
166174

167-
if (response.status != 'error') {
168-
print("Link powered by diffy.org:");
175+
if (response.status !== 'error') {
176+
print('Link powered by diffy.org:');
169177
print(response.url);
170178

171179
if (postType === 'browser') {
@@ -174,7 +182,7 @@ function postToDiffy(diff, postType) {
174182
runCmd('echo "' + response.url + '" | pbcopy');
175183
}
176184
} else {
177-
print("Error: " + message);
185+
print('Error: ' + message);
178186
}
179187
});
180188
}
@@ -188,11 +196,11 @@ function post(url, payload, callback) {
188196
.on('response', function(response) {
189197
response.on('data', function(body) {
190198
try {
191-
callback(null, JSON.parse(body.toString('utf8')));
199+
return callback(null, JSON.parse(body.toString('utf8')));
192200
} catch (err) {
193-
callback(new Error('could not parse response'));
201+
return callback(new Error('could not parse response'));
194202
}
195-
})
203+
});
196204
})
197205
.on('error', function(err) {
198206
callback(err);
@@ -211,6 +219,16 @@ function error(msg) {
211219
console.error(msg);
212220
}
213221

222+
function existsSync(filePath) {
223+
try {
224+
fs.existsSync(filePath);
225+
} catch (ignore) {
226+
return false;
227+
}
228+
229+
return true;
230+
}
231+
214232
function readFileSync(filePath) {
215233
return fs.readFileSync(filePath, 'utf8');
216234
}

0 commit comments

Comments
 (0)