Skip to content

Instantly share code, notes, and snippets.

@EdThePro101
Created January 21, 2021 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EdThePro101/e296b7c6e61ef5d276901cc332c4f1dd to your computer and use it in GitHub Desktop.
Save EdThePro101/e296b7c6e61ef5d276901cc332c4f1dd to your computer and use it in GitHub Desktop.
This is a small modification of the `styles.js` file of colors.js.
// Original: https://github.com/Marak/colors.js/blob/master/lib/styles.js
//
// This is a slight modification of the `styles.js` file of colors.js.
// I've made it a bit more readable and added comments to explain what it does.
// I've also added a helper function that can be used to make your codes a bit more readable.
//
// Example usage:
//
// ```js
// const styles = require("/path/to/styles.js");
// console.log(styles.applyStyles("Hello!", [styles.bold, styles.fgYellow]));
// ```
// ------------------------------------------------------------------------------------
/** This is a list of possibly all the available styles that can be used in the console. */
const styleCodes = {
// Reset all styles.
reset: [0, 0],
// Text styles.
bold: [1, 22],
dim: [2, 22],
italic: [3, 23],
underline: [4, 24],
inverse: [7, 27],
hidden: [8, 28],
strikethrough: [9, 29],
// Foregound classic colours.
fgBlack: [30, 39],
fgRed: [31, 39],
fgGreen: [32, 39],
fgYellow: [33, 39],
fgBlue: [34, 39],
fgMagenta: [35, 39],
fgCyan: [36, 39],
fgWhite: [37, 39],
fgGray: [90, 39],
// Foreground bright colours.
fgBrightRed: [91, 39],
fgBrightGreen: [92, 39],
fgBrightYellow: [93, 39],
fgBrightBlue: [94, 39],
fgBrightMagenta: [95, 39],
fgBrightCyan: [96, 39],
fgBrightWhite: [97, 39],
// Background basic colours.
bgBlack: [40, 49],
bgRed: [41, 49],
bgGreen: [42, 49],
bgYellow: [43, 49],
bgBlue: [44, 49],
bgMagenta: [45, 49],
bgCyan: [46, 49],
bgWhite: [47, 49],
bgGray: [100, 49],
bgGrey: [100, 49],
// Background bright colours.
bgBrightRed: [101, 49],
bgBrightGreen: [102, 49],
bgBrightYellow: [103, 49],
bgBrightBlue: [104, 49],
bgBrightMagenta: [105, 49],
bgBrightCyan: [106, 49],
bgBrightWhite: [107, 49],
};
/** This object contains all the style codes for the console. */
const styles = {};
// Loop over all the style codes and assign them to the `styles` object.
//
// The a `styleCode` in the `styleCodes` object consists of two numbers:
// Index 0: The opening style code (In HTML this can be the opening <b> tag).
// Index 1: The closing style code (In HTML this can be the closing </b> tag).
for (let styleCode of Object.keys(styleCodes)) {
styles[styleCode] = {
open: `\x1B[${styleCodes[styleCode][0]}m`,
close: `\x1B[${styleCodes[styleCode][1]}m`,
};
}
/**
* Apply styles to a string `str`.
*
* @param {string} str - The string that needs some styling.
* @param {{open: string, close: string}[]} stylesArray - The array of styles that need to be applied.
*
* @returns {string} The string `str` that is styled with the styles mentioned in `stylesArray`.
*/
function applyStyles(str, stylesArray) {
for (let style of stylesArray) {
str = style.open + str + style.close;
}
return str;
}
// `require` imports modules as objects.
// And I find that writing `styles.styles.fgRed` is kinda ugly.
//
// That's why I destructure the object.
// And the reason why I also pass the full styles object is because TypeScript imports are a thing.
// I don't want others to struggle importing every individual style.
module.exports = { styles, ...styles, applyStyles };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment