Skip to content

Commit

Permalink
feat(text-canvas): major update/rewrite, format support
Browse files Browse the repository at this point in the history
- use Uint32Array as backing buffer
- add support for arbitrary format IDs (highest 16bit)
- add configurable string formatting
- add ANSI & HTML format presets
- add color & format constants
- re-org source files
  • Loading branch information
postspectacular committed Feb 15, 2020
1 parent 0587a66 commit 57a7487
Show file tree
Hide file tree
Showing 12 changed files with 775 additions and 322 deletions.
26 changes: 26 additions & 0 deletions packages/text-canvas/src/ansi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { memoize1 } from "@thi.ng/memoize";
import { StringFormat } from "./api";

const ANSI_RESET = `\u001B[0m`;

const ANSI_FLAGS = ["", "1", "2", "1;2", "4", "1;4", "2;4", "1;2;4"];

/**
* String format preset, translating canvas format info to ANSI 4bit
* control sequences.
*/
export const FMT_ANSI16: StringFormat = {
start: memoize1((x: number) => {
let res = [];
let y = x & 0xf;
y && res.push(29 + ((x >> 4) & 1) * 60 + y);
y = (x >> 5) & 0xf;
y && res.push(39 + ((x >> 9) & 1) * 60 + y);
y = x >> 10;
y && res.push(ANSI_FLAGS[y]);
return "\u001b[" + res.join(";") + "m";
}),
end: ANSI_RESET,
prefix: ANSI_RESET,
suffix: "\n"
};
98 changes: 96 additions & 2 deletions packages/text-canvas/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,100 @@
import { IObjectOf } from "@thi.ng/api";
import { Fn, IObjectOf } from "@thi.ng/api";

export const ENDINGS = "▶◀▲▼•●";
export interface ClipRect {
x1: number;
y1: number;
x2: number;
y2: number;
w: number;
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.
*/
end: string;
/**
* Prefix for entire canvas result string
*/
prefix: string;
/**
* Suffix for entire canvas result string
*/
suffix: string;
}

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: string;
dim: string;
underline: string;
}

// bits 0-3: fg
// bit 3: bright fg
// bits 4-7: bg
// bit 8: bright bg
// bit 9: bold
// bit 10: dim
// bit 11: 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_WHITE = 8;

export const BG_BLACK = 1 << 5;
export const BG_RED = 2 << 5;
export const BG_GREEN = 3 << 5;
export const BG_YELLOW = 4 << 5;
export const BG_BLUE = 5 << 5;
export const BG_MAGENTA = 6 << 5;
export const BG_CYAN = 7 << 5;
export const BG_WHITE = 8 << 5;

export const FG_BRIGHT = 1 << 4;
export const BG_BRIGHT = 1 << 9;

export const BOLD = 1 << 10;
export const DIM = 1 << 11;
export const UNDERLINE = 1 << 12;

export const ENDINGS = "()[]{}<>◀▶▲▼•●";

export interface StrokeStyle {
hl: string;
Expand Down
Loading

0 comments on commit 57a7487

Please sign in to comment.