Skip to content

Commit f1177cc

Browse files
committed
feat: added support for xterm colors
1 parent cda0e86 commit f1177cc

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

README.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ You can also only show the label:
6262
```js
6363
const { badge } = require('../index');
6464

65-
const onlyLabel = badge('❤️ donate', '', { labelColor: 'magenta' });
65+
const onlyLabel = badge('❤️ donate', '', { labelColor: 169 });
6666

6767
console.log(onlyLabel);
6868
```
@@ -82,10 +82,10 @@ export function badge(
8282
label?: string,
8383
message?: string,
8484
options?: {
85-
labelBg?: string;
86-
messageBg?: string;
87-
labelColor?: string;
88-
messageColor?: string;
85+
labelBg?: string | number;
86+
messageBg?: string | number;
87+
labelColor?: string | number;
88+
messageColor?: string | number;
8989
labelStyle?: any;
9090
messageStyle?: any;
9191
labelWidth?: any;
@@ -136,6 +136,10 @@ export function badge(
136136
- `cyanBright`
137137
- `whiteBright`
138138

139+
There are more colors available using xterm colors, see [cli-color xterm colors](https://github.com/medikoo/cli-color#xterm-colors-256-colors-table) for the complete list of available colors.
140+
141+
> **Not supported on Windows and some terminals.** However if used in not supported environment, the closest color from basic (16 colors) palette is chosen.
142+
139143
### Links
140144

141145
You can output badges with a link attached to it, that can be clicked in some terminals.

example/xterm-colors.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const { badge } = require('../index');
2+
3+
console.log(
4+
badge('test', 'xterm', { messageColor: 26, labelBg: 164 })
5+
);

index.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,26 @@ const padd = (s = '', width) => {
1111
return paddStr + s + paddStr;
1212
};
1313
const cap = (s) => s.charAt(0).toUpperCase() + s.substring(1, s.length);
14-
const getBg = (clc, color) => clc[`bg${cap(color)}`] || clc.bgBlue;
14+
const getBg = (clc, color) => {
15+
const noColor = color == undefined || color === null;
16+
const isString = typeof color === 'string';
17+
const isNumber = typeof color === 'number';
18+
19+
if (noColor) return clc.bgBlue;
20+
if (isString) return clc[`bg${cap(color)}`];
21+
if (isNumber) return clc.bgXterm(color);
22+
};
23+
24+
const getLabel = (clc, color) => {
25+
const noColor = color == undefined || color === null;
26+
const isString = typeof color === 'string';
27+
const isNumber = typeof color === 'number';
28+
29+
if (noColor) return clc.blue;
30+
if (isString) return clc[color];
31+
if (isNumber) return clc.xterm(color);
32+
};
33+
1534

1635
const formatters = {
1736
bold: (clc, s) => clc.bold(s),
@@ -35,8 +54,8 @@ module.exports = {
3554
messageWidth = null,
3655
link = null,
3756
} = {}) {
38-
const lblColorer = getBg(clc, labelBg)[labelColor];
39-
const msgColorer = getBg(clc, messageBg)[messageColor];
57+
const lblColorer = getLabel(getBg(clc, labelBg), labelColor);
58+
const msgColorer = getLabel(getBg(clc, messageBg), messageColor);
4059

4160
const lblFormatted = format(lblColorer, padd(label, labelWidth), labelStyle)
4261
const msgFormatted = format(msgColorer, padd(message, messageWidth), messageStyle)

0 commit comments

Comments
 (0)