Skip to content

Commit

Permalink
feat: improve rule id to always align to right of terminal on the las…
Browse files Browse the repository at this point in the history
…t line of message
  • Loading branch information
spence-s committed Apr 9, 2024
1 parent 4a3a5dd commit 37ff7d5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
50 changes: 43 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import stringWidth from 'string-width';
import ansiEscapes from 'ansi-escapes';
import {supportsHyperlink} from 'supports-hyperlinks';
import getRuleDocs from 'eslint-rule-docs';
import terminalSize from 'terminal-size';

export default function eslintFormatterPretty(results, data) {
const lines = [];
Expand All @@ -17,6 +18,8 @@ export default function eslintFormatterPretty(results, data) {
let maxMessageWidth = 0;
let showLineNumbers = false;

const termSize = terminalSize();

for (const result of results
.sort((a, b) => {
if (a.errorCount === b.errorCount) {
Expand Down Expand Up @@ -86,7 +89,6 @@ export default function eslintFormatterPretty(results, data) {
maxColumnWidth = Math.max(columnWidth, maxColumnWidth);
maxMessageWidth = Math.max(messageWidth, maxMessageWidth);
showLineNumbers = showLineNumbers || x.line || x.column;

lines.push({
type: 'message',
severity: (x.fatal || x.severity === 2 || x.severity === 'error') ? 'error' : 'warning',
Expand Down Expand Up @@ -128,13 +130,47 @@ export default function eslintFormatterPretty(results, data) {
} catch {}
}

const preLine = '';
const severity = x.severity === 'warning' ? logSymbols.warning : logSymbols.error;
const range = ' '.repeat(maxLineWidth - x.lineWidth) + chalk.dim(x.line + chalk.gray(':') + x.column);
const message = ' '.repeat(maxColumnWidth - x.columnWidth) + x.message;

const fullMessage = [preLine, severity, range, message].join(' ');

const fullMessageWidth = 3 + stringWidth(fullMessage);
const ruleIdWidth = stringWidth(x.ruleId);
const totalWidth = fullMessageWidth + ruleIdWidth;

// If fullMessageWidth is greater than the terminal width
// then we need to ONLY use the last line of the message to determine where to place the ruleId

let gapWidth = 0;

// So first we check
const totalWidthIsLessThanTerminalWidth = totalWidth < termSize.columns;

if (totalWidthIsLessThanTerminalWidth) {
gapWidth = termSize.columns - totalWidth;
} else {
// Calculate how many times the width will wrap the terminal
const wrapTimes = Math.floor(totalWidth / termSize.columns);
const totalLastLineWidth = totalWidth - (wrapTimes * termSize.columns);
gapWidth = termSize.columns - totalLastLineWidth;
}

const rule = ruleUrl && supportsHyperlink(process.stdout)
? ansiEscapes.link(chalk.dim(x.ruleId), ruleUrl)
: chalk.dim(x.ruleId);

const gap = ' '.repeat(gapWidth);

const line = [
'',
x.severity === 'warning' ? logSymbols.warning : logSymbols.error,
' '.repeat(maxLineWidth - x.lineWidth) + chalk.dim(x.line + chalk.gray(':') + x.column),
' '.repeat(maxColumnWidth - x.columnWidth) + x.message,
' '.repeat(maxMessageWidth - x.messageWidth)
+ (ruleUrl && supportsHyperlink(process.stdout) ? ansiEscapes.link(chalk.dim(x.ruleId), ruleUrl) : chalk.dim(x.ruleId)),
preLine,
severity,
range,
message,
gap,
rule,
];

if (!showLineNumbers) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"log-symbols": "^6.0.0",
"plur": "^5.1.0",
"string-width": "^7.0.0",
"supports-hyperlinks": "^3.0.0"
"supports-hyperlinks": "^3.0.0",
"terminal-size": "^4.0.0"
},
"devDependencies": {
"ava": "^5.3.1",
Expand Down

0 comments on commit 37ff7d5

Please sign in to comment.