Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
26 additions
and
32 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,38 @@ | ||
import { stdout } from 'process' | ||
|
||
import Chalk from 'chalk' | ||
import { stdout as supportsColor } from 'supports-color' | ||
|
||
// Retrieve `chalk` instance. | ||
// Allows forcing `colors` with `true` or `false` (default: guessed). | ||
// Use `stdout.getColorDepth()` instead of chalk's default behavior (relying | ||
// on `supports-color`) because it behaves more correctly. | ||
export const getChalk = function (colors) { | ||
const level = getLevel(colors) | ||
return new Chalk.Instance({ level }) | ||
const chalk = new Chalk.Instance({ level }) | ||
return chalk | ||
} | ||
|
||
const getLevel = function (colors) { | ||
if (!colors) { | ||
if (colors === false) { | ||
return 0 | ||
} | ||
|
||
const terminalLevel = getTerminalLevel() | ||
|
||
if (colors === undefined) { | ||
return terminalLevel | ||
} | ||
|
||
return Math.max(terminalLevel, 1) | ||
} | ||
|
||
const getTerminalLevel = function () { | ||
if (!stdout.isTTY) { | ||
return 0 | ||
} | ||
|
||
return Math.max(supportsColor.level, 1) | ||
return DEPTH_TO_LEVEL[stdout.getColorDepth()] | ||
} | ||
|
||
export const DEFAULT_COLORS = Boolean(supportsColor) | ||
// Maps chalk levels to color depth | ||
const DEPTH_TO_LEVEL = { 1: 0, 4: 1, 8: 2, 24: 3 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters