Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@thi.ng/text-format

Package Overview
Dependencies
Maintainers
1
Versions
101
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@thi.ng/text-format - npm Package Compare versions

Comparing version 1.4.20 to 1.4.21

145

ansi.js
import { memoize1 } from "@thi.ng/memoize/memoize1";
import { defFormat } from "./format.js";
const ANSI_RESET = `\x1b[0m`;
const ANSI_RESET = `\x1B[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.
*
* https://stackoverflow.com/a/33206814/294515
*/
export const FMT_ANSI16 = {
start: memoize1((x) => {
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 "\x1b[" + res.join(";") + "m";
}),
end: ANSI_RESET,
prefix: ANSI_RESET,
suffix: "\n",
const FMT_ANSI16 = {
start: memoize1((x) => {
let res = [];
let y = x & 15;
y && res.push(29 + (x >> 4 & 1) * 60 + y);
y = x >> 5 & 15;
y && res.push(39 + (x >> 9 & 1) * 60 + y);
y = x >> 10;
y && res.push(ANSI_FLAGS[y]);
return "\x1B[" + res.join(";") + "m";
}),
end: ANSI_RESET,
prefix: ANSI_RESET,
suffix: "\n"
};
/**
* String format preset, translating canvas format info to ANSI 8bit control
* sequences. Only foreground/background colors are supported, no other
* formatting (e.g. bold etc.).
*
* @remarks
* https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
*
* Also see {@link format256}.
*/
export const FMT_ANSI256 = {
start: (x) => `\x1b[38;5;${x & 0xff};48;5;${x >>> 8}m`,
end: ANSI_RESET,
prefix: ANSI_RESET,
suffix: "\n",
zero: true,
const FMT_ANSI256 = {
start: (x) => `\x1B[38;5;${x & 255};48;5;${x >>> 8}m`,
end: ANSI_RESET,
prefix: ANSI_RESET,
suffix: "\n",
zero: true
};
const F5 = 255 / 31;
const F6 = 255 / 63;
/**
* String format preset, interpreting the canvas 16bit format info as text color
* and translating it into ANSI 24bit control sequences. No other formatting
* (e.g. bold etc.) supported in this mode.
*
* @remarks
* This mode is intended for image displays, e.g. using thi.ng/pixel and
* {@link imageRaw}. Also see {@link format556} and {@link format565_8bit}.
*
* https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
*/
export const FMT_ANSI565 = {
start: (x) => `\x1b[38;2;${(((x >> 11) & 31) * F5) | 0};${(((x >> 5) & 63) * F6) | 0};${((x & 31) * F5) | 0}m`,
end: ANSI_RESET,
prefix: ANSI_RESET,
suffix: "\n",
zero: true,
const FMT_ANSI565 = {
start: (x) => `\x1B[38;2;${(x >> 11 & 31) * F5 | 0};${(x >> 5 & 63) * F6 | 0};${(x & 31) * F5 | 0}m`,
end: ANSI_RESET,
prefix: ANSI_RESET,
suffix: "\n",
zero: true
};
/**
* Takes 2 ANSI256 values and returns a combined 16bit format ID.
*
* @remarks
* https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
*
* @param fg -
* @param bg -
*/
export const format256 = (fg, bg = 0) => ((bg & 0xff) << 8) | (fg & 0xff);
/**
* Takes _normalized_ RGB values ([0..1] range) and converts them into an RGB565
* value for later use with {@link FMT_ANSI565} or {@link FMT_HTML_565}. Does
* NOT perform clipping!
*
* @param r -
* @param g -
* @param b -
*/
export const format565 = (r, g, b) => ((r * 0x1f) << 11) | ((g * 0x3f) << 5) | (b * 0x1f);
/**
* Converts RGB (8bit channels) into a 16bit RGB565 value for later use with
* {@link FMT_ANSI565} or {@link FMT_HTML_565}. Does NOT perform clipping!
*
* @param r -
* @param g -
* @param b -
*/
export const format565_8bit = (r, g, b) => ((r << 8) & 0xf800) | ((g << 3) & 0x7e0) | (b >> 3);
/**
* Syntax sugar for `defFormat(FMT_ANSI16, ...)`
*
* @param col -
*/
export const defAnsi16 = (col) => defFormat(FMT_ANSI16, col);
/**
* Syntax sugar for `defFormat(FMT_ANSI256, ...)`
*
* @param col -
*/
export const defAnsi256 = (col) => defFormat(FMT_ANSI256, col);
/**
* Syntax sugar for `defFormat(FMT_ANSI565, ...)`
*
* @param col -
*/
export const defAnsi565 = (col) => defFormat(FMT_ANSI565, col);
const format256 = (fg, bg = 0) => (bg & 255) << 8 | fg & 255;
const format565 = (r, g, b) => r * 31 << 11 | g * 63 << 5 | b * 31;
const format565_8bit = (r, g, b) => r << 8 & 63488 | g << 3 & 2016 | b >> 3;
const defAnsi16 = (col) => defFormat(FMT_ANSI16, col);
const defAnsi256 = (col) => defFormat(FMT_ANSI256, col);
const defAnsi565 = (col) => defFormat(FMT_ANSI565, col);
export {
FMT_ANSI16,
FMT_ANSI256,
FMT_ANSI565,
defAnsi16,
defAnsi256,
defAnsi565,
format256,
format565,
format565_8bit
};

@@ -1,77 +0,109 @@

// 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 const PRESETS_TPL = {
black: FG_BLACK,
blue: FG_BLUE,
cyan: FG_CYAN,
gray: FG_GRAY,
green: FG_GREEN,
magenta: FG_MAGENTA,
red: FG_RED,
white: FG_WHITE,
yellow: FG_YELLOW,
lightBlue: FG_LIGHT_BLUE,
lightCyan: FG_LIGHT_CYAN,
lightGray: FG_LIGHT_GRAY,
lightGreen: FG_LIGHT_GREEN,
lightMagenta: FG_LIGHT_MAGENTA,
lightRed: FG_LIGHT_RED,
lightYellow: FG_LIGHT_YELLOW,
bgBlack: BG_BLACK,
bgBlue: BG_BLUE,
bgCyan: BG_CYAN,
bgGray: BG_GRAY,
bgGreen: BG_GREEN,
bgMagenta: BG_MAGENTA,
bgRed: BG_RED,
bgWhite: BG_WHITE,
bgYellow: BG_YELLOW,
bgLightBlue: BG_LIGHT_BLUE,
bgLightCyan: BG_LIGHT_CYAN,
bgLightGray: BG_LIGHT_GRAY,
bgLightGreen: BG_LIGHT_GREEN,
bgLightMagenta: BG_LIGHT_MAGENTA,
bgLightRed: BG_LIGHT_RED,
bgLightYellow: BG_LIGHT_YELLOW,
const NONE = 0;
const FG_BLACK = 1;
const FG_RED = 2;
const FG_GREEN = 3;
const FG_YELLOW = 4;
const FG_BLUE = 5;
const FG_MAGENTA = 6;
const FG_CYAN = 7;
const FG_LIGHT_GRAY = 8;
const FG_GRAY = 17;
const FG_LIGHT_RED = 18;
const FG_LIGHT_GREEN = 19;
const FG_LIGHT_YELLOW = 20;
const FG_LIGHT_BLUE = 21;
const FG_LIGHT_MAGENTA = 22;
const FG_LIGHT_CYAN = 23;
const FG_WHITE = 24;
const BG_BLACK = 32;
const BG_RED = 64;
const BG_GREEN = 96;
const BG_YELLOW = 128;
const BG_BLUE = 160;
const BG_MAGENTA = 192;
const BG_CYAN = 224;
const BG_LIGHT_GRAY = 256;
const BG_GRAY = 544;
const BG_LIGHT_RED = 576;
const BG_LIGHT_GREEN = 608;
const BG_LIGHT_YELLOW = 640;
const BG_LIGHT_BLUE = 672;
const BG_LIGHT_MAGENTA = 704;
const BG_LIGHT_CYAN = 736;
const BG_WHITE = 768;
const BOLD = 1024;
const DIM = 2048;
const UNDERLINE = 4096;
const PRESETS_TPL = {
black: FG_BLACK,
blue: FG_BLUE,
cyan: FG_CYAN,
gray: FG_GRAY,
green: FG_GREEN,
magenta: FG_MAGENTA,
red: FG_RED,
white: FG_WHITE,
yellow: FG_YELLOW,
lightBlue: FG_LIGHT_BLUE,
lightCyan: FG_LIGHT_CYAN,
lightGray: FG_LIGHT_GRAY,
lightGreen: FG_LIGHT_GREEN,
lightMagenta: FG_LIGHT_MAGENTA,
lightRed: FG_LIGHT_RED,
lightYellow: FG_LIGHT_YELLOW,
bgBlack: BG_BLACK,
bgBlue: BG_BLUE,
bgCyan: BG_CYAN,
bgGray: BG_GRAY,
bgGreen: BG_GREEN,
bgMagenta: BG_MAGENTA,
bgRed: BG_RED,
bgWhite: BG_WHITE,
bgYellow: BG_YELLOW,
bgLightBlue: BG_LIGHT_BLUE,
bgLightCyan: BG_LIGHT_CYAN,
bgLightGray: BG_LIGHT_GRAY,
bgLightGreen: BG_LIGHT_GREEN,
bgLightMagenta: BG_LIGHT_MAGENTA,
bgLightRed: BG_LIGHT_RED,
bgLightYellow: BG_LIGHT_YELLOW
};
export {
BG_BLACK,
BG_BLUE,
BG_CYAN,
BG_GRAY,
BG_GREEN,
BG_LIGHT_BLUE,
BG_LIGHT_CYAN,
BG_LIGHT_GRAY,
BG_LIGHT_GREEN,
BG_LIGHT_MAGENTA,
BG_LIGHT_RED,
BG_LIGHT_YELLOW,
BG_MAGENTA,
BG_RED,
BG_WHITE,
BG_YELLOW,
BOLD,
DIM,
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,
NONE,
PRESETS_TPL,
UNDERLINE
};
# Change Log
- **Last updated**: 2023-12-09T19:12:04Z
- **Last updated**: 2023-12-11T10:07:09Z
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub)

@@ -5,0 +5,0 @@

@@ -1,76 +0,42 @@

import { PRESETS_TPL, } from "./api.js";
/**
* 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, code) => (x) => fmt.start(code) + x + fmt.end;
/**
* 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) => Object.keys(PRESETS_TPL).reduce((acc, id) => ((acc[id] = defFormat(fmt, PRESETS_TPL[id])), acc), {});
/**
* Returns string representation of a single line/row (of a potentially larger
* buffer/canvas), using the given string formatter. Use {@link formatNone} to
* create a plain string representation, ignoring any character format data.
*
* @param format -
* @param data -
* @param width - line length (default length of data)
* @param offset - read offset in data buffer
*/
export const format = (format, data, width = data.length, offset = 0) => {
const { start, end, prefix, suffix, zero } = format;
const check = zero ? () => prevID !== -1 : () => prevID !== 0;
const res = [prefix];
let prevID, ch, id;
prevID = zero ? -1 : 0;
for (let x = 0; x < width; x++) {
ch = data[x + offset];
id = ch >>> 16;
if (id != prevID) {
check() && res.push(end);
(zero || id) && res.push(start(id));
prevID = id;
}
res.push(String.fromCharCode(ch & 0xffff));
import {
PRESETS_TPL
} from "./api.js";
const defFormat = (fmt, code) => (x) => fmt.start(code) + x + fmt.end;
const defFormatPresets = (fmt) => Object.keys(PRESETS_TPL).reduce(
(acc, id) => (acc[id] = defFormat(fmt, PRESETS_TPL[id]), acc),
{}
);
const format = (format2, data, width = data.length, offset = 0) => {
const { start, end, prefix, suffix, zero } = format2;
const check = zero ? () => prevID !== -1 : () => prevID !== 0;
const res = [prefix];
let prevID, ch, id;
prevID = zero ? -1 : 0;
for (let x = 0; x < width; x++) {
ch = data[x + offset];
id = ch >>> 16;
if (id != prevID) {
check() && res.push(end);
(zero || id) && res.push(start(id));
prevID = id;
}
check() && res.push(end);
res.push(suffix);
return res.join("");
res.push(String.fromCharCode(ch & 65535));
}
check() && res.push(end);
res.push(suffix);
return res.join("");
};
/**
* Returns plain string representation of a single line/row (of a potentially
* larger buffer/canvas), ignoring any formatting data (i.e. upper 16 bits of
* each character).
*
* @remarks
* Also see {@link format} for actually applying a {@link StringFormat}.
*
* @param data -
* @param width - line length (default length of data)
* @param offset - read offset in data buffer
*/
export const formatNone = (data, width = data.length, offset = 0) => {
const res = [];
for (let x = 0; x < width; x++) {
res.push(String.fromCharCode(data[x + offset] & 0xffff));
}
res.push("\n");
return res.join("");
const formatNone = (data, width = data.length, offset = 0) => {
const res = [];
for (let x = 0; x < width; x++) {
res.push(String.fromCharCode(data[x + offset] & 65535));
}
res.push("\n");
return res.join("");
};
export {
defFormat,
defFormatPresets,
format,
formatNone
};

@@ -5,98 +5,96 @@ import { U8 } from "@thi.ng/hex";

const F6 = 255 / 63;
/**
* Constructs an HTML formatter using given config options and for use w/
* default format IDs only.
*
* @param opts -
*/
export const formatHtml = ({ colors, attrib, delim, fg, bg, bold, dim, underline, }) => ({
start: memoize1((x) => {
let y = x & 0xf;
let res = `<span ${attrib}="${fg}${colors[(y - 1) | ((x >> 1) & 0x8)]}${delim}`;
y = (x >> 5) & 0x0f;
y && (res += `${bg}${colors[(y - 1) | ((x >> 6) & 0x8)]}${delim}`);
x & 0x400 && (res += bold + delim);
x & 0x800 && (res += dim + delim);
x & 0x1000 && (res += underline + delim);
return res + '">';
}),
end: "</span>",
prefix: "",
suffix: "<br/>",
const formatHtml = ({
colors,
attrib,
delim,
fg,
bg,
bold,
dim,
underline
}) => ({
start: memoize1((x) => {
let y = x & 15;
let res = `<span ${attrib}="${fg}${colors[y - 1 | x >> 1 & 8]}${delim}`;
y = x >> 5 & 15;
y && (res += `${bg}${colors[y - 1 | x >> 6 & 8]}${delim}`);
x & 1024 && (res += bold + delim);
x & 2048 && (res += dim + delim);
x & 4096 && (res += underline + delim);
return res + '">';
}),
end: "</span>",
prefix: "",
suffix: "<br/>"
});
/**
* Preset HTML string formatter for use w/ default format IDs, generating
* `<span>` elements with inline CSS.
*/
export const FMT_HTML_INLINE_CSS = formatHtml({
colors: [
"#000",
"#a00",
"#0a0",
"#aa0",
"#00a",
"#a0a",
"#0aa",
"#aaa",
"#555",
"#f55",
"#5f5",
"#ff5",
"#55f",
"#f5f",
"#5ff",
"#fff",
],
attrib: "style",
delim: ";",
fg: "color:",
bg: "background:",
bold: "font-weight:bold",
dim: "opacity:0.5",
underline: "text-decoration:underline",
const FMT_HTML_INLINE_CSS = formatHtml({
colors: [
"#000",
"#a00",
"#0a0",
"#aa0",
"#00a",
"#a0a",
"#0aa",
"#aaa",
"#555",
"#f55",
"#5f5",
"#ff5",
"#55f",
"#f5f",
"#5ff",
"#fff"
],
attrib: "style",
delim: ";",
fg: "color:",
bg: "background:",
bold: "font-weight:bold",
dim: "opacity:0.5",
underline: "text-decoration:underline"
});
/**
* Preset HTML formatter for use w/ default format IDs, generating `<span>`
* elements with Tachyons CSS classes.
*/
export const FMT_HTML_TACHYONS = formatHtml({
colors: [
"black",
"dark-red",
"dark-green",
"gold",
"dark-blue",
"dark-pink",
"light-blue",
"moon-gray",
"mid-gray",
"red",
"green",
"yellow",
"blue",
"hot-pink",
"lightest-blue",
"white",
],
attrib: "class",
delim: " ",
fg: "",
bg: "bg-",
bold: "b",
dim: "o-50",
underline: "underline",
const FMT_HTML_TACHYONS = formatHtml({
colors: [
"black",
"dark-red",
"dark-green",
"gold",
"dark-blue",
"dark-pink",
"light-blue",
"moon-gray",
"mid-gray",
"red",
"green",
"yellow",
"blue",
"hot-pink",
"lightest-blue",
"white"
],
attrib: "class",
delim: " ",
fg: "",
bg: "bg-",
bold: "b",
dim: "o-50",
underline: "underline"
});
/**
* Higher order custom HTML formatter for 16bit (RGB565) colors (without
* additional formatting flags). By default assigns text color, but can be
* assigned to any CSS property via given `prop` argument.
*
* @param prop -
*/
export const FMT_HTML565 = (prop = "color") => ({
start: memoize1((x) => `<span style="${prop}:#${U8((x >> 11) * F5)}${U8(((x >> 5) & 0x3f) * F6)}${U8((x & 0x1f) * F5)}">`),
end: "</span>",
prefix: "",
suffix: "<br/>",
zero: true,
const FMT_HTML565 = (prop = "color") => ({
start: memoize1(
(x) => `<span style="${prop}:#${U8((x >> 11) * F5)}${U8(
(x >> 5 & 63) * F6
)}${U8((x & 31) * F5)}">`
),
end: "</span>",
prefix: "",
suffix: "<br/>",
zero: true
});
export {
FMT_HTML565,
FMT_HTML_INLINE_CSS,
FMT_HTML_TACHYONS,
formatHtml
};

@@ -1,10 +0,9 @@

/**
* Convenience {@link StringFormat} which ignores any formatting and results in
* plain text.
*/
export const FMT_NONE = {
prefix: "",
suffix: "\n",
start: () => "",
end: "",
const FMT_NONE = {
prefix: "",
suffix: "\n",
start: () => "",
end: ""
};
export {
FMT_NONE
};
{
"name": "@thi.ng/text-format",
"version": "1.4.20",
"version": "1.4.21",
"description": "Customizable color text formatting with presets for ANSI & HTML",

@@ -27,3 +27,5 @@ "type": "module",

"scripts": {
"build": "yarn clean && tsc --declaration",
"build": "yarn build:esbuild && yarn build:decl",
"build:decl": "tsc --declaration --emitDeclarationOnly",
"build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
"clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",

@@ -37,8 +39,9 @@ "doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",

"dependencies": {
"@thi.ng/api": "^8.9.11",
"@thi.ng/hex": "^2.3.23",
"@thi.ng/memoize": "^3.1.45"
"@thi.ng/api": "^8.9.12",
"@thi.ng/hex": "^2.3.24",
"@thi.ng/memoize": "^3.1.46"
},
"devDependencies": {
"@microsoft/api-extractor": "^7.38.3",
"esbuild": "^0.19.8",
"rimraf": "^5.0.5",

@@ -106,3 +109,3 @@ "tools": "^0.0.1",

},
"gitHead": "25f2ac8ff795a432a930119661b364d4d93b59a0\n"
"gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n"
}
import { FMT_ANSI16 } from "./ansi.js";
import { defFormatPresets } from "./format.js";
import { FMT_NONE } from "./none.js";
export const PRESET_ANSI16 = defFormatPresets(FMT_ANSI16);
export const PRESET_NONE = defFormatPresets(FMT_NONE);
const PRESET_ANSI16 = defFormatPresets(FMT_ANSI16);
const PRESET_NONE = defFormatPresets(FMT_NONE);
export {
PRESET_ANSI16,
PRESET_NONE
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc