chalkdart 2.0.1 copy "chalkdart: ^2.0.1" to clipboard
chalkdart: ^2.0.1 copied to clipboard

outdated

Console text coloring and styling library for Dart. 'Terminal string styling done right'

vscode debug console
windows terminal

Console/Terminal text coloring and styling library for Dart. #

'Terminal string styling done right' #

I created this for my Dart/Flutter development logging almost 2 years ago now, and I have finally taken the time to clean it up and get it released as a package. In the mean time I added full ANSI support to the Visual Studio Code debugging console as well as just finishing full ANSI support for the Dart-Pad console as well. You can use this within your VSCode debugger to enable colorful, styled logging today. 😊

Check out example/chalkdart_example.dart for some cool examples of what it is capable of.

If you have used the Chalk.js package within the npm/node.js environment you know how nice and easy it makes text coloring and styling! This ChalkDart version can be used essentially exactly as the js version.

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Highlights #

  • Expressive API
  • Highly performant
  • Ability to nest styles
  • supports dynamic argument list and automatically handles printing Maps, Lists, Iterables and Function closures
  • 256/Truecolor color support
  • Ignores Auto-detected ANSI color support/as common Dart/Flutter IDE's report this incorrectly.
  • Not an extentension of the String class.
  • Clean and focused
  • Actively maintained

Install #

$ dart pub add chalkdart

Usage #

import 'package:chalkdart/chalk.dart';

print(chalk.yellow.onBlue('Hello world!'));

Chalk comes with an easy to use composable API where you just chain and nest the styles you want.

import 'package:chalkdart/chalk.dart';

// Combine styled and normal strings
print(chalk.blue('Hello') + ' World' + chalk.red('!'));

// Compose multiple styles using the chainable API
print(chalk.blue.onRed.bold('Hello world!'));

// Pass in multiple arguments
print(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'));

// Nest styles
print(chalk.red('Hello', chalk.underline.bgBlue('world') + '!'));

// Nest styles of the same type even (color, underline, background)
print(chalk.green(
     'I am a green line ' +
     chalk.blue.underline.bold('with a blue substring') +
     ' that becomes green again!'
));

// use in multiline string with interpolation
print('''
      CPU: ${chalk.red('90%')}
      RAM: ${chalk.green('40%')}
      DISK: ${chalk.yellow('70%')}
   ''');

// or with inline calcs
print('''
CPU: ${chalk.red(cpu.totalPercent)}%
RAM: ${chalk.green((ram.used / ram.total * 100))}%
DISK: ${chalk.rgb(255,131,0)((disk.used / disk.total * 100))}%
''');

// Use RGB colors in debug console or terminals that support it.
print(chalk.keyword('orange')('Yay for orange colored text!'));
print(chalk.rgb(123, 45, 67).underline('Underlined reddish color'));
print(chalk.hex('#DEADED').bold('Bold gray!'));

Easily define your own themes:

import 'package:chalkdart/chalk.dart';

const error = chalk.bold.red;
const warning = chalk.keyword('orange');

print(error('Error!'));
print(warning('Warning!'));
const name = 'Tim Maffett';
print(chalk.green('Hello $name'));

//=> 'Hello Tim Maffett'

API #

chalk.<style>[.<style>...](string, [string...]) #

Example: chalk.red.bold.underline('Hello', 'world');

Chain styles and call the last one as a method with a string argument. Order doesn't matter, and later styles take precedent in case of a conflict. This simply means that chalk.red.yellow.green is equivalent to chalk.green.

String interpolation can be ised or multiple arguments can be listed and will be separated by space (or the string specified by joinWith). The arguments can be of any type, Map<>, List<>, Iterable and Functions/Function closures are also supported. By default Lists are joined together with a ' '(space), but the joinWith method can be used to specify a different join string. Up to 15 arguments can be included as arguments to a Chalk object.

chalk.level #

Specifies the level of color support.

Color support is automatically detected, but you can override it by setting the level property. You should however only do this in your own code as it applies globally to all Chalk consumers. Note dart determines VSCode debug console and Android Studio debug console do not support ansi control sequences, when in fact they do, so by default the level is set to 3 (full color/sequence support) as the default, no matter what the console/terminal reports. You must set this yourslef if you want a different value.

If you need to change this in a reusable module, create a new instance:

var chalkWithLevel0 = chalk.instance(level: 0);
// this version is provided for users of the JS version of Chalk
var chalkWithLevel0 = chalk.Instance(level: 0);
Level Description
0 All colors disabled
1 Basic color support (16 colors)
2 256 color support
3 Truecolor support (16 million colors)

Styles #

Modifiers #

  • reset - Resets the current color chain.
  • blink - Make text blink.
  • rapidblink - Make text blink rapidly.
  • bold - Make text bold.
  • dim - Emitting only a small amount of light.
  • italic - Make text italic.
  • underline - Make text underline.
  • doubleunderline - Thicker text underline.
  • overline - Make text overline.
  • inverse (or invert)- Inverse background and foreground colors.
  • hidden - Prints the text, but makes it invisible.
  • superscript - Make text superscript.
  • subscript - Make text subscript.
  • strikethrough - Puts a horizontal line through the center of the text.
  • visible- Prints the text only when Chalk has a color level > 0. Can be useful for things that are purely cosmetic.
  • font1-font10 - Changes the font

Some of these ANSI styles are not widely supported within all terminal emulators, but I have added COMPLETE support to the standard Visual Studio Code debug console as well as to the Dart-Pad console. Android Studio also has fairly complete support within it's debug console. See the VSCode section for info on using CSS to map fonts to the font1 through font10.

Colors #

The exact RGB values for the base ANSI colors are rendered console/terminal dependent - and can be configured on some terminals such Windows Terminal - more info here

I include original JS Chalk 'xxxxBright' method names for users that may want legacy compatability with original JS Chalk).

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white
  • brightBlack      (alias blackBright, gray, grey)
  • brightRed          (alias redBright)
  • brightGreen      (alias greenBright)
  • brightYellow     (alias yellowBright)
  • brightBlue          (alias blueBright)
  • brightMagenta    (alias magentaBright)
  • brightCyan          (alias cyanBright)
  • brightWhite        (alias whiteBright)

