Skip to content

Commit

Permalink
Support 256 color output on Windows 10 (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
adalinesimonian authored and sindresorhus committed Jun 30, 2017
1 parent de8d312 commit 340588e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
const os = require('os');
const hasFlag = require('has-flag');

const env = process.env;
Expand Down Expand Up @@ -45,6 +46,20 @@ let supportLevel = (() => {
}

if (process.platform === 'win32') {
// Node.js 7.5.0 is the first version of Node.js to include a patch to
// libuv that enables 256 color output on Windows. Anything earlier and it
// won't work. However, here we target Node.js 8 at minimum as it is an LTS
// release, and Node.js 7 is not. Windows 10 build 10586 is the first Windows
// release that supports 256 colors.
const osRelease = os.release().split('.');
if (
Number(process.version.split('.')[0]) >= 8 &&
Number(osRelease[0]) >= 10 &&
Number(osRelease[2]) >= 10586
) {
return 2;
}

return 1;
}

Expand Down
58 changes: 58 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import os from 'os';
import {serial as test} from 'ava';
import importFresh from 'import-fresh';

test.beforeEach(() => {
Object.defineProperty(process, 'platform', {
value: 'linux'
});
process.stdout.isTTY = true;
process.argv = [];
process.env = {};
Expand Down Expand Up @@ -185,6 +189,9 @@ test('support screen-256color', t => {
});

test('level should be 3 when using iTerm 3.0', t => {
Object.defineProperty(process, 'platform', {
value: 'darwin'
});
process.env = {
TERM_PROGRAM: 'iTerm.app',
TERM_PROGRAM_VERSION: '3.0.10'
Expand All @@ -194,10 +201,61 @@ test('level should be 3 when using iTerm 3.0', t => {
});

test('level should be 2 when using iTerm 2.9', t => {
Object.defineProperty(process, 'platform', {
value: 'darwin'
});
process.env = {
TERM_PROGRAM: 'iTerm.app',
TERM_PROGRAM_VERSION: '2.9.3'
};
const result = importFresh('.');
t.is(result.level, 2);
});

test('return level 1 if on Windows earlier than 10 build 10586 and Node version is < 8.0.0', t => {
Object.defineProperty(process, 'platform', {
value: 'win32'
});
Object.defineProperty(process, 'version', {
value: '7.5.0'
});
os.release = () => '10.0.10240';
const result = importFresh('.');
t.is(result.level, 1);
});

test('return level 1 if on Windows 10 build 10586 or later and Node version is < 8.0.0', t => {
Object.defineProperty(process, 'platform', {
value: 'win32'
});
Object.defineProperty(process, 'version', {
value: '7.5.0'
});
os.release = () => '10.0.10586';
const result = importFresh('.');
t.is(result.level, 1);
});

test('return level 1 if on Windows earlier than 10 build 10586 and Node version is >= 8.0.0', t => {
Object.defineProperty(process, 'platform', {
value: 'win32'
});
Object.defineProperty(process, 'version', {
value: '8.0.0'
});
os.release = () => '10.0.10240';
const result = importFresh('.');
t.is(result.level, 1);
});

test('return level 2 if on Windows 10 build 10586 or later and Node version is >= 8.0.0', t => {
Object.defineProperty(process, 'platform', {
value: 'win32'
});
Object.defineProperty(process, 'version', {
value: '8.0.0'
});
os.release = () => '10.0.10586';
const result = importFresh('.');
t.is(result.level, 2);
});

0 comments on commit 340588e

Please sign in to comment.