Skip to content

Commit

Permalink
Add --no-newline flag (#16)
Browse files Browse the repository at this point in the history
Co-authored-by: Marc Abramowitz <[email protected]>
  • Loading branch information
Qix- and msabramo committed Sep 14, 2021
1 parent 8caedad commit 64416bc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
30 changes: 26 additions & 4 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ const cli = meow(`
$ 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.
--template, -t Style template. The \`~\` character negates the style.
--stdin Read input from stdin rather than from arguments.
--no-newline, -n Don't emit a newline (\`\\n\`) after the input.
--demo Demo of all Chalk styles.
Examples
$ chalk red bold 'Unicorns & Rainbows'
Expand All @@ -61,6 +62,10 @@ const cli = meow(`
stdin: {
type: 'boolean'
},
noNewline: {
type: 'boolean',
alias: 'n'
},
demo: {
type: 'boolean'
}
Expand All @@ -78,7 +83,24 @@ function init(data) {
}

const fn = dotProp.get(chalk, styles.join('.'));
console.log(fn(data.replace(/\n$/, '')));
process.stdout.write(fn(data.replace(/\n$/, '')));

// The following is unfortunately a bit complex, because we're trying to
// support both `-n` and `--no-newline` flags and this is a little tricky
// with the current state of [meow](https://www.npmjs.com/package/meow) and
// [yargs-parser](https://github.com/yargs/yargs-parser), which meow uses.
//
// There are two conditions in the following `if` statement:
//
// - `cli.flags.noNewline` is set when `-n` is passed.
// - `cli.flags.newline` is set to `false` when `--no-newline` is passed.
//
// We're hoping to simplify this in the future. See:
// https://github.com/chalk/chalk-cli/issues/30
//
if (!cli.flags.noNewline && cli.flags.newline !== false) {
process.stdout.write('\n');
}
}

if (process.stdin.isTTY || !cli.flags.stdin) {
Expand Down
7 changes: 4 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ $ chalk --help
$ echo <string> | chalk <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.
--template, -t Style template. The `~` character negates the style.
--stdin Read input from stdin rather than from arguments.
--no-newline, -n Don't emit a newline (`\n`) after the input.
--demo Demo of all Chalk styles.
Examples
$ chalk red bold 'Unicorns & Rainbows'
Expand Down
10 changes: 10 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,13 @@ test('template escaping #1', templateMacro, '{red hey\\} still red} not red',
chalk.red('hey} still red') + ' not red');
test('template escaping #2', templateMacro, '{red hey\\\\} not red',
chalk.red('hey\\') + ' not red');

test('without -n, output has trailing newline', macro,
{args: ['red', 'bold', 'unicorn'], opts: {stripEof: false}},
chalk.red.bold('unicorn') + '\n');
test('with -n, output has NO trailing newline', macro,
{args: ['-n', 'red', 'bold', 'unicorn'], opts: {stripEof: false}},
chalk.red.bold('unicorn') /* No trailing newline */);
test('with --no-newline, output has NO trailing newline', macro,
{args: ['--no-newline', 'red', 'bold', 'unicorn'], opts: {stripEof: false}},
chalk.red.bold('unicorn') /* No trailing newline */);

0 comments on commit 64416bc

Please sign in to comment.