diff --git a/README.md b/README.md index 53da6c3..f5c830f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/bin/qrcode-terminal.js b/bin/qrcode-terminal.js index fcc7ee9..6476287 100755 --- a/bin/qrcode-terminal.js +++ b/bin/qrcode-terminal.js @@ -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