Skip to content

Commit

Permalink
feat: add comments lines #3
Browse files Browse the repository at this point in the history
  • Loading branch information
hua1995116 committed May 4, 2020
1 parent 888f5b8 commit ccf8207
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 24 deletions.
34 changes: 24 additions & 10 deletions lib/linec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var colors = require('colors');
var logSymbols = require('log-symbols');
var langColors = require('./static/colors');
var langExt = require('./static/ext');
var commentParser = require('common-comment-parser').default;
var ignoreFilter = require('./utils/ignore');
var ROOTPATH = process.cwd();
var START_TIME = new Date();
Expand All @@ -22,9 +23,9 @@ var FILE_COUNT = 0;
var filterCustom = [];
var suffixList = [];

var tableColWidths = [15, 10, 10, 10];
var tableColAligns = ['left', 'right', 'right', 'right'];
var tableHead = ['language', 'files', 'blank', 'code'];
var tableColWidths = [15, 10, 10, 10, 10];
var tableColAligns = ['left', 'right', 'right', 'right', 'right'];
var tableHead = ['language', 'files', 'blank', 'comments', 'code'];
var tableStyle = {
'padding-left': 0,
'padding-right': 0,
Expand Down Expand Up @@ -192,6 +193,8 @@ function hanldeTable(langInfo) {
var totalFiles = 0;
var totalBlank = 0;
var totalCode = 0;
var totalComments = 0;
console.log(langInfo);
Object.keys(langInfo).map(function (item, index) {
if (maxCount < langInfo[item].totalLines) {
maxCount = langInfo[item].totalLines;
Expand All @@ -201,18 +204,20 @@ function hanldeTable(langInfo) {
totalFiles += langInfo[item].file;
totalBlank += langInfo[item].blankLines;
totalCode += langInfo[item].totalLines;
tablesContent.push([item, langInfo[item].file, langInfo[item].blankLines, langInfo[item].totalLines]);
totalComments += langInfo[item].commentLines;
tablesContent.push([item, langInfo[item].file, langInfo[item].blankLines, langInfo[item].commentLines, langInfo[item].totalLines]);
});
var maxLine = tablesContent[maxIndex];
if (maxLine) {
var colorMax = [maxLine[0].yellow, ('' + maxLine[1]).yellow, ('' + maxLine[2]).yellow, ('' + maxLine[3]).yellow];
var colorMax = [maxLine[0].yellow, ('' + maxLine[1]).yellow, ('' + maxLine[2]).yellow, ('' + maxLine[3]).yellow, ('' + maxLine[4]).yellow];
tablesContent.splice(maxIndex, 1, colorMax);
}

return {
totalFiles: totalFiles,
totalCode: totalCode,
totalBlank: totalBlank,
totalComments: totalComments,
tablesContent: tablesContent,
maxName: maxName
};
Expand All @@ -230,10 +235,11 @@ function outputTbale(totalData) {
var totalFiles = totalData.totalFiles,
totalCode = totalData.totalCode,
totalBlank = totalData.totalBlank,
totalComments = totalData.totalComments,
tablesContent = totalData.tablesContent;

content.push.apply(content, _toConsumableArray(tablesContent));
bottom.push(['SUM'.cyan, ('' + totalFiles).cyan, ('' + totalBlank).cyan, ('' + totalCode).cyan]);
bottom.push(['SUM'.cyan, ('' + totalFiles).cyan, ('' + totalBlank).cyan, ('' + totalComments).cyan, ('' + totalCode).cyan]);

var _handleSpeed = handleSpeed(totalFiles),
totalTime = _handleSpeed.totalTime,
Expand Down Expand Up @@ -292,6 +298,7 @@ function getFile(dirPath, langInfo) {
file: 1,
blankLines: 0,
totalLines: 0,
commentLines: 0,
color: langColors[languageName] || '#fff'
};
}
Expand All @@ -312,13 +319,19 @@ function fileCount(name, currentPath, langInfo) {
if (FILE_COUNT % 100 === 0) {
progress.setValue(FILE_COUNT);
}
var lines = fs.readFileSync(currentPath, 'utf-8').split('\n');
var context = fs.readFileSync(currentPath, 'utf-8');
var ext = path.extname(currentPath).replace('.', '');
var lines = context.split(/(\n|\r)/);
var rmLines = lines.filter(function (item) {
return item.trim() !== '' && item.trim() !== '\r';
});
var commentLines = commentParser(context, ext).reduce(function (prev, current) {
return prev + current.commentLine;
}, 0);
var blankLines = lines.length - rmLines.length;
langInfo[name].totalLines += lines.length;
langInfo[name].blankLines += blankLines;
langInfo[name].commentLines += commentLines;
}

/**
Expand All @@ -338,6 +351,7 @@ function outputHtml(fileData, totalData) {
var totalFiles = totalData.totalFiles,
totalCode = totalData.totalCode,
totalBlank = totalData.totalBlank,
totalComments = totalData.totalComments,
maxName = totalData.maxName;

var _handleSpeed2 = handleSpeed(totalFiles),
Expand All @@ -346,7 +360,7 @@ function outputHtml(fileData, totalData) {
lineSpeed = _handleSpeed2.lineSpeed;

var speed = '<div class="speed">T=' + totalTime + ' s, (' + fileSpeed + ' files/s, ' + lineSpeed + ' lines/s)</div>';
var header = '<div class="header"><div class="col col-4">language</div><div class="col col-2">files</div><div class="col col-2">blank</div><div class="col col-2">code</div></div>';
var header = '<div class="header"><div class="col col-2">language</div><div class="col col-2">files</div><div class="col col-2">blank</div><div class="col col-2">comments</div><div class="col col-2">code</div></div>';
var body = '<div class="body">';

Object.keys(fileData).map(function (item) {
Expand All @@ -357,12 +371,12 @@ function outputHtml(fileData, totalData) {
} else {
tr += '<div class="tr" style="color: ' + languageItem.color + '">';
}
tr += '<div class="col col-4"">' + item + '</div><div class="col col-2">' + languageItem.file + '</div><div class="col col-2">' + languageItem.blankLines + '</div><div class="col col-2">' + languageItem.totalLines + '</div></div>';
tr += '<div class="col col-2"">' + item + '</div><div class="col col-2">' + languageItem.file + '</div><div class="col col-2">' + languageItem.blankLines + '</div><div class="col col-2">' + languageItem.commentLines + '</div><div class="col col-2">' + languageItem.totalLines + '</div></div>';
body += tr;
});
body += '</div>';

var bottom = '<div class="bottom"><div class="col col-4">SUM</div><div class="col col-2">' + totalFiles + '</div><div class="col col-2">' + totalBlank + '</div><div class="col col-2">' + totalCode + '</div></div>';
var bottom = '<div class="bottom"><div class="col col-2">SUM</div><div class="col col-2">' + totalFiles + '</div><div class="col col-2">' + totalBlank + '</div><div class="col col-2">' + totalComments + '</div><div class="col col-2">' + totalCode + '</div></div>';
var readData = fs.readFileSync(path.join(__dirname, '../src/static/template.html')).toString();
var ouputHtml = readData.replace('$', speed + header + body + bottom);
var outputPath = process.cwd() + '/linec_output.html';
Expand Down
2 changes: 1 addition & 1 deletion linec_output.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
<body>
<div class="table">
<div class="info">by <a href="https://github.com/hua1995116/linec">https://github.com/hua1995116/linec</a> <img src="https://camo.githubusercontent.com/ae4dd2ff917d3c8668fbb4833d2b29a2700929af/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f6c696e65632e737667" data-canonical-src="https://img.shields.io/npm/v/linec.svg" style="max-width:100%;"></div>
<div class="speed">T=0.023 s, (173.9 files/s, 173.9 lines/s)</div><div class="header"><div class="col col-4">language</div><div class="col col-2">files</div><div class="col col-2">blank</div><div class="col col-2">code</div></div><div class="body"><div class="tr tr-max" style="color: #563d7c"><div class="col col-4"">CSS</div><div class="col col-2">1</div><div class="col col-2">0</div><div class="col col-2">4</div></div><div class="tr" style="color: #f1e05a"><div class="col col-4"">JavaScript</div><div class="col col-2">1</div><div class="col col-2">0</div><div class="col col-2">1</div></div><div class="tr" style="color: #fff"><div class="col col-4"">JSON</div><div class="col col-2">1</div><div class="col col-2">0</div><div class="col col-2">3</div></div><div class="tr" style="color: #fff"><div class="col col-4"">Markdown</div><div class="col col-2">1</div><div class="col col-2">0</div><div class="col col-2">1</div></div></div><div class="bottom"><div class="col col-4">SUM</div><div class="col col-2">4</div><div class="col col-2">0</div><div class="col col-2">9</div></div>
<div class="speed">T=0.025 s, (160.0 files/s, 160.0 lines/s)</div><div class="header"><div class="col col-2">language</div><div class="col col-2">files</div><div class="col col-2">blank</div><div class="col col-2">comments</div><div class="col col-2">code</div></div><div class="body"><div class="tr tr-max" style="color: #563d7c"><div class="col col-2"">CSS</div><div class="col col-2">1</div><div class="col col-2">3</div><div class="col col-2">0</div><div class="col col-2">7</div></div><div class="tr" style="color: #f1e05a"><div class="col col-2"">JavaScript</div><div class="col col-2">1</div><div class="col col-2">0</div><div class="col col-2">0</div><div class="col col-2">1</div></div><div class="tr" style="color: #fff"><div class="col col-2"">JSON</div><div class="col col-2">1</div><div class="col col-2">2</div><div class="col col-2">0</div><div class="col col-2">5</div></div><div class="tr" style="color: #fff"><div class="col col-2"">Markdown</div><div class="col col-2">1</div><div class="col col-2">0</div><div class="col col-2">0</div><div class="col col-2">1</div></div></div><div class="bottom"><div class="col col-2">SUM</div><div class="col col-2">4</div><div class="col col-2">5</div><div class="col col-2">0</div><div class="col col-2">14</div></div>
</div>
</body>
</html>
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "linec",
"version": "1.3.11",
"version": "1.4.0",
"description": "line count",
"main": "index.js",
"bin": {
Expand Down Expand Up @@ -28,6 +28,7 @@
"cli-table": "^0.3.1",
"colors": "^1.3.3",
"commander": "^2.20.0",
"common-comment-parser": "^0.1.2",
"ignore": "^4.0.3",
"log-symbols": "^2.2.0"
},
Expand Down
32 changes: 22 additions & 10 deletions src/linec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const colors = require('colors');
const logSymbols = require('log-symbols');
const langColors = require('./static/colors');
const langExt = require('./static/ext');
const commentParser = require('common-comment-parser').default;
const ignoreFilter = require('./utils/ignore');
let ROOTPATH = process.cwd();
const START_TIME = new Date();
Expand All @@ -15,9 +16,9 @@ const filterCustom = [];
const suffixList = [];


const tableColWidths = [15, 10, 10, 10];
const tableColAligns = ['left', 'right', 'right', 'right'];
const tableHead = ['language', 'files', 'blank', 'code']
const tableColWidths = [15, 10, 10, 10, 10];
const tableColAligns = ['left', 'right', 'right', 'right', 'right'];
const tableHead = ['language', 'files', 'blank', 'comments', 'code']
const tableStyle = {
'padding-left': 0,
'padding-right': 0,
Expand Down Expand Up @@ -179,6 +180,8 @@ function hanldeTable(langInfo) {
let totalFiles = 0;
let totalBlank = 0;
let totalCode = 0;
let totalComments = 0;
console.log(langInfo);
Object.keys(langInfo).map((item, index) => {
if (maxCount < langInfo[item].totalLines) {
maxCount = langInfo[item].totalLines;
Expand All @@ -188,18 +191,20 @@ function hanldeTable(langInfo) {
totalFiles += langInfo[item].file;
totalBlank += langInfo[item].blankLines;
totalCode += langInfo[item].totalLines;
tablesContent.push([item, langInfo[item].file, langInfo[item].blankLines, langInfo[item].totalLines]);
totalComments += langInfo[item].commentLines;
tablesContent.push([item, langInfo[item].file, langInfo[item].blankLines, langInfo[item].commentLines, langInfo[item].totalLines]);
})
const maxLine = tablesContent[maxIndex];
if (maxLine) {
const colorMax = [maxLine[0].yellow, `${maxLine[1]}`.yellow, `${maxLine[2]}`.yellow, `${maxLine[3]}`.yellow]
const colorMax = [maxLine[0].yellow, `${maxLine[1]}`.yellow, `${maxLine[2]}`.yellow, `${maxLine[3]}`.yellow, `${maxLine[4]}`.yellow]
tablesContent.splice(maxIndex, 1, colorMax);
}

return {
totalFiles,
totalCode,
totalBlank,
totalComments,
tablesContent,
maxName
}
Expand All @@ -219,10 +224,11 @@ function outputTbale(totalData) {
totalFiles,
totalCode,
totalBlank,
totalComments,
tablesContent
} = totalData;
content.push(...tablesContent);
bottom.push(['SUM'.cyan, `${totalFiles}`.cyan, `${totalBlank}`.cyan, `${totalCode}`.cyan]);
bottom.push(['SUM'.cyan, `${totalFiles}`.cyan, `${totalBlank}`.cyan, `${totalComments}`.cyan,`${totalCode}`.cyan]);

const {
totalTime,
Expand Down Expand Up @@ -282,6 +288,7 @@ function getFile(dirPath, langInfo) {
file: 1,
blankLines: 0,
totalLines: 0,
commentLines: 0,
color: langColors[languageName] || '#fff'
}
}
Expand All @@ -303,11 +310,15 @@ function fileCount(name, currentPath, langInfo) {
if (FILE_COUNT % 100 === 0) {
progress.setValue(FILE_COUNT);
}
const lines = fs.readFileSync(currentPath, 'utf-8').split('\n');
const context = fs.readFileSync(currentPath, 'utf-8');
const ext = path.extname(currentPath).replace('.', '');
const lines = context.split(/(\n|\r)/);
const rmLines = lines.filter(item => (item.trim() !== '' && item.trim() !== '\r'));
const commentLines = commentParser(context, ext).reduce((prev, current) => (prev + current.commentLine), 0);
const blankLines = lines.length - rmLines.length;
langInfo[name].totalLines += lines.length;
langInfo[name].blankLines += blankLines;
langInfo[name].commentLines += commentLines;
}

/**
Expand All @@ -328,6 +339,7 @@ function outputHtml(fileData, totalData) {
totalFiles,
totalCode,
totalBlank,
totalComments,
maxName
} = totalData;

Expand All @@ -337,7 +349,7 @@ function outputHtml(fileData, totalData) {
lineSpeed
} = handleSpeed(totalFiles);
const speed = `<div class="speed">T=${totalTime} s, (${fileSpeed} files/s, ${lineSpeed} lines/s)</div>`
const header = `<div class="header"><div class="col col-4">language</div><div class="col col-2">files</div><div class="col col-2">blank</div><div class="col col-2">code</div></div>`;
const header = `<div class="header"><div class="col col-2">language</div><div class="col col-2">files</div><div class="col col-2">blank</div><div class="col col-2">comments</div><div class="col col-2">code</div></div>`;
let body = `<div class="body">`;

Object.keys(fileData).map(item => {
Expand All @@ -348,12 +360,12 @@ function outputHtml(fileData, totalData) {
} else {
tr += `<div class="tr" style="color: ${languageItem.color}">`;
}
tr += `<div class="col col-4"">${item}</div><div class="col col-2">${languageItem.file}</div><div class="col col-2">${languageItem.blankLines}</div><div class="col col-2">${languageItem.totalLines}</div></div>`;
tr += `<div class="col col-2"">${item}</div><div class="col col-2">${languageItem.file}</div><div class="col col-2">${languageItem.blankLines}</div><div class="col col-2">${languageItem.commentLines}</div><div class="col col-2">${languageItem.totalLines}</div></div>`;
body += tr;
})
body += '</div>'

const bottom = `<div class="bottom"><div class="col col-4">SUM</div><div class="col col-2">${totalFiles}</div><div class="col col-2">${totalBlank}</div><div class="col col-2">${totalCode}</div></div>`;
const bottom = `<div class="bottom"><div class="col col-2">SUM</div><div class="col col-2">${totalFiles}</div><div class="col col-2">${totalBlank}</div><div class="col col-2">${totalComments}</div><div class="col col-2">${totalCode}</div></div>`;
const readData = fs.readFileSync(path.join(__dirname, '../src/static/template.html')).toString();
const ouputHtml = readData.replace('$',speed + header + body + bottom )
const outputPath = `${process.cwd()}/linec_output.html`;
Expand Down
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('linec files test', () => {

const dirObj = linec('html', url)

const expectData = '<div class="header"><div class="col col-4">language</div><div class="col col-2">files</div><div class="col col-2">blank</div><div class="col col-2">code</div></div><div class="body"><div class="tr tr-max" style="color: #563d7c"><div class="col col-4"">CSS</div><div class="col col-2">1</div><div class="col col-2">0</div><div class="col col-2">4</div></div><div class="tr" style="color: #f1e05a"><div class="col col-4"">JavaScript</div><div class="col col-2">1</div><div class="col col-2">0</div><div class="col col-2">1</div></div><div class="tr" style="color: #fff"><div class="col col-4"">JSON</div><div class="col col-2">1</div><div class="col col-2">0</div><div class="col col-2">3</div></div><div class="tr" style="color: #fff"><div class="col col-4"">Markdown</div><div class="col col-2">1</div><div class="col col-2">0</div><div class="col col-2">1</div></div></div><div class="bottom"><div class="col col-4">SUM</div><div class="col col-2">4</div><div class="col col-2">0</div><div class="col col-2">9</div></div>';
const expectData = '<div class="header"><div class="col col-2">language</div><div class="col col-2">files</div><div class="col col-2">blank</div><div class="col col-2">comments</div><div class="col col-2">code</div></div><div class="body"><div class="tr tr-max" style="color: #563d7c"><div class="col col-2"">CSS</div><div class="col col-2">1</div><div class="col col-2">3</div><div class="col col-2">0</div><div class="col col-2">7</div></div><div class="tr" style="color: #f1e05a"><div class="col col-2"">JavaScript</div><div class="col col-2">1</div><div class="col col-2">0</div><div class="col col-2">0</div><div class="col col-2">1</div></div><div class="tr" style="color: #fff"><div class="col col-2"">JSON</div><div class="col col-2">1</div><div class="col col-2">2</div><div class="col col-2">0</div><div class="col col-2">5</div></div><div class="tr" style="color: #fff"><div class="col col-2"">Markdown</div><div class="col col-2">1</div><div class="col col-2">0</div><div class="col col-2">0</div><div class="col col-2">1</div></div></div><div class="bottom"><div class="col col-2">SUM</div><div class="col col-2">4</div><div class="col col-2">5</div><div class="col col-2">0</div><div class="col col-2">14</div></div>';

assert.equal(dirObj, expectData);

Expand All @@ -20,7 +20,7 @@ describe('linec files test', () => {

const dirObj = JSON.stringify(linec('table', url));

const expectData = JSON.stringify([["\u001b[33mCSS\u001b[39m","\u001b[33m1\u001b[39m","\u001b[33m0\u001b[39m","\u001b[33m4\u001b[39m"],["JavaScript",1,0,1],["JSON",1,0,3],["Markdown",1,0,1]]);
const expectData = JSON.stringify([["\u001b[33mCSS\u001b[39m","\u001b[33m1\u001b[39m","\u001b[33m3\u001b[39m","\u001b[33m0\u001b[39m","\u001b[33m7\u001b[39m"],["JavaScript",1,0,0,1],["JSON",1,2,0,5],["Markdown",1,0,0,1]]);

assert.equal(dirObj, expectData);

Expand Down

0 comments on commit ccf8207

Please sign in to comment.