From c884e0d449427cbf5922f3e46e8ba50ed45f4fef Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 17 Mar 2021 17:33:06 +0700 Subject: [PATCH] Improve detection for terminals supporting Unicode --- index.js | 12 +++--------- index.test-d.ts | 4 ++-- package.json | 9 +++++---- test.js | 10 +++++----- 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index 1d1e977..6725050 100644 --- a/index.js +++ b/index.js @@ -7,19 +7,13 @@ const logSymbols = require('log-symbols'); const stripAnsi = require('strip-ansi'); const wcwidth = require('wcwidth'); const isInteractive = require('is-interactive'); +const isUnicodeSupported = require('is-unicode-supported'); const {BufferListStream} = require('bl'); const TEXT = Symbol('text'); const PREFIX_TEXT = Symbol('prefixText'); - const ASCII_ETX_CODE = 0x03; // Ctrl+C emits this code -const terminalSupportsUnicode = () => ( - process.platform !== 'win32' || - process.env.TERM_PROGRAM === 'vscode' || - Boolean(process.env.WT_SESSION) -); - class StdinDiscarder { constructor() { this.requests = 0; @@ -27,7 +21,7 @@ class StdinDiscarder { this.mutedStream = new BufferListStream(); this.mutedStream.pipe(process.stdout); - const self = this; + const self = this; // eslint-disable-line unicorn/no-this-assignment this.ourEmit = function (event, data, ...args) { const {stdin} = process; if (self.requests > 0 || stdin.emit === self.ourEmit) { @@ -169,7 +163,7 @@ class Ora { } this._spinner = spinner; - } else if (!terminalSupportsUnicode()) { + } else if (!isUnicodeSupported()) { this._spinner = cliSpinners.line; } else if (spinner === undefined) { // Set default spinner diff --git a/index.test-d.ts b/index.test-d.ts index 93a37db..7c74fe1 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,7 +1,7 @@ import {expectType} from 'tsd'; import {PassThrough as PassThroughStream} from 'stream'; -import ora = require('.'); -import {promise} from '.'; +import ora = require('./index.js'); +import {promise} from './index.js'; const spinner = ora('Loading unicorns'); ora({text: 'Loading unicorns'}); diff --git a/package.json b/package.json index cbe3730..db17774 100644 --- a/package.json +++ b/package.json @@ -37,20 +37,21 @@ "idle" ], "dependencies": { - "bl": "^4.0.3", + "bl": "^4.1.0", "chalk": "^4.1.0", "cli-cursor": "^3.1.0", "cli-spinners": "^2.5.0", "is-interactive": "^1.0.0", - "log-symbols": "^4.0.0", + "is-unicode-supported": "^0.1.0", + "log-symbols": "^4.1.0", "strip-ansi": "^6.0.0", "wcwidth": "^1.0.1" }, "devDependencies": { - "@types/node": "^14.14.16", + "@types/node": "^14.14.35", "ava": "^2.4.0", "get-stream": "^6.0.0", "tsd": "^0.14.0", - "xo": "^0.36.1" + "xo": "^0.38.2" } } diff --git a/test.js b/test.js index e4a2ada..6f0e6c2 100644 --- a/test.js +++ b/test.js @@ -2,9 +2,9 @@ import {PassThrough as PassThroughStream} from 'stream'; import getStream from 'get-stream'; import test from 'ava'; import stripAnsi from 'strip-ansi'; -import Ora from '.'; +import Ora from './index.js'; -const spinnerChar = process.platform === 'win32' ? '-' : '⠋'; +const spinnerCharacter = process.platform === 'win32' ? '-' : '⠋'; const noop = () => {}; const getPassThroughStream = () => { @@ -41,7 +41,7 @@ const macro = async (t, fn, expected, extraOptions = {}) => { test('main', macro, spinner => { spinner.stop(); -}, new RegExp(`${spinnerChar} foo`)); +}, new RegExp(`${spinnerCharacter} foo`)); test('title shortcut', async t => { const stream = getPassThroughStream(); @@ -58,7 +58,7 @@ test('title shortcut', async t => { stream.end(); - t.is(await output, `${spinnerChar} foo`); + t.is(await output, `${spinnerCharacter} foo`); }); test('`.id` is not set when created', t => { @@ -165,7 +165,7 @@ test('.promise() - resolves', async t => { test('.promise() - rejects', async t => { const stream = getPassThroughStream(); const output = getStream(stream); - const rejects = Promise.reject(new Error()); + const rejects = Promise.reject(new Error()); // eslint-disable-line unicorn/error-message Ora.promise(rejects, { stream,