Skip to content

Commit

Permalink
Improve detection for terminals supporting Unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Mar 17, 2021
1 parent 498c40a commit c884e0d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 20 deletions.
12 changes: 3 additions & 9 deletions index.js
Expand Up @@ -7,27 +7,21 @@ 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;

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) {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions 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'});
Expand Down
9 changes: 5 additions & 4 deletions package.json
Expand Up @@ -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"
}
}
10 changes: 5 additions & 5 deletions test.js
Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -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();
Expand All @@ -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 => {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit c884e0d

Please sign in to comment.