Skip to content

Commit

Permalink
chore: tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Sep 16, 2024
1 parent 014c2cd commit 559acb3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
20 changes: 11 additions & 9 deletions pkg/src/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ import {
replaceLast
} from './utils.js'

const picoHasColors = picocolors.createColors(true)
const picoNoColors = picocolors.createColors(false)
/** @type { import('picocolors/types.js').Colors | undefined } */
let _picocolorsHasColors
/** @type { import('picocolors/types.js').Colors | undefined } */
let _picocolorsNoColors

/**
* @param {import('../index.d.ts').Message} m
* @param {import('./utils.js').Pkg} pkg
* @param {import('../utils.d.ts').FormatMessageOpt} opt
* @param {import('../utils.d.ts').FormatMessageOptions} opts
*/
export function formatMessage(m, pkg, opt = {}) {
/** @type { import("picocolors/types.js").Colors } */
export function formatMessage(m, pkg, opts = {}) {
/** @type { import('picocolors/types.js').Colors } */
let c = picocolors
if (opt.color === true) {
c = picoHasColors
} else if (opt.color === false) {
c = picoNoColors
if (opts.color === true) {
c = _picocolorsHasColors ??= picocolors.createColors(true)
} else if (opts.color === false) {
c = _picocolorsNoColors ??= picocolors.createColors(false)
}

/** @param {string[]} path */
Expand Down
12 changes: 6 additions & 6 deletions pkg/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import type { Message } from './index.js'

type Pkg = Record<string, any>

export interface FormatMessageOpt {
export interface FormatMessageOptions {
/**
* Used to determine if the returned string contains color.
* - true: Force has color.
* - false: Force no color.
* - undefined: Default to whether the environment supports color (already handled by picocolors by default).
* Whether the returned string should contain color.
* - `true`: Force has color.
* - `false`: Force no color.
* - `undefined`: Default to whether the environment supports color.
*/
color?: boolean | undefined
}
Expand All @@ -17,5 +17,5 @@ export declare function getPkgPathValue(pkg: Pkg, path: string[]): any
export declare function formatMessage(
msg: Message,
pkg: Pkg,
opt?: FormatMessageOpt
opt?: FormatMessageOptions
): string | undefined

0 comments on commit 559acb3

Please sign in to comment.