@kvndy/attributed-string 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/attributed-string.d.ts +7 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.js +49 -0
- package/dist/types.d.ts +16 -0
- package/package.json +30 -5
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kevin Doughty
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { AttributeRun } from "./types.js";
|
|
2
|
+
export type { AttributedString } from "./types.js";
|
|
3
|
+
/** Transitional adapter for the existing component/DOM span makers.
|
|
4
|
+
* Offsets are UTF-16 and half-open. Later runs override individual properties.
|
|
5
|
+
* Invalid ranges throw; input order need not be sorted. No attributes are shifted on edits.
|
|
6
|
+
*/
|
|
7
|
+
export declare function iterateAttributes<T>(text: string, runs: readonly AttributeRun[], make: (text: string, foreground: string, background: string, start: number, end: number, className: string) => T, start?: number, end?: number, backtrack?: boolean, direction?: number): Generator<T, void>;
|
package/dist/main.d.ts
ADDED
package/dist/main.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** Transitional adapter for the existing component/DOM span makers.
|
|
2
|
+
* Offsets are UTF-16 and half-open. Later runs override individual properties.
|
|
3
|
+
* Invalid ranges throw; input order need not be sorted. No attributes are shifted on edits.
|
|
4
|
+
*/
|
|
5
|
+
function* iterateAttributes(text, runs, make, start = 0, end = text.length, backtrack = false, direction = 1) {
|
|
6
|
+
start = Math.max(0, Math.min(text.length, start));
|
|
7
|
+
end = Math.max(start, Math.min(text.length, end));
|
|
8
|
+
if (backtrack && start > 0)
|
|
9
|
+
start = text.lastIndexOf("\n", start - 1) + 1;
|
|
10
|
+
const boundaries = new Set([start, end]);
|
|
11
|
+
const active = runs.filter(run => {
|
|
12
|
+
const { start: a, end: b } = run.range;
|
|
13
|
+
if (!Number.isInteger(a) || !Number.isInteger(b) || a < 0 || b < a || b > text.length) {
|
|
14
|
+
throw new RangeError(`Invalid attribute range [${a}, ${b}) for text length ${text.length}`);
|
|
15
|
+
}
|
|
16
|
+
if (a >= end || b <= start || a === b)
|
|
17
|
+
return false;
|
|
18
|
+
boundaries.add(Math.max(start, a));
|
|
19
|
+
boundaries.add(Math.min(end, b));
|
|
20
|
+
return true;
|
|
21
|
+
});
|
|
22
|
+
const points = [...boundaries].sort((a, b) => a - b);
|
|
23
|
+
const segments = [];
|
|
24
|
+
for (let i = 0; i < points.length - 1; i++) {
|
|
25
|
+
const a = points[i], b = points[i + 1];
|
|
26
|
+
if (a === b)
|
|
27
|
+
continue;
|
|
28
|
+
const attributes = {};
|
|
29
|
+
for (const run of active)
|
|
30
|
+
if (run.range.start <= a && run.range.end >= b) {
|
|
31
|
+
if (run.attributes.foreground !== undefined)
|
|
32
|
+
attributes.foreground = run.attributes.foreground;
|
|
33
|
+
if (run.attributes.background !== undefined)
|
|
34
|
+
attributes.background = run.attributes.background;
|
|
35
|
+
}
|
|
36
|
+
segments.push({ start: a, end: b, attributes });
|
|
37
|
+
}
|
|
38
|
+
if (direction < 0)
|
|
39
|
+
segments.reverse();
|
|
40
|
+
for (const segment of segments) {
|
|
41
|
+
let content = text.slice(segment.start, segment.end);
|
|
42
|
+
// Preserve the reference renderer's trailing-newline compensation for now.
|
|
43
|
+
if (segment.end === text.length && content.endsWith("\n"))
|
|
44
|
+
content += " ";
|
|
45
|
+
yield make(content, segment.attributes.foreground ?? "", segment.attributes.background ?? "", segment.start, segment.end, "");
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { iterateAttributes };
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface TextRange {
|
|
2
|
+
readonly start: number;
|
|
3
|
+
readonly end: number;
|
|
4
|
+
}
|
|
5
|
+
export interface TextAttributes {
|
|
6
|
+
readonly foreground?: string;
|
|
7
|
+
readonly background?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface AttributeRun {
|
|
10
|
+
readonly range: TextRange;
|
|
11
|
+
readonly attributes: TextAttributes;
|
|
12
|
+
}
|
|
13
|
+
export interface AttributedString {
|
|
14
|
+
readonly text: string;
|
|
15
|
+
readonly runs: readonly AttributeRun[];
|
|
16
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@kvndy/attributed-string",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "String attributes for monospace text and background colors",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Kevin Doughty",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://gitlab.com/kevindoughty/attributed-string.git",
|
|
11
|
+
"directory": "attributed-string"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"main": "./dist/main.js",
|
|
17
|
+
"types": "./dist/main.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/main.d.ts",
|
|
21
|
+
"import": "./dist/main.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsc && rollup -c"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"rollup": "^4.0.0",
|
|
29
|
+
"typescript": "^7.0.2"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/README.md
DELETED