SBoudrias / Inquirer.js

A collection of common interactive command line user interfaces.
MIT License
20.28k stars 1.3k forks source link

Redirect Output #519

Closed brianantonelli closed 7 years ago

brianantonelli commented 7 years ago

Would love to see a configuration option to redirect output to stderr instead of stdout. That way programs could support functionality like:

eval $(myapp dosomething)

Then we could then display the prompt through stderr so that only the response will be evaluated.

remyrylan commented 7 years ago

@brianantonelli I needed this too and just discovered it's already available.

const inquirer = require('inquirer')

const prompt = inquirer.createPromptModule({ output: process.stderr })

prompt(/* question array/object here */)
SBoudrias commented 7 years ago

@jrylan thanks for following up with the proper way to handle it <3

brianantonelli commented 7 years ago

Thx!

brianantonelli commented 7 years ago

So I'm still seeing some issues with this. I followed @jrylan 's instructions and my prompts are now directed to stderr which is perfect. However, I'm still seeing some rogue control characters being sent to stdout.. Thoughts?

^[[?25l^[[?25hexport AWS_ACCESS_KEY_ID=aaa && export AWS_SECRET_ACCESS_KEY=bbb && export AWS_SESSION_TOKEN=ccc
^[[?25h
remyrylan commented 7 years ago

@brianantonelli That's strange, is that on AWS? I'm using iTerm2 as my terminal on macOS and all the output is identical when using process.stderr -- everything works perfectly fine.

brianantonelli commented 7 years ago

No, running this on OSX in Terminal. As soon as I make the call to inquirer I get those control chars. The one at the end really throws me because its printing that many function calls later. I only see these characters in VIM. I caught this because it breaks wrapping the output in a eval $(foo) call.

SBoudrias commented 7 years ago

@brianantonelli weird, but that doesn't seem relate to Inquirer. As you might guess, we're not snooping in on your AWS creds.

brianantonelli commented 7 years ago

That's just an example output from an application that leverages this library. In my application if I pipe all data to stderr using the above solution and only do a single output to stdout (the AWS keys export command in this case) I see these control characters. If I remove the call to inquirer the control chars disappear.

SBoudrias commented 7 years ago

@brianantonelli I'm not super familiar with that, but any chance some ANSI control chars can't be use on stderr?

brianantonelli commented 7 years ago

This only occurs if you use the list prompt. If I do a standard input prompt the characters aren't there. Here's a snippet to reproduce this:

var inquirer = require('inquirer'),
    prompt   = inquirer.createPromptModule({ output: process.stderr });

console.error('start');

if(process.argv[2] === 'inq'){
    prompt([ { type: 'list', name: 'test', message: 'Test', choices: ['a', 'b', 'c'] }]).then(function(answers){
        console.error(answers);
    });
}

console.log('test');
console.error('stop');
brianantonelli commented 7 years ago

Any ideas?

popey456963 commented 7 years ago

@brianantonelli Those control codes are for colours. This is something inquirer uses. Vi doesn't support those control code colours, so displays the base codes. Hence you're running into an issue.

brianantonelli commented 7 years ago

Can't those be disabled?

popey456963 commented 7 years ago

@brianantonelli You can use something like strip-ansi to remove ansi from a string, I've opened an issue to be able to use it with streams https://github.com/jlord/sheetsee.js/issues/26. However, it would probably be pretty simple to allow it to support streams yourself.

It's been a while since I've manipulated such streams, but with a stream.Transform it should work.

SBoudrias commented 7 years ago

Simpler yet, I believe chalk doesn't print colors when --no-color is in the process argv.