cstyler 2.0.1 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +65 -23
  2. package/index.js +99 -70
  3. package/package.json +11 -2
  4. package/readme.txt +0 -23
package/README.md CHANGED
@@ -1,31 +1,73 @@
1
- # CSTYLER
2
- Hello World!
1
+ # CSTYLER 🎨
2
+ **A lightweight, powerful console styler by kormoi.com**
3
3
 
4
- Style your console log with your cstyler package by kormoi.com
4
+ `cstyler` is a zero-dependency Node.js toolkit designed to make your terminal output beautiful. It features an intuitive chainable API, support for HEX/RGB colors, and a unique nested inline styling system.
5
5
 
6
- This cstyler kit is fully free to use. You can colorize your console log useing this kit. You can use dot notation to add more style like 'bold, underline, italic, dark(for color), bg for background'. You have all red, green, blue, yellow, cyan, purpal, colors on both text color and background color.
6
+ ---
7
7
 
8
- You can not use two color names e.g. 'red.blue'.
9
- How to use:
10
- INSTALLING:
8
+ ## 🚀 Key Features
9
+ * **Auto-Detection:** Automatically strips styles if the terminal doesn't support them (perfect for logs).
10
+ * **Chainable API:** Mix styles easily using dot notation.
11
+ * **TrueColor Support:** Use HEX and RGB for millions of colors.
12
+ * **Smart Nesting:** Complex nested styling using tagged template literals.
13
+ * **No Overhead:** Built to be fast and lightweight.
11
14
 
12
- > npm install cstyler
13
- >
15
+ ---
14
16
 
15
- Then it will install packages.
16
- IMPORT:
17
- const cstyle = require("cstyle");
17
+ ## 📦 Installation
18
18
 