Background colors #

They are named by prefixing the color with 'on'. Chalk'Dart favors the 'onXXXXX' style of specifying background colors because the verb 'on' makes chained methods read better as a sentence (it's the Dart way). I include original Chalk 'bgXXXX' method names for users that may prefer that scheme (essentially for legacy compatability with original JS Chalk).

For example 'onBlue' and 'bgBlue' are the same, it is a matter of users preference on how they want their code to read.

  • onBlack      (alias bgBlack)
  • onRed          (alias bgRed)
  • onGreen      (alias bgGreen)
  • onYellow     (alias bgYellow)
  • onBlue         (alias bgBlue)
  • onMagenta   (alias bgMagenta)
  • onCyan         (alias bgCyan)
  • onWhite       (alias bgWhite)
  • onBrightBlack / onGray / onGrey      (alias: bgBlackBright, bgGray, bgGrey)
  • onBrightRed            (alias bgRedBright)
  • onBrightGreen        (alias bgGreenBright)
  • onBrightYellow      (alias bgYellowBright)
  • onBrightBlue          (alias bgBlueBright)
  • onBrightMagenta    (alias bgMagentaBright)
  • onBrightCyan          (alias bgCyanBright)
  • onBrightWhite        (alias bgWhiteBright)

Tagged template literal #

Chalk can be used as a tagged template literal.

import 'package:chalkdart/chalk.dart';

const miles = 18;

int calculateFeet(miles) => miles * 5280;

print(chalk`
	There are {bold 5280 feet} in a mile.
	In {bold ${miles} miles}, there are {green.bold ${calculateFeet(miles)} feet}.
`);

Blocks are delimited by an opening curly brace ({), a style, some content, and a closing curly brace (}).

Template styles are chained exactly like normal Chalk styles. The following three statements are equivalent:

print(chalk.bold.rgb(10, 100, 200)('Hello!'));
print(chalk.bold.rgb(10, 100, 200)`Hello!`);
print(`{chalk.bold.rgb(10,100,200) Hello!}`);

Note that function styles (rgb(), hsl(), keyword(), etc.) may not contain spaces between parameters.

All interpolated values (chalk`${foo}`) are converted to strings via the .toString() method. All curly braces ({ and }) in interpolated value strings are escaped.

256 and Truecolor color support #

Chalk supports 256 colors and Truecolor (16 million colors) on supported terminal apps.

Colors are downsampled from 16 million RGB values to an ANSI color format that is supported by the terminal emulator (or by specifying {level: n} as a Chalk option). For example, Chalk configured to run at level 1 (basic color support) will downsample an RGB value of #FF0000 (red) to 31 (ANSI escape for red).

Examples:

  • chalk.hex('#DEADED').underline('Hello, world!')
  • chalk.keyword('orange')('Some orange text')
  • chalk.rgb(15, 100, 204).inverse('Hello!')

Background versions of these models are prefixed with bg and the first level of the module capitalized (e.g. keyword for foreground colors and bgKeyword for background colors).

  • chalk.onHex('#DEADED').underline('Hello, world!')
  • chalk.onKeyword('orange')('Some orange text')
  • chalk.onRgb(15, 100, 204).inverse('Hello!')

