Skip to content

Commit

Permalink
refactor(text-canvas): update to use thi.ng/text-format
Browse files Browse the repository at this point in the history
BREAKING CHANGE: migrate formatting consts/functions to new pkg

- see 8c28655 for details
- rename `toString()` => `formatCanvas()`
- update dependencies
  • Loading branch information
postspectacular committed Sep 19, 2021
1 parent 8c28655 commit aa67a5a
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 507 deletions.
4 changes: 1 addition & 3 deletions packages/text-canvas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@thi.ng/math": "^4.0.6",
"@thi.ng/memoize": "^2.1.21",
"@thi.ng/strings": "^2.1.7",
"@thi.ng/text-format": "^0.0.1",
"@thi.ng/transducers": "^7.9.2"
},
"devDependencies": {
Expand Down Expand Up @@ -114,9 +115,6 @@
"./rect": {
"import": "./rect.js"
},
"./string": {
"import": "./string.js"
},
"./style": {
"import": "./style.js"
},
Expand Down
123 changes: 1 addition & 122 deletions packages/text-canvas/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Fn, FnN, NumOrString } from "@thi.ng/api";
import type { FnN, NumOrString } from "@thi.ng/api";

export enum Align {
LEFT,
Expand Down Expand Up @@ -77,127 +77,6 @@ export interface ClipRect {
h: number;
}

export interface StringFormat {
/**
* Function translating canvas character format codes to the actual
* output format. This function will only be called when needed,
* i.e. when a character's format is different than that of the
* previous.
*/
start: Fn<number, string>;
/**
* Format end string (e.g. to ANSI reset or `</span>`).
*/
end: string;
/**
* Prefix for each canvas row / line result string
*/
prefix: string;
/**
* Suffix for each canvas row / line result string (e.g. linebreak)
*/
suffix: string;
/**
* If true, DON'T skip 0-valued format IDs during formatting.
*
* @remarks
* This is needed for various custom color-only formats, e.g. in order to
* reproduce black in `FMT_ANSI565`. In the default format, a zero refers to
* the default format of the target.
*
* @defaultValue false
*/
zero?: boolean;
}

export interface HtmlFormatOpts {
/**
* Array of 16 color strings, in this order: black, red, green,
* yellow, blue, magenta, cyan, white, then repeated as bright
* versions.
*/
colors: string[];
/**
* HTML attrib name.
*/
attrib: string;
/**
* Delimiter between individual formatting terms (e.g. `;` for CSS
* rules or ` ` for CSS class names).
*/
delim: string;
/**
* Prefix string for foreground colors
*/
fg: string;
/**
* Prefix string for background colors
*/
bg: string;
/**
* Bold format string
*/
bold: string;
/**
* Dimmed format string
*/
dim: string;
/**
* Underline format string
*/
underline: string;
}

// bits 0-3: fg
// bit 4: bright fg
// bits 5-8: bg
// bit 9: bright bg
// bit 10: bold
// bit 11: dim
// bit 12: underline

export const NONE = 0;

export const FG_BLACK = 1;
export const FG_RED = 2;
export const FG_GREEN = 3;
export const FG_YELLOW = 4;
export const FG_BLUE = 5;
export const FG_MAGENTA = 6;
export const FG_CYAN = 7;
export const FG_LIGHT_GRAY = 8;

export const FG_GRAY = 0x11;
export const FG_LIGHT_RED = 0x12;
export const FG_LIGHT_GREEN = 0x13;
export const FG_LIGHT_YELLOW = 0x14;
export const FG_LIGHT_BLUE = 0x15;
export const FG_LIGHT_MAGENTA = 0x16;
export const FG_LIGHT_CYAN = 0x17;
export const FG_WHITE = 0x18;

export const BG_BLACK = 0x20;
export const BG_RED = 0x40;
export const BG_GREEN = 0x60;
export const BG_YELLOW = 0x80;
export const BG_BLUE = 0xa0;
export const BG_MAGENTA = 0xc0;
export const BG_CYAN = 0xe0;
export const BG_LIGHT_GRAY = 0x100;

export const BG_GRAY = 0x220;
export const BG_LIGHT_RED = 0x240;
export const BG_LIGHT_GREEN = 0x260;
export const BG_LIGHT_YELLOW = 0x280;
export const BG_LIGHT_BLUE = 0x2a0;
export const BG_LIGHT_MAGENTA = 0x2c0;
export const BG_LIGHT_CYAN = 0x2e0;
export const BG_WHITE = 0x300;

export const BOLD = 0x400;
export const DIM = 0x800;
export const UNDERLINE = 0x1000;

export interface StrokeStyle {
hl: string;
vl: string;
Expand Down
3 changes: 2 additions & 1 deletion packages/text-canvas/src/canvas.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Fn0, NumOrString } from "@thi.ng/api";
import { peek } from "@thi.ng/arrays/peek";
import { clamp } from "@thi.ng/math/interval";
import { ClipRect, NONE, StrokeStyle, STYLE_ASCII } from "./api";
import { NONE } from "@thi.ng/text-format/api";
import { ClipRect, StrokeStyle, STYLE_ASCII } from "./api";
import { charCode, intersectRect } from "./utils";

export class Canvas {
Expand Down
Loading

0 comments on commit aa67a5a

Please sign in to comment.