Skip to content

Commit

Permalink
Use dots animation in certain Windows shells that support it (#12)
Browse files Browse the repository at this point in the history
* Use dots animation in certain Windows shells that support it

* Update changelog.md

Co-Authored-By: Juan Bautista Carpanelli <juanbanelli@gmail.com>

* Update index.js

Co-Authored-By: Juan Bautista Carpanelli <juanbanelli@gmail.com>

* Update utils.js

Co-Authored-By: Juan Bautista Carpanelli <juanbanelli@gmail.com>

* Perform suggested refactor

* Fix function call in utils.js
  • Loading branch information
MLoughry authored and jbcarpanelli committed Jul 1, 2019
1 parent 6033b14 commit 80fa603
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ Parameters
- **spinnerColor** - `string`: Any valid [chalk color](https://github.com/chalk/chalk#colors). The default value is `greenBright`.
- **succeedPrefix** - `string`: The default value is ✓.
- **failPrefix**- `string`: The default value is ✖.
- **spinner**- `object`:
- **spinner**- `object`:
- **interval** - `number`
- **frames** - `string[]`

You can see the already provided spinner [here](https://github.com/jcarpanelli/spinnies/blob/master/spinners.json).
- **disableSpins** - `boolean`: Disable spins (will still print raw messages).

*Note: If you are working in any `win32` platform, the default spin animation will be overriden. You can get rid of this defining a different spinner animation manually.*
*Note: If you are working in any `win32` platform, the default spin animation will be overriden. You can get rid of this defining a different spinner animation manually, or by using the integrated VSCode terminal or Windows Terminal.*

Example:
Example:

```js
const spinner = { interval: 80, frames: ['🍇', '🍈', '🍉', '🍋'] }
Expand Down Expand Up @@ -132,7 +132,7 @@ Parameters:

Return value: Returns the spinner's options.

Example:
Example:

```js
const spinnies = new Spinnies();
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Changed
- Use the Unicode dots animation when run inside a VSCode integrated terminal or Windows Terminal.

## [0.4.2] - 2019-06-18
### Fixed
- Fix line breaks when a custom succeedPrefix/failPrefix is provided
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ const chalk = require('chalk');
const cliCursor = require('cli-cursor');
const { dashes, dots } = require('./spinners');

const { purgeSpinnerOptions, purgeSpinnersOptions, colorOptions, breakText, getLinesLength } = require('./utils');
const { purgeSpinnerOptions, purgeSpinnersOptions, colorOptions, breakText, getLinesLength, terminalSupportsUnicode } = require('./utils');
const { isValidStatus, writeStream, cleanStream } = require('./utils');

class Spinnies {
constructor(options = {}) {
options = purgeSpinnersOptions(options);
this.options = {
this.options = {
spinnerColor: 'greenBright',
succeedColor: 'green',
failColor: 'red',
spinner: process.platform === 'win32' ? dashes : dots,
spinner: terminalSupportsUnicode() ? dots : dashes,
disableSpins: false,
...options
};
Expand Down
20 changes: 17 additions & 3 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function purgeSpinnersOptions({ spinner, disableSpins, ...others }) {
}

function turnToValidSpinner(spinner = {}) {
const platformSpinner = process.platform === 'win32' ? dashes : dots;
const platformSpinner = terminalSupportsUnicode() ? dots : dashes;
if (!typeof spinner === 'object') return platformSpinner;
let { interval, frames } = spinner;
if (!Array.isArray(frames) || frames.length < 1) frames = platformSpinner.frames;
Expand All @@ -47,8 +47,13 @@ function colorOptions({ color, succeedColor, failColor, spinnerColor }) {
}

function prefixOptions({ succeedPrefix, failPrefix }) {
succeedPrefix = succeedPrefix ? succeedPrefix : (process.platform === 'win32' ? '√' : '✓');
failPrefix = failPrefix ? failPrefix : (process.platform === 'win32' ? '×' : '✖');
if(terminalSupportsUnicode()) {
succeedPrefix = succeedPrefix || '✓';
failPrefix = failPrefix || '✖';
} else {
succeedPrefix = succeedPrefix || '√';
failPrefix = failPrefix || '×';
}

return { succeedPrefix, failPrefix };
}
Expand Down Expand Up @@ -90,6 +95,14 @@ function cleanStream(stream, rawLines) {
readline.moveCursor(stream, 0, -rawLines.length);
}

function terminalSupportsUnicode() {
// The default command prompt and powershell in Windows do not support Unicode characters.
// However, the VSCode integrated terminal and the Windows Terminal both do.
return process.platform !== 'win32'
|| process.env.TERM_PROGRAM === 'vscode'
|| !!process.env.WT_SESSION
}

module.exports = {
purgeSpinnersOptions,
purgeSpinnerOptions,
Expand All @@ -98,4 +111,5 @@ module.exports = {
getLinesLength,
writeStream,
cleanStream,
terminalSupportsUnicode,
}

0 comments on commit 80fa603

Please # to comment.