19
- USE:
20
- console.log(cstyle.bold.underline.italic.dark.yellow("Here goes your text"));
19
+ ```bash
20
+ npm install cstyler
21
+ ```
22
+ ## 🛠 Usage
23
+ ### 1. Basic Styling (Chainable)
24
+ You can combine text styles and colors using simple dot notation.
25
+ JavaScript
26
+ ```js
27
+ const cstyler = require("cstyler");
21
28
 
22
- You can add these colors by name: red, green, blue, yellow, purpal, grey. You can darken them with a .dark property accessor. You can add rbg, hex, bgrgb and bghex color using .hex('#123456'), and rgb(100,100,100) and same for bg hex and bg rgb.
29
+ // Bold, Underline, and Yellow text
30
+ console.log(cstyler.bold.underline.yellow("Hello World!"));
23
31
 
24
- Feel free to use.
25
- <<<<<<< HEAD
26
- Regards from kormoi.com
27
- =======
28
- Regards from
29
- kormoi.com
30
- MD NASIRUDDIN AHMED
31
- >>>>>>> 7cb483c (Second commit)
32
+ // Darkened green text
33
+ console.log(cstyler.green.dark("Subtle green message"));
34
+ ```
35
+ ### 2. Custom Colors (HEX & RGB)
36
+ Go beyond the standard 16 colors with TrueColor support.
37
+ JavaScript
38
+ ```js
39
+ // Foreground Colors
40
+ console.log(cstyler.hex('#FF5733')("Vibrant Orange"));
41
+ console.log(cstyler.rgb(100, 150, 200)("Custom Blue"));
42
+
43
+ // Background Colors
44
+ console.log(cstyler.bgHex('#333').white("White text on dark grey"));
45
+ ```
46
+ ### 3. Inline Nested Styling (Tagged Templates)
47
+ This is the most powerful way to style complex strings. Use {style text} syntax inside backticks.
48
+ JavaScript
49
+ ```js
50
+ console.log(cstyler`
51
+ {bold.red Critical Error:} {italic.gray Connection failed.}
52
+ {bold.green Success! {italic.white kormoi.com is online.}}
53
+ {hex('#00dbde').bold Powerful custom colors work inside too!}
54
+ `);
55
+ ```
56
+ 🎨 Available Styles
57
+ | Category | Available Properties |
58
+ | :--- | :--- |
59
+ | **Formatting** | bold, italic, underline, dark |
60
+ | **Colors** | red, green, blue, yellow, purple, cyan, white, gray, magenta |
61
+ | **Backgrounds** | bgRed, bgGreen, bgBlue, bgYellow, bgPurple, bgGray |
62
+ | **Custom** | .hex(), .rgb(), .bgHex(), .bgRgb() |
63
+
64
+ > Note: You cannot use two foreground colors at once (e.g., red.blue). The last color in the chain will take priority.
65
+
66
+ ## 🛡 Smart Terminal Detection
67
+ `cstyler` is smart. If you pipe your output to a file or run it in an environment that doesn't support ANSI colors, it will **silently return plain text.** Your logs will remain clean and readable without any "garbage" characters.
68
+
69
+ ## 👨‍💻 Author
70
+ ### MD NASIRUDDIN AHMED
71
+ Visit us at: kormoi.com
72
+
73
+ `Feel free to use and contribute! Regards from kormoi.com.`
package/index.js CHANGED
@@ -1,109 +1,138 @@
1
1
  class cstyler {
2
2
  constructor() {
3
- return this.createStyler([]);
4
- }
3
+ // SILENT CHECK: No printing, just querying the environment
4
+ this.enabled = process.stdout.isTTY &&
5
+ (process.stdout.getColorDepth ? process.stdout.getColorDepth() > 1 : true);
5
6
 
6
- createStyler(styles) {
7
- const styler = (text) => {
8
- return styles.reduce((str, styleCode) => styleCode.open + str + styleCode.close, text);
9
- };
10
-
11
- const addStyle = (open, close) => this.createStyler([...styles, { open, close }]);
12
-
13
- const styleMap = {
7
+ this.styleMap = {
14
8
  // Text styles
15
9
  bold: ['\x1b[1m', '\x1b[22m'],
16
10
  italic: ['\x1b[3m', '\x1b[23m'],
17
11
  underline: ['\x1b[4m', '\x1b[24m'],
18
12
  dark: ['\x1b[2m', '\x1b[22m'],
13
+
19
14
  // Foreground colors
20
15
  red: ['\x1b[31m', '\x1b[39m'],
21
16
  green: ['\x1b[32m', '\x1b[39m'],
22
17
  yellow: ['\x1b[33m', '\x1b[39m'],
23
18
  blue: ['\x1b[34m', '\x1b[39m'],
24
- purpal: ['\x1b[35m', '\x1b[39m'],
25
- gray: ['\x1b[30m', '\x1b[39m'],
19
+ purple: ['\x1b[35m', '\x1b[39m'],
20
+ gray: ['\x1b[90m', '\x1b[39m'],
21
+ white: ['\x1b[97m', '\x1b[39m'],
22
+ cyan: ['\x1b[36m', '\x1b[39m'],
23
+ magenta: ['\x1b[35m', '\x1b[39m'],
24
+
26
25
  // Background colors
27
26
  bgRed: ['\x1b[41m', '\x1b[49m'],
28
27
  bgGreen: ['\x1b[42m', '\x1b[49m'],
29
28
  bgYellow: ['\x1b[43m', '\x1b[49m'],
30
29
  bgBlue: ['\x1b[44m', '\x1b[49m'],
31
- bgPurpal: ['\x1b[45m', '\x1b[49m'],
32
- bgGray: ['\x1b[40m', '\x1b[49m']
30
+ bgPurple: ['\x1b[45m', '\x1b[49m'],
31
+ bgGray: ['\x1b[100m', '\x1b[49m'],
33
32
  };
34
33
 
35
- // Dynamically define valid style accessors
36
- for (const [name, [open, close]] of Object.entries(styleMap)) {
34
+ return this.createStyler([]);
35
+ }
36
+
37
+ createStyler(styles) {
38
+ const styler = (text) => {
39
+ // Return plain text if terminal doesn't support styling
40
+ if (!this.enabled) return text;
41
+ return styles.reduce((str, { open, close }) => open + str + close, text);
42
+ };
43
+
44
+ const addStyle = (open, close) => this.createStyler([...styles, { open, close }]);
45
+
46
+ // Map standard styles
47
+ for (const [name, [open, close]] of Object.entries(this.styleMap)) {
37
48
  Object.defineProperty(styler, name, {
38
- get: () => addStyle(open, close)
49
+ get: () => this.enabled ? addStyle(open, close) : styler,
39
50
  });
40
51
  }
41
52
 
42
- // RGB support
53
+ // RGB Support
43
54
  styler.rgb = (r, g, b) => {
44
- if (![r, g, b].every(n => Number.isInteger(n) && n >= 0 && n <= 255)) {
45
- console.error('Invalid RGB value. Falling back to white.');
46
- return addStyle('\x1b[37m', '\x1b[39m'); // white
47
- }
48
- const open = `\x1b[38;2;${r};${g};${b}m`;
49
- const close = '\x1b[39m';
50
- return addStyle(open, close);
55
+ if (!this.enabled) return styler;
56
+ return addStyle(`\x1b[38;2;${r};${g};${b}m`, '\x1b[39m');
51
57
  };
52
58
 
53
- // Hex color support
59
+ // Hex Support
54
60
  styler.hex = (hex) => {
55
- try {
56
- if (typeof hex !== 'string') throw new Error();
57
- hex = hex.replace('#', '').slice(0, 6);
58
- const r = parseInt(hex.slice(0, 2), 16);
59
- const g = parseInt(hex.slice(2, 4), 16);
60
- const b = parseInt(hex.slice(4, 6), 16);
61
- return styler.rgb(r, g, b);
62
- } catch (e) {
63
- console.error('Invalid hex color. Falling back to white.');
64
- return addStyle('\x1b[37m', '\x1b[39m'); // white
65
- }
61
+ if (!this.enabled) return styler;
62
+ const cleanHex = hex.replace(/^#/, '');
63
+ const r = parseInt(cleanHex.slice(0, 2), 16);
64
+ const g = parseInt(cleanHex.slice(2, 4), 16);
65
+ const b = parseInt(cleanHex.slice(4, 6), 16);
66
+ return styler.rgb(r, g, b);
66
67
  };
67
68
 
68
- // Background RGB
69
- styler.bgrgb = (r, g, b) => {
70
- if (![r, g, b].every(n => Number.isInteger(n) && n >= 0 && n <= 255)) {
71
- console.error('Invalid background RGB value. Falling back to white.');
72
- return addStyle('\x1b[47m', '\x1b[49m');
73
- }
74
- const open = `\x1b[48;2;${r};${g};${b}m`;
75
- const close = '\x1b[49m';
76
- return addStyle(open, close);
69
+ // Tagged Template Parser
70
+ styler.tag = (strings, ...values) => {
71
+ const fullText = strings.reduce((acc, str, i) => acc + str + (values[i] || ''), '');
72
+ return this.parseInlineStyles(fullText);
77
73
  };
78
74
 
79
- // Background Hex
80
- styler.bghex = (hex) => {
81
- try {
82
- if (typeof hex !== 'string') throw new Error();
83
- hex = hex.replace('#', '').slice(0, 6);
84
- const r = parseInt(hex.slice(0, 2), 16);
85
- const g = parseInt(hex.slice(2, 4), 16);
86
- const b = parseInt(hex.slice(4, 6), 16);
87
- return styler.bgrgb(r, g, b);
88
- } catch (e) {
89
- console.error('Invalid background hex. Falling back to white.');
90
- return addStyle('\x1b[47m', '\x1b[49m');
91
- }
75
+ this.parseInlineStyles = (text) => {
76
+ const parse = (str) => {
77
+ let result = '', i = 0;
78
+ while (i < str.length) {
79
+ if (str[i] === '{') {
80
+ let level = 1, j = i + 1;
81
+ while (j < str.length && level > 0) {
82
+ if (str[j] === '{') level++;
83
+ else if (str[j] === '}') level--;
84
+ j++;
85
+ }
86
+ if (level !== 0) { result += str.slice(i); break; }
87
+
88
+ const content = str.slice(i + 1, j - 1);
89
+ const spaceIdx = content.indexOf(' ');
90
+
91
+ if (spaceIdx === -1) {
92
+ result += '{' + content + '}';
93
+ } else {
94
+ const styleStr = content.slice(0, spaceIdx);
95
+ const innerText = content.slice(spaceIdx + 1);
96
+ const parsedInner = parse(innerText);
97
+
98
+ if (!this.enabled) {
99
+ result += parsedInner;
100
+ } else {
101
+ // regex: match style name OR styleName(params)
102
+ const styleParts = styleStr.match(/(\w+(\([^)]*\))?)/g) || [];
103
+ let styledText = parsedInner;
104
+
105
+ for (let part of styleParts.reverse()) {
106
+ if (part.startsWith('hex')) {
107
+ const val = part.match(/\(([^)]+)\)/)?.[1].replace(/['"]/g, '');
108
+ styledText = styler.hex(val)(styledText);
109
+ } else if (part.startsWith('rgb')) {
110
+ const vals = part.match(/\(([^)]+)\)/)?.[1].split(',').map(Number);
111
+ styledText = styler.rgb(...vals)(styledText);
112
+ } else if (this.styleMap[part]) {
113
+ const [open, close] = this.styleMap[part];
114
+ styledText = open + styledText + close;
115
+ }
116
+ }
117
+ result += styledText;
118
+ }
119
+ }
120
+ i = j;
121
+ } else {
122
+ result += str[i++];
123
+ }
124
+ }
125
+ return result;
126
+ };
127
+ return parse(text);
92
128
  };
93
129
 
94
- // Use Proxy to catch invalid property access
95
130
  return new Proxy(styler, {
96
- get(target, prop) {
97
- if (prop in target) {
98
- return target[prop];
99
- } else {
100
- console.log(`Wrong style: ${String(prop)}`);
101
- console.error(`Invalid property accessor used: ${String(prop)}`);
102
- return target; // Return unstyled version to continue chain safely
103
- }
104
- }
131
+ apply: (target, thisArg, args) =>
132
+ (Array.isArray(args[0]) && 'raw' in args[0]) ? styler.tag(...args) : styler(...args),
133
+ get: (target, prop) => (prop in styler) ? styler[prop] : styler,
105
134
  });
106
135
  }
107
136
  }
108
137
 
109
- module.exports = new cstyler();
138
+ module.exports = new cstyler();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cstyler",
3
- "version": "2.0.1",
3
+ "version": "4.0.0",
4
4
  "description": "Chainable terminal text styling tool (bold, colors, etc.)",
5
5
  "main": "index.js",
6
6
  "keywords": [
@@ -10,7 +10,16 @@
10
10
  "chainable",
11
11
  "bold",
12
12
  "style",
13
- "styler"
13
+ "styler",
14
+ "ansi",
15
+ "colors",
16
+ "terminal",
17
+ "styling",
18
+ "cli",
19
+ "template",
20
+ "rgb",
21
+ "hex",
22
+ "hsl"
14
23
  ],
15
24
  "author": "MD Nasiruddin Ahmed",
16
25
  "license": "ISC",
package/readme.txt DELETED
@@ -1,23 +0,0 @@
1
- # cstyler
2
- Hello World!
3
-
4
- You can style with color and style on the console text with this package
5
-
6
- This cstyler kit is fully free to use. You can colorize your console log useing this kit. You can use dot notation to add more style like 'bold, underline, italic, dark(for color), bg for background'. You have all red, green, blue, yellow, cyan, purpal, colors on both text color and background color.
7
-
8
- You can not use two color names e.g. 'red.blue'.
9
- How to use:
10
- INSTALLING:
11
-
12
- > npm install cstyler
13
- >
14
-
15
- Then it will install packages.
16
- IMPORT:
17
- const cstyle = require("cstyle");
18
-
19
- USE:
20
- console.log(cstyle.bold.underline.italic.dark.yellow("Here goes your text"));
21
-
22
- Feel free to use.
23
- Regards from kormoi.com