1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- 'use strict';
- var _ = require('lodash');
- var MuteStream = require('mute-stream');
- var readline = require('readline');
-
-
-
- class UI {
- constructor(opt) {
-
-
- if (!this.rl) {
- this.rl = readline.createInterface(setupReadlineOptions(opt));
- }
-
- this.rl.resume();
-
- this.onForceClose = this.onForceClose.bind(this);
-
-
- process.on('exit', this.onForceClose);
-
-
- this.rl.on('SIGINT', this.onForceClose);
- }
-
-
-
-
- onForceClose() {
- this.close();
- process.kill(process.pid, 'SIGINT');
- console.log('');
- }
-
-
-
-
- close() {
-
- this.rl.removeListener('SIGINT', this.onForceClose);
- process.removeListener('exit', this.onForceClose);
-
- this.rl.output.unmute();
-
- if (this.activePrompt && typeof this.activePrompt.close === 'function') {
- this.activePrompt.close();
- }
-
-
- this.rl.output.end();
- this.rl.pause();
- this.rl.close();
- }
- }
-
- function setupReadlineOptions(opt) {
- opt = opt || {};
-
-
- var input = opt.input || process.stdin;
-
-
- var ms = new MuteStream();
- ms.pipe(opt.output || process.stdout);
- var output = ms;
-
- return _.extend(
- {
- terminal: true,
- input: input,
- output: output
- },
- _.omit(opt, ['input', 'output'])
- );
- }
-
- module.exports = UI;
|