Skip to content

Commit

Permalink
release: v0.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Sep 1, 2020
1 parent 50c90a2 commit 2493171
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ jobs:
- name: Run examples/
run: |
deno run examples/simple.ts
deno run examples/custom.ts
deno run examples/medium.ts
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ interface BadgeOptions {
labelStyle?: string; // default is null
msgWidth?: number; // default is msg length + 2
labelWidth?: number; // default is label length + 2
is_8bit?: boolean; // default is false as it uses 24 bits
}
```

Expand All @@ -72,8 +73,6 @@ interface BadgeOptions {
- `brightCyan`
- `brightWhite`

Custom color support coming soon

### Background Colors

- `black`
Expand All @@ -93,7 +92,11 @@ Custom color support coming soon
- `brightCyan`
- `brightWhite`

Custom color support coming soon
### Custom Colors

Custom Colors are available and can be used for `msgBg`, `labelmsg`, `msgColor` and `labelColor` only. There are options to make `8 bit` and `24 bit` colors respectively. Set `is_8bit` to true if you want custom 8 bit RGB colors else it will be set to 24 bit RBG colors by default.

**NOTE**: `24 bit` Colors **must** be in a range of `0x000000` and `0xffffff` and `8 bit` Colors **must** in a range of `0x0` and `0xFF`.

### Styles

Expand Down
File renamed without changes.
41 changes: 34 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as color from "https://deno.land/[email protected]/fmt/colors.ts";

type Color =
| number
| "black"
| "red"
| "blue"
Expand Down Expand Up @@ -30,6 +31,7 @@ export interface BadgeOptions {
labelStyle?: Format;
msgWidth?: number;
labelWidth?: number;
is_8bit?: boolean;
}

const colorBgTypes: ColorType = {
Expand Down Expand Up @@ -90,19 +92,33 @@ function padd(str: string, width?: number): string {

function getBgColor(
colr?: Color,
is_8bit?: boolean,
): (str: string) => string {
if (!colr) {
return color.bgBrightBlack;
}
if (typeof colr === "number") {
if (is_8bit) {
return (str: string) => color.bgRgb8(str, colr);
}
return (str: string) => color.bgRgb24(str, colr);
}
return colorBgTypes[colr];
}

function getTextColor(
colr?: Color,
is_8bit?: boolean,
): (str: string) => string {
if (!colr) {
return color.white;
}
if (typeof colr === "number") {
if (is_8bit) {
return (str: string) => color.rgb8(str, colr);
}
return (str: string) => color.rgb24(str, colr);
}
return colorTypes[colr];
}

Expand All @@ -119,7 +135,7 @@ function format(
return str;
}

export const DEFAULT_OPTIONS: BadgeOptions = {
export const DEFAULT_OPTIONS: Partial<BadgeOptions> = {
msgBg: "blue",
labelBg: "brightBlack",
msgColor: "white",
Expand All @@ -140,17 +156,28 @@ export function badges(
msgColor,
msgBg,
msgStyle,
is_8bit,
} = opts;

const lblStr = padd(label, labelWidth);
const msgStr = padd(msg, msgWidth);

const msgColored = getTextColor(msgColor)(
getBgColor(msgBg)(msgStr),
);
const lblColored = getTextColor(labelColor)(
getBgColor(labelBg)(lblStr),
);
let msgColored: string, lblColored: string;

if (!labelBg) {
lblColored = getTextColor(labelColor, is_8bit)(color.bgBrightBlack(lblStr));
} else {
lblColored = getTextColor(labelColor, is_8bit)(
getBgColor(labelBg, is_8bit)(lblStr),
);
}
if (!msgBg) {
msgColored = getTextColor(msgColor, is_8bit)(color.bgBlue(msgStr));
} else {
msgColored = getTextColor(msgColor, is_8bit)(
getBgColor(msgBg, is_8bit)(msgStr),
);
}

const labelformat = format(lblColored, labelStyle);
const msgformat = format(msgColored, msgStyle);
Expand Down

0 comments on commit 2493171

Please sign in to comment.