@thi.ng/text-format
Advanced tools
Comparing version 1.4.21 to 2.0.0
@@ -6,2 +6,3 @@ import { memoize1 } from "@thi.ng/memoize/memoize1"; | ||
const FMT_ANSI16 = { | ||
format: (code, x) => FMT_ANSI16.start(code) + x + FMT_ANSI16.end, | ||
start: memoize1((x) => { | ||
@@ -22,2 +23,3 @@ let res = []; | ||
const FMT_ANSI256 = { | ||
format: (code, x) => FMT_ANSI256.start(code) + x + FMT_ANSI256.end, | ||
start: (x) => `\x1B[38;5;${x & 255};48;5;${x >>> 8}m`, | ||
@@ -32,2 +34,3 @@ end: ANSI_RESET, | ||
const FMT_ANSI565 = { | ||
format: (code, x) => FMT_ANSI565.start(code) + x + FMT_ANSI565.end, | ||
start: (x) => `\x1B[38;2;${(x >> 11 & 31) * F5 | 0};${(x >> 5 & 63) * F6 | 0};${(x & 31) * F5 | 0}m`, | ||
@@ -34,0 +37,0 @@ end: ANSI_RESET, |
17
api.d.ts
@@ -73,10 +73,17 @@ import type { Fn, Keys } from "@thi.ng/api"; | ||
export type PresetID = Keys<typeof PRESETS_TPL>; | ||
export type FormatPresets = Record<PresetID, Fn<any, string>>; | ||
export type FormatPresets = Record<PresetID, Fn<any, string>> & { | ||
format: StringFormat; | ||
}; | ||
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. | ||
* Coerces `x` into a string and wraps it with formatting corresponding to | ||
* provided `code`. See {@link StringFormat.start} and | ||
* {@link StringFormat.end}. | ||
*/ | ||
format(code: number, x: any): string; | ||
/** | ||
* Function translating format codes (e.g. {@link BG_RED}) 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>; | ||
@@ -83,0 +90,0 @@ /** |
# Change Log | ||
- **Last updated**: 2023-12-11T10:07:09Z | ||
- **Last updated**: 2023-12-18T13:41:19Z | ||
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub) | ||
@@ -12,2 +12,11 @@ | ||
# [2.0.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/text-format@2.0.0) (2023-12-18) | ||
#### 🛑 Breaking changes | ||
- update StringFormat interface ([c04c723](https://github.com/thi-ng/umbrella/commit/c04c723)) | ||
- BREAKING CHANGE: add StringFormat.format() | ||
- update all existing StringFormat impls | ||
- add FormatPresets.format to expose ref to underlying StringFormat | ||
### [1.4.16](https://github.com/thi-ng/umbrella/tree/@thi.ng/text-format@1.4.16) (2023-11-09) | ||
@@ -14,0 +23,0 @@ |
@@ -24,5 +24,9 @@ import { type FormatPresets, type StringFormat } from "./api.js"; | ||
* | ||
* @remarks | ||
* The underlying {@link StringFormat} can be obtained from the result object | ||
* via `.format`. | ||
* | ||
* @param fmt - | ||
*/ | ||
export declare const defFormatPresets: (fmt: StringFormat) => FormatPresets; | ||
export declare const defFormatPresets: (format: StringFormat) => FormatPresets; | ||
/** | ||
@@ -29,0 +33,0 @@ * Returns string representation of a single line/row (of a potentially larger |
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 defFormat = (fmt, code) => (x) => fmt.format(code, x); | ||
const defFormatPresets = (format2) => Object.keys(PRESETS_TPL).reduce( | ||
(acc, id) => (acc[id] = defFormat(format2, PRESETS_TPL[id]), acc), | ||
{ format: format2 } | ||
); | ||
@@ -9,0 +9,0 @@ const format = (format2, data, width = data.length, offset = 0) => { |
40
html.js
@@ -14,4 +14,4 @@ import { U8 } from "@thi.ng/hex"; | ||
underline | ||
}) => ({ | ||
start: memoize1((x) => { | ||
}) => { | ||
const start = memoize1((x) => { | ||
let y = x & 15; | ||
@@ -25,7 +25,12 @@ let res = `<span ${attrib}="${fg}${colors[y - 1 | x >> 1 & 8]}${delim}`; | ||
return res + '">'; | ||
}), | ||
end: "</span>", | ||
prefix: "", | ||
suffix: "<br/>" | ||
}); | ||
}); | ||
const end = "</span>"; | ||
return { | ||
format: (code, x) => start(code) + x + end, | ||
start, | ||
end, | ||
prefix: "", | ||
suffix: "<br/>" | ||
}; | ||
}; | ||
const FMT_HTML_INLINE_CSS = formatHtml({ | ||
@@ -85,13 +90,18 @@ colors: [ | ||
}); | ||
const FMT_HTML565 = (prop = "color") => ({ | ||
start: memoize1( | ||
const FMT_HTML565 = (prop = "color") => { | ||
const 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 | ||
}); | ||
); | ||
const end = "</span>"; | ||
return { | ||
format: (code, x) => start(code) + x + end, | ||
start, | ||
end, | ||
prefix: "", | ||
suffix: "<br/>", | ||
zero: true | ||
}; | ||
}; | ||
export { | ||
@@ -98,0 +108,0 @@ FMT_HTML565, |
const FMT_NONE = { | ||
format: (_, x) => String(x), | ||
prefix: "", | ||
@@ -3,0 +4,0 @@ suffix: "\n", |
{ | ||
"name": "@thi.ng/text-format", | ||
"version": "1.4.21", | ||
"version": "2.0.0", | ||
"description": "Customizable color text formatting with presets for ANSI & HTML", | ||
@@ -38,5 +38,5 @@ "type": "module", | ||
"dependencies": { | ||
"@thi.ng/api": "^8.9.12", | ||
"@thi.ng/hex": "^2.3.24", | ||
"@thi.ng/memoize": "^3.1.46" | ||
"@thi.ng/api": "^8.9.13", | ||
"@thi.ng/hex": "^2.3.25", | ||
"@thi.ng/memoize": "^3.1.47" | ||
}, | ||
@@ -73,3 +73,3 @@ "devDependencies": { | ||
"engines": { | ||
"node": ">=12.7" | ||
"node": ">=18" | ||
}, | ||
@@ -109,3 +109,3 @@ "files": [ | ||
}, | ||
"gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n" | ||
"gitHead": "25a42a81fac8603a1e440a7aa8bc343276211ff4\n" | ||
} |
@@ -211,3 +211,3 @@ <!-- This file is generated - DO NOT EDIT! --> | ||
Package sizes (brotli'd, pre-treeshake): ESM: 1.63 KB | ||
Package sizes (brotli'd, pre-treeshake): ESM: 1.69 KB | ||
@@ -214,0 +214,0 @@ ## Dependencies |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44670
661
Updated@thi.ng/api@^8.9.13
Updated@thi.ng/hex@^2.3.25
Updated@thi.ng/memoize@^3.1.47