Print to the console in style.
npm install styleme@3
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
console.log(StyleMe.red("a string"))
console.log(StyleMe.red.bgBlue("another string"))
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
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}"))
// 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())
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 |
You can add your own colors using this function. Example:
StyleMe.addStyle("lol","\x1b[5m");
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
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"))
StyleMe
Static class that is used to easily add colors to console outputs.
Styles the given string according to the given style codes.
str
String String to stylestyleCodes
(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
Styles a string with inline style markers
str
String String to style with inline style markers
- Throws any Will give error if braces are mismatched.
Returns String Returns the styled string
Styles the given string according to the given style codes if given and uses inline style markers otherwise.
str
String String to stylestyleCodes
(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
Extends String.prototype with helper methods that can be used to easily style a string
Flattens an array style into a single array
- Throws any Error if style code not found or if invalid
Returns Array Flattened array
Adds a style
styleCode
String style code string (alphabetical string, no spaces)style
(String | SpecialStyleFn) Style string or function
Sets a theme
theme
Object
Styles the string according to the method's code
Returns String
Styles the string according to the given style codes if given and uses inline style markers otherwise.
- Throws any Will throw error if an invalid style code is provided or if braces are mismatched.
Returns String Styled string
Styles the string according to the method's code
Returns String
A function that adds styling to individual characters
Type: Function
char
char Character involvedindex
number Index of the characterstyles
Object Object containing the styles as properties.
Returns any Should return the styled character.
Wraps objects with a Proxy for code chaining
The colors/styles defined. ASCII codes reference: https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
Can be a string or function.