-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Right now it is very difficult to determine if a terminal supports colors or not. This function adds this functionality by detecting environment variables and checking process. Backport-PR-URL: #19230 PR-URL: #17615 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
- Loading branch information
1 parent
5aa3a2d
commit ead727c
Showing
6 changed files
with
175 additions
and
8 deletions.
There are no files selected for viewing
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
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
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert').strict; | ||
/* eslint-disable no-restricted-properties */ | ||
const { openSync } = require('fs'); | ||
const tty = require('tty'); | ||
|
||
const { WriteStream } = require('tty'); | ||
|
||
// Do our best to grab a tty fd. | ||
function getTTYfd() { | ||
const ttyFd = [0, 1, 2, 4, 5].find(tty.isatty); | ||
if (ttyFd === undefined) { | ||
try { | ||
return openSync('/dev/tty'); | ||
} catch (e) { | ||
// There aren't any tty fd's available to use. | ||
return -1; | ||
} | ||
} | ||
return ttyFd; | ||
} | ||
|
||
const fd = getTTYfd(); | ||
|
||
// Give up if we did not find a tty | ||
if (fd === -1) | ||
common.skip(); | ||
|
||
const writeStream = new WriteStream(fd); | ||
|
||
let depth = writeStream.getColorDepth(); | ||
|
||
assert.equal(typeof depth, 'number'); | ||
assert(depth >= 1 && depth <= 24); | ||
|
||
// If the terminal does not support colors, skip the rest | ||
if (depth === 1) | ||
common.skip(); | ||
|
||
assert.notEqual(writeStream.getColorDepth({ TERM: 'dumb' }), depth); | ||
|
||
// Deactivate colors | ||
const tmp = process.env.NODE_DISABLE_COLORS; | ||
process.env.NODE_DISABLE_COLORS = 1; | ||
|
||
depth = writeStream.getColorDepth(); | ||
|
||
assert.equal(depth, 1); | ||
|
||
process.env.NODE_DISABLE_COLORS = tmp; |