The following color models can be used:

  • rgb - Example: chalk.rgb(255, 136, 0).bold('Orange!')

  • hex - Example: chalk.hex('#FF8800').bold('Orange!')

  • hsl - Example: chalk.hsl(32, 100, 50).bold('Orange!')

  • hsv - Example: chalk.hsv(32, 100, 100).bold('Orange!')

  • hwb - Example: chalk.hwb(32, 0, 50).bold('Orange!')

  • xyz - Example: chalk.xyz(0.9, 0.9, 0.1).bold('Yellow!')

  • lab - Example: chalk.lab(85, 0, 108).bold('yellow-Orange!')

  • ansi - Example: chalk.ansi(31).bgAnsi(93)('red on yellowBright')

  • ansi256 - Example: chalk.onAnsi256(194)('Honeydew, more or less')

  • keyword (X11/CSS/SVG color keywords) - Example: chalk.cornFlowerBlue.onBeige or chalk.keyword('orange').bold('Orange!') The keyword table can be extended via and the new keywords can be accessed via

Chalk.addColorKeywordHex('myfavorite', 0x6495ED ); // using hex int
chalk.color.myfavorite('This is my favorite color');
Chalk.addColorKeywordHex('my2ndFavorite', '#6A5ACD' );  // or using string
chalk.color.my2ndfavorite('This is my 2nd favorite color');

or

