timer-logs
Advanced tools
Comparing version 1.7.2 to 1.7.4
@@ -13,3 +13,4 @@ "use strict"; | ||
timer.tsql `SELECT * FROM persons`; | ||
timer.tlog `I am a very long string and should be wrapped over multiple lines in most regular sized command line terminals if using a small monitor/laptop screen to test timer-logs package from npm`; | ||
}; | ||
exports.taggedLiteralRunner(); |
@@ -15,4 +15,6 @@ import Timer from "../index"; | ||
timer.tsql`SELECT * FROM persons`; | ||
timer.tlog`I am a very long string and should be wrapped over multiple lines in most regular sized command line terminals if using a small monitor/laptop screen to test timer-logs package from npm`; | ||
}; | ||
taggedLiteralRunner(); |
{ | ||
"name": "timer-logs", | ||
"version": "1.7.2", | ||
"version": "1.7.4", | ||
"devDependencies": { | ||
@@ -16,3 +16,4 @@ "@types/node": "^15.00.0", | ||
"scripts": { | ||
"build": "tsc --removeComments && tsc --declaration --emitDeclarationOnly" | ||
"build": "tsc --removeComments && tsc --declaration --emitDeclarationOnly", | ||
"prepublish": "npm run build" | ||
}, | ||
@@ -19,0 +20,0 @@ "keywords": [ |
@@ -26,5 +26,25 @@ "use strict"; | ||
}; | ||
const splitByWidth = (message, maxWidth) => { | ||
var _a; | ||
let remainingMessage = message; | ||
let lines = []; | ||
while (remainingMessage.length > maxWidth) { | ||
const lastSpace = remainingMessage.lastIndexOf(" ", maxWidth); | ||
const lastDash = remainingMessage.lastIndexOf("-", maxWidth); | ||
const spaceRemoved = lastSpace !== -1; | ||
const splitOnIndex = (_a = [lastSpace, lastDash].find((width) => width > 0)) !== null && _a !== void 0 ? _a : maxWidth; | ||
lines.push(remainingMessage.slice(0, splitOnIndex)); | ||
remainingMessage = remainingMessage.slice(splitOnIndex + (spaceRemoved ? 1 : 0)); | ||
} | ||
lines.push(remainingMessage); | ||
return lines; | ||
}; | ||
const printTimestampedLog = async (timestamp, severity, messages) => { | ||
var _a; | ||
const linesArray = messages.map((message) => message.split("\n")); | ||
const [maxLineWidth] = process.stdout.getWindowSize(); | ||
const maxMessageWidth = maxLineWidth - longestFilename - severityWidth - timestampWidth - 9; | ||
const linesArray = messages.map((message) => message | ||
.split("\n") | ||
.map((message) => splitByWidth(message, maxMessageWidth)) | ||
.flat()); | ||
const lineCount = Math.max(...linesArray.map((lines) => lines.length), 1); | ||
@@ -31,0 +51,0 @@ const timestamps = centerMultilineLog(lineCount === 1 |
@@ -31,2 +31,25 @@ import { LogPresenter } from "../../types/interfaces/LogPresenter"; | ||
/** | ||
* Split a string into chunks of a certain width. Used to split messages over multiple lines in terminal. | ||
*/ | ||
const splitByWidth = (message: string, maxWidth: number): string[] => { | ||
let remainingMessage = message; | ||
let lines = []; | ||
while (remainingMessage.length > maxWidth) { | ||
// split on last space or dash if one exists | ||
const lastSpace = remainingMessage.lastIndexOf(" ", maxWidth); | ||
const lastDash = remainingMessage.lastIndexOf("-", maxWidth); | ||
// if split lines on a space, remove that leading space from the second line | ||
const spaceRemoved = lastSpace !== -1; | ||
const splitOnIndex = | ||
[lastSpace, lastDash].find((width) => width > 0) ?? maxWidth; | ||
lines.push(remainingMessage.slice(0, splitOnIndex)); | ||
remainingMessage = remainingMessage.slice( | ||
splitOnIndex + (spaceRemoved ? 1 : 0) | ||
); | ||
} | ||
lines.push(remainingMessage); | ||
return lines; | ||
}; | ||
const printTimestampedLog = async ( | ||
@@ -38,3 +61,11 @@ timestamp: Date, | ||
// linesArray is an array of arrays, where each message has its own array [['message1'],['message2']] | ||
const linesArray = messages.map((message) => message.split("\n")); | ||
const [maxLineWidth] = process.stdout.getWindowSize(); | ||
const maxMessageWidth = | ||
maxLineWidth - longestFilename - severityWidth - timestampWidth - 9; // minus nine for pipes and spacing | ||
const linesArray = messages.map((message) => | ||
message | ||
.split("\n") | ||
.map((message) => splitByWidth(message, maxMessageWidth)) | ||
.flat() | ||
); | ||
// gets the message with the most lines lineCount | ||
@@ -41,0 +72,0 @@ const lineCount = Math.max(...linesArray.map((lines) => lines.length), 1); |
@@ -38,4 +38,15 @@ "use strict"; | ||
function formatSql(query) { | ||
const newlineKeywords = ["SELECT", "FROM", "WHERE"]; | ||
const indentKeywords = ["JOIN", "AND", "OR"]; | ||
const newlineKeywords = ["SELECT", "FROM", "WHERE", "INSERT", "UPDATE"]; | ||
const indentKeywords = [ | ||
"FULL OUTER JOIN", | ||
"LEFT OUTER JOIN", | ||
"LEFT JOIN", | ||
"RIGHT JOIN", | ||
"RIGHT OUTER JOIN", | ||
"INNER JOIN", | ||
"NATURAL JOIN", | ||
"JOIN", | ||
"AND", | ||
"OR", | ||
]; | ||
const doubleIndentKeywords = ["ON"]; | ||
@@ -55,2 +66,3 @@ const oneSpaceSymbol = new RegExp(/\s*[<=>]+\s*/, "g"); | ||
"SELECT", | ||
"AS", | ||
"FROM", | ||
@@ -61,2 +73,8 @@ "WHERE", | ||
"JOIN", | ||
"RIGHT", | ||
"OUTER", | ||
"INNER", | ||
"FULL", | ||
"NATURAL", | ||
"LEFT", | ||
"ON", | ||
@@ -67,9 +85,16 @@ "LIKE", | ||
"ANY", | ||
"INSERT", | ||
"INTO", | ||
"UPDATE", | ||
"SET", | ||
"DO", | ||
"CONFLICT", | ||
"EXCLUDED", | ||
]; | ||
const isString = new RegExp(/('.*?')|(".*?")/, "g"); | ||
const isString = new RegExp(/('.*?')|(".*?")|(`.*?`)/, "g"); | ||
const isNumber = new RegExp(/[0-9]+/, "g"); | ||
const isPunctuation = new RegExp(/[,<=\-~>*]/, "g"); | ||
const isPunctuation = new RegExp(/[,<=\-~>*]|(;$)/, "g"); | ||
const highlightedQuery = query | ||
.replace(isNumber, (num) => ColourConverters_1.wrap(num, cPalette.numbers)) | ||
.replace(isIn(sqlKeywords), (keyword) => ColourConverters_1.wrap(keyword, cPalette.keywords)) | ||
.replace(isIn(sqlKeywords), (keyword) => ColourConverters_1.wrap(keyword.toUpperCase(), cPalette.keywords)) | ||
.replace(isString, (str) => ColourConverters_1.wrap(str, cPalette.strings)) | ||
@@ -76,0 +101,0 @@ .replace(isPunctuation, (punc) => { var _a; return ColourConverters_1.wrap(punc, (_a = cPalette === null || cPalette === void 0 ? void 0 : cPalette.punctuation) !== null && _a !== void 0 ? _a : cPalette.keywords); }); |
@@ -51,4 +51,15 @@ import { SqlColourPalette } from "../../types/dataStructures/SqlColourPalette"; | ||
function formatSql(query: string): string { | ||
const newlineKeywords = ["SELECT", "FROM", "WHERE"]; | ||
const indentKeywords = ["JOIN", "AND", "OR"]; | ||
const newlineKeywords = ["SELECT", "FROM", "WHERE", "INSERT", "UPDATE"]; | ||
const indentKeywords = [ | ||
"FULL OUTER JOIN", | ||
"LEFT OUTER JOIN", | ||
"LEFT JOIN", | ||
"RIGHT JOIN", | ||
"RIGHT OUTER JOIN", | ||
"INNER JOIN", | ||
"NATURAL JOIN", | ||
"JOIN", | ||
"AND", | ||
"OR", | ||
]; | ||
const doubleIndentKeywords = ["ON"]; | ||
@@ -84,2 +95,3 @@ | ||
"SELECT", | ||
"AS", | ||
"FROM", | ||
@@ -90,2 +102,8 @@ "WHERE", | ||
"JOIN", | ||
"RIGHT", | ||
"OUTER", | ||
"INNER", | ||
"FULL", | ||
"NATURAL", | ||
"LEFT", | ||
"ON", | ||
@@ -96,9 +114,18 @@ "LIKE", | ||
"ANY", | ||
"INSERT", | ||
"INTO", | ||
"UPDATE", | ||
"SET", | ||
"DO", | ||
"CONFLICT", | ||
"EXCLUDED", | ||
]; | ||
const isString = new RegExp(/('.*?')|(".*?")/, "g"); | ||
const isString = new RegExp(/('.*?')|(".*?")|(`.*?`)/, "g"); | ||
const isNumber = new RegExp(/[0-9]+/, "g"); | ||
const isPunctuation = new RegExp(/[,<=\-~>*]/, "g"); | ||
const isPunctuation = new RegExp(/[,<=\-~>*]|(;$)/, "g"); | ||
const highlightedQuery = query | ||
.replace(isNumber, (num) => wrap(num, cPalette.numbers)) | ||
.replace(isIn(sqlKeywords), (keyword) => wrap(keyword, cPalette.keywords)) | ||
.replace(isIn(sqlKeywords), (keyword) => | ||
wrap(keyword.toUpperCase(), cPalette.keywords) | ||
) | ||
.replace(isString, (str) => wrap(str, cPalette.strings)) | ||
@@ -105,0 +132,0 @@ .replace(isPunctuation, (punc) => |
Sorry, the diff of this file is not supported yet
111816
68
2273