├── .editorconfig ├── .gitattributes ├── .github ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 22 14 | - 20 15 | - 18 16 | steps: 17 | - uses: actions/checkout@v4 18 | - uses: actions/setup-node@v4 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | declare const cliCursor: { 2 | /** 3 | Show cursor. 4 | 5 | @param stream - Default: `process.stderr`. 6 | 7 | @example 8 | ``` 9 | import cliCursor from 'cli-cursor'; 10 | 11 | cliCursor.show(); 12 | ``` 13 | */ 14 | show(stream?: NodeJS.WritableStream): void; 15 | 16 | /** 17 | Hide cursor. 18 | 19 | @param stream - Default: `process.stderr`. 20 | 21 | @example 22 | ``` 23 | import cliCursor from 'cli-cursor'; 24 | 25 | cliCursor.hide(); 26 | ``` 27 | */ 28 | hide(stream?: NodeJS.WritableStream): void; 29 | 30 | /** 31 | Toggle cursor visibility. 32 | 33 | @param force - Is useful to show or hide the cursor based on a boolean. 34 | @param stream - Default: `process.stderr`. 35 | 36 | @example 37 | ``` 38 | import cliCursor from 'cli-cursor'; 39 | 40 | const unicornsAreAwesome = true; 41 | cliCursor.toggle(unicornsAreAwesome); 42 | ``` 43 | */ 44 | toggle(force?: boolean, stream?: NodeJS.WritableStream): void; 45 | }; 46 | 47 | export default cliCursor; 48 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import process from 'node:process'; 2 | import restoreCursor from 'restore-cursor'; 3 | 4 | let isHidden = false; 5 | 6 | const cliCursor = {}; 7 | 8 | cliCursor.show = (writableStream = process.stderr) => { 9 | if (!writableStream.isTTY) { 10 | return; 11 | } 12 | 13 | isHidden = false; 14 | writableStream.write('\u001B[?25h'); 15 | }; 16 | 17 | cliCursor.hide = (writableStream = process.stderr) => { 18 | if (!writableStream.isTTY) { 19 | return; 20 | } 21 | 22 | restoreCursor(); 23 | isHidden = true; 24 | writableStream.write('\u001B[?25l'); 25 | }; 26 | 27 | cliCursor.toggle = (force, writableStream) => { 28 | if (force !== undefined) { 29 | isHidden = force; 30 | } 31 | 32 | if (isHidden) { 33 | cliCursor.show(writableStream); 34 | } else { 35 | cliCursor.hide(writableStream); 36 | } 37 | }; 38 | 39 | export default cliCursor; 40 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cli-cursor", 3 | "version": "5.0.0", 4 | "description": "Toggle the CLI cursor", 5 | "license": "MIT", 6 | "repository": "sindresorhus/cli-cursor", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "default": "./index.js" 17 | }, 18 | "sideEffects": false, 19 | "engines": { 20 | "node": ">=18" 21 | }, 22 | "scripts": { 23 | "test": "xo && ava && tsc index.d.ts" 24 | }, 25 | "files": [ 26 | "index.js", 27 | "index.d.ts" 28 | ], 29 | "keywords": [ 30 | "cli", 31 | "cursor", 32 | "ansi", 33 | "toggle", 34 | "display", 35 | "show", 36 | "hide", 37 | "term", 38 | "terminal", 39 | "console", 40 | "tty", 41 | "shell", 42 | "command-line" 43 | ], 44 | "dependencies": { 45 | "restore-cursor": "^5.0.0" 46 | }, 47 | "devDependencies": { 48 | "@types/node": "^20.14.12", 49 | "ava": "^6.1.3", 50 | "typescript": "^5.5.4", 51 | "xo": "^0.59.2" 52 | }, 53 | "ava": { 54 | "workerThreads": false 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # cli-cursor 2 | 3 | > Toggle the CLI cursor 4 | 5 | The cursor is [gracefully restored](https://github.com/sindresorhus/restore-cursor) if the process exits. 6 | 7 | ## Install 8 | 9 | ```sh 10 | npm install cli-cursor 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | import cliCursor from 'cli-cursor'; 17 | 18 | cliCursor.hide(); 19 | 20 | const unicornsAreAwesome = true; 21 | cliCursor.toggle(unicornsAreAwesome); 22 | ``` 23 | 24 | ## API 25 | 26 | ### .show(stream?) 27 | 28 | ### .hide(stream?) 29 | 30 | ### .toggle(force?, stream?) 31 | 32 | #### force 33 | 34 | Useful for showing or hiding the cursor based on a boolean. 35 | 36 | #### stream 37 | 38 | Type: `stream.Writable`\ 39 | Default: `process.stderr` 40 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import process from 'node:process'; 2 | import childProcess from 'node:child_process'; 3 | import test from 'ava'; 4 | import cliCursor from './index.js'; 5 | 6 | const {stderr: {write}} = process; 7 | const SHOW = '\u001B[?25h'; 8 | const HIDE = '\u001B[?25l'; 9 | 10 | process.stderr.isTTY = true; 11 | 12 | function getStderr(function_) { 13 | let result = ''; 14 | 15 | process.stderr.setEncoding('utf8'); 16 | process.stderr.write = string => { 17 | result += string; 18 | }; 19 | 20 | function_(); 21 | process.stderr.write = write; 22 | return result; 23 | } 24 | 25 | test('show', t => { 26 | t.is(getStderr(cliCursor.show), SHOW); 27 | }); 28 | 29 | test('hide', t => { 30 | t.is(getStderr(cliCursor.hide), HIDE); 31 | }); 32 | 33 | test('toggle', t => { 34 | cliCursor.hide(); 35 | t.is(getStderr(cliCursor.toggle), SHOW); 36 | }); 37 | 38 | test('toggle 2', t => { 39 | cliCursor.show(); 40 | t.is(getStderr(cliCursor.toggle), HIDE); 41 | }); 42 | 43 | test('toggle force', t => { 44 | cliCursor.show(); 45 | t.is(getStderr(cliCursor.toggle.bind(null, true)), SHOW); 46 | }); 47 | 48 | test('toggle force 2', t => { 49 | cliCursor.hide(); 50 | t.is(getStderr(cliCursor.toggle.bind(null, true)), SHOW); 51 | }); 52 | 53 | test('toggle force 3', t => { 54 | cliCursor.show(); 55 | t.is(getStderr(cliCursor.toggle.bind(null, false)), HIDE); 56 | }); 57 | 58 | // Used to fail, see sindresorhus/log-update#2 59 | test('require', t => { 60 | t.is(childProcess.execSync('node index.js', {encoding: 'utf8'}), ''); 61 | }); 62 | --------------------------------------------------------------------------------