Skip to content

Commit

Permalink
feat: support color options
Browse files Browse the repository at this point in the history
  • Loading branch information
zanminkian committed Sep 15, 2024
1 parent 6d00f6b commit 014c2cd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
16 changes: 14 additions & 2 deletions pkg/src/message.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import c from 'picocolors'
import picocolors from 'picocolors'
import {
formatMessagePath as fp,
getPkgPathValue,
replaceLast
} from './utils.js'

const picoHasColors = picocolors.createColors(true)
const picoNoColors = picocolors.createColors(false)

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

/** @param {string[]} path */
const pv = (path) => getPkgPathValue(pkg, path)

Expand Down
13 changes: 12 additions & 1 deletion pkg/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@ import type { Message } from './index.js'

type Pkg = Record<string, any>

export interface FormatMessageOpt {
/**
* 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).
*/
color?: boolean | undefined
}

export declare function formatMessagePath(path: string[]): string
export declare function getPkgPathValue(pkg: Pkg, path: string[]): any
export declare function formatMessage(
msg: Message,
pkg: Pkg
pkg: Pkg,
opt?: FormatMessageOpt
): string | undefined

0 comments on commit 014c2cd

Please sign in to comment.