npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

styleme

v3.0.0

Published

Print to the console in style

Downloads

95

Readme

NPM styleme

Print to the console in style.

Installation

npm install styleme@3

Usage

const StyleMe = require('styleme')

// It is recommended to extend the `String` prototype methods for convenience.
StyleMe.extend()

There are four methods to use this module

1. Chaining properties/methods

console.log(StyleMe.red("a string"))
console.log(StyleMe.red.bgBlue("another string"))

2. The StyleMe Method

console.log(StyleMe.styleMe("some text","red,bgBlue")) // red text with blue background
console.log(StyleMe.styleMe("same text",["red", "bgBlue"])) // also red text with blue background

3. The Inline StyleMe Method

console.log(StyleMe.styleMe("This is normal [red]{this is red [blue]{this is blue} back to red}"))
console.log(StyleMe.styleMe("[blue,bgRed]{This is blue with a red background}"))

4. String.prototype extensions

// Chaining
console.log("a string".red())
console.log("another string".red().bgBlue())

// StyleMe
console.log("some text".styleMe("red,bgBlue"))

// Inline StyleMe
console.log("This is normal [red]{this is red [blue]{this is blue} back to red}".styleMe())

Color Codes

These color codes are available for use:

| Color Code | | :------------ | | reset | | bright | | dim | | underline | | strikethrough | | blink | | reverse | | hidden | | black | | red | | green | | yellow | | blue | | magenta | | cyan | | white | | bgBlack | | bgRed | | bgGreen | | bgYellow | | bgBlue | | bgMagenta | | bgCyan | | bgWhite | | clear |

Special Codes (from Colors.js) are also supported

| Stye Code | | :---------- | | america | | rainbow | | random | | blacknwhite |

Adding Colors

StyleMe.addStyle(code, style)

You can add your own colors using this function. Example:

StyleMe.addStyle("lol","\x1b[5m");

Adding Special Styles

StyleMe.addSpecial(code,function)

You can add your own special styles using this function. Example:

StyleMe.addStyle("rednblue", (char, index, colors) => { // colors => a object where you can get colors without using ansi codes
    switch (index % 2) { // Red and blue inverse pattern
        case 0:
            return colors.bgBlue + colors.red + char;
        case 1:
            return colors.bgRed + colors.blue + char
    }
});

Please note that you cannot use colors[index]. You must use colors.colorcode instead

Adding Themes

Themes allow you to use multiple colors/aliases at the same.

// Set the theme
StyleMe.setTheme({
    rnb: ["red", "bgBlue"], // red and blue background
    ynr: ["yellow", "bgMagenta"], // yellow and magenta background
})

// Can use it now!
console.log(StyleMe.rnb("test"))

API

Table of Contents

StyleMe

src/StyleMe.js:10-303

StyleMe

Static class that is used to easily add colors to console outputs.

style

src/StyleMe.js:19-53

Styles the given string according to the given style codes.

Parameters

  • str String String to style
  • styleCodes (String | Array) Comma seperated list (or array) of style codes.
  • Throws any Will throw error if an invalid style code is provided.

Returns String Styled output

styleInline

src/StyleMe.js:62-177

Styles a string with inline style markers

Parameters

  • str String String to style with inline style markers
  • Throws any Will give error if braces are mismatched.

Returns String Returns the styled string

styleMe

src/StyleMe.js:187-193

Styles the given string according to the given style codes if given and uses inline style markers otherwise.

Parameters

  • str String String to style
  • styleCodes (String | Array)? Optional comma seperated list (or array) of style codes.
  • Throws any Will throw error if an invalid style code is provided or if braces are mismatched.

Returns String Styled output

extend

src/StyleMe.js:198-223

Extends String.prototype with helper methods that can be used to easily style a string

flattenStyle

src/StyleMe.js:232-256

Flattens an array style into a single array

Parameters

  • Throws any Error if style code not found or if invalid

Returns Array Flattened array

addStyle

src/StyleMe.js:272-292

Adds a style

Parameters

setTheme

src/StyleMe.js:298-302

Sets a theme

Parameters

code

src/StyleMe.js:207-209

Styles the string according to the method's code

Returns String

styleMe

src/StyleMe.js:220-222

Styles the string according to the given style codes if given and uses inline style markers otherwise.

Parameters

  • Throws any Will throw error if an invalid style code is provided or if braces are mismatched.

Returns String Styled string

styleCode

src/StyleMe.js:288-290

Styles the string according to the method's code

Returns String

SpecialStyleFn

src/StyleMe.js:272-292

A function that adds styling to individual characters

Type: Function

Parameters

  • char char Character involved
  • index number Index of the character
  • styles Object Object containing the styles as properties.

Returns any Should return the styled character.

recursiveProxy

src/StyleMe.js:311-328

Wraps objects with a Proxy for code chaining

Parameters

Styles

styles/Styles.js:6-36

The colors/styles defined. ASCII codes reference: https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797

Can be a string or function.