diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..8e925c6 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,27 @@ +name: CI +on: + push: + branches: [master] + pull_request: + types: [opened, synchronize] +jobs: + build: + runs-on: ubuntu-16.04 + strategy: + matrix: + deno: ["v1.3.2"] + name: Deno ${{ matrix.deno }} sample + steps: + - uses: actions/checkout@v2 + - name: Setup Deno + uses: denolib/setup-deno@v2 + with: + deno-version: ${{ matrix.deno }} + - name: Check formatted codebase + run: | + deno fmt --check examples/ + deno fmt --check index.ts + - name: Run examples/ + run: | + deno run examples/simple.ts + deno run examples/custom.ts diff --git a/README.md b/README.md index f7858e6..884194b 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ 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) +[![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) [![GitHub Actions](https://github.com/Delta456/box-cli-maker/workflows/cli%20badges/badge.svg)](https://github.com/Delta456/cli_badges/actions?query=workflow%3ACI) ## Features @@ -16,7 +16,7 @@ Generate Badges for your CLI. ## Usage ```ts -import { badges } from "https://deno.land/x/cli_badges@v0.0.3/index.ts"; +import { badges } from "https://deno.land/x/cli_badges@v0.0.4/index.ts"; console.log(badges('failed', '2', {msgBg: "red"})) diff --git a/examples/custom.ts b/examples/custom.ts new file mode 100644 index 0000000..a7d0dc2 --- /dev/null +++ b/examples/custom.ts @@ -0,0 +1,7 @@ +import { badges } from "https://deno.land/x/cli_badges@v0.0.3/index.ts"; + +console.log(badges("failed", "2", { msgBg: "red" })); + +console.log(badges("success", "2", { msgBg: "green" })); + +console.log(badges("skipped", "2", { msgBg: "yellow" })); diff --git a/examples/simple.ts b/examples/simple.ts new file mode 100644 index 0000000..687413a --- /dev/null +++ b/examples/simple.ts @@ -0,0 +1,3 @@ +import { badges } from "https://deno.land/x/cli_badges@v0.0.3/index.ts"; + +console.log(badges("counter", "5")); diff --git a/index.ts b/index.ts index 040d384..8865b0f 100644 --- a/index.ts +++ b/index.ts @@ -1,14 +1,33 @@ import * as color from "https://deno.land/std@0.67.0/fmt/colors.ts"; -type ColorType = { [x: string]: (str: string) => string }; +type Color = + | "black" + | "red" + | "blue" + | "green" + | "yellow" + | "magenta" + | "cyan" + | "white" + | "brightBlack" + | "brightRed" + | "brightBlue" + | "brightGreen" + | "brightYellow" + | "brightMagenta" + | "brightCyan" + | "brightWhite"; +type Format = "italic" | "underline" | "bold" | "inverse" | "dim" | "strike"; +type ColorType = { [c in Color]: (str: string) => string }; +type FormatType = { [format in Format]: (str: string) => string }; export interface BadgeOptions { - msgBg: string; - labelBg: string; - msgColor: string; - labelColor: string; - msgStyle?: string; - labelStyle?: string; + msgBg: Color; + labelBg: Color; + msgColor: Color; + labelColor: Color; + msgStyle?: Format; + labelStyle?: Format; msgWidth?: number; labelWidth?: number; } @@ -51,7 +70,7 @@ const colorTypes: ColorType = { brightWhite: (str: string) => color.brightWhite(str), }; -const formatters: ColorType = { +const formatters: FormatType = { bold: (str: string) => color.bold(str), italic: (str: string) => color.italic(str), inverse: (str: string) => color.inverse(str), @@ -60,7 +79,7 @@ const formatters: ColorType = { underline: (str: string) => color.underline(str), }; -function padd(str: string, width: number | undefined): string { +function padd(str: string, width?: number): string { if (!width) width = str.length + 2; // one space on each side const halfWith = Math.ceil((width - str.length) / 2); @@ -70,7 +89,7 @@ function padd(str: string, width: number | undefined): string { } function getBgColor( - colr: string | undefined, + colr?: Color, ): (str: string) => string { if (!colr) { return color.bgBrightBlack; @@ -79,17 +98,17 @@ function getBgColor( } function getTextColor( - colr: string | undefined, + colr?: Color, ): (str: string) => string { if (!colr) { - return color.blue; + return color.white; } return colorTypes[colr]; } function format( str: string, - formatterName: string | undefined, + formatterName?: Format, ): string { if (!formatterName) return str; @@ -126,12 +145,12 @@ export function badges( const lblStr = padd(label, labelWidth); const msgStr = padd(msg, msgWidth); - const lblColored = getTextColor(labelColor)( - getBgColor(labelBg)(lblStr), - ); const msgColored = getTextColor(msgColor)( getBgColor(msgBg)(msgStr), ); + const lblColored = getTextColor(labelColor)( + getBgColor(labelBg)(lblStr), + ); const labelformat = format(lblColored, labelStyle); const msgformat = format(msgColored, msgStyle);