Skip to content

Commit

Permalink
Don't use stdin unless --stdin flag is specified (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
msabramo committed Sep 13, 2021
1 parent 932d798 commit 14a98bc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
9 changes: 7 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,27 @@ const printAllStyles = () => {
const cli = meow(`
Usage
$ chalk <style> … <string>
$ echo <string> | chalk <style> …
$ echo <string> | chalk --stdin <style> …
Options
--template, -t Style template. The \`~\` character negates the style.
--stdin Read input from stdin rather than from arguments.
--demo Demo of all Chalk styles.
Examples
$ chalk red bold 'Unicorns & Rainbows'
$ chalk -t '{red.bold Unicorns & Rainbows}'
$ chalk -t '{red.bold Dungeons and Dragons {~bold.blue (with added fairies)}}'
$ echo 'Unicorns from stdin' | chalk --stdin red bold
`, {
flags: {
template: {
type: 'string',
alias: 't'
},
stdin: {
type: 'boolean'
},
demo: {
type: 'boolean'
}
Expand All @@ -75,7 +80,7 @@ function init(data) {
console.log(fn(data.replace(/\n$/, '')));
}

if (process.stdin.isTTY || cli.flags.stdin === false) {
if (process.stdin.isTTY || !cli.flags.stdin) {
if (cli.flags.demo) {
printAllStyles();
} else if (cli.flags.template) {
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ $ chalk --help
Options
--template, -t Style template. The `~` character negates the style.
--stdin Read input from stdin rather than from arguments.
--demo Demo of all Chalk styles.
Examples
$ chalk red bold 'Unicorns & Rainbows'
$ chalk -t '{red.bold Unicorns & Rainbows}'
$ chalk -t '{red.bold Dungeons and Dragons {~bold.blue (with added fairies)}}'
$ echo 'Unicorns from stdin' | chalk --stdin red bold
```

See [supported styles](https://github.com/chalk/chalk#styles).
Expand Down
4 changes: 3 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ const templateMacro = (t, input, expected) => {

test('main', macro, {args: ['red', 'bold', 'unicorn', '--no-stdin']},
chalk.red.bold('unicorn'));
test('stdin', macro, {args: ['red', 'bold'], opts: {input: 'unicorn'}},
test('default to args; not stdin (#11)', macro, {args: ['red', 'bold', 'unicorn'], opts: {input: ''}},
chalk.red.bold('unicorn'));
test('stdin', macro, {args: ['red', 'bold', '--stdin'], opts: {input: 'unicorn'}},
chalk.red.bold('unicorn'));
test('number', macro, {args: ['red', 'bold', '123', '--no-stdin']},
chalk.red.bold('123'));
Expand Down

0 comments on commit 14a98bc

Please sign in to comment.