Skip to content

Commit

Permalink
feat(color): Add NO_COLOR support
Browse files Browse the repository at this point in the history
  • Loading branch information
angelnext committed Nov 19, 2023
1 parent 0e15ffd commit 6b56e20
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 23 deletions.
26 changes: 13 additions & 13 deletions lib/levels.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @callback Level
* @param {Rubiks} self - The instance
* @param {import("./rubiks").Rubiks} self - The instance
* @param {string} content - The content you provide.
* @returns {void}
*/
Expand All @@ -22,9 +22,9 @@ export function log(self, content) {
export function error(self, content) {
self.level = "error";
console.error(
`${self.prefixes.all || ""}${
self.prefixes[self.level] || ""
}\x1b[31;1merror\x1b[0m: ${content}${self.suffixes.all || ""}`,
`${self.prefixes.all || ""}${self.prefixes[self.level] || ""}${
self.noColor ? "" : "\x1b[31;1m"
}error\x1b[0m: ${content}${self.suffixes.all || ""}`,
);
self.level = "";
}
Expand All @@ -37,9 +37,9 @@ export function error(self, content) {
export function warn(self, content) {
self.level = "warn";
console.warn(
`${self.prefixes.all || ""}${
self.prefixes[self.level] || ""
}\x1b[33;1mwarning\x1b[0m: ${content}${self.suffixes.all || ""}`,
`${self.prefixes.all || ""}${self.prefixes[self.level] || ""}${
self.noColor ? "" : "\x1b[33;1m"
}warning\x1b[0m: ${content}${self.suffixes.all || ""}`,
);
self.level = "";
}
Expand All @@ -52,9 +52,9 @@ export function warn(self, content) {
export function success(self, content) {
self.level = "success";
console.log(
`${self.prefixes.all || ""}${
self.prefixes[self.level] || ""
}\x1b[32;1msuccess\x1b[0m: ${content}${self.suffixes.all || ""}`,
`${self.prefixes.all || ""}${self.prefixes[self.level] || ""}${
self.noColor ? "" : "\x1b[32;1m"
}success\x1b[0m: ${content}${self.suffixes.all || ""}`,
);
self.level = "";
}
Expand All @@ -67,9 +67,9 @@ export function success(self, content) {
export function info(self, content) {
self.level = "info";
console.log(
`${self.prefixes.all || ""}${
self.prefixes[self.level] || ""
}\x1b[34;1minfo\x1b[0m: ${content}${self.suffixes.all || ""}`,
`${self.prefixes.all || ""}${self.prefixes[self.level] || ""}${
self.noColor ? "" : "\x1b[34;1m"
}info\x1b[0m: ${content}${self.suffixes.all || ""}`,
);
self.level = "";
}
16 changes: 8 additions & 8 deletions lib/modifiers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @callback Modifier
* @param {Rubiks} self - The instance
* @param {import("./rubiks").Rubiks} self - The instance
* @param {string} format - the format to alter.
* @returns {string}
*/
Expand Down Expand Up @@ -29,19 +29,19 @@ export function nerdIcons(
) {
return function (self, format) {
if (options.error) {
self.prefixes.error = "\x1b[31m \x1b[0m ";
self.prefixes.error = self.noColor ? " " : "\x1b[31m\x1b[0m ";
}

if (options.warn) {
self.prefixes.warn = "\x1b[33m \x1b[0m ";
self.prefixes.warn = self.noColor ? " " : "\x1b[33m\x1b[0m ";
}

if (options.success) {
self.prefixes.success = "\x1b[32m\x1b[0m ";
self.prefixes.success = self.noColor ? " " : "\x1b[32m\x1b[0m ";
}

if (options.info) {
self.prefixes.info = "\x1b[34m\x1b[0m ";
self.prefixes.info = self.noColor ? " " : "\x1b[34m\x1b[0m ";
}

return format;
Expand All @@ -57,8 +57,8 @@ export function nerdIcons(
export function withDates(self, format) {
const date = new Date();

self.prefixes.all = `${
self.prefixes.all || ""
}\x1b[90m[${date.toLocaleDateString()} ${date.toLocaleTimeString()}]\x1b[0m `;
self.prefixes.all = `${self.prefixes.all || ""}${
self.noColor ? "" : "\x1b[90m"
}[${date.toLocaleDateString()} ${date.toLocaleTimeString()}]\x1b[0m `;
return format;
}
15 changes: 15 additions & 0 deletions lib/rubiks.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ export class Rubiks {
*/
suffixes = { all: "" };

/**
* Is NO_COLOR enabled?
* @type {boolean}
* @public
*/
noColor = false;

constructor() {
if (process) {
this.noColor = process?.env?.NO_COLOR !== undefined ? true : false;
} else if (Deno?.noColor !== undefined) {
this.noColor = Deno?.noColor;
}
}

/**
* Applies a modifier to the logger
* @param {import("./modifiers").Modifier} modifier
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json.schemastore.org/package.json",
"name": "rubiks",
"version": "0.4.0",
"version": "0.5.1",
"type": "module",
"description": "Rubiks is a 0 dependency extendable logging library for modern applications.",
"main": "./lib/index.js",
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"declaration": true,
"emitDeclarationOnly": true,
"allowJs": true
}
},
"include": ["lib"],
}

0 comments on commit 6b56e20

Please sign in to comment.