Skip to content

Commit

Permalink
Release 0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
kandepatilkunal committed Jun 21, 2024
1 parent 09e2fd8 commit 0afa525
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "style-logs",
"version": "0.0.1",
"version": "0.0.2",
"description": "Design your console/terminal in HTML style.",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
22 changes: 11 additions & 11 deletions src/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface StyleCodes {
};
}

const style: StyleCodes = {
const logs: StyleCodes = {
reset: "\x1b[0m",
styles: {
bold: "\x1b[1m",
Expand Down Expand Up @@ -48,7 +48,7 @@ const style: StyleCodes = {
}
};

export const log = (text: string): void => {
export function style(text: string): string {
const regex = /<text\s*(.*?)>(.*?)<\/text>/gs;
let result = '';

Expand All @@ -59,28 +59,28 @@ export const log = (text: string): void => {
while ((match = regex.exec(line)) !== null) {
const attributes = match[1].trim();
const content = match[2];
let colorCode = style.reset;
let colorCode = logs.reset;

if (attributes) {
const attributeRegex = /(f|b|s)-(\w+)/g;
let attrMatch;
while ((attrMatch = attributeRegex.exec(attributes)) !== null) {
const [, type, styleName] = attrMatch;
if (type === 'f' && style.fg[styleName]) {
colorCode += style.fg[styleName];
} else if (type === 'b' && style.bg[styleName]) {
colorCode += style.bg[styleName];
} else if (type === 's' && style.styles[styleName]) {
colorCode += style.styles[styleName];
if (type === 'f' && logs.fg[styleName]) {
colorCode += logs.fg[styleName];
} else if (type === 'b' && logs.bg[styleName]) {
colorCode += logs.bg[styleName];
} else if (type === 's' && logs.styles[styleName]) {
colorCode += logs.styles[styleName];
}
}
}

processedLine = processedLine.replace(match[0], `${colorCode}${content}${style.reset}`);
processedLine = processedLine.replace(match[0], `${colorCode}${content}${logs.reset}`);
}

result += processedLine + '\n';
});

console.log(result.trim());
return result.trim()
};
14 changes: 7 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { log } = require("../dist/index");
const { style } = require("../dist/index");

log(`<text f-red>Hello There</text>`) // Foreground Color
log(`<text b-red>Hello There</text>`) // Background Color
log(`<text s-underline>Hello There</text>`) // Style (Underline)
log(`<text s-bold>Hello There</text>`) // Style (Bold)
log(`<text s-inverse>Hello There</text>`) // Style (Inverse)
console.log(style(`<text f-red>Hello There</text>`)) // Foreground Color
console.log(style(`<text b-red>Hello There</text>`)) // Background Color
console.log(style(`<text s-underline>Hello There</text>`)) // Style (Underline)
console.log(style(`<text s-bold>Hello There</text>`)) // Style (Bold)
console.log(style(`<text s-inverse>Hello There</text>`)) // Style (Inverse)

log(`<text f-red s-bold s-underline>Hello</text> There`) // Multiuse
console.log(style(`<text f-red s-bold s-underline>Hello</text> There`)) // Multiuse

0 comments on commit 0afa525

Please sign in to comment.