Skip to content

Commit

Permalink
Merge pull request #23 from webdiscus/fix-win
Browse files Browse the repository at this point in the history
fix: correct detect TTY on Windows platform
  • Loading branch information
webdiscus authored Jul 23, 2024
2 parents 9cdd5c7 + 4e03acd commit 07d7305
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Change log

## 3.3.2 (2024-07-22)
## 3.3.2 (2024-07-23)

- fix: correct detect TTY on Windows platform
- chore: optimize code to reduce the size by ~50 bytes
- chore: add benchmarks for `kolorist` package
- test: add test matrix for windows on GitHub
Expand Down
12 changes: 7 additions & 5 deletions src/color-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import { SPACE_MONO, SPACE_16_COLORS, SPACE_256_COLORS, SPACE_TRUE_COLORS } from
*
* @param {object} env
* @param {boolean} isTTY
* @param {boolean} isWin
* @return {number}
*/
const detectColorSpace = (env, isTTY) => {
const detectColorSpace = (env, isTTY, isWin) => {
const inEnvSome = (arr) => arr.some(val => val in env);
const { TERM: term, COLORTERM: colorterm } = env;

Expand Down Expand Up @@ -48,6 +49,9 @@ const detectColorSpace = (env, isTTY) => {
// unknown output or colors are not supported
if (!isTTY || /-mono|dumb/i.test(term)) return SPACE_MONO;

// truecolor support starts from Windows 10 build 14931 (2016-09-21), in 2024 we assume modern Windows is used
if (isWin) return SPACE_TRUE_COLORS;

// terminals, that support truecolor, e.g., iTerm, VSCode
if (colorterm === 'truecolor' || colorterm === '24bit') return SPACE_TRUE_COLORS;

Expand All @@ -63,8 +67,7 @@ const detectColorSpace = (env, isTTY) => {
if (/-256(colou?r)?$/i.test(term)) return SPACE_256_COLORS;

// known terminals supporting 16 colors
if (/^screen|^tmux|^xterm|^vt[1-5][0-9]([0-9])?|^ansi|color|cygwin|linux|mintty|rxvt/i.test(
term)) return SPACE_16_COLORS;
if (/^screen|^tmux|^xterm|^vt[1-5][0-9]([0-9])?|^ansi|color|cygwin|linux|mintty|rxvt/i.test(term)) return SPACE_16_COLORS;

// note: for unknown terminals we allow truecolor output,
// because all terminals supporting only 16 or 256 colors have already been detected above
Expand Down Expand Up @@ -138,8 +141,7 @@ export const getColorSpace = (mockThis) => {
if (isForceDisabled) return SPACE_MONO;

if (colorSpace < 0) {
// truecolor support starts from Windows 10 build 14931 (2016-09-21), in 2024 we assume modern Windows is used
colorSpace = isWin ? SPACE_TRUE_COLORS : detectColorSpace(env, isTTY);
colorSpace = detectColorSpace(env, isTTY, isWin);
}

return isForceEnabled && colorSpace === SPACE_MONO ? SPACE_TRUE_COLORS : colorSpace;
Expand Down
4 changes: 3 additions & 1 deletion test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,8 @@ describe('Node.JS different env', () => {
platform: 'win32',
env: {},
argv: [],
stdout: { isTTY: true },
stderr: { isTTY: true },
},

});
Expand Down Expand Up @@ -776,7 +778,7 @@ describe('Deno support', () => {
build: {
os: 'win32',
},
isatty: (rid) => false, // analog to process.stdout.isTTY in node
isatty: (rid) => true, // analog to process.stdout.isTTY in node
},

});
Expand Down
21 changes: 16 additions & 5 deletions test/utils/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { execSync } from 'child_process';

const isWin = process.platform === 'win32';

/**
* Escape the slash `\` in ESC-symbol.
* Use it to show by an error the received ESC sequence string in console output.
Expand All @@ -12,13 +14,22 @@ export const esc = (str) => str.replace(/\x1b/g, '\\x1b');
/**
* Return output of javascript file.
*
* @param {string} file
* @param {Array<string>} flags
* @param {Array<string>} env
* @return {string}
* @param {string} file The file path to execute.
* @param {Array<string>} flags The CLI flags.
* @param {Array<string>} env The environment variables.
* @return {string} CLI output as result of execution.
*/
export const execScriptSync = (file, flags = [], env = []) => {
const envVars = env.length ? '' + env.join(' ') + ' ' : '';
let envVars = env.length ? '' + env.join(' ') + ' ' : '';

// set ENV variables on Windows, e.g.: `set FORCE_COLOR=true | node app.js`
if (isWin) {
envVars = [];
env.forEach((expression) => {
envVars.push(`set ${expression} | `);
});
}

const cmd = envVars + 'node ' + file + ' ' + flags.join(' ');
const result = execSync(cmd);

Expand Down

0 comments on commit 07d7305

Please sign in to comment.