Skip to content

Commit

Permalink
fix: check supports-color in colorize
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Jun 3, 2024
1 parent 7e2dd1b commit ac32408
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"bugs": "https://github.com/oclif/core/issues",
"dependencies": {
"ansi-escapes": "^4.3.2",
"ansis": "^3.0.1",
"ansis": "^3.1.1",
"clean-stack": "^3.0.1",
"cli-spinners": "^2.9.2",
"cosmiconfig": "^9.0.0",
Expand All @@ -18,7 +18,7 @@
"is-wsl": "^2.2.0",
"minimatch": "^9.0.4",
"string-width": "^4.2.3",
"supports-color": "^9.4.0",
"supports-color": "^8",
"widest-line": "^3.1.0",
"wordwrap": "^1.0.0",
"wrap-ansi": "^7.0.0"
Expand All @@ -40,6 +40,7 @@
"@types/node": "^18",
"@types/pnpapi": "^0.0.5",
"@types/sinon": "^17.0.3",
"@types/supports-color": "^8.1.3",
"@types/wordwrap": "^1.0.3",
"@types/wrap-ansi": "^3.0.0",
"benchmark": "^2.1.4",
Expand Down
9 changes: 6 additions & 3 deletions src/ux/theme.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import ansis from 'ansis'
import {stderr, stdout} from 'supports-color'

import {STANDARD_ANSI, StandardAnsi, Theme} from '../interfaces/theme'

function isStandardChalk(color: any): color is StandardAnsi {
function isStandardAnsi(color: any): color is StandardAnsi {
return STANDARD_ANSI.includes(color)
}

Expand All @@ -14,7 +15,9 @@ function isStandardChalk(color: any): color is StandardAnsi {
*/
export function colorize(color: string | StandardAnsi | undefined, text: string): string {
if (!color) return text
if (isStandardChalk(color)) return ansis[color](text)
if (!stdout) return text
if (!stderr) return text
if (isStandardAnsi(color)) return ansis[color](text)
if (color.startsWith('#')) return ansis.hex(color)(text)
if (color.startsWith('rgb')) {
const [red, green, blue] = color
Expand All @@ -36,5 +39,5 @@ export function parseTheme(theme: Record<string, string | Record<string, string>
}

function isValid(color: string): string | undefined {
return color.startsWith('#') || color.startsWith('rgb') || isStandardChalk(color) ? color : undefined
return color.startsWith('#') || color.startsWith('rgb') || isStandardAnsi(color) ? color : undefined
}
14 changes: 7 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,11 @@
resolved "https://registry.yarnpkg.com/@types/sinonjs__fake-timers/-/sinonjs__fake-timers-8.1.5.tgz#5fd3592ff10c1e9695d377020c033116cc2889f2"
integrity sha512-mQkU2jY8jJEF7YHjHvsQO8+3ughTL1mcnn96igfhONmR+fUPSKIkefQYpSe8bsly2Ep7oQbn/6VG5/9/0qcArQ==

"@types/supports-color@^8.1.3":
version "8.1.3"
resolved "https://registry.yarnpkg.com/@types/supports-color/-/supports-color-8.1.3.tgz#b769cdce1d1bb1a3fa794e35b62c62acdf93c139"
integrity sha512-Hy6UMpxhE3j1tLpl27exp1XqHD7n8chAiNPzWfz16LPZoMMoSc4dzLl6w9qijkEb/r5O1ozdu1CWGA2L83ZeZg==

"@types/wordwrap@^1.0.3":
version "1.0.3"
resolved "https://registry.yarnpkg.com/@types/wordwrap/-/wordwrap-1.0.3.tgz#1faf6e6002e8d63534bd9ad329a32bc772d4983e"
Expand Down Expand Up @@ -1472,12 +1477,7 @@ ansicolors@~0.3.2:
resolved "https://registry.yarnpkg.com/ansicolors/-/ansicolors-0.3.2.tgz#665597de86a9ffe3aa9bfbe6cae5c6ea426b4979"
integrity sha1-ZlWX3oap/+Oqm/vmyuXG6kJrSXk=

ansis@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/ansis/-/ansis-3.0.1.tgz#46e731a2973e55378a18b87713906153c23c4229"
integrity sha512-NMqcJGNewJ+tVAK5fJFY7kBo698ViFSH8ceUfEwBHGZb4SNxHnbJPDBW0wNQU6rYjnBGBwBfUSmeIab/KrA5/A==

ansis@^3.2.0:
ansis@^3.1.1, ansis@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/ansis/-/ansis-3.2.0.tgz#0e050c5be94784f32ffdac4b84fccba064aeae4b"
integrity sha512-Yk3BkHH9U7oPyCN3gL5Tc7CpahG/+UFv/6UG03C311Vy9lzRmA5uoxDTpU9CO3rGHL6KzJz/pdDeXZCZ5Mu/Sg==
Expand Down Expand Up @@ -6394,7 +6394,7 @@ stylus-lookup@^3.0.1:
commander "^2.8.1"
debug "^4.1.0"

[email protected], supports-color@^8.1.1:
[email protected], supports-color@^8, supports-color@^8.1.1:
version "8.1.1"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-8.1.1.tgz#cd6fc17e28500cff56c1b86c0a7fd4a54a73005c"
integrity sha512-MpUEN2OodtUzxvKQl72cUF7RQ5EiHsGvSsVG0ia9c5RbWGL2CI4C7EpPS8UTBIplnlzZiNuV56w+FuNxy3ty2Q==
Expand Down

0 comments on commit ac32408

Please sign in to comment.