@enmaq/mdgl 0.0.0-stage → 0.3.0
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-APACHE +202 -0
- package/LICENSE-MIT +21 -0
- package/README.md +86 -2
- package/UNICODE-LICENSE.txt +39 -0
- package/bin/mdgl-adapter.js +31 -0
- package/bin/mdgl.js +105 -0
- package/dist/adapter.d.ts +7 -0
- package/dist/adapter.js +231 -0
- package/dist/check.d.ts +48 -0
- package/dist/check.js +374 -0
- package/dist/eval.d.ts +53 -0
- package/dist/eval.js +496 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +38 -0
- package/dist/parse.d.ts +117 -0
- package/dist/parse.js +424 -0
- package/dist/play.d.ts +44 -0
- package/dist/play.js +153 -0
- package/dist/unicode-tables.d.ts +7 -0
- package/dist/unicode-tables.js +15 -0
- package/dist/unicode.d.ts +12 -0
- package/dist/unicode.js +109 -0
- package/package.json +55 -4
package/LICENSE-APACHE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/LICENSE-MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Enmaq and MDGL contributors
|
|
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.
|
package/README.md
CHANGED
|
@@ -1,3 +1,87 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @enmaq/mdgl (npm)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
One npm package for JavaScript and TypeScript. TypeScript source is `src/`
|
|
4
|
+
(`parse.ts` syntax, `check.ts` semantics); `npm run build` (`tsc`) compiles it to
|
|
5
|
+
`dist/*.js` (CommonJS) plus `.d.ts`. `dist/` is a build artifact and is not committed.
|
|
6
|
+
|
|
7
|
+
## Status
|
|
8
|
+
|
|
9
|
+
Implemented: full MDGL 0.1 lexical parsing and semantic checking per `SPEC.md`:
|
|
10
|
+
Go-style quoted escapes, UTF-8 byte columns, finite non-exponent decimals,
|
|
11
|
+
scene/actor/action ordering, actor references, forward parents and cycles, all
|
|
12
|
+
actor and action argument types, duplicates and unknowns with did-you-mean,
|
|
13
|
+
overlapping properties, timing/period/repeat/fallback, and the stage, count and
|
|
14
|
+
text limits. Also implemented: evaluator and terminal-cell renderer (`compile`, `frameNs`,
|
|
15
|
+
`fallbackFrame`; all nine actions, easing, repeat/Done/fallback, parent transforms, text, braille rect/circle,
|
|
16
|
+
wire cube) and the NDJSON conformance adapter (`bin/mdgl-adapter.js`, `handleLine`). Rect/circle follow a coordinator reference profile, not SPEC text.
|
|
17
|
+
The current shared corpus has 86 fixtures (64 normative + 22 non-normative or profile-assumption); all five adapters (Go, Rust, C, TypeScript/JavaScript, Python) passed it in an independent verification on 2026-10-10 UTC, accepted by the coordinator. That is bounded evidence under conformance protocol v1, not a universal SPEC certification.
|
|
18
|
+
Braille lines are quantized to 1e-9, clipped to the stage dot box, then stepped (ceil of the longer span, 1..800), as the SPEC
|
|
19
|
+
rule and the coordinator reference profile state; this reproduces all 24 cube fixtures. No full-conformance claim: rect/circle and cube colour have only
|
|
20
|
+
non-normative reference-profile observable fixtures (`obs_ref_*`), and normative rect/circle coverage is still an open gap.
|
|
21
|
+
The 0.2.0 package was an earlier limited header/resource check.
|
|
22
|
+
|
|
23
|
+
## API
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { check, parse, parseChecked } from '@enmaq/mdgl';
|
|
27
|
+
check(source); // Diag[] ({line, col, message}); [] means valid; the first entry is the compat diagnostic
|
|
28
|
+
parse(source); // source-located statements, no semantic checks
|
|
29
|
+
parseChecked(source); // { ok: true, doc: {scene, actors, actions} } or { ok: false, diagnostics }
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Non-strings throw `TypeError`. Times in the checked model are integer nanoseconds.
|
|
33
|
+
|
|
34
|
+
## Unicode compatibility profile
|
|
35
|
+
|
|
36
|
+
Grapheme counting and cell width use pinned Unicode 15.0.0 tables (`src/unicode-tables.ts`, generated by
|
|
37
|
+
`scripts/gen-unicode.js` from the official unicode.org 15.0.0 data; input hashes are in the table header;
|
|
38
|
+
licence in `UNICODE-LICENSE.txt`). This is a compatibility profile, not a new normative MDGL rule. One extended
|
|
39
|
+
grapheme cluster (UAX #29, no GB9c) occupies one cell. `cellRune` emits the first non-mark rune; U+FFFD when the
|
|
40
|
+
cluster is width 2 (East Asian W/F first rune, regional indicators including flag pairs, emoji-style VS16 and
|
|
41
|
+
keycap sequences); a blank only for a cluster made entirely of marks (zero-width format or default-ignorable runes are non-mark and are emitted, still one cell).
|
|
42
|
+
Not modelled: width of ZWJ sequences beyond their first rune, and terminal-specific width tables.
|
|
43
|
+
|
|
44
|
+
## Examples
|
|
45
|
+
|
|
46
|
+
Two runnable examples render one frame of the shared `../examples/text.mdgl` using the public API
|
|
47
|
+
(`check`, `compile`, `frameNs`, `fallbackFrame`):
|
|
48
|
+
|
|
49
|
+
```
|
|
50
|
+
npm install
|
|
51
|
+
npm run example:js # builds, then node examples/render.mjs
|
|
52
|
+
npm run example:ts # builds, compiles examples/render.ts, runs it
|
|
53
|
+
node examples/render.mjs ../examples/ui-motion.mdgl --time 2.5 # another scene, absolute seconds
|
|
54
|
+
node examples/render.mjs --fallback # fallback frame (no done flag)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Invalid MDGL prints `file:line:col: message` and exits 1; unreadable files or a bad `--time` exit 2.
|
|
58
|
+
Circle radius is limited to 100 (diameter 200); rect and cube sizes to 200.
|
|
59
|
+
|
|
60
|
+
## Terminal player: `mdgl play FILE`
|
|
61
|
+
|
|
62
|
+
The package installs an `mdgl` command (and the `mdgl-adapter` conformance adapter):
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
npm install @enmaq/mdgl # or: npx -p @enmaq/mdgl mdgl play scene.mdgl
|
|
66
|
+
mdgl play scene.mdgl # animate in the terminal; Ctrl+C stops
|
|
67
|
+
mdgl check scene.mdgl # validate; file:line:col: message on stderr
|
|
68
|
+
mdgl frames scene.mdgl --at 0,1.5 # print frames at absolute seconds
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Playback follows the SPEC: frame time is the monotonic elapsed time (late timers drop frames, never
|
|
72
|
+
change state), the scene `fps` sets the rate, only changed cells are rewritten (fg and bg), each
|
|
73
|
+
update is one DEC synchronized-output (mode 2026) chunk, and the cursor is hidden and restored on every exit path. Finite scenes stop
|
|
74
|
+
when the evaluator reports done; infinite repeat runs until interrupted. Cell glyphs are sanitised, so
|
|
75
|
+
a scene can never inject terminal escapes. When stdout is not a terminal, or `NO_COLOR` or
|
|
76
|
+
`MDGL_REDUCED_MOTION` is set to a non-empty value, `play` prints the plain fallback frame once, with no escapes or timers.
|
|
77
|
+
|
|
78
|
+
Exit codes: 0 normal, 1 invalid scene or output error, 2 usage error or unreadable file, 130 Ctrl+C, 143 SIGTERM.
|
|
79
|
+
Limits: the terminal must be at least as wide and tall as the scene (no resize handling); `play` needs an ANSI terminal.
|
|
80
|
+
|
|
81
|
+
Library: `play(scene, {write, clock, onWriteError?, onInterrupt?})` returns `{done, stop}` and does no I/O itself;
|
|
82
|
+
`examples/play.mjs` shows the wiring, and `plainFallback(scene)` gives the redirected-output text.
|
|
83
|
+
|
|
84
|
+
## Development
|
|
85
|
+
|
|
86
|
+
`npm test` builds, type-checks `test/consumer/consumer.ts` and runs the tests,
|
|
87
|
+
including every `.mdgl` file in `../conformance`.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
UNICODE LICENSE V3
|
|
2
|
+
|
|
3
|
+
COPYRIGHT AND PERMISSION NOTICE
|
|
4
|
+
|
|
5
|
+
Copyright © 1991-2026 Unicode, Inc.
|
|
6
|
+
|
|
7
|
+
NOTICE TO USER: Carefully read the following legal agreement. BY
|
|
8
|
+
DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING DATA FILES, AND/OR
|
|
9
|
+
SOFTWARE, YOU UNEQUIVOCALLY ACCEPT, AND AGREE TO BE BOUND BY, ALL OF THE
|
|
10
|
+
TERMS AND CONDITIONS OF THIS AGREEMENT. IF YOU DO NOT AGREE, DO NOT
|
|
11
|
+
DOWNLOAD, INSTALL, COPY, DISTRIBUTE OR USE THE DATA FILES OR SOFTWARE.
|
|
12
|
+
|
|
13
|
+
Permission is hereby granted, free of charge, to any person obtaining a
|
|
14
|
+
copy of data files and any associated documentation (the "Data Files") or
|
|
15
|
+
software and any associated documentation (the "Software") to deal in the
|
|
16
|
+
Data Files or Software without restriction, including without limitation
|
|
17
|
+
the rights to use, copy, modify, merge, publish, distribute, and/or sell
|
|
18
|
+
copies of the Data Files or Software, and to permit persons to whom the
|
|
19
|
+
Data Files or Software are furnished to do so, provided that either (a)
|
|
20
|
+
this copyright and permission notice appear with all copies of the Data
|
|
21
|
+
Files or Software, or (b) this copyright and permission notice appear in
|
|
22
|
+
associated Documentation.
|
|
23
|
+
|
|
24
|
+
THE DATA FILES AND SOFTWARE ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
|
|
25
|
+
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
26
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
|
|
27
|
+
THIRD PARTY RIGHTS.
|
|
28
|
+
|
|
29
|
+
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE
|
|
30
|
+
BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES,
|
|
31
|
+
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
|
32
|
+
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
|
33
|
+
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THE DATA
|
|
34
|
+
FILES OR SOFTWARE.
|
|
35
|
+
|
|
36
|
+
Except as contained in this notice, the name of a copyright holder shall
|
|
37
|
+
not be used in advertising or otherwise to promote the sale, use or other
|
|
38
|
+
dealings in these Data Files or Software without prior written
|
|
39
|
+
authorization of the copyright holder.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
// Newline-delimited JSON conformance adapter (PROTOCOL v1). One response line per request line,
|
|
4
|
+
// processed in order; the final non-empty line without a newline is answered once.
|
|
5
|
+
const { handleLine, invalidUtf8, MAX_REQUEST_LINE } = require('../dist/adapter.js');
|
|
6
|
+
// Fatal decode of the complete line bytes: malformed UTF-8 is rejected, never replaced; a BOM is kept (ignoreBOM) so JSON parsing rejects it.
|
|
7
|
+
const dec = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });
|
|
8
|
+
let buf = Buffer.alloc(0), skipping = false;
|
|
9
|
+
const emit = (s) => process.stdout.write(s + '\n');
|
|
10
|
+
function line(b) {
|
|
11
|
+
if (b.length > MAX_REQUEST_LINE) return emit(handleLine('', b.length));
|
|
12
|
+
let text;
|
|
13
|
+
try { text = dec.decode(b); } catch (e) { return emit(invalidUtf8()); }
|
|
14
|
+
emit(handleLine(text, b.length));
|
|
15
|
+
}
|
|
16
|
+
process.stdin.on('data', (chunk) => {
|
|
17
|
+
buf = Buffer.concat([buf, chunk]);
|
|
18
|
+
for (;;) {
|
|
19
|
+
const nl = buf.indexOf(10);
|
|
20
|
+
if (nl < 0) {
|
|
21
|
+
if (buf.length > MAX_REQUEST_LINE && !skipping) { emit(handleLine('', MAX_REQUEST_LINE + 1)); skipping = true; }
|
|
22
|
+
if (skipping) buf = Buffer.alloc(0);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const l = buf.subarray(0, nl);
|
|
26
|
+
buf = buf.subarray(nl + 1);
|
|
27
|
+
if (skipping) { skipping = false; continue; }
|
|
28
|
+
line(l);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
process.stdin.on('end', () => { if (!skipping && buf.length > 0) line(buf); });
|
package/bin/mdgl.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
// mdgl command line: check FILE | frames FILE --at t1,t2 | play FILE.
|
|
4
|
+
// Exit codes: 0 ok, 1 invalid scene or output error, 2 usage or unreadable file,
|
|
5
|
+
// 130 interrupted by Ctrl+C (SIGINT), 143 terminated (SIGTERM).
|
|
6
|
+
const fs = require('node:fs');
|
|
7
|
+
const { performance } = require('node:perf_hooks');
|
|
8
|
+
const mdgl = require('../dist/index.js');
|
|
9
|
+
const pkg = require('../package.json');
|
|
10
|
+
|
|
11
|
+
const USAGE = `usage: mdgl <command> [options]
|
|
12
|
+
|
|
13
|
+
commands:
|
|
14
|
+
play FILE play FILE in the terminal (Ctrl+C stops it)
|
|
15
|
+
check FILE validate FILE; prints file:line:col: message and exits 1 if invalid
|
|
16
|
+
frames FILE --at T,T print the frame at each absolute time T (seconds), conformance format
|
|
17
|
+
|
|
18
|
+
play shows the fallback frame once, without escapes or timers, when stdout is not a terminal
|
|
19
|
+
or NO_COLOR or MDGL_REDUCED_MOTION is set to a non-empty value.
|
|
20
|
+
|
|
21
|
+
options:
|
|
22
|
+
-h, --help show this help
|
|
23
|
+
-V, --version show the version
|
|
24
|
+
|
|
25
|
+
exit codes: 0 ok, 1 invalid scene or output error, 2 usage or unreadable file, 130 Ctrl+C
|
|
26
|
+
`;
|
|
27
|
+
const usage = (msg) => { if (msg) process.stderr.write(`mdgl: ${msg}\n`); process.stderr.write(USAGE); process.exitCode = 2; };
|
|
28
|
+
|
|
29
|
+
function load(file) {
|
|
30
|
+
let src;
|
|
31
|
+
try { src = fs.readFileSync(file, 'utf8'); } catch (e) { process.stderr.write(`mdgl: cannot read ${file}\n`); process.exitCode = 2; return null; }
|
|
32
|
+
const r = mdgl.compile(src);
|
|
33
|
+
if (!r.ok) {
|
|
34
|
+
for (const d of r.diagnostics) process.stderr.write(`${file}:${d.line}:${d.col}: ${d.message}\n`);
|
|
35
|
+
process.exitCode = 1;
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
return r.scene;
|
|
39
|
+
}
|
|
40
|
+
/** decimal seconds -> integer nanoseconds, or null */
|
|
41
|
+
function seconds(s) {
|
|
42
|
+
const m = /^([0-9]+)(?:\.([0-9]+))?$/.exec(s);
|
|
43
|
+
if (!m) return null;
|
|
44
|
+
const ns = Number(m[1]) * 1e9 + Number(((m[2] || '') + '000000000').slice(0, 9));
|
|
45
|
+
return Number.isSafeInteger(ns) ? ns : null;
|
|
46
|
+
}
|
|
47
|
+
function writeOut(text) {
|
|
48
|
+
return new Promise((resolve) => {
|
|
49
|
+
const onErr = (e) => { process.stderr.write(`mdgl: write error: ${e && e.code ? e.code : 'failed'}\n`); process.exitCode = 1; resolve(); };
|
|
50
|
+
process.stdout.once('error', onErr);
|
|
51
|
+
process.stdout.write(text, () => { process.stdout.off('error', onErr); resolve(); });
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function play(file) {
|
|
56
|
+
const scene = load(file);
|
|
57
|
+
if (!scene) return;
|
|
58
|
+
const reduced = (process.env.NO_COLOR || '') !== '' || (process.env.MDGL_REDUCED_MOTION || '') !== '';
|
|
59
|
+
if (!process.stdout.isTTY || reduced) { await writeOut(mdgl.plainFallback(scene)); return; }
|
|
60
|
+
const out = process.stdout;
|
|
61
|
+
const handle = mdgl.play(scene, {
|
|
62
|
+
write: (s) => { out.write(s); },
|
|
63
|
+
clock: { now: () => performance.now(), setTimeout: (f, ms) => setTimeout(f, ms), clearTimeout: (h) => clearTimeout(h) },
|
|
64
|
+
onWriteError: (cb) => { out.on('error', cb); return () => out.off('error', cb); },
|
|
65
|
+
onInterrupt: (cb) => {
|
|
66
|
+
const a = () => cb(130), b = () => cb(143);
|
|
67
|
+
process.on('SIGINT', a);
|
|
68
|
+
process.on('SIGTERM', b);
|
|
69
|
+
return () => { process.off('SIGINT', a); process.off('SIGTERM', b); };
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
const res = await handle.done;
|
|
73
|
+
// keep a late stream error from crashing during teardown
|
|
74
|
+
out.on('error', () => {});
|
|
75
|
+
if (res.reason === 'write-error') process.stderr.write(`mdgl: write error: ${res.error && res.error.code ? res.error.code : 'failed'}\n`);
|
|
76
|
+
process.exitCode = res.code;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async function main(argv) {
|
|
80
|
+
const [cmd, ...rest] = argv;
|
|
81
|
+
if (cmd === undefined) return usage();
|
|
82
|
+
if (cmd === '-h' || cmd === '--help' || cmd === 'help') { process.stdout.write(USAGE); return; }
|
|
83
|
+
if (cmd === '-V' || cmd === '--version') { process.stdout.write(`${pkg.version}\n`); return; }
|
|
84
|
+
if (!['play', 'check', 'frames'].includes(cmd)) return usage(`unknown command ${JSON.stringify(cmd)}`);
|
|
85
|
+
if (rest.includes('-h') || rest.includes('--help')) { process.stdout.write(USAGE); return; }
|
|
86
|
+
const at = rest.indexOf('--at');
|
|
87
|
+
const times = at >= 0 ? rest.splice(at, 2)[1] : undefined;
|
|
88
|
+
const flags = rest.filter((a) => a.startsWith('-'));
|
|
89
|
+
if (flags.length) return usage(`unknown option ${flags[0]}`);
|
|
90
|
+
if (rest.length !== 1) return usage('expected exactly one FILE');
|
|
91
|
+
const file = rest[0];
|
|
92
|
+
if (cmd === 'check') { if (load(file)) process.stdout.write('ok\n'); return; }
|
|
93
|
+
if (cmd === 'frames') {
|
|
94
|
+
if (times === undefined) return usage('frames needs --at T,T');
|
|
95
|
+
const list = times.split(',');
|
|
96
|
+
const ns = list.map(seconds);
|
|
97
|
+
if (ns.includes(null)) return usage('--at takes non-negative decimal seconds');
|
|
98
|
+
const scene = load(file);
|
|
99
|
+
if (!scene) return;
|
|
100
|
+
await writeOut(list.map((t, i) => `@${t}\n${mdgl.frameNs(scene, ns[i]).cells.map((r) => r.map((c) => mdgl.sanitizeRune(c.r)).join('').replace(/ +$/, '')).join('\n')}\n`).join(''));
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
await play(file);
|
|
104
|
+
}
|
|
105
|
+
main(process.argv.slice(2)).catch((e) => { process.stderr.write(`mdgl: ${e && e.message ? e.message : e}\n`); process.exitCode = 1; });
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const MAX_REQUEST_LINE = 2097152;
|
|
2
|
+
/** Exact mathematical integer value of a JSON number lexeme, or null if it is not an integer. */
|
|
3
|
+
export declare function mathInt(lex: string): bigint | null;
|
|
4
|
+
/** bad_request response for a line whose bytes are not valid UTF-8 (the id is unrecoverable). */
|
|
5
|
+
export declare const invalidUtf8: () => string;
|
|
6
|
+
/** Handle one request line (without its newline). Always returns exactly one response line. */
|
|
7
|
+
export declare function handleLine(line: string, lineBytes?: number): string;
|