@ansi-tools/parser 0.0.0 → 0.0.2
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/README.md +6 -2
- package/dist/escaped.d.ts +4 -1
- package/dist/escaped.js +20 -11
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/{parse-ClmKWMZx.js → parse-BE4NnMTj.js} +30 -21
- package/dist/{parse-BirjVUvQ.d.ts → parse-BtpkwNjf.d.ts} +1 -0
- package/package.json +20 -9
- package/src/constants.ts +0 -40
- package/src/escaped.ts +0 -4
- package/src/index.ts +0 -4
- package/src/parse.escaped.test.ts +0 -86
- package/src/parse.test.ts +0 -86
- package/src/parse.ts +0 -111
- package/src/parsers/csi.test.ts +0 -55
- package/src/parsers/csi.ts +0 -54
- package/src/parsers/dcs.test.ts +0 -47
- package/src/parsers/dcs.ts +0 -36
- package/src/parsers/dec.test.ts +0 -24
- package/src/parsers/dec.ts +0 -30
- package/src/parsers/esc.test.ts +0 -19
- package/src/parsers/esc.ts +0 -6
- package/src/parsers/osc.test.ts +0 -36
- package/src/parsers/osc.ts +0 -29
- package/src/tokenize.escaped.test.ts +0 -410
- package/src/tokenize.escaped.ts +0 -191
- package/src/tokenize.test.ts +0 -118
- package/src/tokenize.ts +0 -140
- package/src/types.ts +0 -24
- package/tsconfig.json +0 -16
package/src/parsers/csi.test.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { parseCSI, parsePrivateCSI } from "./csi.ts";
|
|
4
|
-
import { CODE_TYPES } from "../constants.ts";
|
|
5
|
-
|
|
6
|
-
test("parseCSI simple command", () => {
|
|
7
|
-
const result = parseCSI(0, "\\e[31m", "31", "m");
|
|
8
|
-
assert.deepEqual(result, { type: CODE_TYPES.CSI, pos: 0, raw: "\\e[31m", params: ["31"], command: "m" });
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
test("parseCSI with multiple params", () => {
|
|
12
|
-
const result = parseCSI(0, "\\e[1;31m", "1;31", "m");
|
|
13
|
-
assert.deepEqual(result, { type: CODE_TYPES.CSI, pos: 0, raw: "\\e[1;31m", params: ["1", "31"], command: "m" });
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
test("parseCSI with missing parameters", () => {
|
|
17
|
-
const result = parseCSI(0, "\\e[;31m", ";31", "m");
|
|
18
|
-
assert.deepEqual(result, { type: CODE_TYPES.CSI, pos: 0, raw: "\\e[;31m", params: ["-1", "31"], command: "m" });
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
test("parseCSI with trailing semicolon", () => {
|
|
22
|
-
const result = parseCSI(0, "\\e[31;m", "31;", "m");
|
|
23
|
-
assert.deepEqual(result, { type: CODE_TYPES.CSI, pos: 0, raw: "\\e[31;m", params: ["31", "-1"], command: "m" });
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
test("parseCSI with leading semicolon", () => {
|
|
27
|
-
const result = parseCSI(0, "\\e[;m", ";", "m");
|
|
28
|
-
assert.deepEqual(result, { type: CODE_TYPES.CSI, pos: 0, raw: "\\e[;m", params: ["-1", "-1"], command: "m" });
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test("parseCSI no data", () => {
|
|
32
|
-
const result = parseCSI(0, "\\e[m", "", "m");
|
|
33
|
-
assert.deepEqual(result, { type: CODE_TYPES.CSI, pos: 0, raw: "\\e[m", params: [], command: "m" });
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
test("parsePrivateCSI with < introducer", () => {
|
|
37
|
-
const result = parsePrivateCSI(0, "\\e[<31m", "<31", "m");
|
|
38
|
-
assert.deepEqual(result, { type: CODE_TYPES.PRIVATE, pos: 0, raw: "\\e[<31m", params: ["31"], command: "<m" });
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test("parsePrivateCSI with > introducer", () => {
|
|
42
|
-
const result = parsePrivateCSI(0, "\\e[>c", ">", "c");
|
|
43
|
-
assert.deepEqual(result, { type: CODE_TYPES.PRIVATE, pos: 0, raw: "\\e[>c", params: [], command: ">c" });
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
test("parsePrivateCSI with = introducer", () => {
|
|
47
|
-
const result = parsePrivateCSI(0, "\\e[=1;2c", "=1;2", "c");
|
|
48
|
-
assert.deepEqual(result, {
|
|
49
|
-
type: CODE_TYPES.PRIVATE,
|
|
50
|
-
pos: 0,
|
|
51
|
-
raw: "\\e[=1;2c",
|
|
52
|
-
params: ["1", "2"],
|
|
53
|
-
command: "=c",
|
|
54
|
-
});
|
|
55
|
-
});
|
package/src/parsers/csi.ts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { CODE_TYPES } from "../constants.ts";
|
|
2
|
-
import type { CONTROL_CODE } from "../types.ts";
|
|
3
|
-
|
|
4
|
-
export function parseCSI(pos: number, raw: string, data: string, final: string): CONTROL_CODE {
|
|
5
|
-
const params = [];
|
|
6
|
-
let intermediates = "";
|
|
7
|
-
if (data) {
|
|
8
|
-
let i = 0;
|
|
9
|
-
let paramSection = "";
|
|
10
|
-
while (i < data.length && data.charCodeAt(i) >= 0x30 && data.charCodeAt(i) <= 0x3f) {
|
|
11
|
-
paramSection += data[i];
|
|
12
|
-
i++;
|
|
13
|
-
}
|
|
14
|
-
intermediates = data.slice(i);
|
|
15
|
-
if (paramSection) {
|
|
16
|
-
let current = "";
|
|
17
|
-
for (let j = 0; j < paramSection.length; j++) {
|
|
18
|
-
if (paramSection[j] === ";") {
|
|
19
|
-
params.push(current || "-1");
|
|
20
|
-
current = "";
|
|
21
|
-
} else {
|
|
22
|
-
current += paramSection[j];
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
params.push(current || "-1");
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
const command = intermediates + final;
|
|
29
|
-
return { type: CODE_TYPES.CSI, pos, raw, command, params };
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function parsePrivateCSI(pos: number, raw: string, data: string, final: string): CONTROL_CODE {
|
|
33
|
-
const privateIndicator = data[0];
|
|
34
|
-
const withoutIndicator = data.slice(1);
|
|
35
|
-
const match = withoutIndicator.match(/^([\d;]*)(.*)/);
|
|
36
|
-
const paramsRaw = match?.[1] ?? "";
|
|
37
|
-
const intermediates = match?.[2] ?? "";
|
|
38
|
-
const command = `${privateIndicator}${intermediates}${final}`;
|
|
39
|
-
const params = [];
|
|
40
|
-
if (paramsRaw) {
|
|
41
|
-
let current = "";
|
|
42
|
-
for (let i = 0; i < paramsRaw.length; i++) {
|
|
43
|
-
if (paramsRaw[i] === ";") {
|
|
44
|
-
params.push(current || "-1");
|
|
45
|
-
current = "";
|
|
46
|
-
} else {
|
|
47
|
-
current += paramsRaw[i];
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
params.push(current || "-1");
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return { type: CODE_TYPES.PRIVATE, pos, raw, command, params };
|
|
54
|
-
}
|
package/src/parsers/dcs.test.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { parseDCS } from "./dcs.ts";
|
|
4
|
-
import { CODE_TYPES } from "../constants.ts";
|
|
5
|
-
|
|
6
|
-
test("parseDCS simple sequence", () => {
|
|
7
|
-
const result = parseDCS(0, "\\eP0;1|name\\e\\\\", "0;1|name");
|
|
8
|
-
assert.deepEqual(result, {
|
|
9
|
-
type: CODE_TYPES.DCS,
|
|
10
|
-
pos: 0,
|
|
11
|
-
raw: "\\eP0;1|name\\e\\\\",
|
|
12
|
-
params: ["0;1|name"],
|
|
13
|
-
command: "",
|
|
14
|
-
});
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
test("parseDCS with missing parameters", () => {
|
|
18
|
-
const result = parseDCS(0, "\\eP$q;\\e\\\\", "$q;");
|
|
19
|
-
assert.deepEqual(result, {
|
|
20
|
-
type: CODE_TYPES.DCS,
|
|
21
|
-
pos: 0,
|
|
22
|
-
raw: "\\eP$q;\\e\\\\",
|
|
23
|
-
params: ["-1", "-1"],
|
|
24
|
-
command: "$q",
|
|
25
|
-
});
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
test("parseDCS with known pattern", () => {
|
|
29
|
-
const result = parseDCS(0, "\\eP$qm\\e\\\\", "$qm");
|
|
30
|
-
assert.deepEqual(result, { type: CODE_TYPES.DCS, pos: 0, raw: "\\eP$qm\\e\\\\", params: ["m"], command: "$q" });
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
test("parseDCS empty data", () => {
|
|
34
|
-
const result = parseDCS(0, "\\eP\\e\\\\", "");
|
|
35
|
-
assert.deepEqual(result, { type: CODE_TYPES.DCS, pos: 0, raw: "\\eP\\e\\\\", command: "", params: [] });
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
test("parseDCS unknown pattern", () => {
|
|
39
|
-
const result = parseDCS(0, "\\ePunknown\\e\\\\", "unknown");
|
|
40
|
-
assert.deepEqual(result, {
|
|
41
|
-
type: CODE_TYPES.DCS,
|
|
42
|
-
pos: 0,
|
|
43
|
-
raw: "\\ePunknown\\e\\\\",
|
|
44
|
-
command: "",
|
|
45
|
-
params: ["unknown"],
|
|
46
|
-
});
|
|
47
|
-
});
|
package/src/parsers/dcs.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { CODE_TYPES } from "../constants.ts";
|
|
2
|
-
import type { CONTROL_CODE } from "../types.ts";
|
|
3
|
-
|
|
4
|
-
const DCS_PATTERNS = new Map([
|
|
5
|
-
["$q", 2],
|
|
6
|
-
["+q", 2],
|
|
7
|
-
["+p", 2],
|
|
8
|
-
["|", 1],
|
|
9
|
-
["{", 1],
|
|
10
|
-
]);
|
|
11
|
-
|
|
12
|
-
export function parseDCS(pos: number, raw: string, data: string): CONTROL_CODE {
|
|
13
|
-
if (!data) return { type: CODE_TYPES.DCS, pos, raw, command: "", params: [] };
|
|
14
|
-
|
|
15
|
-
for (const [pattern, length] of DCS_PATTERNS) {
|
|
16
|
-
if (data.startsWith(pattern)) {
|
|
17
|
-
const remainder = data.slice(length);
|
|
18
|
-
const params = [];
|
|
19
|
-
if (remainder) {
|
|
20
|
-
let current = "";
|
|
21
|
-
for (let i = 0; i < remainder.length; i++) {
|
|
22
|
-
if (remainder[i] === ";") {
|
|
23
|
-
params.push(current || "-1");
|
|
24
|
-
current = "";
|
|
25
|
-
} else {
|
|
26
|
-
current += remainder[i];
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
params.push(current || "-1");
|
|
30
|
-
}
|
|
31
|
-
return { type: CODE_TYPES.DCS, pos, raw, command: pattern, params };
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return { type: CODE_TYPES.DCS, pos, raw, command: "", params: [data] };
|
|
36
|
-
}
|
package/src/parsers/dec.test.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { parseDEC } from "./dec.ts";
|
|
4
|
-
import { CODE_TYPES } from "../constants.ts";
|
|
5
|
-
|
|
6
|
-
test("parseDEC basic sequence", () => {
|
|
7
|
-
const result = parseDEC(0, "\\e[?25h", "?25", "h");
|
|
8
|
-
assert.deepEqual(result, { type: CODE_TYPES.DEC, pos: 0, raw: "\\e[?25h", params: ["25"], command: "h" });
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
test("parseDEC with missing parameters", () => {
|
|
12
|
-
const result = parseDEC(0, "\\e[?;h", "?;", "h");
|
|
13
|
-
assert.deepEqual(result, { type: CODE_TYPES.DEC, pos: 0, raw: "\\e[?;h", params: ["-1", "-1"], command: "h" });
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
test("parseDEC with intermediate bytes", () => {
|
|
17
|
-
const result = parseDEC(0, "\\e[?1$p", "?1$", "p");
|
|
18
|
-
assert.deepEqual(result, { type: CODE_TYPES.DEC, pos: 0, raw: "\\e[?1$p", params: ["1"], command: "$p" });
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
test("parseDEC multiple parameters with intermediates", () => {
|
|
22
|
-
const result = parseDEC(0, "\\e[?1;2$p", "?1;2$", "p");
|
|
23
|
-
assert.deepEqual(result, { type: CODE_TYPES.DEC, pos: 0, raw: "\\e[?1;2$p", params: ["1", "2"], command: "$p" });
|
|
24
|
-
});
|
package/src/parsers/dec.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { CODE_TYPES } from "../constants.ts";
|
|
2
|
-
import type { CONTROL_CODE } from "../types.ts";
|
|
3
|
-
|
|
4
|
-
export function parseDEC(pos: number, raw: string, data: string, final: string): CONTROL_CODE {
|
|
5
|
-
const withoutPrefix = data.slice(1);
|
|
6
|
-
let i = 0;
|
|
7
|
-
let paramsRaw = "";
|
|
8
|
-
while (
|
|
9
|
-
i < withoutPrefix.length &&
|
|
10
|
-
((withoutPrefix.charCodeAt(i) >= 48 && withoutPrefix.charCodeAt(i) <= 57) || withoutPrefix[i] === ";")
|
|
11
|
-
) {
|
|
12
|
-
paramsRaw += withoutPrefix[i];
|
|
13
|
-
i++;
|
|
14
|
-
}
|
|
15
|
-
const command = withoutPrefix.slice(i) + final;
|
|
16
|
-
const params = [];
|
|
17
|
-
if (paramsRaw) {
|
|
18
|
-
let current = "";
|
|
19
|
-
for (let j = 0; j < paramsRaw.length; j++) {
|
|
20
|
-
if (paramsRaw[j] === ";") {
|
|
21
|
-
params.push(current || "-1");
|
|
22
|
-
current = "";
|
|
23
|
-
} else {
|
|
24
|
-
current += paramsRaw[j];
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
params.push(current || "-1");
|
|
28
|
-
}
|
|
29
|
-
return { type: CODE_TYPES.DEC, pos, raw, command, params };
|
|
30
|
-
}
|
package/src/parsers/esc.test.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { parseESC } from "./esc.ts";
|
|
4
|
-
import { CODE_TYPES } from "../constants.ts";
|
|
5
|
-
|
|
6
|
-
test("parseESC simple command", () => {
|
|
7
|
-
const result = parseESC(0, "\\e=", "=");
|
|
8
|
-
assert.deepEqual(result, { type: CODE_TYPES.ESC, pos: 0, raw: "\\e=", command: "=", params: [] });
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
test("parseESC with data", () => {
|
|
12
|
-
const result = parseESC(0, "\\e(A", "(", "A");
|
|
13
|
-
assert.deepEqual(result, { type: CODE_TYPES.ESC, pos: 0, raw: "\\e(A", command: "(", params: ["A"] });
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
test("parseESC charset sequence", () => {
|
|
17
|
-
const result = parseESC(0, "\\e)A", ")", "A");
|
|
18
|
-
assert.deepEqual(result, { type: CODE_TYPES.ESC, pos: 0, raw: "\\e)A", command: ")", params: ["A"] });
|
|
19
|
-
});
|
package/src/parsers/esc.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { CODE_TYPES } from "../constants.ts";
|
|
2
|
-
import type { CONTROL_CODE } from "../types.ts";
|
|
3
|
-
|
|
4
|
-
export function parseESC(pos: number, raw: string, command: string, data?: string): CONTROL_CODE {
|
|
5
|
-
return { type: CODE_TYPES.ESC, pos, raw, command, params: data ? [data] : [] };
|
|
6
|
-
}
|
package/src/parsers/osc.test.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { parseOSC } from "./osc.ts";
|
|
4
|
-
import { CODE_TYPES } from "../constants.ts";
|
|
5
|
-
|
|
6
|
-
test("parseOSC simple command", () => {
|
|
7
|
-
const result = parseOSC(0, "\\e]8;;http://example.com\\a", "8;;http://example.com");
|
|
8
|
-
assert.deepEqual(result, {
|
|
9
|
-
type: CODE_TYPES.OSC,
|
|
10
|
-
pos: 0,
|
|
11
|
-
raw: "\\e]8;;http://example.com\\a",
|
|
12
|
-
params: ["", "http://example.com"],
|
|
13
|
-
command: "8",
|
|
14
|
-
});
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
test("parseOSC no parameters", () => {
|
|
18
|
-
const result = parseOSC(0, "\\e]0\\a", "0");
|
|
19
|
-
assert.deepEqual(result, { type: CODE_TYPES.OSC, pos: 0, raw: "\\e]0\\a", command: "0", params: [] });
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test("parseOSC with trailing semicolon (empty remainder)", () => {
|
|
23
|
-
const result = parseOSC(0, "\\e]8;\\a", "8;");
|
|
24
|
-
assert.deepEqual(result, { type: CODE_TYPES.OSC, pos: 0, raw: "\\e]8;\\a", command: "8", params: [] });
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
test("parseOSC real-world example", () => {
|
|
28
|
-
const result = parseOSC(0, "\\e]2;Window Title\\a", "2;Window Title");
|
|
29
|
-
assert.deepEqual(result, {
|
|
30
|
-
type: CODE_TYPES.OSC,
|
|
31
|
-
pos: 0,
|
|
32
|
-
raw: "\\e]2;Window Title\\a",
|
|
33
|
-
command: "2",
|
|
34
|
-
params: ["Window Title"],
|
|
35
|
-
});
|
|
36
|
-
});
|
package/src/parsers/osc.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { CODE_TYPES } from "../constants.ts";
|
|
2
|
-
import type { CONTROL_CODE } from "../types.ts";
|
|
3
|
-
|
|
4
|
-
export function parseOSC(pos: number, raw: string, data: string): CONTROL_CODE {
|
|
5
|
-
const semicolonIndex = data.indexOf(";");
|
|
6
|
-
if (semicolonIndex === -1) {
|
|
7
|
-
return { type: CODE_TYPES.OSC, pos, raw, command: data, params: [] };
|
|
8
|
-
}
|
|
9
|
-
const command = data.slice(0, semicolonIndex);
|
|
10
|
-
const remainder = data.slice(semicolonIndex + 1);
|
|
11
|
-
|
|
12
|
-
if (command === "1337") return { type: CODE_TYPES.OSC, pos, raw, command, params: [remainder] };
|
|
13
|
-
|
|
14
|
-
const params = [];
|
|
15
|
-
if (remainder) {
|
|
16
|
-
let current = "";
|
|
17
|
-
for (let i = 0; i < remainder.length; i++) {
|
|
18
|
-
if (remainder[i] === ";") {
|
|
19
|
-
params.push(current);
|
|
20
|
-
current = "";
|
|
21
|
-
} else {
|
|
22
|
-
current += remainder[i];
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
params.push(current);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return { type: CODE_TYPES.OSC, pos, raw, command, params };
|
|
29
|
-
}
|