Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: chalk/chalk-cli
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v5.0.0
Choose a base ref
...
head repository: chalk/chalk-cli
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v5.0.1
Choose a head ref
  • 11 commits
  • 7 files changed
  • 2 contributors

Commits on Sep 14, 2021

  1. Add test for --help (#35)

    msabramo authored Sep 14, 2021
    Copy the full SHA
    7df74af View commit details

Commits on Sep 16, 2021

  1. Copy the full SHA
    c0ec891 View commit details

Commits on Sep 17, 2021

  1. Copy the full SHA
    6dcb95f View commit details

Commits on Sep 22, 2021

  1. Simplify the logic (#43)

    Fixes #31
    sindresorhus authored Sep 22, 2021
    Copy the full SHA
    eb21da0 View commit details

Commits on Sep 26, 2021

  1. Print all styles (#37)

    msabramo authored Sep 26, 2021
    Copy the full SHA
    42db064 View commit details

Commits on Sep 27, 2021

  1. Add test for --demo flag (#38)

    msabramo authored Sep 27, 2021
    Copy the full SHA
    b04fefc View commit details
  2. Copy the full SHA
    7f9e37e View commit details
  3. Meta tweaks

    sindresorhus committed Sep 27, 2021
    Copy the full SHA
    54cef67 View commit details

Commits on Mar 18, 2023

  1. Copy the full SHA
    a0dce6e View commit details
  2. Meta tweaks

    sindresorhus committed Mar 18, 2023
    Copy the full SHA
    a31205a View commit details
  3. 5.0.1

    sindresorhus committed Mar 18, 2023
    Copy the full SHA
    e699de5 View commit details
Showing with 150 additions and 75 deletions.
  1. +2 −0 .replit
  2. +88 −61 cli.js
  3. +1 −1 package.json
  4. +3 −3 readme.md
  5. +39 −10 test.js
  6. +17 −0 test.js.md
  7. BIN test.js.snap
2 changes: 2 additions & 0 deletions .replit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
language = "nodejs"
run = "node cli.js --help && node cli.js --demo"
149 changes: 88 additions & 61 deletions cli.js
Original file line number Diff line number Diff line change
@@ -7,56 +7,71 @@ import getStdin from 'get-stdin';
import meow from 'meow';

const printAllStyles = () => {
const styles = [
'bold',
'dim',
'italic',
'underline',
'inverse',
'strikethrough',
'black',
'red',
'green',
'yellow',
'blue',
'magenta',
'cyan',
'white',
'gray',
'bgBlack',
'bgRed',
'bgGreen',
'bgYellow',
'bgBlue',
'bgMagenta',
'bgCyan',
'bgWhite',
];

console.log(styles.map(style => chalk[style](style)).join(' '));
const textStyles = ['bold', 'dim', 'italic', 'underline', 'strikethrough'];
const colorStyles = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'gray'];
const brightColorStyles = ['blackBright', 'redBright', 'greenBright', 'yellowBright', 'blueBright', 'magentaBright', 'cyanBright', 'whiteBright'];
const bgColorStyles = ['bgBlack', 'bgRed', 'bgGreen', 'bgYellow', 'bgBlue', 'bgMagenta', 'bgCyan', 'bgWhite', 'bgGray'];
const bgBrightColorStyles = ['bgBlackBright', 'bgRedBright', 'bgGreenBright', 'bgYellowBright', 'bgBlueBright', 'bgMagentaBright', 'bgCyanBright', 'bgWhiteBright'];

function styled(style, text) {
if (/^bg[^B]/.test(style)) {
text = chalk.black(text);
}

return chalk[style](text);
}

function showStyles(stylesArray) {
const output = stylesArray.map(style => styled(style, style)).join(' ');
console.log(output);
}

console.log('Available styles:\n');
showStyles(textStyles);
showStyles(colorStyles);
showStyles(brightColorStyles);
showStyles(bgColorStyles);
showStyles(bgBrightColorStyles);
};

const cli = meow(`
Usage
$ chalk <style> … <string>
$ echo <string> | chalk --stdin <style> …
${chalk.greenBright.inverse(' Usage ')}
$ ${chalk.green('chalk')} ${chalk.yellow('[options…]')} ${chalk.cyan('<style> … <string>')}
$ ${chalk.green('echo')} ${chalk.cyan('<string>')} | ${chalk.green('chalk')} ${chalk.yellow('--stdin [options…]')} ${chalk.cyan('<style> …')}
Options
--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.
${chalk.yellowBright.inverse(' Options ')}
${chalk.yellow('--template, -t')} Style template. The \`~\` character negates the style.
${chalk.yellow('--stdin')} Read input from stdin rather than from arguments.
${chalk.yellow('--no-newline, -n')} Don't emit a newline (\`\\n\`) after the input.
${chalk.yellow('--demo')} Demo of all Chalk styles.
${chalk.redBright.inverse(' Examples ')}
Examples
$ chalk red bold 'Unicorns & Rainbows'
${chalk.red.bold('Unicorns & Rainbows')}
$ chalk -t '{red.bold Unicorns & Rainbows}'
${chalk.red.bold('Unicorns & Rainbows')}
$ chalk -t '{red.bold Dungeons and Dragons {~bold.blue (with added fairies)}}'
${chalk`{red.bold Dungeons and Dragons {~bold.blue (with added fairies)}}`}
$ echo 'Unicorns from stdin' | chalk --stdin red bold
${chalk.red.bold('Unicorns from stdin')}
`, {
importMeta: import.meta,
// TODO: Disabled until https://github.com/sindresorhus/meow/issues/197 is fixed.
// allowUnknownFlags: false,
allowUnknownFlags: false,
flags: {
// TODO: Can be removed when https://github.com/sindresorhus/meow/issues/197 is fixed.
help: {
type: 'boolean',
},
version: {
type: 'boolean',
},

template: {
type: 'string',
alias: 't',
@@ -76,10 +91,29 @@ const cli = meow(`

const styles = cli.input;

function handleTemplateFlag(template) {
if (cli.input.length > 0) {
console.error('The --template option only accepts 1 argument');
process.exitCode = 1;
return;
}

try {
const tagArray = [template];
tagArray.raw = tagArray;
console.log(chalk(tagArray));
} catch (error) {
console.error('Something went wrong! Maybe review your syntax?\n');
console.error(error.stack);
process.exitCode = 1;
}
}

function init(data) {
for (const style of styles) {
if (!Object.keys(ansiStyles).includes(style)) {
console.error(`Invalid style: ${style}`);
console.error(chalk`{red Invalid style: {bold ${style}}}\n`);
printAllStyles();
process.exit(1);
}
}
@@ -108,35 +142,28 @@ function init(data) {
function processDataFromArgs() {
if (cli.flags.demo) {
printAllStyles();
} else if (cli.flags.template) {
if (cli.input.length === 0) {
try {
const tagArray = [cli.flags.template];
tagArray.raw = tagArray;
console.log(chalk(tagArray));
} catch (error) {
console.error('Something went wrong! Maybe review your syntax?\n');
console.error(error.stack);
process.exit(1);
}
} else {
console.error('The --template option only accepts 1 argument');
process.exit(1);
}
} else {
if (styles.length < 2) {
console.error('Input required');
process.exit(1);
}
return;
}

init(styles.pop());
if (cli.flags.template) {
handleTemplateFlag(cli.flags.template);
return;
}

if (styles.length < 2) {
console.error('Input required');
process.exitCode = 1;
return;
}

init(styles.pop());
}

async function processDataFromStdin() {
if (styles.length === 0) {
console.error('Input required');
process.exit(1);
process.exitCode = 1;
return;
}

init(await getStdin());
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chalk-cli",
"version": "5.0.0",
"version": "5.0.1",
"description": "Terminal string styling done right",
"license": "MIT",
"repository": "github:chalk/chalk-cli",
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# chalk-cli
# chalk-cli [![Downloads](https://badgen.net/npm/dt/chalk-cli)](https://www.npmjs.com/package/chalk-cli) [![run on repl.it](https://repl.it/badge/github/chalk/chalk-cli)](https://repl.it/github/chalk/chalk-cli)

> Terminal string styling done right
<img src="screenshot.png" width="631">

## Install

```
$ npm install --global chalk-cli
```sh
npm install --global chalk-cli
```

## Usage
49 changes: 39 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
@@ -4,21 +4,35 @@ import execa from 'execa';

chalk.level = 1;

const macro = async (t, {args, opts}, expected) => {
const {stdout} = await execa('./cli.js', args, opts);
const macro = async (t, {arguments: arguments_, options}, expected) => {
const {stdout} = await execa('./cli.js', arguments_, options);
t.is(stdout, expected);
};

const snapshotMacro = async (t, {arguments: arguments_, options}) => {
const {stdout} = await execa('./cli.js', arguments_, options);
t.snapshot(stdout);
};

const templateMacro = (t, input, expected) =>
macro(t, {args: ['--template', input, '--no-stdin']}, expected);
macro(t, {arguments: ['--template', input, '--no-stdin']}, expected);

test('help',
async (t, {arguments: arguments_, options}, expectedRegex) => {
const {stdout} = await execa('./cli.js', arguments_, options);
t.regex(stdout, expectedRegex);
},
{arguments: ['--help']},
/Terminal string styling done right/,
);

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

test('template', templateMacro, '{red.bold unicorn}',
@@ -36,11 +50,26 @@ 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: {stripFinalNewline: false}},
{arguments: ['red', 'bold', 'unicorn'], options: {stripFinalNewline: false}},
chalk.red.bold('unicorn') + '\n');
test('with -n, output has NO trailing newline', macro,
{args: ['-n', 'red', 'bold', 'unicorn'], opts: {stripFinalNewline: false}},
{arguments: ['-n', 'red', 'bold', 'unicorn'], options: {stripFinalNewline: 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: {stripFinalNewline: false}},
{arguments: ['--no-newline', 'red', 'bold', 'unicorn'], options: {stripFinalNewline: false}},
chalk.red.bold('unicorn') /* No trailing newline */);

test('demo', snapshotMacro, {arguments: ['--demo']});

test('unknown flag',
async (t, {arguments: arguments_, options}, expectedRegex) => {
try {
await execa('./cli.js', arguments_, options);
} catch (error) {
t.is(error.exitCode, 2);
t.regex(error.toString(), expectedRegex);
}
},
{arguments: ['--this-is-not-a-supported-flag'], options: {input: 'unicorn'}},
/Unknown flag/,
);
17 changes: 17 additions & 0 deletions test.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Snapshot report for `test.js`

The actual snapshot is saved in `test.js.snap`.

Generated by [AVA](https://avajs.dev).

## demo

> Snapshot 1
`Available styles:␊
bold dim italic underline strikethrough␊
black red green yellow blue magenta cyan white gray␊
blackBright redBright greenBright yellowBright blueBright magentaBright cyanBright whiteBright␊
bgBlack bgRed bgGreen bgYellow bgBlue bgMagenta bgCyan bgWhite bgGray␊
bgBlackBright bgRedBright bgGreenBright bgYellowBright bgBlueBright bgMagentaBright bgCyanBright bgWhiteBright`
Binary file added test.js.snap
Binary file not shown.