Skip to content

Commit

Permalink
Accept piped input.
Browse files Browse the repository at this point in the history
  • Loading branch information
kblum committed Oct 19, 2016
1 parent 9b412b3 commit 6d28c24
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -51,6 +51,7 @@ If you want to display small output, provide `opts` with `small`:

$ qrcode-terminal --help
$ qrcode-terminal 'http://github.com'
$ echo 'http://github.com' | qrcode-terminal

# Support

Expand Down
65 changes: 49 additions & 16 deletions bin/qrcode-terminal.js
Expand Up @@ -9,35 +9,68 @@ var qrcode = require('../lib/main'),
fs = require('fs');

/*!
* Parse the process name and input
* Parse the process name
*/

var name = process.argv[1].replace(/^.*[\\\/]/, '').replace('.js', ''),
input = process.argv[2];
var name = process.argv[1].replace(/^.*[\\\/]/, '').replace('.js', '');

/*!
* Display help
* Parse the input
*/

if (!input || input === '-h' || input === '--help') {
help();
process.exit();
}
if (process.stdin.isTTY) {
// called with input as argument, e.g.:
// ./qrcode-terminal.js "INPUT"

/*!
* Display version
*/
var input = process.argv[2];
handleInput(input);
} else {
// called with piped input, e.g.:
// echo "INPUT" | ./qrcode-terminal.js

if (input === '-v' || input === '--version') {
version();
process.exit();
var readline = require('readline');

var interface = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});

interface.on('line', function(line) {
handleInput(line);
});
}

/*!
* Render the QR Code
* Process the input
*/

qrcode.generate(input);
function handleInput(input) {

/*!
* Display help
*/

if (!input || input === '-h' || input === '--help') {
help();
process.exit();
}

/*!
* Display version
*/

if (input === '-v' || input === '--version') {
version();
process.exit();
}

/*!
* Render the QR Code
*/

qrcode.generate(input);
}

/*!
* Helper functions
Expand Down

0 comments on commit 6d28c24

Please sign in to comment.