Skip to content

Commit

Permalink
fixed color for markdown reports
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Jul 3, 2024
1 parent fff3c17 commit daf0f36
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 154 deletions.
130 changes: 41 additions & 89 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,14 @@ const getReportGroup = (reports, lcov, dataType) => {

// ========================================================================================================

const pFormatter = (v, row, column, markdown) => {
if (typeof v === 'number') {
return Util.getColorStrByStatus(Util.PSF(v, 100, 2), row.status, markdown);
}
return v;
};

const nFormatter = (v) => {
if (typeof v === 'number') {
return Util.NF(v);
}
return v;
};

const getRowData = (rowName, summary, metrics, markdown) => {
const getRowData = (rowName, summary, metrics, color) => {
const summaryRow = {};
let lowest = {
pct: 100,
Expand All @@ -161,7 +154,7 @@ const getRowData = (rowName, summary, metrics, markdown) => {
if (typeof percent !== 'number') {
return;
}
summaryRow[k] = Util.getColorStrByStatus(Util.PSF(percent, 100, 2), s.status, markdown);
summaryRow[k] = Util.getColorStrByStatus(Util.PSF(percent, 100, 2), s.status, color);
if (percent < lowest.pct) {
lowest = s;
}
Expand All @@ -171,73 +164,7 @@ const getRowData = (rowName, summary, metrics, markdown) => {
return summaryRow;
};

const getUncoveredLines = (file, markdown) => {
const lines = [];

const dataLines = file.data.lines;

let startLine;
let endLine;

const addLines = () => {
if (!startLine) {
return;
}
if (endLine) {
// range
if (markdown) {
lines.push(`${startLine.line}-${endLine.line}`);
} else {
const link = startLine.color === 'yellow' && endLine.color === 'yellow' ? EC.yellow('-') : EC.red('-');
lines.push(EC[startLine.color](startLine.line) + link + EC[endLine.color](endLine.line));
}
startLine = null;
endLine = null;
} else {
// only start
if (markdown) {
lines.push(startLine.line);
} else {
lines.push(EC[startLine.color](startLine.line));
}
startLine = null;
}
};

const setLines = (line, color) => {
if (startLine) {
endLine = {
line,
color
};
return;
}
startLine = {
line,
color
};
};

Object.keys(dataLines).forEach((line) => {
const count = dataLines[line];
if (count === 0) {
setLines(line, 'red');
return;
}
// 0 < count < 1
if (typeof count === 'string') {
setLines(line, 'yellow');
return;
}
// count >= 1
addLines();
});
addLines();

return lines.join(',');
};

const getDetailsRows = (files, metrics, cdOptions) => {
const getDetailsRows = (files, metrics, cdOptions, color) => {

const skipPercent = cdOptions.skipPercent;
if (typeof skipPercent === 'number' && skipPercent > 0) {
Expand All @@ -262,8 +189,8 @@ const getDetailsRows = (files, metrics, cdOptions) => {
}

const { sourcePath, summary } = file;
const fileRow = getRowData(sourcePath, summary, metrics);
fileRow.uncoveredLines = getUncoveredLines(file);
const fileRow = getRowData(sourcePath, summary, metrics, color);
fileRow.uncoveredLines = Util.getUncoveredLines(file, color);
flatRows.push(fileRow);
});

Expand Down Expand Up @@ -344,11 +271,13 @@ const handleConsoleDetailsReport = (reportData, reportOptions, options) => {
Util.logInfo(EC.cyan(name));
}

const color = 'ansicode';

const metrics = Util.getMetrics(cdOptions.metrics, type);
const rows = getDetailsRows(files, metrics, cdOptions);
const summaryRow = getRowData('Summary', summary, metrics);
const rows = getDetailsRows(files, metrics, cdOptions, color);
const summaryRow = getRowData('Summary', summary, metrics, color);
if (summaryRow.nameStatus) {
summaryRow.name = Util.getColorStrByStatus(summaryRow.name, summaryRow.nameStatus);
summaryRow.name = Util.getColorStrByStatus(summaryRow.name, summaryRow.nameStatus, color);
}
// no rows if skipped all by skipPercent
if (rows.length) {
Expand Down Expand Up @@ -383,15 +312,21 @@ const handleConsoleDetailsReport = (reportData, reportOptions, options) => {
});
};

const getSummaryColumns = () => {
const getSummaryColumns = (color) => {

const columns = [{
id: 'name',
name: 'Name'
}, {
id: 'pct',
name: 'Coverage %',
align: 'right',
formatter: pFormatter
formatter: (v, row, column) => {
if (typeof v === 'number') {
return Util.getColorStrByStatus(Util.PSF(v, 100, 2), row.status, color);
}
return v;
}
}, {
id: 'covered',
name: 'Covered',
Expand Down Expand Up @@ -436,7 +371,7 @@ const handleConsoleSummaryReport = (reportData, reportOptions, options) => {
};
});

const columns = getSummaryColumns();
const columns = getSummaryColumns('ansicode');

CG({
columns,
Expand All @@ -446,6 +381,9 @@ const handleConsoleSummaryReport = (reportData, reportOptions, options) => {

const handleMarkdownDetailsReport = async (reportData, reportOptions, options) => {
const mdOptions = {
baseUrl: '',
// unicode, html, or empty for no color
color: 'unicode',
skipPercent: 0,
metrics: [],
outputFile: 'coverage-details.md',
Expand All @@ -471,7 +409,8 @@ const handleMarkdownDetailsReport = async (reportData, reportOptions, options) =
});
}

const markdown = true;
const color = mdOptions.color;
const baseUrl = mdOptions.baseUrl;

const rows = [];
files.forEach((file) => {
Expand All @@ -482,12 +421,23 @@ const handleMarkdownDetailsReport = async (reportData, reportOptions, options) =
}

const { sourcePath, summary } = file;
const fileRow = getRowData(sourcePath, summary, metrics, markdown);
fileRow.uncoveredLines = getUncoveredLines(file, markdown);

const fileRow = getRowData(sourcePath, summary, metrics, color);
if (fileRow.nameStatus) {
fileRow.name = Util.getColorStrByStatus(fileRow.name, fileRow.nameStatus, color);
}
if (baseUrl) {
fileRow.name = `[${fileRow.name}](${baseUrl + sourcePath})`;
}

fileRow.uncoveredLines = Util.getUncoveredLines(file, color);
rows.push(fileRow);
});

const summaryRow = getRowData('Summary', reportData.summary, metrics, markdown);
const summaryRow = getRowData('Summary', reportData.summary, metrics, color);
if (summaryRow.nameStatus) {
summaryRow.name = Util.getColorStrByStatus(summaryRow.name, summaryRow.nameStatus, color);
}
rows.push(summaryRow);

const columns = [{
Expand Down Expand Up @@ -518,6 +468,8 @@ const handleMarkdownDetailsReport = async (reportData, reportOptions, options) =

const handleMarkdownSummaryReport = async (reportData, reportOptions, options) => {
const msOptions = {
// unicode, html, or empty for no color
color: 'unicode',
metrics: [],
outputFile: 'coverage-summary.md',
... reportOptions
Expand All @@ -534,7 +486,7 @@ const handleMarkdownSummaryReport = async (reportData, reportOptions, options) =
};
});

const columns = getSummaryColumns();
const columns = getSummaryColumns(msOptions.color);

const markdownGrid = generateMarkdownGrid({
options: {
Expand Down
5 changes: 2 additions & 3 deletions lib/utils/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const generateMarkdownGrid = (data) => {

const options = {
name: '',
padding: 0,
padding: 1,
nullPlaceholder: '',
... data.options
};
Expand Down Expand Up @@ -115,8 +115,7 @@ const generateMarkdownGrid = (data) => {
v = options.nullPlaceholder;
}
if (column.formatter) {
const markdown = true;
v = column.formatter(v, row, column, markdown);
v = column.formatter(v, row, column);
}
cellValue += v;
}
Expand Down
54 changes: 2 additions & 52 deletions lib/utils/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,6 @@ const EC = require('eight-colors');

const Util = require('./util.js');

const getUncoveredLines = (file) => {
const lines = [];

const dataLines = file.data.lines;

let startLine;
let endLine;

const addLines = () => {
if (!startLine) {
return;
}
if (endLine) {
// range
lines.push(`${startLine.line}-${endLine.line}`);
startLine = null;
endLine = null;
} else {
// only start
lines.push(startLine.line);
startLine = null;
}
};

const setLines = (line) => {
if (startLine) {
endLine = {
line
};
return;
}
startLine = {
line
};
};

Object.keys(dataLines).forEach((line) => {
const count = dataLines[line];
if (count === 0 || typeof count === 'string') {
setLines(line);
return;
}
// count >= 1
addLines();
});
addLines();

return lines.join(',');
};

const mergeSingleSubGroups = (item) => {

if (!item.subs) {
Expand Down Expand Up @@ -97,7 +47,7 @@ const getGroupedRows = (flatRows) => {

let filename = lastName;
if (file.nameStatus) {
filename = Util.getColorStrByStatus(lastName, file.nameStatus);
filename = Util.getColorStrByStatus(lastName, file.nameStatus, 'ansicode');
}
file.name = filename;
subs.push(file);
Expand Down Expand Up @@ -257,7 +207,7 @@ const getSnapshot = (reportData) => {
}
const fileSummary = {};
addPercent(fileSummary, file.summary);
fileSummary.uncoveredLines = getUncoveredLines(file);
fileSummary.uncoveredLines = Util.getUncoveredLines(file);
snapshot.files[file.sourcePath] = fileSummary;
});

Expand Down
Loading

0 comments on commit daf0f36

Please sign in to comment.