Skip to content

Commit dde343a

Browse files
committed
Update ANSI styles styles.js
1 parent 5b84d8b commit dde343a

1 file changed

Lines changed: 51 additions & 38 deletions

File tree

styles.js

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,18 @@ const style = {
4444
wrap: (...styles) => {
4545
let chainedStyles = '';
4646
for (const styleName of styles) {
47+
const isEscSeq = JSON.stringify(styleName).slice(0,6) === '"\\u001';
48+
if (isEscSeq) {
49+
chainedStyles += styleName;
50+
}
51+
// Last item is not style key
4752
if (styles.indexOf(styleName) === styles.length -1) {
4853
chainedStyles += styleName;
4954
} else {
50-
chainedStyles += style[styleName];
55+
if (!isEscSeq) {
56+
// Add style keys
57+
chainedStyles += style[styleName];
58+
}
5159
}
5260
}
5361
chainedStyles += style.reset;
@@ -56,21 +64,21 @@ const style = {
5664
list: (...listItems) => {
5765
let list = '';
5866
for (const item of listItems) {
59-
list += '\n ☞ '+item;
67+
list += `\n ☞ ${item}`;
6068
}
6169
return list;
6270
},
6371
todo: (...listItems) => {
6472
let list = '';
6573
for (const item of listItems) {
6674
if (item[0] === '+') {
67-
list += '\n - [ ] '+item.slice(1);
75+
list += '\n - [ ] '+item.slice(1);
6876
}
6977
if (item[0] === '-') {
70-
list += '\n - [x] '+item.slice(1);
78+
list += '\n - [x] '+item.slice(1);
7179
}
7280
if (item[0] === '~') {
73-
list += '\n - [~] '+style.strike+item.slice(1)+style.reset;
81+
list += '\n - [~] '+style.strike+item.slice(1)+style.reset;
7482
}
7583
if (item[0] === '!') {
7684
if (item[1] === '+') {
@@ -98,18 +106,22 @@ const style = {
98106
},
99107
test: () => {
100108
console.log(
101-
style.color(255,0,255),'Styled with Custom RGB Color',style.reset,
102-
style.color(39),' Some Styled Text',style.reset,
103-
style.bgColor(18),style.orange,' Some Background Styled Text',style.reset,
104-
style.bgColor(0,255,0),style.black,style.bold,' Some Background Styled Text',style.reset,
105-
style.hr.double,style.reset,
106-
style.wrap('italic', 'Make me ITALIC (reset included for `style.wrap()`)'),
107-
style.wrap('black', 'redbg', 'bold', 'strike', 'Make me RED BACKGROUND STRIKED'),
108-
style.chain('red', 'bold'), 'Make me RED BOLD', style.reset,
109-
style.dim, 'Make me DIM', style.reset,
110-
style.list('Make me a LIST ITEM','Make me a LIST ITEM'), style.reset,
111-
style.list(`Make me a ${style.italic}INLINE STYLED${style.reset} LIST ITEM`,`Make me a LIST ITEM`), style.reset,
112-
style.todo('+Make me a TODO LIST ITEM','-Make me a DONE LIST ITEM','~Make me a STRIKED LIST ITEM','!Make me a IMPORTANT TODO LIST ITEM', '!+Make me a IMPORTANT TODO LIST ITEM', '!-Make me a IMPORTANT DONE LIST ITEM'), style.reset,
109+
style.color(255,0,255)+'Styled with Custom RGB Color'+style.reset+
110+
style.color(39)+'Some Styled Text'+style.reset+'\n'+
111+
style.bgColor(18)+style.orange+'Some Background Styled Text'+style.reset+
112+
style.redbg+style.black+style.bold+'Some Background Styled Text'+style.reset+'\n'+
113+
'\x1b[33m'+'Some Styled Text'+'\x1b[0m'+'\n'+
114+
style.hr.double,
115+
style.wrap('red', style.hr.single),
116+
style.wrap(style.color(255,0,255), style.hr.single),
117+
style.wrap('\x1b[33m', style.hr.single),
118+
style.wrap('italic', 'Make me ITALIC (reset included for `style.wrap()`)')+'\n'+
119+
style.wrap('black', 'redbg', 'bold', 'strike', 'Make me RED BACKGROUND STRIKED')+
120+
style.chain('red', 'bold')+ 'Make me RED BOLD'+ style.reset+'\n'+
121+
style.dim+ 'Make me DIM'+ style.reset+'\n'+
122+
style.list('Make me a LIST ITEM','Make me a LIST ITEM')+ style.reset+
123+
style.list(`Make me a ${style.italic}INLINE STYLED${style.reset} LIST ITEM`,`Make me a LIST ITEM`)+ style.reset+
124+
style.todo('+Make me a TODO LIST ITEM','-Make me a DONE LIST ITEM','~Make me a STRIKED LIST ITEM','!Make me a IMPORTANT TODO LIST ITEM', '!+Make me a IMPORTANT TODO LIST ITEM', '!-Make me a IMPORTANT DONE LIST ITEM')+ style.reset
113125
);
114126
},
115127
help: () => {
@@ -129,30 +141,31 @@ E.g., console.log(style.bold, 'Bold ME!', style.reset);
129141
130142
SAMPLES (run \`styles.test()\` to view styled in console)
131143
132-
style.color(255,0,255),'Styled with Custom RGB Color',style.reset,
133-
style.color(39),' Some Styled Text',style.reset,
134-
style.bgColor(18),style.orange,' Some Background Styled Text',style.reset,
135-
style.bgColor(0,255,0),style.black,style.bold,' Some Background Styled Text',style.reset,
136-
style.hr.double,style.reset,
137-
style.wrap('italic', 'Make me ITALIC (reset included for \`style.wrap()\`)'),
138-
style.wrap('black', 'redbg', 'bold', 'strike', 'Make me RED BACKGROUND STRIKED'),
139-
style.chain('red', 'bold'), 'Make me RED BOLD', style.reset,
140-
style.dim, 'Make me DIM', style.reset,
144+
style.color(255,0,255)+'Styled with Custom RGB Color'+style.reset+
145+
style.color(39)+'Some Styled Text'+style.reset+'\\n'+
146+
style.bgColor(18)+style.orange+'Some Background Styled Text'+style.reset+
147+
style.redbg+style.black+style.bold+'Some Background Styled Text'+style.reset+'\\n'+
148+
'\\x1b[33m'+'Some Styled Text'+'\\x1b[0m'+'\\n'+
141149
142-
style.list( 'Make me a LIST ITEM', 'Make me a LIST ITEM'), style.reset,
150+
style.hr.double,
151+
style.wrap('red', style.hr.single),
152+
style.wrap(style.color(255,0,255), style.hr.single),
153+
style.wrap('\\x1b[33m', style.hr.single),
143154
144-
style.list(
145-
\`Make me a \${style.italic}INLINE STYLED\${style.reset} LIST ITEM\`,
146-
\`Make me a LIST ITEM\`), style.reset,
155+
style.wrap('italic', 'Make me ITALIC (reset included for \`style.wrap()\`)')+'\\n'+
156+
style.wrap('black', 'redbg', 'bold', 'strike', 'Make me RED BACKGROUND STRIKED')+
147157
148-
style.todo(
149-
'+Make me a TODO LIST ITEM',
150-
'-Make me a DONE LIST ITEM',
151-
'~Make me a STRIKED LIST ITEM',
152-
'!Make me a IMPORTANT TODO LIST ITEM',
153-
'!+Make me a IMPORTANT TODO LIST ITEM',
154-
'!-Make me a IMPORTANT DONE LIST ITEM'), style.reset,
155-
`);
158+
style.chain('red', 'bold')+ 'Make me RED BOLD'+ style.reset+'\\n'+
159+
style.dim+ 'Make me DIM'+ style.reset+'\\n'+
160+
style.list('Make me a LIST ITEM','Make me a LIST ITEM')+ style.reset+
161+
style.list(\`Make me a \${style.italic}INLINE STYLED\${style.reset} LIST ITEM\`,\`Make me a LIST ITEM\`)+ style.reset+
162+
style.todo('+Make me a TODO LIST ITEM',
163+
'-Make me a DONE LIST ITEM',
164+
'~Make me a STRIKED LIST ITEM',
165+
'!Make me a IMPORTANT TODO LIST ITEM',
166+
'!+Make me a IMPORTANT TODO LIST ITEM',
167+
'!-Make me a IMPORTANT DONE LIST ITEM')+ style.reset
168+
`);
156169
}
157170
}
158171

0 commit comments

Comments
 (0)