GithubHelp home page GithubHelp logo

esc-exit's Introduction

esc-exit

Exit the process when the esc key is pressed

Useful for CLI tools to let the user press esc or control+c to get out.

Install

npm install esc-exit

Usage

import escExit from 'esc-exit';

escExit();

API

Using this prevents the process from exiting normally.

There are multiple ways you can handle this:

  • Call the unsubscribe method that escExit() returns when you're done listening. It will then let the process exit normally.

  • Call process.exit() to exit the process directly.

esc-exit's People

Contributors

cyansalt avatar richienb avatar sindresorhus avatar streetstrider avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

esc-exit's Issues

esc-exit causes inquirer.js to print its statements twice.

I wanted to add the ability to terminate inquirer.js prompts by using the esc key. But every time I call escExit() before I call await inquirer.prompt(prompts) the questions do not get updated but get printed onto a new line:
screen shot 2018-03-13 at 11 43 26
screen shot 2018-03-13 at 11 43 49

I am not sure what is causing the problem here. Here is the code that is used to run my cli:

const escExit = require('esc-exit');
const inquirer = require('inquirer');
const meow = require('meow');
const { PathPrompt } = require('inquirer-path');

const prompt = require('./prompt');

const cli = meow(`
  Usage
    $ carbon <file>

  Options
    --background, -bg          Sets the background color (hex)
    --interactive, -i          Starts interactive mode
    --horizontal-padding, -hp  Sets the horizontal padding
    --linenumbers, -ln         Sets if line numbers should be used
    --theme, -t                Sets the theme
    --shadows, -s              Sets if shadows should be used
    --verbose, -v              Prints more stuff
    --vertical-padding, -vp    Sets the vertical padding
    --window-controls, -wc     Sets if the window controls should be used

  Examples
    carbon showcase.js
`, {
  flags: {
    background: {
      alias: 'bg',
      type: 'string',
    },
    interactive: {
      alias: 'i',
      type: 'boolean',
    },
    "linenumbers": {
      alias: 'ln',
      type: 'boolean',
    },
    "horizontalpadding": {
      alias: 'hp',
      type: 'string',
    },
    theme: {
      alias: 't',
      type: 'string',
    },
    shadows: {
      alias: 's',
      type: 'boolean',
    },
    verbose: {
      alias: 'v',
      type: 'boolean',
    },
    "verticalpadding": {
      alias: 'vp',
      type: 'string',
    },
    "windowcontrols": {
      alias: 'wc',
      type: 'boolean',
    },
  },
});

async function init(flags) {
  escExit();
  await startInteractive();
}

async function startInteractive() {
  // add prompt for files.
  inquirer.prompt.registerPrompt('path', PathPrompt);

  const result = await inquirer.prompt(prompt);
  console.log(result);
}

if (cli.input.length === 0) {
  init(cli.flags);
}

Ctrl+C doesn't trigger SIGINT

When using esc-exit on a TTY terminal Ctrl+C doesn't exit the program as expected.

I looked and it's because the code calls process.stdin.setRawMode(true), which bypasses the native keypress detection.

The fix is to add an additional detection in the listener for Ctrl+C, to emit the SIGINT (or exit the process):

if (key && key.ctrl && key.name === 'c' && process.stdin.isTTY) {
	process.emit('SIGINT');
}

I can create a pull request if needed.

The implementation of the unsubscribe method

I stumbled upon this package and I noticed that the function returned by excExit calls process.stdin.on again. Is this a typo for off? I'm not sure I fully understand how it works ……

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.