diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d588995..ef5d807 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,12 +10,12 @@ jobs: fail-fast: false matrix: node-version: - - 18 - 16 - 14 + - 12 steps: - - uses: actions/checkout@v4 - - uses: actions/setup-node@v4 + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install diff --git a/.replit b/.replit new file mode 100644 index 0000000..cf3930d --- /dev/null +++ b/.replit @@ -0,0 +1,2 @@ +language = "nodejs" +run = "cd examples && node rainbow" diff --git a/examples/rainbow.js b/examples/rainbow.js index 5b2b1d2..3501dab 100644 --- a/examples/rainbow.js +++ b/examples/rainbow.js @@ -1,6 +1,6 @@ -import {setTimeout as delay} from 'node:timers/promises'; import convertColor from 'color-convert'; import updateLog from 'log-update'; +import delay from 'yoctodelay'; import chalk from '../source/index.js'; const ignoreChars = /[^!-~]/g; @@ -10,7 +10,7 @@ function rainbow(string, offset) { return string; } - const hueStep = 360 / string.replaceAll(ignoreChars, '').length; + const hueStep = 360 / string.replace(ignoreChars, '').length; let hue = offset % 360; const characters = []; @@ -33,6 +33,8 @@ async function animateString(string) { } } -console.log(); -await animateString('We hope you enjoy Chalk! <3'); -console.log(); +(async () => { + console.log(); + await animateString('We hope you enjoy Chalk! <3'); + console.log(); +})(); diff --git a/examples/screenshot.js b/examples/screenshot.js index 9ad164b..6f66b77 100644 --- a/examples/screenshot.js +++ b/examples/screenshot.js @@ -6,16 +6,7 @@ import chalk from '../source/index.js'; for (const key of Object.keys(styles)) { let returnValue = key; - // We skip `overline` as almost no terminal supports it so we cannot show it off. - if ( - key === 'reset' - || key === 'hidden' - || key === 'grey' - || key === 'bgGray' - || key === 'bgGrey' - || key === 'overline' - || key.endsWith('Bright') - ) { + if (key === 'reset' || key === 'hidden' || key === 'grey') { continue; } diff --git a/media/screenshot.png b/media/screenshot.png deleted file mode 100644 index da9d89b..0000000 Binary files a/media/screenshot.png and /dev/null differ diff --git a/package.json b/package.json index c9e0dc5..f693c87 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,11 @@ { "name": "chalk", - "version": "5.6.2", + "version": "5.0.0", "description": "Terminal string styling done right", "license": "MIT", "repository": "chalk/chalk", "funding": "https://github.com/chalk/chalk?sponsor=1", "type": "module", - "main": "./source/index.js", "exports": "./source/index.js", "imports": { "#ansi-styles": "./source/vendor/ansi-styles/index.js", @@ -16,7 +15,6 @@ } }, "types": "./source/index.d.ts", - "sideEffects": false, "engines": { "node": "^12.17.0 || ^14.13 || >=16.0.0" }, @@ -59,16 +57,12 @@ "log-update": "^5.0.0", "matcha": "^0.7.0", "tsd": "^0.19.0", - "xo": "^0.57.0", + "xo": "^0.47.0", "yoctodelay": "^2.0.0" }, "xo": { "rules": { - "unicorn/prefer-string-slice": "off", - "@typescript-eslint/consistent-type-imports": "off", - "@typescript-eslint/consistent-type-exports": "off", - "@typescript-eslint/consistent-type-definitions": "off", - "unicorn/expiring-todo-comments": "off" + "unicorn/prefer-string-slice": "off" } }, "c8": { diff --git a/readme.md b/readme.md index ce1f3f3..f6adc80 100644 --- a/readme.md +++ b/readme.md @@ -10,15 +10,63 @@ > Terminal string styling done right [![Coverage Status](https://codecov.io/gh/chalk/chalk/branch/main/graph/badge.svg)](https://codecov.io/gh/chalk/chalk) -[![npm dependents](https://badgen.net/npm/dependents/chalk)](https://www.npmjs.com/package/chalk?activeTab=dependents) -[![Downloads](https://badgen.net/npm/dt/chalk)](https://www.npmjs.com/package/chalk) +[![npm dependents](https://badgen.net/npm/dependents/chalk)](https://www.npmjs.com/package/chalk?activeTab=dependents) [![Downloads](https://badgen.net/npm/dt/chalk)](https://www.npmjs.com/package/chalk) +[![run on repl.it](https://repl.it/badge/github/chalk/chalk)](https://repl.it/github/chalk/chalk) +[![Support Chalk on DEV](https://badge.devprotocol.xyz/0x44d871aebF0126Bf646753E2C976Aa7e68A66c15/descriptive)](https://stakes.social/0x44d871aebF0126Bf646753E2C976Aa7e68A66c15) -![](media/screenshot.png) + -## Info +
-- [Why not switch to a smaller coloring package?](https://github.com/chalk/chalk?tab=readme-ov-file#why-not-switch-to-a-smaller-coloring-package) -- See [yoctocolors](https://github.com/sindresorhus/yoctocolors) for a smaller alternative +--- + +
+

+

+ + Sindre Sorhus' open source work is supported by the community on GitHub Sponsors and Dev + +

+ Special thanks to: +
+
+ + + +
+
+ + + +
+
+ +
+ Doppler +
+ All your environment variables, in one place +
+ Stop struggling with scattered API keys, hacking together home-brewed tools, +
+ and avoiding access controls. Keep your team and servers in sync with Doppler. +
+
+
+ +
+ Strapi +
+ Strapi is the leading open-source headless CMS. +
+ It’s 100% JavaScript, fully customizable, and developer-first. +
+
+

+
+ +--- + +
## Highlights @@ -31,7 +79,7 @@ - Doesn't extend `String.prototype` - Clean and focused - Actively maintained -- [Used by ~115,000 packages](https://www.npmjs.com/browse/depended/chalk) as of July 4, 2024 +- [Used by ~76,000 packages](https://www.npmjs.com/browse/depended/chalk) as of October 26, 2021 ## Install @@ -152,22 +200,6 @@ Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color= `chalkStderr` contains a separate instance configured with color support detected for `stderr` stream instead of `stdout`. Override rules from `supportsColor` apply to this too. `supportsColorStderr` is exposed for convenience. -### modifierNames, foregroundColorNames, backgroundColorNames, and colorNames - -All supported style strings are exposed as an array of strings for convenience. `colorNames` is the combination of `foregroundColorNames` and `backgroundColorNames`. - -This can be useful if you wrap Chalk and need to validate input: - -```js -import {modifierNames, foregroundColorNames} from 'chalk'; - -console.log(modifierNames.includes('bold')); -//=> true - -console.log(foregroundColorNames.includes('pink')); -//=> false -``` - ## Styles ### Modifiers @@ -178,10 +210,10 @@ console.log(foregroundColorNames.includes('pink')); - `italic` - Make the text italic. *(Not widely supported)* - `underline` - Put a horizontal line below the text. *(Not widely supported)* - `overline` - Put a horizontal line above the text. *(Not widely supported)* -- `inverse` - Invert background and foreground colors. +- `inverse`- Invert background and foreground colors. - `hidden` - Print the text but make it invisible. - `strikethrough` - Puts a horizontal line through the center of the text. *(Not widely supported)* -- `visible` - Print the text only when Chalk has a color level above zero. Can be useful for things that are purely cosmetic. +- `visible`- Print the text only when Chalk has a color level above zero. Can be useful for things that are purely cosmetic. ### Colors @@ -223,7 +255,7 @@ console.log(foregroundColorNames.includes('pink')); ## 256 and Truecolor color support -Chalk supports 256 colors and [Truecolor](https://github.com/termstandard/colors) (16 million colors) on supported terminal apps. +Chalk supports 256 colors and [Truecolor](https://gist.github.com/XVilka/8346728) (16 million colors) on supported terminal apps. Colors are downsampled from 16 million RGB values to an ANSI color format that is supported by the terminal emulator (or by specifying `{level: n}` as a Chalk option). For example, Chalk configured to run at level 1 (basic color support) will downsample an RGB value of #FF0000 (red) to 31 (ANSI escape for red). @@ -251,25 +283,15 @@ Since Chrome 69, ANSI escape codes are natively supported in the developer conso If you're on Windows, do yourself a favor and use [Windows Terminal](https://github.com/microsoft/terminal) instead of `cmd.exe`. -## FAQ +## Origin story -### Why not switch to a smaller coloring package? +[colors.js](https://github.com/Marak/colors.js) used to be the most popular string styling module, but it has serious deficiencies like extending `String.prototype` which causes all kinds of [problems](https://github.com/yeoman/yo/issues/68) and the package is unmaintained. Although there are other packages, they either do too much or not enough. Chalk is a clean and focused alternative. -Chalk may be larger, but there is a reason for that. It offers a more user-friendly API, well-documented types, supports millions of colors, and covers edge cases that smaller alternatives miss. Chalk is mature, reliable, and built to last. +## chalk for enterprise -But beyond the technical aspects, there's something more critical: trust and long-term maintenance. I have been active in open source for over a decade, and I'm committed to keeping Chalk maintained. Smaller packages might seem appealing now, but there's no guarantee they will be around for the long term, or that they won't become malicious over time. +Available as part of the Tidelift Subscription. -Chalk is also likely already in your dependency tree (since 100K+ packages depend on it), so switching won’t save space—in fact, it might increase it. npm deduplicates dependencies, so multiple Chalk instances turn into one, but adding another package alongside it will increase your overall size. - -If the goal is to clean up the ecosystem, switching away from Chalk won’t even make a dent. The real problem lies with packages that have very deep dependency trees (for example, those including a lot of polyfills). Chalk has no dependencies. It's better to focus on impactful changes rather than minor optimizations. - -If absolute package size is important to you, I also maintain [yoctocolors](https://github.com/sindresorhus/yoctocolors), one of the smallest color packages out there. - -*\- [Sindre](https://github.com/sindresorhus)* - -### But the smaller coloring package has benchmarks showing it is faster - -[Micro-benchmarks are flawed](https://sindresorhus.com/blog/micro-benchmark-fallacy) because they measure performance in unrealistic, isolated scenarios, often giving a distorted view of real-world performance. Don't believe marketing fluff. All the coloring packages are more than fast enough. +The maintainers of chalk and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-chalk?utm_source=npm-chalk&utm_medium=referral&utm_campaign=enterprise&utm_term=repo) ## Related @@ -289,8 +311,6 @@ If absolute package size is important to you, I also maintain [yoctocolors](http - [chalk-pipe](https://github.com/LitoMore/chalk-pipe) - Create chalk style schemes with simpler style strings - [terminal-link](https://github.com/sindresorhus/terminal-link) - Create clickable links in the terminal -*(Not accepting additional entries)* - ## Maintainers - [Sindre Sorhus](https://github.com/sindresorhus) diff --git a/source/index.d.ts b/source/index.d.ts index 8295d92..b2408c6 100644 --- a/source/index.d.ts +++ b/source/index.d.ts @@ -1,14 +1,76 @@ // TODO: Make it this when TS suports that. -// import {ModifierName, ForegroundColor, BackgroundColor, ColorName} from '#ansi-styles'; // import {ColorInfo, ColorSupportLevel} from '#supports-color'; -import { - ModifierName, - ForegroundColorName, - BackgroundColorName, - ColorName, -} from './vendor/ansi-styles/index.js'; import {ColorInfo, ColorSupportLevel} from './vendor/supports-color/index.js'; +/** +Basic foreground colors. + +[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support) +*/ +export type ForegroundColor = + | 'black' + | 'red' + | 'green' + | 'yellow' + | 'blue' + | 'magenta' + | 'cyan' + | 'white' + | 'gray' + | 'grey' + | 'blackBright' + | 'redBright' + | 'greenBright' + | 'yellowBright' + | 'blueBright' + | 'magentaBright' + | 'cyanBright' + | 'whiteBright'; + +/** +Basic background colors. + +[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support) +*/ +export type BackgroundColor = + | 'bgBlack' + | 'bgRed' + | 'bgGreen' + | 'bgYellow' + | 'bgBlue' + | 'bgMagenta' + | 'bgCyan' + | 'bgWhite' + | 'bgGray' + | 'bgGrey' + | 'bgBlackBright' + | 'bgRedBright' + | 'bgGreenBright' + | 'bgYellowBright' + | 'bgBlueBright' + | 'bgMagentaBright' + | 'bgCyanBright' + | 'bgWhiteBright'; + +/** +Basic colors. + +[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support) +*/ +export type Color = ForegroundColor | BackgroundColor; + +export type Modifiers = + | 'reset' + | 'bold' + | 'dim' + | 'italic' + | 'underline' + | 'overline' + | 'inverse' + | 'hidden' + | 'strikethrough' + | 'visible'; + export interface Options { /** Specify the color support for Chalk. @@ -246,12 +308,6 @@ export const supportsColor: ColorInfo; export const chalkStderr: typeof chalk; export const supportsColorStderr: typeof supportsColor; -export { - ModifierName, ForegroundColorName, BackgroundColorName, ColorName, - modifierNames, foregroundColorNames, backgroundColorNames, colorNames, -// } from '#ansi-styles'; -} from './vendor/ansi-styles/index.js'; - export { ColorInfo, ColorSupport, @@ -259,67 +315,4 @@ export { // } from '#supports-color'; } from './vendor/supports-color/index.js'; -// TODO: Remove these aliases in the next major version -/** -@deprecated Use `ModifierName` instead. - -Basic modifier names. -*/ -export type Modifiers = ModifierName; - -/** -@deprecated Use `ForegroundColorName` instead. - -Basic foreground color names. - -[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support) -*/ -export type ForegroundColor = ForegroundColorName; - -/** -@deprecated Use `BackgroundColorName` instead. - -Basic background color names. - -[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support) -*/ -export type BackgroundColor = BackgroundColorName; - -/** -@deprecated Use `ColorName` instead. - -Basic color names. The combination of foreground and background color names. - -[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support) -*/ -export type Color = ColorName; - -/** -@deprecated Use `modifierNames` instead. - -Basic modifier names. -*/ -export const modifiers: readonly Modifiers[]; - -/** -@deprecated Use `foregroundColorNames` instead. - -Basic foreground color names. -*/ -export const foregroundColors: readonly ForegroundColor[]; - -/** -@deprecated Use `backgroundColorNames` instead. - -Basic background color names. -*/ -export const backgroundColors: readonly BackgroundColor[]; - -/** -@deprecated Use `colorNames` instead. - -Basic color names. The combination of foreground and background color names. -*/ -export const colors: readonly Color[]; - export default chalk; diff --git a/source/index.js b/source/index.js index 8bc993d..302024b 100644 --- a/source/index.js +++ b/source/index.js @@ -204,19 +204,6 @@ Object.defineProperties(createChalk.prototype, styles); const chalk = createChalk(); export const chalkStderr = createChalk({level: stderrColor ? stderrColor.level : 0}); -export { - modifierNames, - foregroundColorNames, - backgroundColorNames, - colorNames, - - // TODO: Remove these aliases in the next major version - modifierNames as modifiers, - foregroundColorNames as foregroundColors, - backgroundColorNames as backgroundColors, - colorNames as colors, -} from './vendor/ansi-styles/index.js'; - export { stdoutColor as supportsColor, stderrColor as supportsColorStderr, diff --git a/source/index.test-d.ts b/source/index.test-d.ts index 92da39d..a1ef854 100644 --- a/source/index.test-d.ts +++ b/source/index.test-d.ts @@ -1,24 +1,5 @@ -import { - expectType, - expectAssignable, - expectError, - expectDeprecated, -} from 'tsd'; -import chalk, { - Chalk, - ChalkInstance, - ColorInfo, - ColorSupport, - ColorSupportLevel, - chalkStderr, - supportsColor, - supportsColorStderr, - ModifierName, - ForegroundColorName, - BackgroundColorName, - ColorName, - Modifiers, -} from './index.js'; +import {expectType, expectAssignable, expectError} from 'tsd'; +import chalk, {Chalk, ChalkInstance, Color, ColorInfo, ColorSupport, ColorSupportLevel, chalkStderr, supportsColor, supportsColorStderr} from './index.js'; // - supportsColor - expectType(supportsColor); @@ -160,20 +141,6 @@ expectType(chalk.underline``); expectType(chalk.red.bgGreen.bold`Hello {italic.blue ${name}}`); expectType(chalk.strikethrough.cyanBright.bgBlack`Works with {reset {bold numbers}} {bold.red ${1}}`); -// -- Modifiers types -expectAssignable('strikethrough'); -expectError('delete'); - -// -- Foreground types -expectAssignable('red'); -expectError('pink'); - -// -- Background types -expectAssignable('bgRed'); -expectError('bgPink'); - -// -- Color types -- -expectAssignable('red'); -expectAssignable('bgRed'); -expectError('hotpink'); -expectError('bgHotpink'); +// -- Color types == +expectAssignable('red'); +expectError('hotpink'); diff --git a/source/utilities.js b/source/utilities.js index 4366dee..5d49dad 100644 --- a/source/utilities.js +++ b/source/utilities.js @@ -9,7 +9,7 @@ export function stringReplaceAll(string, substring, replacer) { let endIndex = 0; let returnValue = ''; do { - returnValue += string.slice(endIndex, index) + substring + replacer; + returnValue += string.substr(endIndex, index - endIndex) + substring + replacer; endIndex = index + substringLength; index = string.indexOf(substring, endIndex); } while (index !== -1); @@ -23,7 +23,7 @@ export function stringEncaseCRLFWithFirstIndex(string, prefix, postfix, index) { let returnValue = ''; do { const gotCR = string[index - 1] === '\r'; - returnValue += string.slice(endIndex, (gotCR ? index - 1 : index)) + prefix + (gotCR ? '\r\n' : '\n') + postfix; + returnValue += string.substr(endIndex, (gotCR ? index - 1 : index) - endIndex) + prefix + (gotCR ? '\r\n' : '\n') + postfix; endIndex = index + 1; index = string.indexOf('\n', endIndex); } while (index !== -1); diff --git a/source/vendor/ansi-styles/index.d.ts b/source/vendor/ansi-styles/index.d.ts index 58f133a..7518d2a 100644 --- a/source/vendor/ansi-styles/index.d.ts +++ b/source/vendor/ansi-styles/index.d.ts @@ -180,52 +180,6 @@ export interface ConvertColor { hexToAnsi(hex: string): number; } -/** -Basic modifier names. -*/ -export type ModifierName = keyof Modifier; - -/** -Basic foreground color names. - -[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support) -*/ -export type ForegroundColorName = keyof ForegroundColor; - -/** -Basic background color names. - -[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support) -*/ -export type BackgroundColorName = keyof BackgroundColor; - -/** -Basic color names. The combination of foreground and background color names. - -[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support) -*/ -export type ColorName = ForegroundColorName | BackgroundColorName; - -/** -Basic modifier names. -*/ -export const modifierNames: readonly ModifierName[]; - -/** -Basic foreground color names. -*/ -export const foregroundColorNames: readonly ForegroundColorName[]; - -/** -Basic background color names. -*/ -export const backgroundColorNames: readonly BackgroundColorName[]; - -/* -Basic color names. The combination of foreground and background color names. -*/ -export const colorNames: readonly ColorName[]; - declare const ansiStyles: { readonly modifier: Modifier; readonly color: ColorBase & ForegroundColor; diff --git a/source/vendor/ansi-styles/index.js b/source/vendor/ansi-styles/index.js index eaa7bed..5746537 100644 --- a/source/vendor/ansi-styles/index.js +++ b/source/vendor/ansi-styles/index.js @@ -6,72 +6,68 @@ const wrapAnsi256 = (offset = 0) => code => `\u001B[${38 + offset};5;${code}m`; const wrapAnsi16m = (offset = 0) => (red, green, blue) => `\u001B[${38 + offset};2;${red};${green};${blue}m`; -const styles = { - modifier: { - reset: [0, 0], - // 21 isn't widely supported and 22 does the same thing - bold: [1, 22], - dim: [2, 22], - italic: [3, 23], - underline: [4, 24], - overline: [53, 55], - inverse: [7, 27], - hidden: [8, 28], - strikethrough: [9, 29], - }, - color: { - black: [30, 39], - red: [31, 39], - green: [32, 39], - yellow: [33, 39], - blue: [34, 39], - magenta: [35, 39], - cyan: [36, 39], - white: [37, 39], - - // Bright color - blackBright: [90, 39], - gray: [90, 39], // Alias of `blackBright` - grey: [90, 39], // Alias of `blackBright` - redBright: [91, 39], - greenBright: [92, 39], - yellowBright: [93, 39], - blueBright: [94, 39], - magentaBright: [95, 39], - cyanBright: [96, 39], - whiteBright: [97, 39], - }, - bgColor: { - bgBlack: [40, 49], - bgRed: [41, 49], - bgGreen: [42, 49], - bgYellow: [43, 49], - bgBlue: [44, 49], - bgMagenta: [45, 49], - bgCyan: [46, 49], - bgWhite: [47, 49], - - // Bright color - bgBlackBright: [100, 49], - bgGray: [100, 49], // Alias of `bgBlackBright` - bgGrey: [100, 49], // Alias of `bgBlackBright` - bgRedBright: [101, 49], - bgGreenBright: [102, 49], - bgYellowBright: [103, 49], - bgBlueBright: [104, 49], - bgMagentaBright: [105, 49], - bgCyanBright: [106, 49], - bgWhiteBright: [107, 49], - }, -}; - -export const modifierNames = Object.keys(styles.modifier); -export const foregroundColorNames = Object.keys(styles.color); -export const backgroundColorNames = Object.keys(styles.bgColor); -export const colorNames = [...foregroundColorNames, ...backgroundColorNames]; - function assembleStyles() { const codes = new Map(); + const styles = { + modifier: { + reset: [0, 0], + // 21 isn't widely supported and 22 does the same thing + bold: [1, 22], + dim: [2, 22], + italic: [3, 23], + underline: [4, 24], + overline: [53, 55], + inverse: [7, 27], + hidden: [8, 28], + strikethrough: [9, 29], + }, + color: { + black: [30, 39], + red: [31, 39], + green: [32, 39], + yellow: [33, 39], + blue: [34, 39], + magenta: [35, 39], + cyan: [36, 39], + white: [37, 39], + + // Bright color + blackBright: [90, 39], + redBright: [91, 39], + greenBright: [92, 39], + yellowBright: [93, 39], + blueBright: [94, 39], + magentaBright: [95, 39], + cyanBright: [96, 39], + whiteBright: [97, 39], + }, + bgColor: { + bgBlack: [40, 49], + bgRed: [41, 49], + bgGreen: [42, 49], + bgYellow: [43, 49], + bgBlue: [44, 49], + bgMagenta: [45, 49], + bgCyan: [46, 49], + bgWhite: [47, 49], + + // Bright color + bgBlackBright: [100, 49], + bgRedBright: [101, 49], + bgGreenBright: [102, 49], + bgYellowBright: [103, 49], + bgBlueBright: [104, 49], + bgMagentaBright: [105, 49], + bgCyanBright: [106, 49], + bgWhiteBright: [107, 49], + }, + }; + + // Alias bright black as gray (and grey) + styles.color.gray = styles.color.blackBright; + styles.bgColor.bgGray = styles.bgColor.bgBlackBright; + styles.color.grey = styles.color.blackBright; + styles.bgColor.bgGrey = styles.bgColor.bgBlackBright; for (const [groupName, group] of Object.entries(styles)) { for (const [styleName, style] of Object.entries(group)) { @@ -109,7 +105,7 @@ function assembleStyles() { // From https://github.com/Qix-/color-convert/blob/3f0e0d4e92e235796ccb17f6e85c72094a651f49/conversions.js Object.defineProperties(styles, { rgbToAnsi256: { - value(red, green, blue) { + value: (red, green, blue) => { // We use the extended greyscale palette here, with the exception of // black and white. normal palette only has 4 greyscale shades. if (red === green && green === blue) { @@ -132,13 +128,13 @@ function assembleStyles() { enumerable: false, }, hexToRgb: { - value(hex) { - const matches = /[a-f\d]{6}|[a-f\d]{3}/i.exec(hex.toString(16)); + value: hex => { + const matches = /(?[a-f\d]{6}|[a-f\d]{3})/i.exec(hex.toString(16)); if (!matches) { return [0, 0, 0]; } - let [colorString] = matches; + let {colorString} = matches.groups; if (colorString.length === 3) { colorString = [...colorString].map(character => character + character).join(''); @@ -161,7 +157,7 @@ function assembleStyles() { enumerable: false, }, ansi256ToAnsi: { - value(code) { + value: code => { if (code < 8) { return 30 + code; } diff --git a/source/vendor/supports-color/browser.js b/source/vendor/supports-color/browser.js index fbb6ce0..f4e9acd 100644 --- a/source/vendor/supports-color/browser.js +++ b/source/vendor/supports-color/browser.js @@ -1,30 +1,13 @@ /* eslint-env browser */ -const level = (() => { - if (!('navigator' in globalThis)) { - return 0; - } +const isBlinkBasedBrowser = /\b(Chrome|Chromium)\//.test(navigator.userAgent); - if (globalThis.navigator.userAgentData) { - const brand = navigator.userAgentData.brands.find(({brand}) => brand === 'Chromium'); - if (brand && brand.version > 93) { - return 3; - } - } - - if (/\b(Chrome|Chromium)\//.test(globalThis.navigator.userAgent)) { - return 1; - } - - return 0; -})(); - -const colorSupport = level !== 0 && { - level, +const colorSupport = isBlinkBasedBrowser ? { + level: 1, hasBasic: true, - has256: level >= 2, - has16m: level >= 3, -}; + has256: false, + has16m: false, +} : false; const supportsColor = { stdout: colorSupport, diff --git a/source/vendor/supports-color/index.d.ts b/source/vendor/supports-color/index.d.ts index db44a78..98e3618 100644 --- a/source/vendor/supports-color/index.d.ts +++ b/source/vendor/supports-color/index.d.ts @@ -1,13 +1,13 @@ -import type {WriteStream} from 'node:tty'; +import {WriteStream} from 'node:tty'; -export type Options = { +export interface Options { /** Whether `process.argv` should be sniffed for `--color` and `--no-color` flags. @default true */ readonly sniffFlags?: boolean; -}; +} /** Levels: @@ -21,7 +21,7 @@ export type ColorSupportLevel = 0 | 1 | 2 | 3; /** Detect whether the terminal supports color. */ -export type ColorSupport = { +export interface ColorSupport { /** The color level. */ @@ -41,11 +41,11 @@ export type ColorSupport = { Whether Truecolor 16 million colors are supported. */ has16m: boolean; -}; +} export type ColorInfo = ColorSupport | false; -export function createSupportsColor(stream?: WriteStream, options?: Options): ColorInfo; +export function createSupportsColor(stream: WriteStream, options?: Options): ColorInfo; declare const supportsColor: { stdout: ColorInfo; diff --git a/source/vendor/supports-color/index.js b/source/vendor/supports-color/index.js index 265d7f8..55f813c 100644 --- a/source/vendor/supports-color/index.js +++ b/source/vendor/supports-color/index.js @@ -3,8 +3,7 @@ import os from 'node:os'; import tty from 'node:tty'; // From: https://github.com/sindresorhus/has-flag/blob/main/index.js -/// function hasFlag(flag, argv = globalThis.Deno?.args ?? process.argv) { -function hasFlag(flag, argv = globalThis.Deno ? globalThis.Deno.args : process.argv) { +function hasFlag(flag, argv = process.argv) { const prefix = flag.startsWith('-') ? '' : (flag.length === 1 ? '-' : '--'); const position = argv.indexOf(prefix + flag); const terminatorPosition = argv.indexOf('--'); @@ -81,12 +80,6 @@ function _supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) { } } - // Check for Azure DevOps pipelines. - // Has to be above the `!streamIsTTY` check. - if ('TF_BUILD' in env && 'AGENT_NAME' in env) { - return 1; - } - if (haveStream && !streamIsTTY && forceColor === undefined) { return 0; } @@ -112,11 +105,7 @@ function _supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) { } if ('CI' in env) { - if (['GITHUB_ACTIONS', 'GITEA_ACTIONS', 'CIRCLECI'].some(key => key in env)) { - return 3; - } - - if (['TRAVIS', 'APPVEYOR', 'GITLAB_CI', 'BUILDKITE', 'DRONE'].some(sign => sign in env) || env.CI_NAME === 'codeship') { + if (['TRAVIS', 'CIRCLECI', 'APPVEYOR', 'GITLAB_CI', 'GITHUB_ACTIONS', 'BUILDKITE', 'DRONE'].some(sign => sign in env) || env.CI_NAME === 'codeship') { return 1; } @@ -127,33 +116,23 @@ function _supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) { return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0; } + // Check for Azure DevOps pipelines + if ('TF_BUILD' in env && 'AGENT_NAME' in env) { + return 1; + } + if (env.COLORTERM === 'truecolor') { return 3; } - if (env.TERM === 'xterm-kitty') { - return 3; - } - - if (env.TERM === 'xterm-ghostty') { - return 3; - } - - if (env.TERM === 'wezterm') { - return 3; - } - if ('TERM_PROGRAM' in env) { const version = Number.parseInt((env.TERM_PROGRAM_VERSION || '').split('.')[0], 10); switch (env.TERM_PROGRAM) { - case 'iTerm.app': { + case 'iTerm.app': return version >= 3 ? 3 : 2; - } - - case 'Apple_Terminal': { + case 'Apple_Terminal': return 2; - } // No default } } diff --git a/test/level.js b/test/level.js index 367baa6..f8efeba 100644 --- a/test/level.js +++ b/test/level.js @@ -38,6 +38,6 @@ test('propagate enable/disable changes from child colors', t => { }); test('disable colors if they are not supported', async t => { - const {stdout} = await execaNode(fileURLToPath(new URL('_fixture.js', import.meta.url))); + const {stdout} = await execaNode(fileURLToPath(new URL('./_fixture.js', import.meta.url))); t.is(stdout, 'testout testerr'); });