Skip to content

Commit

Permalink
feat(text-canvas): add formatter fns/utils
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 4, 2021
1 parent a2127ba commit fb4470d
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
15 changes: 15 additions & 0 deletions packages/text-canvas/src/ansi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { memoize1 } from "@thi.ng/memoize";
import type { StringFormat } from "./api";
import { defFormat } from "./string";

const ANSI_RESET = `\x1b[0m`;

Expand Down Expand Up @@ -45,3 +46,17 @@ export const FMT_ANSI256: StringFormat = {
*/
export const format256 = (fg: number, bg = 0) =>
((bg & 0xff) << 8) | (fg & 0xff);

/**
* Syntax sugar for `defFormat(FMT_ANSI16, ...)`
*
* @param col
*/
export const defAnsi16 = (col: number) => defFormat(FMT_ANSI16, col);

/**
* Syntax sugar for `defFormat(FMT_ANSI256, ...)`
*
* @param col
*/
export const defAnsi256 = (col: number) => defFormat(FMT_ANSI256, col);
78 changes: 77 additions & 1 deletion packages/text-canvas/src/string.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
import type { StringFormat } from "./api";
import type { Fn, Keys } from "@thi.ng/api";
import {
FG_BLACK,
FG_BLUE,
FG_CYAN,
FG_GRAY,
FG_GREEN,
FG_LIGHT_BLUE,
FG_LIGHT_CYAN,
FG_LIGHT_GRAY,
FG_LIGHT_GREEN,
FG_LIGHT_MAGENTA,
FG_LIGHT_RED,
FG_LIGHT_YELLOW,
FG_MAGENTA,
FG_RED,
FG_WHITE,
FG_YELLOW,
StringFormat,
} from "./api";
import type { Canvas } from "./canvas";

/**
Expand Down Expand Up @@ -41,3 +60,60 @@ export const toString = (canvas: Canvas, format?: StringFormat) => {
return res.join("");
}
};

/**
* HOF format function. Returns a single-arg function which wraps a given value
* into a fully prefixed & suffixed format string using given
* {@link StringFormat} impl.
*
* @example
* ```ts
* const red = defFormat(FMT_ANSI16, FG_RED);
*
* red("hello");
* // "\x1B[31mhello\x1B[0m"
* ```
*
* @param fmt
* @param code
*/
export const defFormat = (fmt: StringFormat, code: number) => (x: any) =>
fmt.start(code) + x + fmt.end;

const PRESETS = {
black: FG_BLACK,
red: FG_RED,
green: FG_GREEN,
yellow: FG_YELLOW,
blue: FG_BLUE,
magenta: FG_MAGENTA,
cyan: FG_CYAN,
lightGray: FG_LIGHT_GRAY,
gray: FG_GRAY,
lightRed: FG_LIGHT_RED,
lightGreen: FG_LIGHT_GREEN,
lightYellow: FG_LIGHT_YELLOW,
lightBlue: FG_LIGHT_BLUE,
lightMagenta: FG_LIGHT_MAGENTA,
lightCyan: FG_LIGHT_CYAN,
white: FG_WHITE,
};

type PresetID = Keys<typeof PRESETS>;

/**
* Takes a {@link StringFormat} impl supporting preset format ID constants (e.g.
* {@link FG_GREEN}) and returns an object of formatting functions for each
* `FG_XXX` preset ID.
*
* @param fmt
*/
export const defFormatPresets = (fmt: StringFormat) =>
<Record<PresetID, Fn<any, string>>>(
Object.keys(PRESETS).reduce(
(acc, id) => (
(acc[id] = defFormat(fmt, PRESETS[<PresetID>id])), acc
),
<any>{}
)
);

0 comments on commit fb4470d

Please sign in to comment.