Skip to content

Commit

Permalink
release: v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 authored Sep 6, 2020
1 parent 2493171 commit 152ac87
Show file tree
Hide file tree
Showing 7 changed files with 416 additions and 36 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-16.04
strategy:
matrix:
deno: ["v1.3.2"]
deno: ["v1.3.2", "v1.3.3"]
name: Deno ${{ matrix.deno }} sample
steps:
- uses: actions/checkout@v2
Expand All @@ -19,9 +19,12 @@ jobs:
deno-version: ${{ matrix.deno }}
- name: Check formatted codebase
run: |
deno fmt --check examples/
deno fmt --check index.ts
deno fmt --check mod.ts
deno fmt --check badge_test.ts
- name: Run tests
run: deno test badge_test.ts
- name: Run examples/
run: |
deno run examples/simple.ts
deno run examples/medium.ts
deno run examples/8_bit.ts
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,36 @@ Generate Badges for your CLI.

[![deno land](http://img.shields.io/badge/available%20on-deno.land/x-lightgrey.svg?logo=deno&labelColor=black)](https://deno.land/x/cli_badges) [![deno version](https://img.shields.io/badge/deno-^1.3.2-lightgrey?logo=deno)](https://github.com/denoland/deno) [![GitHub release](https://img.shields.io/github/release/Delta456/cli_badges.svg)](https://github.com/Delta456/cli_badges/releases) [![CI](https://github.com/Delta456/cli_badges/workflows/CI/badge.svg)](https://github.com/Delta456/cli_badges/actions?query=workflow%3ACI)

**NOTE**: It is recommended to use update pre-exisiting versions of this module to `^v0.1.0` as there are many breaking changes from this release in the API.

## Features
- Make Beautiful Badges in CLI 🤩
- Make Beautiful Badges for CLI 🤩
- Works across all terminals 🦄
- Link support 🔜
- Link support 🔗
- Variety of colors to choose from 🎨
- Written in TS with Deno 🦕
- many more coming...

## Usage

```ts
import { badges } from "https://deno.land/x/cli_badges@v0.0.4/index.ts";
import { badge } from "https://deno.land/x/cli_badges@v0.1.0/mod.ts";

console.log(badges('failed', '2', {msgBg: "red"}))
console.log(badge("failed", "2", { msgBg: "red" }));

console.log(badges('success', '2', {msgBg: "green"}))
console.log(badge("success", "2", { msgBg: "green" }));

console.log(badges('skipped', '2', { msgBg: "yellow"}))
console.log(badge("skipped", "2", { msgBg: "yellow" }));
```

## Output

![sample_output](img/sample_output.png)

## `badges` function
See [examples](./examples/) for more usages

## `badge` function

`badges` function accepts the following arguments:
`badge` function accepts the following arguments:

- `label`: label of the badge
- `msg`: message of the badge
Expand All @@ -49,6 +52,7 @@ interface BadgeOptions {
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
hyper_link?: string; // hyperlink for the badge
}
```

Expand Down Expand Up @@ -107,6 +111,10 @@ Custom Colors are available and can be used for `msgBg`, `labelmsg`, `msgColor`
- `strike`
- `underline`

### Hyperlink Support

Hyperlink is only supported on [some](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda) terminals.

## Acknowledgments

I thank the author of [nombrekeff/cli-badges](https://github.com/nombrekeff/cli-badges) for his original implementation in JS wth Node and also helped me with some Deno issues and giving me guidance on this project as this is my first project with TS.
Expand Down
293 changes: 293 additions & 0 deletions badge_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
import { badge, Color } from "./mod.ts";
import {
assertEquals,
assertNotEquals,
} from "https://deno.land/std@0.67.0/testing/asserts.ts";

const cases = [{
name: "Only Label",
options: { label: "hello", msg: "" },
output: "\u001b[37m\u001b[100m hello \u001b[49m\u001b[39m ",
}, {
name: "Only Msg",
options: { label: "", msg: "hello" },
output: "\u001b[37m\u001b[44m hello \u001b[49m\u001b[39m ",
}, {
name: "Counter Badge",
options: { label: "counter", msg: "5" },
output:
"\u001b[37m\u001b[100m counter \u001b[49m\u001b[39m\u001b[37m\u001b[44m 5 \u001b[49m\u001b[39m ",
}];

for (const { name, options, output } of cases) {
Deno.test(name, function () {
assertEquals(badge(options.label, options.msg), output);
});
}

const color_cases: {
name: string;
options: {
label: string;
msg: string;
opts: {
msgBg: Color;
};
};
output: string;
}[] = [{
name: "Success Badge",
options: { label: "success", msg: "5", opts: { msgBg: "green" } },
output:
"\u001b[37m\u001b[100m success \u001b[49m\u001b[39m\u001b[37m\u001b[42m 5 \u001b[49m\u001b[39m ",
}, {
name: "Fail Badge",
options: { label: "fail", msg: "5", opts: { msgBg: "red" } },
output:
"\u001b[37m\u001b[100m fail \u001b[49m\u001b[39m\u001b[37m\u001b[41m 5 \u001b[49m\u001b[39m ",
}];

for (const { name, options, output } of color_cases) {
Deno.test(name, function (): void {
assertEquals(badge(options.label, options.msg, options.opts), output);
});
}

Deno.test("Custom Badge", function (): void {
assertEquals(
badge("custom", "badge", { msgBg: "red", labelBg: "yellow" }),
`\u001b[37m\u001b[43m custom \u001b[49m\u001b[39m\u001b[37m\u001b[41m badge \u001b[49m\u001b[39m `,
);
});

Deno.test("Invalid Badges", function (): void {
// they all may look the same but they are not!
assertNotEquals(
badge("success", "5", { msgBg: "green" }),
"\u001b[48;5;8m\u001b[38;5;15m success \u001b[0m\u001b[0m\u001b[48;5;2m\u001b[38;5;15m 5 \u001b[0m\u001b[0m ",
);
assertNotEquals(
badge("counter", "5"),
"\u001b[48;5;8m\u001b[38;5;15m counter \u001b[0m\u001b[0m\u001b[48;5;4m\u001b[38;5;15m 5 \u001b[0m\u001b[0m ",
);
assertNotEquals(
badge("fail", "5", { msgBg: "red" }),
"\u001b[48;5;8m\u001b[38;5;15m fail \u001b[0m\u001b[0m\u001b[48;5;1m\u001b[38;5;15m 5 \u001b[0m\u001b[0m ",
);
assertNotEquals(
badge("", "fail"),
"\u001b[48;5;8m\u001b[38;5;15m\u001b[0m\u001b[0m\u001b[48;5;4m\u001b[38;5;15m fail \u001b[0m\u001b[0m",
);
assertNotEquals(
badge("custom", "badge", { msgBg: "red", labelBg: "yellow" }),
"\u001b[48;5;3m\u001b[38;5;15m custom \u001b[0m\u001b[0m\u001b[48;5;1m\u001b[38;5;15m badge \u001b[0m\u001b[0m ",
);
});

Deno.test("Bold", function (): void {
assertEquals(
badge("hello", "", { labelStyle: "bold" }),
"\u001b[1m\u001b[37m\u001b[100m hello \u001b[49m\u001b[39m\u001b[22m ",
);
assertEquals(
badge("hello", "world", { labelStyle: "bold", msgStyle: "bold" }),
"\u001b[1m\u001b[37m\u001b[100m hello \u001b[49m\u001b[39m\u001b[22m\u001b[1m\u001b[37m\u001b[44m world \u001b[49m\u001b[39m\u001b[22m ",
);
assertEquals(
badge("foo", "bar", { labelBg: "cyan", labelStyle: "bold" }),
"\u001b[1m\u001b[37m\u001b[46m foo \u001b[49m\u001b[39m\u001b[22m\u001b[37m\u001b[44m bar \u001b[49m\u001b[39m ",
);

assertEquals(
badge(
"foo",
"bar",
{
labelBg: "green",
labelStyle: "bold",
msgBg: "magenta",
msgStyle: "bold",
},
),
"\u001b[1m\u001b[37m\u001b[42m foo \u001b[49m\u001b[39m\u001b[22m\u001b[1m\u001b[37m\u001b[45m bar \u001b[49m\u001b[39m\u001b[22m ",
);

assertEquals(
badge(
"foo",
"bar",
{
labelBg: "green",
labelStyle: "bold",
msgBg: "magenta",
msgStyle: "bold",
labelColor: "red",
},
),
"\u001b[1m\u001b[31m\u001b[42m foo \u001b[49m\u001b[39m\u001b[22m\u001b[1m\u001b[37m\u001b[45m bar \u001b[49m\u001b[39m\u001b[22m ",
);

assertEquals(
badge(
"foo",
"bar",
{
labelBg: "green",
labelStyle: "bold",
msgBg: "magenta",
msgStyle: "bold",
labelColor: "yellow",
msgColor: "green",
},
),
"\u001b[1m\u001b[33m\u001b[42m foo \u001b[49m\u001b[39m\u001b[22m\u001b[1m\u001b[32m\u001b[45m bar \u001b[49m\u001b[39m\u001b[22m ",
);
});

Deno.test("Italic", function (): void {
assertEquals(
badge("hello", "", { labelStyle: "italic" }),
"\u001b[3m\u001b[37m\u001b[100m hello \u001b[49m\u001b[39m\u001b[23m ",
);
assertEquals(
badge("hello", "world", { labelStyle: "italic", msgStyle: "italic" }),
"\u001b[3m\u001b[37m\u001b[100m hello \u001b[49m\u001b[39m\u001b[23m\u001b[3m\u001b[37m\u001b[44m world \u001b[49m\u001b[39m\u001b[23m ",
);
assertEquals(
badge(
"hello",
"world",
{ labelStyle: "italic", msgStyle: "italic", msgBg: "cyan" },
),
"\u001b[3m\u001b[37m\u001b[100m hello \u001b[49m\u001b[39m\u001b[23m\u001b[3m\u001b[37m\u001b[46m world \u001b[49m\u001b[39m\u001b[23m ",
);
assertEquals(
badge(
"hello",
"world",
{
labelStyle: "italic",
msgStyle: "italic",
msgBg: "cyan",
labelBg: "red",
},
),
"\u001b[3m\u001b[37m\u001b[41m hello \u001b[49m\u001b[39m\u001b[23m\u001b[3m\u001b[37m\u001b[46m world \u001b[49m\u001b[39m\u001b[23m ",
);
assertEquals(
badge(
"hello",
"world",
{
labelStyle: "italic",
msgStyle: "italic",
msgBg: "cyan",
labelBg: "red",
labelColor: "magenta",
},
),
"\u001b[3m\u001b[35m\u001b[41m hello \u001b[49m\u001b[39m\u001b[23m\u001b[3m\u001b[37m\u001b[46m world \u001b[49m\u001b[39m\u001b[23m ",
);
assertEquals(
badge(
"foo",
"bar",
{
labelStyle: "italic",
msgStyle: "italic",
msgBg: "cyan",
labelBg: "red",
labelColor: "magenta",
msgColor: "blue",
},
),
"\u001b[3m\u001b[35m\u001b[41m foo \u001b[49m\u001b[39m\u001b[23m\u001b[3m\u001b[34m\u001b[46m bar \u001b[49m\u001b[39m\u001b[23m ",
);
});

Deno.test("Inverse", function (): void {
assertEquals(
badge("foo", "", { labelStyle: "inverse" }),
"\u001b[7m\u001b[37m\u001b[100m foo \u001b[49m\u001b[39m\u001b[27m ",
);
assertEquals(
badge("foo", "bar", { labelStyle: "inverse", msgStyle: "inverse" }),
"\u001b[7m\u001b[37m\u001b[100m foo \u001b[49m\u001b[39m\u001b[27m\u001b[7m\u001b[37m\u001b[44m bar \u001b[49m\u001b[39m\u001b[27m ",
);
assertEquals(
badge(
"foo",
"bar",
{ labelStyle: "inverse", msgStyle: "inverse", labelBg: "red" },
),
"\u001b[7m\u001b[37m\u001b[41m foo \u001b[49m\u001b[39m\u001b[27m\u001b[7m\u001b[37m\u001b[44m bar \u001b[49m\u001b[39m\u001b[27m ",
);
assertEquals(
badge(
"foo",
"bar",
{
labelStyle: "inverse",
msgStyle: "inverse",
labelBg: "red",
msgBg: "yellow",
},
),
"\u001b[7m\u001b[37m\u001b[41m foo \u001b[49m\u001b[39m\u001b[27m\u001b[7m\u001b[37m\u001b[43m bar \u001b[49m\u001b[39m\u001b[27m ",
);
});

Deno.test("Dim", function (): void {
assertEquals(
badge("foo", "", { labelStyle: "dim" }),
"\u001b[2m\u001b[37m\u001b[100m foo \u001b[49m\u001b[39m\u001b[22m ",
);
assertEquals(
badge("foo", "bar", { labelStyle: "dim", msgStyle: "dim" }),
"\u001b[2m\u001b[37m\u001b[100m foo \u001b[49m\u001b[39m\u001b[22m\u001b[2m\u001b[37m\u001b[44m bar \u001b[49m\u001b[39m\u001b[22m ",
);
});

Deno.test("Strike", function (): void {
assertEquals(
badge("foo", "", { labelStyle: "strike" }),
"\u001b[9m\u001b[37m\u001b[100m foo \u001b[49m\u001b[39m\u001b[29m ",
);
assertEquals(
badge("foo", "bar", { labelStyle: "strike", msgStyle: "strike" }),
"\u001b[9m\u001b[37m\u001b[100m foo \u001b[49m\u001b[39m\u001b[29m\u001b[9m\u001b[37m\u001b[44m bar \u001b[49m\u001b[39m\u001b[29m ",
);
});

Deno.test("Underline", function (): void {
assertEquals(
badge("foo", "", { labelStyle: "underline" }),
"\u001b[4m\u001b[37m\u001b[100m foo \u001b[49m\u001b[39m\u001b[24m ",
);
assertEquals(
badge("foo", "bar", { labelStyle: "underline", msgStyle: "underline" }),
"\u001b[4m\u001b[37m\u001b[100m foo \u001b[49m\u001b[39m\u001b[24m\u001b[4m\u001b[37m\u001b[44m bar \u001b[49m\u001b[39m\u001b[24m ",
);
});

Deno.test("8 bit", function (): void {
assertEquals(
badge("foo", "", { labelColor: 0x34, is_8bit: true }),
"\u001b[38;5;52m\u001b[100m foo \u001b[49m\u001b[39m ",
);
assertEquals(
badge("foo", "bar", { labelColor: 0x34, is_8bit: true, msgColor: 0x32 }),
"\u001b[38;5;52m\u001b[100m foo \u001b[49m\u001b[39m\u001b[38;5;50m\u001b[44m bar \u001b[49m\u001b[39m ",
);
});

Deno.test("Hyperlink", function (): void {
assertEquals(
badge("foo", "", { hyper_link: "https://github.com/" }),
"\u001b]8;; https://github.com/ \u0007 \u001b[37m\u001b[100m foo \u001b[49m\u001b[39m \u001b]8;;\u0007",
);
assertEquals(
badge("foo", "bar", { hyper_link: "https://github.com/" }),
"\u001b]8;; https://github.com/ \u0007 \u001b[37m\u001b[100m foo \u001b[49m\u001b[39m\u001b[37m\u001b[44m bar \u001b[49m\u001b[39m \u001b]8;;\u0007",
);
});
Loading

0 comments on commit 152ac87

Please sign in to comment.