youch-terminal
Advanced tools
Comparing version 2.1.5 to 2.2.0
97
index.js
@@ -14,5 +14,9 @@ 'use strict' | ||
const { sep } = require('path') | ||
const { bgRed, dim, yellow, green, white, red } = require('kleur') | ||
const wordwrap = require('wordwrap') | ||
const stringWidth = require('string-width') | ||
const { dim, yellow, green, red, cyan } = require('kleur') | ||
const TERMINAL_SIZE = process.stdout.columns | ||
const POINTER = platform === 'win32' && !process.env.WT_SESSION ? '>' : '❯' | ||
const DASH = platform === 'win32' && !process.env.WT_SESSION ? '⁃' : '⁃' | ||
@@ -100,2 +104,3 @@ /** | ||
const space = whiteSpace(String(maxCounter), String(counter)) | ||
if (isMain) { | ||
@@ -108,19 +113,36 @@ return `${prefix}${red(POINTER)}${space}${red(counter)}${red('|')}${space} ${red(line)}` | ||
/** | ||
* Returns the main error title | ||
* | ||
* @method getTitle | ||
* | ||
* @param {Object} error | ||
* | ||
* @return {Array} | ||
* Returns the error message | ||
*/ | ||
function getTitle (error, prefix) { | ||
return [`${prefix} ${bgRed(white(` ${error.code ? error.code : ''}${error.name} `))}`, prefix] | ||
function getMessage(error, prefix, hideErrorTitle) { | ||
let message | ||
const wrapper = wordwrap(stringWidth(prefix) + 2, TERMINAL_SIZE) | ||
if (!hideErrorTitle) { | ||
message = `${prefix} ${red(wrapper(`${error.name}: ${error.message}`).trim())}` | ||
} else { | ||
message = `${prefix} ${red(wrapper(`${error.message}`).trim())}` | ||
} | ||
return [message, prefix] | ||
} | ||
/** | ||
* Returns the error message | ||
* Returns the error help text | ||
*/ | ||
function getMessage(error, prefix) { | ||
return [`${prefix} ${error.message}`, prefix] | ||
function getHelpText(error, prefix) { | ||
const help = error.help | ||
if (!help) { | ||
return [] | ||
} | ||
const wrapper = wordwrap(stringWidth(prefix) + 4, TERMINAL_SIZE) | ||
if (Array.isArray(help)) { | ||
return help.map((line) => { | ||
return `${prefix} ${cyan(wrapper(`- ${line}`).trim())}` | ||
}).concat([prefix]) | ||
} | ||
return [`${prefix} ${cyan(help)}`, prefix] | ||
} | ||
@@ -130,5 +152,5 @@ | ||
* Get the relative path for a given file path, from the current working directory | ||
* | ||
* | ||
* @param {String} filePath | ||
* | ||
* | ||
* @return {String} | ||
@@ -155,4 +177,4 @@ */ | ||
const filePath = displayShortPath ? getShortPath(frame.filePath) : frame.filePath | ||
return [`${prefix} at ${yellow(`${frameMethod(frame)}`)} ${green(filePath)}:${green(frame.line)}`] | ||
const filePath = displayShortPath ? frame.filePath.replace(`${cwd()}${sep}`, '') : frame.filePath | ||
return [`${prefix} at ${yellow(`${frameMethod(frame)}`)} ${green(filePath)}:${green(frame.line)}`] | ||
} | ||
@@ -206,11 +228,12 @@ | ||
const totalFrames = String(frames.length) | ||
return frames.map((frame, index) => { | ||
const frameNumber = String(index + 1) | ||
const padding = frameNumber.padStart(totalFrames.length - frameNumber.length, '0') | ||
const filePath = displayShortPath ? getShortPath(frame.filePath) : frame.filePath | ||
const padding = whiteSpace(String(totalFrames.length), '') | ||
return frames.map((frame) => { | ||
const filePath = displayShortPath | ||
? frame.filePath.replace(`${cwd()}${sep}`, '') | ||
: frame.filePath | ||
return [ | ||
prefix, | ||
`${prefix} ${dim(padding)} ${yellow(frameMethod(frame))}`, | ||
`${prefix}${whiteSpace(padding, '')} ${green(filePath)}${':' + green(frame.line)}` | ||
`${prefix}${padding}${yellow(`${DASH} ${frameMethod(frame)}`)}`, | ||
`${prefix}${padding} ${green(filePath)}${':' + green(frame.line)}` | ||
].join('\n') | ||
@@ -231,2 +254,3 @@ }) | ||
* @param {String} options.prefix | ||
* @param {Number} options.framesMaxLimit | ||
* @param {Boolean} options.displayShortPath | ||
@@ -241,17 +265,22 @@ * @param {Boolean} options.hideErrorTitle | ||
const firstFrame = mainFrame(error.frames) | ||
options = { prefix: '', ...options } | ||
options = { prefix: ' ', framesMaxLimit: 3, ...options } | ||
const otherFrames = options.displayMainFrameOnly && firstFrame | ||
? [] | ||
: getFramesInfo( | ||
filterNativeFrames(error.frames, firstFrame), | ||
options.prefix, | ||
options.displayShortPath | ||
) | ||
return [''] | ||
.concat(options.hideErrorTitle ? [] : getTitle(error, options.prefix)) | ||
.concat(options.hideMessage ? [] : getMessage(error, options.prefix)) | ||
.concat(options.hideMessage ? [] : getMessage(error, options.prefix, options.hideErrorTitle)) | ||
.concat(getHelpText(error, options.prefix)) | ||
.concat(getMainFrameLocation(firstFrame, options.prefix, options.displayShortPath)) | ||
.concat(getCodeLines(firstFrame, options.prefix)) | ||
.concat(otherFrames.length ? [''] : []) | ||
.concat( | ||
options.displayMainFrameOnly && firstFrame | ||
? [] | ||
: getFramesInfo( | ||
filterNativeFrames(error.frames, firstFrame), | ||
options.prefix, | ||
options.displayShortPath | ||
) | ||
Number.isFinite(options.framesMaxLimit) | ||
? otherFrames.slice(0, options.framesMaxLimit) | ||
: otherFrames | ||
) | ||
@@ -258,0 +287,0 @@ .concat(['']) |
{ | ||
"name": "youch-terminal", | ||
"version": "2.1.5", | ||
"version": "2.2.0", | ||
"description": "Show youch error on terminal", | ||
@@ -16,6 +16,8 @@ "main": "index.js", | ||
"dependencies": { | ||
"kleur": "^4.1.5" | ||
"kleur": "^4.1.5", | ||
"string-width": "^4.2.3", | ||
"wordwrap": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"youch": "^3.2.1" | ||
"youch": "^3.2.3" | ||
}, | ||
@@ -22,0 +24,0 @@ "repository": { |
@@ -26,13 +26,16 @@ # Youch terminal | ||
// Defaults to empty string | ||
prefix: '', | ||
// Defaults to single whitspace | ||
prefix: ' ', | ||
// Defaults to false | ||
hideErrorTitle: false | ||
hideErrorTitle: false, | ||
// Defaults to false | ||
hideMessage: false | ||
hideMessage: false, | ||
// Defaults to false | ||
displayMainFrameOnly: false | ||
displayMainFrameOnly: false, | ||
// Defaults to 3 | ||
framesMaxLimit: 3, | ||
} | ||
@@ -39,0 +42,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8470
249
44
3
2
+ Addedstring-width@^4.2.3
+ Addedwordwrap@^1.0.0
+ Addedansi-regex@5.0.1(transitive)
+ Addedemoji-regex@8.0.0(transitive)
+ Addedis-fullwidth-code-point@3.0.0(transitive)
+ Addedstring-width@4.2.3(transitive)
+ Addedstrip-ansi@6.0.1(transitive)
+ Addedwordwrap@1.0.0(transitive)