Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split and align on linebreaks and too long lines #94

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
"displayFilename": false,
"displayLabel": true,
"displayTimestamp": false,
"splitLongLines": false,
"splitLinebreaks": false,
"underlineLabel": true,
"underlineMessage": false,
"underlinePrefix": false,
Expand Down
152 changes: 122 additions & 30 deletions src/signale.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,29 +176,40 @@ class Signale {

_meta() {
const meta = [];
let metaCharLength = 0;

if (this._config.displayDate) {
meta.push(this._formatDate());
const str = this._formatDate();
meta.push(str);
metaCharLength += this._stringUnicodeLength(str);
}

if (this._config.displayTimestamp) {
meta.push(this._formatTimestamp());
const str = this._formatTimestamp();
meta.push(str);
metaCharLength += this._stringUnicodeLength(str);
}

if (this._config.displayFilename) {
meta.push(this._formatFilename());
const str = this._formatFilename();
meta.push(str);
metaCharLength += this._stringUnicodeLength(str);
}

if (this._scopeName.length !== 0 && this._config.displayScope) {
meta.push(this._formatScopeName());
const str = this._formatScopeName();
meta.push(str);
metaCharLength += this._stringUnicodeLength(str);
}

if (meta.length !== 0) {
meta.push(`${figures.pointerSmall}`);
return meta.map(item => grey(item));
const str = `${figures.pointerSmall}`;
meta.push(str);
metaCharLength += this._stringUnicodeLength(str);
return [meta.map(item => grey(item)), metaCharLength];
}

return meta;
return [meta, metaCharLength];
}

_hasAdditional({suffix, prefix}, args) {
Expand All @@ -220,56 +231,114 @@ class Signale {
msg = this._formatMessage(args);
}

const signale = this._meta();
let [firstLine, firstLineCharLength] = this._meta();

if (additional.prefix) {
if (this._config.underlinePrefix) {
signale.push(underline(additional.prefix));
firstLine.push(underline(additional.prefix));
} else {
signale.push(additional.prefix);
firstLine.push(additional.prefix);
}

firstLineCharLength += this._stringUnicodeLength(additional.prefix);
}

if (this._config.displayBadge && type.badge) {
signale.push(chalk[type.color](this._padEnd(type.badge, type.badge.length + 1)));
const str = this._padEnd(type.badge, type.badge.length + 1);
firstLine.push(chalk[type.color](str));
firstLineCharLength += this._stringUnicodeLength(str);
}

if (this._config.displayLabel && type.label) {
const label = this._config.uppercaseLabel ? type.label.toUpperCase() : type.label;

if (this._config.underlineLabel) {
signale.push(chalk[type.color](this._padEnd(underline(label), this._longestUnderlinedLabel.length + 1)));
firstLine.push(chalk[type.color](this._padEnd(underline(label), this._longestUnderlinedLabel.length + 1)));
} else {
signale.push(chalk[type.color](this._padEnd(label, this._longestLabel.length + 1)));
firstLine.push(chalk[type.color](this._padEnd(label, this._longestLabel.length + 1)));
}

firstLineCharLength += this._longestLabel.length + 1;
}

if (msg instanceof Error && msg.stack) {
const [name, ...rest] = msg.stack.split('\n');
if (this._config.underlineMessage) {
signale.push(underline(name));
const suffix = [];
let suffixCharLength = 0;

if (additional.suffix) {
if (this._config.underlineSuffix) {
suffix.push(chalk.grey(underline(additional.suffix)));
} else {
signale.push(name);
suffix.push(chalk.grey(additional.suffix));
}

signale.push(grey(rest.map(l => l.replace(/^/, '\n')).join('')));
return signale.join(' ');
suffixCharLength += this._stringUnicodeLength(additional.suffix);
}

if (this._config.underlineMessage) {
signale.push(underline(msg));
let lines;

if (this._config.splitLinebreaks) {
lines = msg.split('\n');
} else {
signale.push(msg);
lines = [msg];
}

if (additional.suffix) {
if (this._config.underlineSuffix) {
signale.push(underline(additional.suffix));
if (this._config.splitLongLines) {
let size = 0;
if (typeof this._config.splitLongLines === 'number') {
size = parseInt(this._config.splitLongLines, 10);
} else if (this._config.splitLongLines === 'auto') {
size = process.stdout.columns;
} else {
signale.push(additional.suffix);
size = 80;
}

size -= firstLineCharLength + firstLine.length + suffixCharLength + suffix.length;

lines = lines.map(str => {
const chunks = [];

for (let i = 0; i < str.length; i += size) {
chunks.push(str.substr(i, size));
}

return chunks;
}).flat();
}

return signale.join(' ');
lines = lines.map((msg, index) => {
let signale;
if (index === 0) {
signale = firstLine.slice();
} else {
signale = [this._padLength(firstLineCharLength + firstLine.length - 1)];
}

if (msg instanceof Error && msg.stack) {
const [name, ...rest] = msg.stack.split('\n');
if (this._config.underlineMessage) {
signale.push(underline(name));
} else {
signale.push(name);
}

signale.push(grey(rest.map(l => l.replace(/^/, '\n')).join('')));
return signale.join(' ');
}

if (this._config.underlineMessage) {
signale.push(underline(msg));
} else {
signale.push(msg);
}

if (index === 0 && suffix.length !== 0) {
signale = signale.concat(suffix);
}

return signale.join(' ');
});

return lines.join('\n');
}

_write(stream, message) {
Expand Down Expand Up @@ -313,6 +382,28 @@ class Signale {
return str + ' '.repeat(_targetLength);
}

_stringUnicodeLength(val) {
if (typeof val === 'number') {
return val;
}

if (typeof val === 'string') {
return [...val].length;
}

return 0;
}

_padLength(val) {
const length = (typeof val === 'number') ? val : this._stringUnicodeLength(val);

if (length <= 0) {
return '';
}

return ' '.repeat(length);
}

addSecrets(secrets) {
if (!Array.isArray(secrets)) {
throw new TypeError('Argument must be an array.');
Expand Down Expand Up @@ -360,7 +451,7 @@ class Signale {

this._timers.set(label, this._now);

const message = this._meta();
const [message] = this._meta();
message.push(green(this._padEnd(this._types.start.badge, 2)));

if (this._config.underlineLabel) {
Expand All @@ -387,7 +478,7 @@ class Signale {
const span = this._timeSpan(this._timers.get(label));
this._timers.delete(label);

const message = this._meta();
const [message] = this._meta();
message.push(red(this._padEnd(this._types.pause.badge, 2)));

if (this._config.underlineLabel) {
Expand All @@ -406,3 +497,4 @@ class Signale {
}

module.exports = Signale;

  NODES
COMMUNITY 2
Note 1
Project 4
USERS 1