chalk.keyword('myfavorite)('Using the keyword() method');
chalk.color.my2ndfavorite('This is my 2nd favorite color');

or import the X11 extension methods to get proper methods for each of the X11/CSS/SVG color names. With that you get code completion for available colors as well as compile time checking of color names.

import 'package:chalkdart/chalk.dart';
import 'package:chalkdart/chalk_x11.dart'; // get methods for x11/css/svg color name keywords

chalk.cornflowerBlue.onLimeGreen('Hey there!);

// without extension methods you can use the dynamic keyword lookup method:
chalk.color.cornflowerBlue.onLimeGreen('Hi Again!);
// or off x11
chalk.x11.cornflowerBlue.onLimeGreen('Hi Again!);
// or off csscolor
chalk.csscolor.cornflowerBlue.onLimeGreen('Hi Again!);

IDE Support #

The terminals and debug consoles in current versions of both Android Studio, IntelliJ and Visual Studio Code support 24bit/16M color ansi codes and most attributes. However, they both report that they DO NOT support ANSI codes. For this reason Chalk'Dart defaults to supporting full level 3 (24 bit) ANSI codes.

VSCode #

It is possible to set the fonts that VSCode uses in the debug console. More information can be found here.

ANSI Color Codes for .ansi() and .onAnsi() #

256 Color Mode ANSI Color Codes for use with chalk.ansi() and chalk.onAnsi()
Standard colors 'Bright' colors
 0   1   2   3   4   5   6   7   8   9  10 11 12 13 14 15
216 colors
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
Grayscale colors
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

X11/CSS/SVG Colors #

This is reference table of the X11/CSS/SVG foreground/background colors and the method names to access them.

Chalk X11/CSS/SVG Color Style Methods
Method name to set as forground colorMethod name to set as background color
.aliceBlue .onAliceBlue
.antiqueWhite .onAntiqueWhite
.aqua .onAqua
.aquamarine .onAquamarine
.azure .onAzure
.beige .onBeige
.bisque .onBisque
.blackX11 .onBlackX11
.blanchedAlmond .onBlanchedAlmond
.blueX11 .onBlueX11
.blueViolet .onBlueViolet
.brown .onBrown
.burlywood .onBurlywood
.cadetBlue .onCadetBlue
.chartreuse .onChartreuse
.chocolate .onChocolate
.coral .onCoral
.cornflowerBlue .onCornflowerBlue
.cornsilk .onCornsilk
.crimson .onCrimson
.cyanX11 .onCyanX11
.darkBlue .onDarkBlue
.darkCyan .onDarkCyan
.darkGoldenrod .onDarkGoldenrod
.darkGray .onDarkGray
.darkGreen .onDarkGreen
.darkGrey .onDarkGrey
.darkKhaki .onDarkKhaki
.darkMagenta .onDarkMagenta
.darkOliveGreen .onDarkOliveGreen
.darkOrange .onDarkOrange
.darkOrchid .onDarkOrchid
.darkRed .onDarkRed
.darkSalmon .onDarkSalmon
.darkSeaGreen .onDarkSeaGreen
.darkSlateBlue .onDarkSlateBlue
.darkSlateGray .onDarkSlateGray
.darkSlateGrey .onDarkSlateGrey
.darkTurquoise .onDarkTurquoise
.darkViolet .onDarkViolet
.deepPink .onDeepPink
.deepSkyBlue .onDeepSkyBlue
.dimGray .onDimGray
.dimGrey .onDimGrey
.dodgerBlue .onDodgerBlue
.fireBrick .onFireBrick
.floralWhite .onFloralWhite
.forestGreen .onForestGreen
.fuchsia .onFuchsia
.gainsboro .onGainsboro
.ghostWhite .onGhostWhite
.gold .onGold
.goldenrod .onGoldenrod
.grayX11 .onGrayX11
.greenX11 .onGreenX11
.greenYellow .onGreenYellow
.greyX11 .onGreyX11
.honeydew .onHoneydew
.hotPink .onHotPink
.indianRed .onIndianRed
.indigo .onIndigo
.ivory .onIvory
.khaki .onKhaki
.lavender .onLavender
.lavenderBlush .onLavenderBlush
.lawnGreen .onLawnGreen
.lemonChiffon .onLemonChiffon
.lightBlue .onLightBlue
.lightCoral .onLightCoral
.lightCyan .onLightCyan
.lightGoldenrodYellow .onLightGoldenrodYellow
.lightGoldenrod .onLightGoldenrod
.lightGray .onLightGray
.lightGreen .onLightGreen
.lightGrey .onLightGrey
.lightPink .onLightPink
.lightSalmon .onLightSalmon
.lightSeaGreen .onLightSeaGreen
.lightSkyBlue .onLightSkyBlue
.lightSlateGray .onLightSlateGray
.lightSlateGrey .onLightSlateGrey
.lightSteelBlue .onLightSteelBlue
.lightYellow .onLightYellow
.lime .onLime
.limeGreen .onLimeGreen
.linen .onLinen
.magentaX11 .onMagentaX11
.maroon .onMaroon
.mediumAquamarine .onMediumAquamarine
.mediumBlue .onMediumBlue
.mediumOrchid .onMediumOrchid
.mediumPurple .onMediumPurple
.mediumSeaGreen .onMediumSeaGreen
.mediumSlateBlue .onMediumSlateBlue
.mediumSpringGreen .onMediumSpringGreen
.mediumTurquoise .onMediumTurquoise
.mediumVioletRed .onMediumVioletRed
.midnightBlue .onMidnightBlue
.mintCream .onMintCream
.mistyRose .onMistyRose
.moccasin .onMoccasin
.navajoWhite .onNavajoWhite
.navy .onNavy
.oldLace .onOldLace
.olive .onOlive
.oliveDrab .onOliveDrab
.orange .onOrange
.orangeRed .onOrangeRed
.orchid .onOrchid
.paleGoldenrod .onPaleGoldenrod
.paleGreen .onPaleGreen
.paleTurquoise .onPaleTurquoise
.paleVioletRed .onPaleVioletRed
.papayaWhip .onPapayaWhip
.peachPuff .onPeachPuff
.peru .onPeru
.pink .onPink
.plum .onPlum
.powderBlue .onPowderBlue
.purple .onPurple
.rebeccaPurple .onRebeccaPurple
.redX11 .onRedX11
.rosyBrown .onRosyBrown
.royalBlue .onRoyalBlue
.saddleBrown .onSaddleBrown
.salmon .onSalmon
.sandyBrown .onSandyBrown
.seaGreen .onSeaGreen
.seashell .onSeashell
.sienna .onSienna
.silver .onSilver
.skyBlue .onSkyBlue
.slateBlue .onSlateBlue
.slateGray .onSlateGray
.slateGrey .onSlateGrey
.snow .onSnow
.springGreen .onSpringGreen
.steelBlue .onSteelBlue
.tan .onTan
.teal .onTeal
.thistle .onThistle
.tomato .onTomato
.turquoise .onTurquoise
.violet .onViolet
.wheat .onWheat
.whiteX11 .onWhiteX11
.whiteSmoke .onWhiteSmoke
.yellowX11 .onYellowX11
.yellowGreen .onYellowGreen
.brightRed .onBrightRed
.brightGreen .onBrightGreen
.brightBlue .onBrightBlue
.brightCyan .onBrightCyan
.brightYellow .onBrightYellow
.brightMagenta .onBrightMagenta
.brightWhite .onBrightWhite
.brightBlack .onBrightBlack
.red .onRed
.green .onGreen
.blue .onBlue
.cyan .onCyan
.yellow .onYellow
.magenta .onMagenta
.white .onWhite
.black .onBlack

Browser support #

Chrome, Firefox and Edge natively support ANSI escape codes in their respective developer consoles. From dart web code you can use window.console.log(...) to log messages to the browser's debug console.

Windows #

If you're on Windows, do yourself a favor and use Windows Terminal instead of cmd.exe.

  • chalk-js - The original Chalk library module for javascript which was the inspiration for this (as well as being the basis for this readme as well)!

Author/Maintainer #

Other examples #

Charts from chalkdart_example.dart output to windows terminal
40
likes
0
pub points
86%
popularity

Publisher

verified publisherhiveright.tech

Console text coloring and styling library for Dart. 'Terminal string styling done right'

Repository (GitHub)
View/report issues

License

unknown (license)

More

Packages that depend on chalkdart