question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

chalk template strings

See original GitHub issue

Reference

Consider running stuff through the chalk template function like this:

const chalk = require('chalk');
const chalkTemplate = require("chalk/templates");

const myString = "{red RED}";

console.log(chalkTemplate(chalk, myString));

Which is slightly less ugly, but still hacky.

Also see https://github.com/chalk/chalk/issues/258

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
lainocommented, Mar 13, 2018

Fine. How about this then:

const chalk = require('chalk');
const myString = "{red RED}";

console.log("Works:", chalkFormat(myString));

function chalkFormat(string) {
  const arr = [string];
  arr.raw = [string];
  return chalk(arr);
}

That uses the ES2015 template literal API (not 100% to spec, but it works) and it doesn’t use any undocumented chalk features.

If you’d want to do it to spec you’d have to go and replace all all escape sequences in raw with their literal values, but chalk handles that fine anyways.

0reactions
75lbcommented, Mar 25, 2018

i’m back from holiday now (although still jet-lagged!) and have implemented the raw property solution in v5.0.4, as suggested… thanks again, keep me posted.

Read more comments on GitHub >

github_iconTop Results From Across the Web

chalk-template - GitHub
All interpolated values ( chalkTemplate`${foo}` ) are converted to strings via the .toString() method. All curly braces ( { and } ) in...
Read more >
chalk-template - npm
Terminal string styling with tagged template literals. ... Start using chalk-template in your project by running `npm i chalk-template`.
Read more >
chalk tagged template literals within strings - Stack Overflow
i would like chalk to honor my string as it would honor tagged termplate literals. how can it be done? const chalk =...
Read more >
Using Chalk 2.0's Tagged Template Literals For Nested ...
Ben Nadel explores the use of ES6 tagged template literals in the recently-released Chalk 2.0 node module. This feature allows complex, ...
Read more >
chalk-template - npm Package Health Analysis - Snyk
Terminal string styling with tagged template literals For more information about how to use this package see README. Latest version published 10 months...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found