Comparing version 1.3.0 to 1.3.1
@@ -224,3 +224,3 @@ declare type Partial$1<T> = { | ||
* ```typescript | ||
* const timer = getTimer('Example', false, { | ||
* const timer = getTimer('Example', false, chalk.red, chalk, { | ||
* TOTAL: 'TOTAL', | ||
@@ -250,3 +250,3 @@ * INTRO: 'Action 1', | ||
*/ | ||
declare const getTimer: <TName extends INames>(name?: string, verbose?: boolean, displayNames?: TName) => ITimer<TName> & KeysOnly<TName>; | ||
declare const getTimer: <TName extends INames>(name?: string, verbose?: boolean, wrapperFn?: any, chalk?: any, displayNames?: TName) => ITimer<TName> & KeysOnly<TName>; | ||
/** | ||
@@ -253,0 +253,0 @@ * Global timer |
@@ -155,2 +155,9 @@ var __defProp = Object.defineProperty; | ||
// src/tools/fakeChalk.ts | ||
var noWrap = (x) => x; | ||
var noChalk = { | ||
dim: noWrap, | ||
bold: noWrap | ||
}; | ||
// src/tools/timer.ts | ||
@@ -173,6 +180,8 @@ var formatDuration = (duration) => { | ||
}; | ||
var getTimer = (name, verbose = false, displayNames) => { | ||
var getTimer = (name, verbose = false, wrapperFn = noWrap, chalk = noChalk, displayNames) => { | ||
let startTimes = {}; | ||
let endTimes = {}; | ||
let dispNames = displayNames || { TOTAL: "TOTAL" }; | ||
let dispNames = { | ||
...displayNames || {} | ||
}; | ||
const names = Object.fromEntries(Object.keys(dispNames).map((key) => [key, key])); | ||
@@ -183,4 +192,9 @@ const logLine = (label, prefix = "") => { | ||
const duration = end - start; | ||
console.log(`${prefix}${dispNames[label] || label}: ${formatDuration(duration)}`); | ||
const lineStart = `${prefix}${dispNames[label] || label}: `; | ||
const lineEnd = `${formatDuration(duration)}`; | ||
const line = chalk.bold(lineStart) + lineEnd; | ||
console.log(wrapperFn(line)); | ||
return (lineStart + lineEnd).replace(" ", "").length; | ||
}; | ||
startTimes.TOTAL = Date.now(); | ||
return { | ||
@@ -210,6 +224,11 @@ ...names, | ||
console.log(""); | ||
console.log([prefix, name, "Times:"].filter((x) => x && x.trim()).join(" ")); | ||
console.log(wrapperFn(chalk.bold([prefix, name, "Times:"].filter((x) => x && x.trim()).join(" ")))); | ||
let longest = 0; | ||
for (let label of Object.keys(startTimes)) { | ||
logLine(label, " "); | ||
if (label !== "TOTAL") { | ||
longest = Math.max(longest, logLine(label, " ")); | ||
} | ||
} | ||
console.log(wrapperFn(chalk.dim(" " + "\u23AF".repeat(longest)))); | ||
logLine("TOTAL", " "); | ||
console.log(""); | ||
@@ -233,7 +252,2 @@ }, | ||
}); | ||
var noWrap = (x) => x; | ||
var noChalk = { | ||
dim: noWrap, | ||
bold: noWrap | ||
}; | ||
var printLn = (...text) => { | ||
@@ -240,0 +254,0 @@ if (process == null ? void 0 : process.stdout) { |
{ | ||
"name": "swiss-ak", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"author": "Jack Cannon <jackc@annon.co.uk> (http://c.annon.co.uk/)", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -116,3 +116,3 @@ # swiss-ak (Swiss Army Knife) | ||
```typescript | ||
const timer = getTimer('Example', false, { | ||
const timer = getTimer('Example', false, chalk.red, chalk, { | ||
TOTAL: 'TOTAL', | ||
@@ -119,0 +119,0 @@ INTRO: 'Action 1', |
@@ -1,6 +0,2 @@ | ||
const noWrap = (x: any) => x; | ||
const noChalk = { | ||
dim: noWrap, | ||
bold: noWrap | ||
}; | ||
import { noChalk, noWrap } from './fakeChalk'; | ||
@@ -7,0 +3,0 @@ /** |
import { ms, SECOND } from './times'; | ||
import { KeysOnly } from './types'; | ||
import { noChalk, noWrap } from './fakeChalk'; | ||
@@ -42,3 +43,3 @@ // Hacky little display function | ||
* ```typescript | ||
* const timer = getTimer('Example', false, { | ||
* const timer = getTimer('Example', false, chalk.red, chalk, { | ||
* TOTAL: 'TOTAL', | ||
@@ -68,16 +69,32 @@ * INTRO: 'Action 1', | ||
*/ | ||
export const getTimer = <TName extends INames>(name?: string, verbose: boolean = false, displayNames?: TName): ITimer<TName> & KeysOnly<TName> => { | ||
export const getTimer = <TName extends INames>( | ||
name?: string, | ||
verbose: boolean = false, | ||
wrapperFn: any = noWrap, | ||
chalk: any = noChalk, | ||
displayNames?: TName | ||
): ITimer<TName> & KeysOnly<TName> => { | ||
let startTimes: { [label: string]: ms } = {}; | ||
let endTimes: { [label: string]: ms } = {}; | ||
let dispNames = (displayNames || { TOTAL: 'TOTAL' }) as TName; | ||
let dispNames = { | ||
...(displayNames || {}) | ||
} as TName; | ||
const names = Object.fromEntries(Object.keys(dispNames).map((key) => [key, key])) as KeysOnly<TName>; | ||
const logLine = (label: string, prefix: string = '') => { | ||
const logLine = (label: string, prefix: string = ''): number => { | ||
const start = startTimes[label]; | ||
const end = endTimes[label] || Date.now(); | ||
const duration = end - start; | ||
console.log(`${prefix}${dispNames[label] || label}: ${formatDuration(duration)}`); | ||
const lineStart = `${prefix}${dispNames[label] || label}: `; | ||
const lineEnd = `${formatDuration(duration)}`; | ||
const line = chalk.bold(lineStart) + lineEnd; | ||
console.log(wrapperFn(line)); | ||
return (lineStart + lineEnd).replace(' ', '').length; | ||
}; | ||
startTimes.TOTAL = Date.now(); | ||
return { | ||
@@ -105,6 +122,13 @@ ...names, | ||
console.log(''); | ||
console.log([prefix, name, 'Times:'].filter((x) => x && x.trim()).join(' ')); | ||
console.log(wrapperFn(chalk.bold([prefix, name, 'Times:'].filter((x) => x && x.trim()).join(' ')))); | ||
let longest = 0; | ||
for (let label of Object.keys(startTimes)) { | ||
logLine(label, ' '); | ||
if (label !== 'TOTAL') { | ||
longest = Math.max(longest, logLine(label, ' ')); | ||
} | ||
} | ||
console.log(wrapperFn(chalk.dim(' ' + '⎯'.repeat(longest)))); | ||
logLine('TOTAL', ' '); | ||
console.log(''); | ||
@@ -111,0 +135,0 @@ }, |
Sorry, the diff of this file is not supported yet
75835
15
2304