@x-cmd/draw 0.4.6 → 0.5.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/README.md +3 -3
- package/dist/client.d.ts +42 -0
- package/dist/client.js +267 -0
- package/dist/core.d.ts +86 -0
- package/dist/core.js +132 -0
- package/dist/globe.d.ts +4 -0
- package/dist/globe.js +6 -0
- package/dist/layout/cluster.d.ts +2 -0
- package/dist/layout/cluster.js +228 -0
- package/dist/layout/index.d.ts +2 -0
- package/dist/layout/index.js +6 -0
- package/dist/layout/layered.d.ts +51 -0
- package/dist/layout/layered.js +433 -0
- package/dist/layout/portsize.d.ts +18 -0
- package/dist/layout/portsize.js +34 -0
- package/dist/lib.d.ts +17 -4
- package/dist/lib.js +20 -4
- package/dist/node/cmd.d.ts +1 -0
- package/dist/node/cmd.js +3 -0
- package/dist/render/abc-parse.d.ts +75 -0
- package/dist/render/abc-parse.js +345 -0
- package/dist/render/abcjs.d.ts +10 -0
- package/dist/render/abcjs.js +90 -0
- package/dist/render/anno.d.ts +48 -2
- package/dist/render/anno.js +171 -17
- package/dist/render/ansi.d.ts +15 -0
- package/dist/render/ansi.js +142 -0
- package/dist/render/arch.js +8 -3
- package/dist/render/arch3d.d.ts +31 -0
- package/dist/render/arch3d.js +294 -0
- package/dist/render/archmap.js +2 -2
- package/dist/render/area.js +42 -11
- package/dist/render/bar-grid.js +1 -1
- package/dist/render/bar-grouped.js +25 -7
- package/dist/render/bar-stacked.js +22 -6
- package/dist/render/bar.js +27 -6
- package/dist/render/block.d.ts +56 -0
- package/dist/render/block.js +361 -0
- package/dist/render/butterfly.js +4 -1
- package/dist/render/calendar.js +49 -14
- package/dist/render/center.d.ts +26 -0
- package/dist/render/center.js +108 -0
- package/dist/render/chem.d.ts +22 -0
- package/dist/render/chem.js +205 -0
- package/dist/render/chord.d.ts +17 -0
- package/dist/render/chord.js +141 -0
- package/dist/render/combo.js +7 -7
- package/dist/render/compass.js +59 -13
- package/dist/render/echarts-ssr.d.ts +36 -2
- package/dist/render/echarts-ssr.js +77 -18
- package/dist/render/gauge.js +17 -9
- package/dist/render/gitgraph.d.ts +53 -0
- package/dist/render/gitgraph.js +324 -0
- package/dist/render/globe-areas.d.ts +21 -0
- package/dist/render/globe-areas.js +40 -0
- package/dist/render/globe-earth.d.ts +78 -0
- package/dist/render/globe-earth.js +464 -0
- package/dist/render/globe-keys.d.ts +34 -0
- package/dist/render/globe-keys.js +71 -0
- package/dist/render/globe-proj.d.ts +102 -0
- package/dist/render/globe-proj.js +332 -0
- package/dist/render/globe-regions.d.ts +23 -0
- package/dist/render/globe-regions.js +60 -0
- package/dist/render/globe.d.ts +15 -0
- package/dist/render/globe.js +306 -0
- package/dist/render/globe3d.d.ts +113 -0
- package/dist/render/globe3d.js +285 -0
- package/dist/render/hbar-spec.js +23 -10
- package/dist/render/hbar.js +30 -11
- package/dist/render/jianpu.d.ts +10 -0
- package/dist/render/jianpu.js +190 -0
- package/dist/render/journey.d.ts +54 -0
- package/dist/render/journey.js +277 -0
- package/dist/render/kanban.d.ts +50 -0
- package/dist/render/kanban.js +240 -0
- package/dist/render/kinds.js +19 -0
- package/dist/render/line-multi.js +27 -8
- package/dist/render/line.js +32 -14
- package/dist/render/maps.js +4 -2
- package/dist/render/mermaid/block.d.ts +2 -0
- package/dist/render/mermaid/block.js +256 -0
- package/dist/render/mermaid/c4.d.ts +3 -0
- package/dist/render/mermaid/c4.js +165 -0
- package/dist/render/mermaid/class.d.ts +2 -0
- package/dist/render/mermaid/class.js +185 -0
- package/dist/render/mermaid/common.d.ts +34 -0
- package/dist/render/mermaid/common.js +81 -0
- package/dist/render/mermaid/erd.d.ts +2 -0
- package/dist/render/mermaid/erd.js +103 -0
- package/dist/render/mermaid/gitgraph.d.ts +2 -0
- package/dist/render/mermaid/gitgraph.js +139 -0
- package/dist/render/mermaid/journey.d.ts +7 -0
- package/dist/render/mermaid/journey.js +68 -0
- package/dist/render/mermaid/kanban.d.ts +2 -0
- package/dist/render/mermaid/kanban.js +86 -0
- package/dist/render/mermaid/mindmap.d.ts +7 -0
- package/dist/render/mermaid/mindmap.js +74 -0
- package/dist/render/mermaid/packet.d.ts +2 -0
- package/dist/render/mermaid/packet.js +35 -0
- package/dist/render/mermaid/pie.d.ts +7 -0
- package/dist/render/mermaid/pie.js +58 -0
- package/dist/render/mermaid/quadrant.d.ts +8 -0
- package/dist/render/mermaid/quadrant.js +137 -0
- package/dist/render/mermaid/radar.d.ts +8 -0
- package/dist/render/mermaid/radar.js +69 -0
- package/dist/render/mermaid/requirement.d.ts +2 -0
- package/dist/render/mermaid/requirement.js +100 -0
- package/dist/render/mermaid/sankey.d.ts +7 -0
- package/dist/render/mermaid/sankey.js +54 -0
- package/dist/render/mermaid/seq.d.ts +2 -0
- package/dist/render/mermaid/seq.js +147 -0
- package/dist/render/mermaid/state.d.ts +2 -0
- package/dist/render/mermaid/state.js +168 -0
- package/dist/render/mermaid/timeline.d.ts +7 -0
- package/dist/render/mermaid/timeline.js +63 -0
- package/dist/render/mermaid/xychart.d.ts +7 -0
- package/dist/render/mermaid/xychart.js +94 -0
- package/dist/render/mermaid.d.ts +22 -4
- package/dist/render/mermaid.js +173 -7
- package/dist/render/mindmap.js +12 -2
- package/dist/render/nomnoml.d.ts +8 -0
- package/dist/render/nomnoml.js +47 -0
- package/dist/render/packet.d.ts +39 -0
- package/dist/render/packet.js +232 -0
- package/dist/render/params-dispatch.d.ts +20 -0
- package/dist/render/params-dispatch.js +192 -0
- package/dist/render/pie.js +29 -22
- package/dist/render/quadrant.js +38 -9
- package/dist/render/query.d.ts +11 -1
- package/dist/render/query.js +92 -16
- package/dist/render/radar.js +7 -6
- package/dist/render/requirement.d.ts +61 -0
- package/dist/render/requirement.js +355 -0
- package/dist/render/sankey.js +4 -4
- package/dist/render/scatter.js +25 -9
- package/dist/render/scene3d.d.ts +188 -0
- package/dist/render/scene3d.js +433 -0
- package/dist/render/seq.js +13 -8
- package/dist/render/simplify.d.ts +12 -0
- package/dist/render/simplify.js +105 -0
- package/dist/render/source-theme.d.ts +104 -0
- package/dist/render/source-theme.js +261 -0
- package/dist/render/svg.d.ts +81 -1
- package/dist/render/svg.js +259 -2
- package/dist/render/svgdom.d.ts +74 -0
- package/dist/render/svgdom.js +198 -0
- package/dist/render/timeline.d.ts +54 -0
- package/dist/render/timeline.js +290 -0
- package/dist/render/tty.d.ts +17 -0
- package/dist/render/tty.js +173 -0
- package/dist/render/ttycmd.d.ts +7 -0
- package/dist/render/ttycmd.js +152 -0
- package/dist/render/vendor/au.js +1 -1
- package/dist/render/vendor/br.js +1 -1
- package/dist/render/vendor/china-counties.d.ts +32 -0
- package/dist/render/vendor/china-counties.js +8 -0
- package/dist/render/vendor/china-provinces.d.ts +16 -0
- package/dist/render/vendor/china-provinces.js +8 -0
- package/dist/render/vendor/china.js +1 -1
- package/dist/render/vendor/country-aliases.d.ts +207 -0
- package/dist/render/vendor/country-aliases.js +7 -0
- package/dist/render/vendor/de.js +1 -1
- package/dist/render/vendor/eu.js +1 -1
- package/dist/render/vendor/japan.js +1 -1
- package/dist/render/vendor/kr.js +1 -1
- package/dist/render/vendor/world-geo.js +1 -1
- package/dist/render/vendor/world-ne.d.ts +18 -0
- package/dist/render/vendor/world-ne.js +8 -0
- package/dist/render/vendor/world.js +1 -1
- package/dist/render/vexflow.d.ts +10 -0
- package/dist/render/vexflow.js +189 -0
- package/dist/render/wavedrom.d.ts +14 -0
- package/dist/render/wavedrom.js +113 -0
- package/dist/source.d.ts +20 -0
- package/dist/source.js +27 -0
- package/dist/specs/base.js +9 -4
- package/dist/specs/combo.d.ts +1 -0
- package/dist/specs/combo.js +8 -1
- package/dist/specs/gauge.d.ts +1 -0
- package/dist/specs/gauge.js +14 -2
- package/dist/specs/multi.d.ts +3 -0
- package/dist/specs/multi.js +29 -3
- package/dist/specs/quadrant.js +5 -4
- package/dist/specs/scatter.d.ts +1 -0
- package/dist/specs/scatter.js +7 -0
- package/dist/styles/bar.js +3 -0
- package/dist/styles/baseline.d.ts +47 -0
- package/dist/styles/baseline.js +48 -0
- package/dist/styles/hbar.js +3 -0
- package/dist/styles/index.d.ts +1 -1
- package/dist/styles/index.js +16 -4
- package/dist/styles/line.js +9 -0
- package/dist/styles/pie.js +4 -0
- package/dist/styles/sankey.js +4 -0
- package/dist/styles/skins.d.ts +23 -0
- package/dist/styles/skins.js +349 -0
- package/dist/styles/types.d.ts +29 -1
- package/dist/styles/types.js +30 -6
- package/dist/world-data.d.ts +12 -0
- package/dist/world-data.js +192 -0
- package/package.json +38 -5
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
// /draw/timeline —— 时间线:一条横轴,若干 section(时期),每个 section 挂着若干事件。
|
|
2
|
+
//
|
|
3
|
+
// 参数语法(扁平,可重复):
|
|
4
|
+
// title=… 图题
|
|
5
|
+
// subtitle=… 副标题
|
|
6
|
+
// section=<时期名> 一个 section(按出现顺序);可重复
|
|
7
|
+
// event=<段>|<事件>|<时期文字> 一个事件;可重复。段名必须是某个 section;
|
|
8
|
+
// 时期文字可省(留空串即可)。竖线是分隔符,字段里不能再写竖线。
|
|
9
|
+
// w= / h= 画布尺寸;t= 皮肤家族;scheme= 明暗;v= 版本
|
|
10
|
+
//
|
|
11
|
+
// 画法:一根横轴贯穿画布,section 依次排开;每个 section 一个淡色带(带自己的名字),
|
|
12
|
+
// 事件是轴上的一个圆点 + 标签(标签在轴上方/下方交替,免得挤在一起)。
|
|
13
|
+
// 事件的时期文字(若有)用小字贴在事件名旁边。
|
|
14
|
+
import { ApiError } from '../specs/index.js';
|
|
15
|
+
import { BASELINE } from '../styles/baseline.js';
|
|
16
|
+
import { resolveVersion } from '../version.js';
|
|
17
|
+
import { resolveSkin } from '../styles/index.js';
|
|
18
|
+
import { card } from '../theme.js';
|
|
19
|
+
import { esc, r2, sVar, svgDoc, textW, titleBlock, subtitleBlock } from './svg.js';
|
|
20
|
+
import { clampInt, schemeParam } from './query.js';
|
|
21
|
+
const PAD = 26;
|
|
22
|
+
const AXIS_FS = BASELINE.eventName; // 事件名字号
|
|
23
|
+
const SUB_FS = 10; // 时期文字字号
|
|
24
|
+
const SEC_FS = 12; // section 名字号
|
|
25
|
+
const DOT_R = 6; // 事件圆点半径
|
|
26
|
+
const MIN_SLOT = 96; // 两个事件之间至少留这么宽(不然标签压在一起)
|
|
27
|
+
const TL_EXAMPLE = 'section=2002&event=2002|LinkedIn|Founded&event=2002|Facebook|Founded§ion=2004&event=2004|Google|Acquired YouTube&event=2004|Facebook|Launched';
|
|
28
|
+
function bad(detail) {
|
|
29
|
+
throw new ApiError(400, `timeline: ${detail}. Example: /draw/timeline?${TL_EXAMPLE}`);
|
|
30
|
+
}
|
|
31
|
+
/** 把 event= 的一段拆成三段(段|事件|时期)。竖线是分隔符,字段里不能再有竖线。 */
|
|
32
|
+
const splitEvent = (raw) => {
|
|
33
|
+
const f = raw.split('|');
|
|
34
|
+
if (f.length < 2 || f.length > 3) {
|
|
35
|
+
bad(`"event" must be "section|event[|period]". Got: ${JSON.stringify(raw)}`);
|
|
36
|
+
}
|
|
37
|
+
return [f[0].trim(), f[1].trim(), (f[2] ?? '').trim()];
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Build a timeline spec from flat URL params.
|
|
41
|
+
*
|
|
42
|
+
* `section=` declares the periods (order matters); `event=` attaches an event
|
|
43
|
+
* to one of them.
|
|
44
|
+
*
|
|
45
|
+
* @throws ApiError(400) with a corrective example on bad input
|
|
46
|
+
*/
|
|
47
|
+
export function timelineSpecFromParams(params) {
|
|
48
|
+
const sectionNames = params.getAll('section').map((s) => s.trim()).filter(Boolean);
|
|
49
|
+
const spec = { sections: [] };
|
|
50
|
+
const title = params.get('title')?.trim();
|
|
51
|
+
const subtitle = params.get('subtitle')?.trim();
|
|
52
|
+
if (title)
|
|
53
|
+
spec.title = title;
|
|
54
|
+
if (subtitle)
|
|
55
|
+
spec.subtitle = subtitle;
|
|
56
|
+
const byName = new Map();
|
|
57
|
+
for (const name of sectionNames) {
|
|
58
|
+
if (byName.has(name))
|
|
59
|
+
bad(`duplicate section ${JSON.stringify(name)}`);
|
|
60
|
+
const sec = { name, events: [] };
|
|
61
|
+
byName.set(name, sec);
|
|
62
|
+
spec.sections.push(sec);
|
|
63
|
+
}
|
|
64
|
+
for (const raw of params.getAll('event')) {
|
|
65
|
+
const [secName, name, period] = splitEvent(raw);
|
|
66
|
+
if (!name)
|
|
67
|
+
bad(`"event" needs a name. Got: ${JSON.stringify(raw)}`);
|
|
68
|
+
const sec = byName.get(secName);
|
|
69
|
+
if (!sec)
|
|
70
|
+
bad(`"event" refers to unknown section ${JSON.stringify(secName)}`);
|
|
71
|
+
sec.events.push({ name, ...(period ? { period } : {}) });
|
|
72
|
+
}
|
|
73
|
+
if (!spec.sections.length)
|
|
74
|
+
bad(`at least one "section=" is required`);
|
|
75
|
+
if (!spec.sections.some((s) => s.events.length))
|
|
76
|
+
bad(`at least one "event=" is required`);
|
|
77
|
+
return spec;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Parse a timeline spec from JSON.
|
|
81
|
+
*
|
|
82
|
+
* @throws ApiError(400) with a corrective example on bad input
|
|
83
|
+
*/
|
|
84
|
+
export function parseTimelineSpec(raw) {
|
|
85
|
+
if (!raw.trim())
|
|
86
|
+
bad(`empty spec`);
|
|
87
|
+
let obj;
|
|
88
|
+
try {
|
|
89
|
+
obj = JSON.parse(raw);
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
bad(`"d" is not valid JSON`);
|
|
93
|
+
}
|
|
94
|
+
const o = obj;
|
|
95
|
+
const spec = { sections: [] };
|
|
96
|
+
if (o.title !== undefined) {
|
|
97
|
+
if (typeof o.title !== 'string')
|
|
98
|
+
bad(`"title" must be a string`);
|
|
99
|
+
spec.title = o.title;
|
|
100
|
+
}
|
|
101
|
+
if (o.subtitle !== undefined) {
|
|
102
|
+
if (typeof o.subtitle !== 'string')
|
|
103
|
+
bad(`"subtitle" must be a string`);
|
|
104
|
+
spec.subtitle = o.subtitle;
|
|
105
|
+
}
|
|
106
|
+
const secs = o.sections;
|
|
107
|
+
if (!Array.isArray(secs) || !secs.length)
|
|
108
|
+
bad(`"sections" must be a non-empty array`);
|
|
109
|
+
for (const s of secs) {
|
|
110
|
+
if (typeof s !== 'object' || s === null)
|
|
111
|
+
bad(`each section must be an object`);
|
|
112
|
+
const r = s;
|
|
113
|
+
if (typeof r.name !== 'string' || !r.name.trim())
|
|
114
|
+
bad(`each section needs a "name"`);
|
|
115
|
+
const events = [];
|
|
116
|
+
if (r.events !== undefined) {
|
|
117
|
+
if (!Array.isArray(r.events))
|
|
118
|
+
bad(`section "${r.name}": "events" must be an array`);
|
|
119
|
+
for (const e of r.events) {
|
|
120
|
+
if (typeof e === 'string') {
|
|
121
|
+
events.push({ name: e });
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (typeof e !== 'object' || e === null)
|
|
125
|
+
bad(`section "${r.name}": each event must be an object or a string`);
|
|
126
|
+
const er = e;
|
|
127
|
+
if (typeof er.name !== 'string' || !er.name.trim())
|
|
128
|
+
bad(`section "${r.name}": each event needs a "name"`);
|
|
129
|
+
const period = er.period;
|
|
130
|
+
if (period !== undefined && typeof period !== 'string')
|
|
131
|
+
bad(`section "${r.name}": event "period" must be a string`);
|
|
132
|
+
events.push({ name: er.name, ...(period ? { period } : {}) });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
spec.sections.push({ name: r.name.trim(), events });
|
|
136
|
+
}
|
|
137
|
+
if (!spec.sections.some((s) => s.events.length))
|
|
138
|
+
bad(`at least one event is required`);
|
|
139
|
+
return spec;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Render a timeline diagram.
|
|
143
|
+
*/
|
|
144
|
+
export function renderTimeline(spec, o) {
|
|
145
|
+
const evs = spec.sections.flatMap((s) => s.events.map((e) => ({ sec: s, ev: e })));
|
|
146
|
+
const nSeries = Math.max(1, o.skin.series.length);
|
|
147
|
+
const hasTitle = !!spec.title;
|
|
148
|
+
// 无标题也留 30px 角标带(Edit 胶囊在右上 y≈25–45,不留带会压住上排标签)
|
|
149
|
+
const titleH = hasTitle ? 40 : 30;
|
|
150
|
+
const subH = spec.subtitle ? 22 : 0;
|
|
151
|
+
// 每个事件占多宽(按最宽的标签 + 余量),取最大者当统一槽宽 —— 均匀比挤在一头好读
|
|
152
|
+
const slotW = Math.max(MIN_SLOT, ...evs.map(({ ev }) => Math.max(textW(ev.name, AXIS_FS), ev.period ? textW(ev.period, SUB_FS) : 0) + 28));
|
|
153
|
+
const axisY = 0; // 相对内容区的轴 y(下面再补)
|
|
154
|
+
const needW = PAD * 2 + slotW * evs.length;
|
|
155
|
+
const width = o.width !== undefined ? Math.max(200, Math.min(2400, o.width)) : Math.min(Math.max(needW, 560), 2400);
|
|
156
|
+
// 标签上下两排:每排两行(事件名 + 时期文字)
|
|
157
|
+
const rowH = AXIS_FS + SUB_FS + 16;
|
|
158
|
+
const needH = titleH + subH + PAD + rowH * 2 + 56;
|
|
159
|
+
const height = o.height !== undefined ? Math.max(140, Math.min(2000, o.height)) : Math.min(Math.max(needH, 220), 2000);
|
|
160
|
+
// 轴的两端留出半个标签宽 —— 标签是居中挂在点上的,不然首尾标签会出画布
|
|
161
|
+
const halfLabel = Math.ceil(slotW / 2) + 4;
|
|
162
|
+
const x0 = Math.min(PAD + halfLabel, width / 2);
|
|
163
|
+
const x1 = Math.max(width - PAD - halfLabel, width / 2);
|
|
164
|
+
// 均匀铺开:事件按顺序等距落点(两端贴边)
|
|
165
|
+
const step = evs.length > 1 ? (x1 - x0) / (evs.length - 1) : 0;
|
|
166
|
+
const placed = evs.map((it, i) => {
|
|
167
|
+
const x = evs.length > 1 ? x0 + i * step : (x0 + x1) / 2;
|
|
168
|
+
return {
|
|
169
|
+
sec: it.sec,
|
|
170
|
+
ev: it.ev,
|
|
171
|
+
x,
|
|
172
|
+
labelW: textW(it.ev.name, AXIS_FS),
|
|
173
|
+
subW: it.ev.period ? textW(it.ev.period, SUB_FS) : 0,
|
|
174
|
+
above: i % 2 === 0,
|
|
175
|
+
};
|
|
176
|
+
});
|
|
177
|
+
const axisYAbs = titleH + subH + PAD + rowH; // 轴本身
|
|
178
|
+
const bandTop = axisYAbs + rowH + 18; // section 带
|
|
179
|
+
const parts = [];
|
|
180
|
+
// section 带:每个 section 一段淡色底(自己的一段),段名贴在自己的带子中间
|
|
181
|
+
// 分段边界:按事件的段号切
|
|
182
|
+
const secRanges = [];
|
|
183
|
+
placed.forEach((p, i) => {
|
|
184
|
+
const last = secRanges[secRanges.length - 1];
|
|
185
|
+
if (last && last.sec === p.sec)
|
|
186
|
+
last.to = i;
|
|
187
|
+
else
|
|
188
|
+
secRanges.push({ sec: p.sec, from: i, to: i });
|
|
189
|
+
});
|
|
190
|
+
const bandH = 26;
|
|
191
|
+
secRanges.forEach((sr, i) => {
|
|
192
|
+
const from = placed[sr.from].x;
|
|
193
|
+
const to = placed[sr.to].x;
|
|
194
|
+
const bx = Math.max(x0 - 22, from - 26);
|
|
195
|
+
const bw = Math.min(x1 + 22, to + 26) - bx;
|
|
196
|
+
if (bw <= 0)
|
|
197
|
+
return;
|
|
198
|
+
const c = sVar(i % nSeries);
|
|
199
|
+
parts.push(`<rect x="${r2(bx)}" y="${r2(bandTop)}" width="${r2(bw)}" height="${bandH}" rx="6" fill="${c}" fill-opacity="0.07"/>`,
|
|
200
|
+
// 段名下沿一条细线,把段和轴分开
|
|
201
|
+
`<line x1="${r2(bx)}" y1="${r2(bandTop + bandH)}" x2="${r2(bx + bw)}" y2="${r2(bandTop + bandH)}" stroke="${c}" stroke-opacity="0.5" stroke-width="1.4"/>`);
|
|
202
|
+
// 段名:带子中间;太宽就压扁,窄了放不下就竖着写不现实 —— 改放带子上方一行
|
|
203
|
+
const nameW = textW(sr.sec.name, SEC_FS);
|
|
204
|
+
if (nameW + 12 <= bw) {
|
|
205
|
+
parts.push(`<text x="${r2(bx + bw / 2)}" y="${r2(bandTop + bandH / 2 + 4)}" text-anchor="middle" font-size="${SEC_FS}" font-weight="600" fill="var(--d-ink)">${esc(sr.sec.name)}</text>`);
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
parts.push(`<text x="${r2(bx + bw / 2)}" y="${r2(bandTop - 6)}" text-anchor="middle" font-size="${SEC_FS}" font-weight="600" fill="var(--d-ink)">${esc(sr.sec.name)}</text>`);
|
|
209
|
+
}
|
|
210
|
+
// 段色的小方点:段名左边的色点,和带子同色(带子太淡,色点给个锚)
|
|
211
|
+
parts.push(`<circle cx="${r2(bx + bw / 2 - nameW / 2 - 9)}" cy="${r2(bandTop + bandH / 2)}" r="3.5" fill="${c}"/>`);
|
|
212
|
+
});
|
|
213
|
+
// 轴
|
|
214
|
+
parts.push(`<line x1="${r2(x0)}" y1="${r2(axisYAbs)}" x2="${r2(x1)}" y2="${r2(axisYAbs)}" stroke="var(--d-baseline)" stroke-width="1.5"/>`);
|
|
215
|
+
// 事件:圆点 + 引线 + 标签(名字在上/下交替;时期文字在名字下面一行)
|
|
216
|
+
placed.forEach((p, i) => {
|
|
217
|
+
const color = sVar(secRanges.findIndex((sr) => sr.sec === p.sec) % nSeries);
|
|
218
|
+
const tip = [p.ev.name, p.ev.period, p.sec.name].filter(Boolean).join(' · ');
|
|
219
|
+
const ia = o.interactive ? ` data-node="${esc(p.ev.name)}" data-tip="${esc(tip)}" tabindex="0"` : '';
|
|
220
|
+
// 标签两行:名字(粗)+ 时期文字(小、弱色)。两排都按「上到下 = 名字、时期」排 ——
|
|
221
|
+
// 轴上方那一排是向上长的,所以名字在外(上)、时期靠轴。
|
|
222
|
+
const dir = p.above ? -1 : 1;
|
|
223
|
+
const gap = 16; // 轴到标签块的距离
|
|
224
|
+
// 名字基线:上面一排的名字在最外侧,下面一排的名字在最内侧
|
|
225
|
+
const nameY = p.above ? axisYAbs - gap - SUB_FS - 4 : axisYAbs + gap + AXIS_FS;
|
|
226
|
+
const subY = p.above ? axisYAbs - gap : nameY + SUB_FS + 4;
|
|
227
|
+
const totalW = Math.max(p.labelW, p.subW);
|
|
228
|
+
// 引线:从点连到标签块的内侧边缘
|
|
229
|
+
parts.push(`<line x1="${r2(p.x)}" y1="${r2(axisYAbs + dir * (DOT_R + 2))}" x2="${r2(p.x)}" y2="${r2(axisYAbs + dir * (gap - (p.above ? 1 : 0)))}" stroke="${color}" stroke-opacity="0.55" stroke-width="1.2"/>`);
|
|
230
|
+
// 名字
|
|
231
|
+
parts.push(`<text x="${r2(p.x)}" y="${r2(nameY)}" text-anchor="middle" font-size="${AXIS_FS}" font-weight="600" fill="var(--d-ink)"${ia}>${esc(p.ev.name)}</text>`);
|
|
232
|
+
// 时期文字:名字下面那一行,居中挂在点上
|
|
233
|
+
if (p.subW) {
|
|
234
|
+
parts.push(`<text x="${r2(p.x)}" y="${r2(subY)}" text-anchor="middle" font-size="${SUB_FS}" fill="var(--d-muted)">${esc(p.ev.period)}</text>`);
|
|
235
|
+
}
|
|
236
|
+
// 圆点
|
|
237
|
+
parts.push(`<circle cx="${r2(p.x)}" cy="${r2(axisYAbs)}" r="${DOT_R}" fill="${color}"${ia}/>`);
|
|
238
|
+
// 命中区:点的周围一块透明矩形(点太小,悬停不方便)
|
|
239
|
+
if (o.interactive) {
|
|
240
|
+
parts.push(`<rect x="${r2(p.x - Math.max(totalW / 2, 18))}" y="${r2(axisYAbs - 34)}" width="${r2(Math.max(totalW, 36))}" height="68" fill="transparent" pointer-events="all" data-tip="${esc(tip)}"/>`);
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
const titleSvg = titleBlock(spec.title, PAD) + subtitleBlock(spec.subtitle, PAD, hasTitle);
|
|
244
|
+
const canvas = card(width, height, o.skin.rules.canvas !== 'card');
|
|
245
|
+
// interactive:结构化数据(explore 详情卡的数据源)
|
|
246
|
+
let metaSvg = '';
|
|
247
|
+
if (o.interactive) {
|
|
248
|
+
const data = {
|
|
249
|
+
title: spec.title,
|
|
250
|
+
sections: spec.sections.map((s) => ({
|
|
251
|
+
name: s.name,
|
|
252
|
+
events: s.events.map((e) => ({ name: e.name, ...(e.period ? { period: e.period } : {}) })),
|
|
253
|
+
})),
|
|
254
|
+
};
|
|
255
|
+
metaSvg = `<metadata id="tevents">${esc(JSON.stringify(data))}</metadata>`;
|
|
256
|
+
}
|
|
257
|
+
const iStyle = o.interactive
|
|
258
|
+
? `<style>#tl [data-node]{cursor:pointer;transition:filter .12s ease}` +
|
|
259
|
+
`#tl [data-node]:hover{filter:brightness(1.15)}</style>`
|
|
260
|
+
: '';
|
|
261
|
+
const specOut = {
|
|
262
|
+
type: 'timeline',
|
|
263
|
+
...(spec.title ? { title: spec.title } : {}),
|
|
264
|
+
...(spec.subtitle ? { subtitle: spec.subtitle } : {}),
|
|
265
|
+
sections: spec.sections.map((s) => ({
|
|
266
|
+
name: s.name,
|
|
267
|
+
events: s.events.map((e) => ({ name: e.name, ...(e.period ? { period: e.period } : {}) })),
|
|
268
|
+
})),
|
|
269
|
+
};
|
|
270
|
+
const svg = svgDoc(width, height, 'timeline', o.v.version, spec.title ?? `timeline, ${evs.length} events`, specOut, o.skin, `${canvas}${metaSvg}${iStyle}<g id="tl">${titleSvg}${parts.join('')}</g>`, o.tagQuery, 'timeline');
|
|
271
|
+
return { svg, version: o.v.version, cacheControl: o.v.cacheControl };
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* `/draw/timeline` entry: GET ?title=…§ion=…&event=…
|
|
275
|
+
*/
|
|
276
|
+
export function renderTimelineFromUrl(url, interactive = false) {
|
|
277
|
+
const params = url.searchParams;
|
|
278
|
+
const d = params.get('d');
|
|
279
|
+
const spec = d !== null && d !== '' ? parseTimelineSpec(d) : timelineSpecFromParams(params);
|
|
280
|
+
const v = resolveVersion(params.get('v'));
|
|
281
|
+
const skin = resolveSkin('sankey', params.get('t'), schemeParam(params));
|
|
282
|
+
const width = clampInt(params, 'w', 0, 200, 2400);
|
|
283
|
+
const height = clampInt(params, 'h', 0, 140, 2000);
|
|
284
|
+
return renderTimeline(spec, {
|
|
285
|
+
v, skin, interactive,
|
|
286
|
+
width: params.has('w') ? width : undefined,
|
|
287
|
+
height: params.has('h') ? height : undefined,
|
|
288
|
+
tagQuery: url.search,
|
|
289
|
+
});
|
|
290
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface TtyOpts {
|
|
2
|
+
/** 窗口宽(字符数)—— 内容的列数上限 */
|
|
3
|
+
cols?: number;
|
|
4
|
+
/** 窗口高(行数)—— 装不下就截断 */
|
|
5
|
+
rows?: number;
|
|
6
|
+
/** 标题栏上的字(缺省用命令) */
|
|
7
|
+
title?: string;
|
|
8
|
+
/** 提示符(缺省 "$ ") */
|
|
9
|
+
prompt?: string;
|
|
10
|
+
theme?: 'dark' | 'light';
|
|
11
|
+
fontSize?: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* 命令 + 输出 → 终端窗口 SVG。
|
|
15
|
+
* out 里可以带 ANSI 转义序列(自己给,或用 ttycmd.ts 的小 shell 生成)。
|
|
16
|
+
*/
|
|
17
|
+
export declare function renderTty(cmd: string, out: string, opts?: TtyOpts): string;
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// 终端窗口(苹果风格)→ SVG。
|
|
2
|
+
//
|
|
3
|
+
// 一个圆角窗口:顶上一排红黄绿按钮 + 标题,下面一行命令(带提示符和光标),
|
|
4
|
+
// 再下面是输出 —— 输出里的 ANSI 转义序列还原成颜色(ansi.ts)。
|
|
5
|
+
//
|
|
6
|
+
// 这张图是给人贴进 README 的:一段命令行会话,长得像真的终端截图。
|
|
7
|
+
import { parseAnsi, stripAnsi } from './ansi.js';
|
|
8
|
+
const r2 = (v) => Math.round(v * 100) / 100;
|
|
9
|
+
const esc = (s) => s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
10
|
+
/** 两套配色:暗色(终端默认)和亮色 */
|
|
11
|
+
const SKIN = {
|
|
12
|
+
dark: {
|
|
13
|
+
bg: '#1C1C1E', bar: '#2A2A2D', border: '#3A3A3E', sep: '#343438',
|
|
14
|
+
text: '#D7D7DC', dim: '#8E8E93', prompt: '#33D17A',
|
|
15
|
+
dot: ['#FF5F57', '#FEBC2E', '#28C840'], shadow: '#000000',
|
|
16
|
+
},
|
|
17
|
+
light: {
|
|
18
|
+
bg: '#F7F7F8', bar: '#ECECEE', border: '#D6D6DA', sep: '#E0E0E4',
|
|
19
|
+
text: '#2A2A2E', dim: '#8A8A8F', prompt: '#1A7F37',
|
|
20
|
+
dot: ['#FF5F57', '#FEBC2E', '#28C840'], shadow: '#000000',
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
/** 一行的显示宽度(东亚宽字符算两格) */
|
|
24
|
+
function widthOf(s) {
|
|
25
|
+
let w = 0;
|
|
26
|
+
for (const ch of s) {
|
|
27
|
+
const c = ch.codePointAt(0) ?? 0;
|
|
28
|
+
w += (c >= 0x1100 && (c <= 0x115f || c === 0x2329 || c === 0x232a
|
|
29
|
+
|| (c >= 0x2e80 && c <= 0xa4cf && c !== 0x303f)
|
|
30
|
+
|| (c >= 0xac00 && c <= 0xd7a3) || (c >= 0xf900 && c <= 0xfaff)
|
|
31
|
+
|| (c >= 0xfe30 && c <= 0xfe6f) || (c >= 0xff00 && c <= 0xff60)
|
|
32
|
+
|| (c >= 0xffe0 && c <= 0xffe6))) ? 2 : 1;
|
|
33
|
+
}
|
|
34
|
+
return w;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 按显示宽度切行 —— 终端的老规矩:尽量在空格处断,断不了就硬断。
|
|
38
|
+
* 命令太长时绕到下一行,输出太长也一样。
|
|
39
|
+
*/
|
|
40
|
+
function wrapCells(text, maxCols) {
|
|
41
|
+
if (maxCols < 8)
|
|
42
|
+
return [text];
|
|
43
|
+
const out = [];
|
|
44
|
+
let line = '';
|
|
45
|
+
let w = 0;
|
|
46
|
+
let lastSpace = -1;
|
|
47
|
+
for (const ch of text) {
|
|
48
|
+
const cw = widthOf(ch);
|
|
49
|
+
if (w + cw > maxCols && line) {
|
|
50
|
+
if (lastSpace > 0) {
|
|
51
|
+
out.push(line.slice(0, lastSpace));
|
|
52
|
+
line = line.slice(lastSpace + 1);
|
|
53
|
+
w = widthOf(line);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
out.push(line);
|
|
57
|
+
line = '';
|
|
58
|
+
w = 0;
|
|
59
|
+
}
|
|
60
|
+
lastSpace = -1;
|
|
61
|
+
}
|
|
62
|
+
line += ch;
|
|
63
|
+
w += cw;
|
|
64
|
+
if (ch === ' ')
|
|
65
|
+
lastSpace = line.length - 1;
|
|
66
|
+
}
|
|
67
|
+
if (line)
|
|
68
|
+
out.push(line);
|
|
69
|
+
return out.length ? out : [''];
|
|
70
|
+
}
|
|
71
|
+
/** 片段 → 一行 SVG(每段一个 <text>;带底色的段,底下垫一块 <rect>) */
|
|
72
|
+
function lineSvg(runs, x0, y, charW, fs, lineH, skin) {
|
|
73
|
+
let x = x0;
|
|
74
|
+
const parts = [];
|
|
75
|
+
for (const r of runs) {
|
|
76
|
+
const w = widthOf(r.text) * charW;
|
|
77
|
+
// 底色:SVG 的文字没有背景 —— 用一块 rect 垫在字底下(终端里就是这个样子)
|
|
78
|
+
if (r.bg) {
|
|
79
|
+
parts.push(`<rect x="${r2(x)}" y="${r2(y - fs * 0.92)}" width="${r2(w)}" height="${r2(lineH - 3)}" fill="${r.bg}"/>`);
|
|
80
|
+
}
|
|
81
|
+
const st = [`fill="${r.fg ?? skin.text}"`];
|
|
82
|
+
if (r.bold)
|
|
83
|
+
st.push('font-weight="700"');
|
|
84
|
+
if (r.dim)
|
|
85
|
+
st.push('fill-opacity="0.62"');
|
|
86
|
+
if (r.italic)
|
|
87
|
+
st.push('font-style="italic"');
|
|
88
|
+
if (r.underline)
|
|
89
|
+
st.push('text-decoration="underline"');
|
|
90
|
+
parts.push(`<text x="${r2(x)}" y="${r2(y)}" font-size="${fs}" ${st.join(' ')} xml:space="preserve">${esc(r.text)}</text>`);
|
|
91
|
+
x += w;
|
|
92
|
+
}
|
|
93
|
+
return parts.join('');
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* 命令 + 输出 → 终端窗口 SVG。
|
|
97
|
+
* out 里可以带 ANSI 转义序列(自己给,或用 ttycmd.ts 的小 shell 生成)。
|
|
98
|
+
*/
|
|
99
|
+
export function renderTty(cmd, out, opts = {}) {
|
|
100
|
+
const skin = opts.theme === 'light' ? SKIN.light : SKIN.dark;
|
|
101
|
+
const fs = opts.fontSize ?? 13;
|
|
102
|
+
const lineH = Math.round(fs * 1.55);
|
|
103
|
+
const charW = fs * 0.6; // 等宽字体的步进(近似)
|
|
104
|
+
const padX = 16;
|
|
105
|
+
const barH = 34;
|
|
106
|
+
const padTop = 12;
|
|
107
|
+
const prompt = opts.prompt ?? '$ ';
|
|
108
|
+
const cols = Math.max(20, opts.cols ?? 72);
|
|
109
|
+
const maxRows = Math.max(1, opts.rows ?? 24);
|
|
110
|
+
// 命令太长 → 绕到下一行(终端的老规矩)
|
|
111
|
+
const promptW = widthOf(prompt);
|
|
112
|
+
const cmdChunks = wrapCells(cmd, Math.max(8, cols - promptW));
|
|
113
|
+
// 输出按行拆;每行再按 ANSI 片段拆(太长的行也绕)
|
|
114
|
+
const outLines = out.replace(/\r/g, '').split('\n');
|
|
115
|
+
const lines = [];
|
|
116
|
+
for (const l of outLines) {
|
|
117
|
+
if (lines.length >= maxRows)
|
|
118
|
+
break;
|
|
119
|
+
for (const chunk of wrapCells(l, cols)) {
|
|
120
|
+
if (lines.length >= maxRows)
|
|
121
|
+
break;
|
|
122
|
+
lines.push(chunk);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const width = Math.round(padX * 2 + cols * charW);
|
|
126
|
+
const height = barH + padTop + lineH * (lines.length + cmdChunks.length) + 14;
|
|
127
|
+
const parts = [];
|
|
128
|
+
// 阴影(高斯模糊一张黑底 —— feDropShadow 光栅化太慢,不用)
|
|
129
|
+
parts.push(`<rect x="8" y="10" width="${width - 16}" height="${height - 16}" rx="14" fill="${skin.shadow}" opacity="0.45" filter="url(#tty-shadow)"/>`);
|
|
130
|
+
// 窗口
|
|
131
|
+
parts.push(`<rect x="0.5" y="0.5" width="${width - 1}" height="${height - 1}" rx="12" fill="${skin.bg}" stroke="${skin.border}"/>`);
|
|
132
|
+
// 标题栏
|
|
133
|
+
parts.push(`<path d="M12 0.5H${width - 12}a12 12 0 0 1 12 12V${barH}H0.5V12.5A12 12 0 0 1 12 0.5Z" fill="${skin.bar}"/>`);
|
|
134
|
+
parts.push(`<line x1="0" y1="${barH}" x2="${width}" y2="${barH}" stroke="${skin.sep}"/>`);
|
|
135
|
+
skin.dot.forEach((c, i) => {
|
|
136
|
+
parts.push(`<circle cx="${20 + i * 20}" cy="${barH / 2}" r="6" fill="${c}"/>`);
|
|
137
|
+
});
|
|
138
|
+
const title = (opts.title ?? stripAnsi(cmd).trim() ?? '').slice(0, 60);
|
|
139
|
+
if (title) {
|
|
140
|
+
parts.push(`<text x="${width / 2}" y="${barH / 2 + 4}" text-anchor="middle" font-size="12" fill="${skin.dim}">${esc(title)}</text>`);
|
|
141
|
+
}
|
|
142
|
+
// 命令行:提示符 + 命令 + 光标(命令太长就绕到下一行)
|
|
143
|
+
let y = barH + padTop + fs;
|
|
144
|
+
parts.push(`<text x="${padX}" y="${y}" font-size="${fs}" fill="${skin.prompt}" xml:space="preserve">${esc(prompt)}</text>`);
|
|
145
|
+
cmdChunks.forEach((chunk, i) => {
|
|
146
|
+
if (i > 0) {
|
|
147
|
+
y += lineH;
|
|
148
|
+
// 绕行的那几行:顶头对齐(没有提示符)
|
|
149
|
+
parts.push(lineSvg(parseAnsi(chunk), padX, y, charW, fs, lineH, skin));
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
parts.push(lineSvg(parseAnsi(chunk), padX + promptW * charW, y, charW, fs, lineH, skin));
|
|
153
|
+
});
|
|
154
|
+
const lastChunk = cmdChunks[cmdChunks.length - 1] ?? '';
|
|
155
|
+
const cursorX = padX + (cmdChunks.length > 1 ? widthOf(lastChunk) : promptW + widthOf(lastChunk)) * charW;
|
|
156
|
+
parts.push(`<rect x="${cursorX}" y="${y - fs + 2}" width="${charW}" height="${fs}" fill="${skin.text}" opacity="0.75"/>`);
|
|
157
|
+
// 输出
|
|
158
|
+
y += lineH;
|
|
159
|
+
for (const line of lines) {
|
|
160
|
+
if (!line) {
|
|
161
|
+
y += lineH;
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
parts.push(lineSvg(parseAnsi(line), padX, y, charW, fs, lineH, skin));
|
|
165
|
+
y += lineH;
|
|
166
|
+
}
|
|
167
|
+
const font = "ui-monospace,SFMono-Regular,Menlo,Monaco,'Cascadia Mono','Courier New',monospace";
|
|
168
|
+
return (`<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" ` +
|
|
169
|
+
`font-family="${font}" role="img" aria-label="terminal">` +
|
|
170
|
+
`<defs><filter id="tty-shadow" x="-20%" y="-20%" width="140%" height="140%"><feGaussianBlur stdDeviation="9"/></filter></defs>` +
|
|
171
|
+
parts.join('') +
|
|
172
|
+
`</svg>`);
|
|
173
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// 终端里那个小 shell:一行命令 → 一段带颜色的输出。
|
|
2
|
+
//
|
|
3
|
+
// 认这几件事:
|
|
4
|
+
// help 用法
|
|
5
|
+
// echo <话> 回声
|
|
6
|
+
// draw <图种> --labels A,B --values 1,2 把图画成 Unicode 的图(带颜色)
|
|
7
|
+
// clear 清屏(只是句提示)
|
|
8
|
+
//
|
|
9
|
+
// 输出的都是 ANSI 转义序列 —— 由 ansi.ts 负责还原成颜色。
|
|
10
|
+
// 同一段输出既能进网页(实时看),也能进 SVG(截图进 README)。
|
|
11
|
+
/** 站上的色板(暗色一套) */
|
|
12
|
+
const SERIES = ['#34D399', '#F87171', '#A78BFA', '#22D3EE', '#60A5FA', '#FB923C'];
|
|
13
|
+
const fg = (hex) => `\x1b[38;2;${rgb(hex)}m`;
|
|
14
|
+
const bg = (hex) => `\x1b[48;2;${rgb(hex)}m`;
|
|
15
|
+
const RESET = '\x1b[0m';
|
|
16
|
+
const BOLD = '\x1b[1m';
|
|
17
|
+
const DIM = '\x1b[2m';
|
|
18
|
+
function rgb(hex) {
|
|
19
|
+
const h = hex.replace('#', '');
|
|
20
|
+
const r = parseInt(h.slice(0, 2), 16);
|
|
21
|
+
const g = parseInt(h.slice(2, 4), 16);
|
|
22
|
+
const b = parseInt(h.slice(4, 6), 16);
|
|
23
|
+
return `${r};${g};${b}`;
|
|
24
|
+
}
|
|
25
|
+
/** 八个字符的方块,按高低取值 */
|
|
26
|
+
const BLOCKS = ['▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'];
|
|
27
|
+
/** 把一行命令拆成「命令 + 参数」;引号里的空格算一个参数 */
|
|
28
|
+
export function splitArgs(line) {
|
|
29
|
+
const out = [];
|
|
30
|
+
let cur = '';
|
|
31
|
+
let quote = null;
|
|
32
|
+
for (const ch of line) {
|
|
33
|
+
if (quote) {
|
|
34
|
+
if (ch === quote)
|
|
35
|
+
quote = null;
|
|
36
|
+
else
|
|
37
|
+
cur += ch;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (ch === '"' || ch === "'") {
|
|
41
|
+
quote = ch;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (ch === ' ') {
|
|
45
|
+
if (cur) {
|
|
46
|
+
out.push(cur);
|
|
47
|
+
cur = '';
|
|
48
|
+
}
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
cur += ch;
|
|
52
|
+
}
|
|
53
|
+
if (cur)
|
|
54
|
+
out.push(cur);
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
/** 从参数里取值:--labels A,B / --values 1,2 */
|
|
58
|
+
function opt(args, name) {
|
|
59
|
+
const i = args.indexOf(name);
|
|
60
|
+
if (i < 0)
|
|
61
|
+
return undefined;
|
|
62
|
+
return args[i + 1];
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* 跑一条命令,返回带 ANSI 的输出。
|
|
66
|
+
* 认不得的命令 → 一句「command not found」(这也是终端的老规矩)。
|
|
67
|
+
*/
|
|
68
|
+
export function runCommand(line) {
|
|
69
|
+
const cmd = line.trim();
|
|
70
|
+
if (!cmd)
|
|
71
|
+
return '';
|
|
72
|
+
const args = splitArgs(cmd);
|
|
73
|
+
const head = args[0];
|
|
74
|
+
if (head === 'help' || head === '?') {
|
|
75
|
+
return [
|
|
76
|
+
`${BOLD}draw tty${RESET} —— 终端里的小 shell`,
|
|
77
|
+
'',
|
|
78
|
+
' help 这份说明',
|
|
79
|
+
' echo <话> 回声',
|
|
80
|
+
' draw <图种> --labels A,B --values 1,2',
|
|
81
|
+
' 把图画成 Unicode 的图',
|
|
82
|
+
'',
|
|
83
|
+
`${DIM}图种:bar hbar pie line area gauge${RESET}`,
|
|
84
|
+
`${DIM}例:draw bar --labels Mon,Tue,Wed --values 12,30,18${RESET}`,
|
|
85
|
+
].join('\n');
|
|
86
|
+
}
|
|
87
|
+
if (head === 'echo') {
|
|
88
|
+
return args.slice(1).join(' ');
|
|
89
|
+
}
|
|
90
|
+
if (head === 'clear') {
|
|
91
|
+
return `${DIM}(屏幕清空了 —— 这里是静态图,清屏就这一步)${RESET}`;
|
|
92
|
+
}
|
|
93
|
+
if (head === 'draw') {
|
|
94
|
+
return drawInTerminal(args.slice(1));
|
|
95
|
+
}
|
|
96
|
+
return `${fg('#F87171')}command not found: ${head}${RESET}\n${DIM}输入 help 看看能做什么${RESET}`;
|
|
97
|
+
}
|
|
98
|
+
/** draw <图种> --labels … --values … → Unicode 图 */
|
|
99
|
+
function drawInTerminal(args) {
|
|
100
|
+
const kind = (args[0] ?? 'bar').toLowerCase();
|
|
101
|
+
const labels = (opt(args, '--labels') ?? opt(args, '-l') ?? 'A,B,C').split(',').map((s) => s.trim());
|
|
102
|
+
const values = (opt(args, '--values') ?? opt(args, '-v') ?? '1,2,3')
|
|
103
|
+
.split(',').map((s) => Number.parseFloat(s.trim()))
|
|
104
|
+
.filter((n) => Number.isFinite(n));
|
|
105
|
+
const title = opt(args, '--title');
|
|
106
|
+
if (!values.length)
|
|
107
|
+
return `${fg('#F87171')}draw: --values 是空的${RESET}`;
|
|
108
|
+
const n = Math.min(labels.length, values.length);
|
|
109
|
+
const ls = labels.slice(0, n);
|
|
110
|
+
const vs = values.slice(0, n);
|
|
111
|
+
const out = [];
|
|
112
|
+
if (title)
|
|
113
|
+
out.push(`${BOLD}${title}${RESET}`);
|
|
114
|
+
if (kind === 'pie') {
|
|
115
|
+
// 饼图 → 一条按比例分段的彩色带(终端里画不出圆)
|
|
116
|
+
const total = vs.reduce((a, b) => a + b, 0) || 1;
|
|
117
|
+
const width = 40;
|
|
118
|
+
const cells = [];
|
|
119
|
+
vs.forEach((v, i) => {
|
|
120
|
+
const w = Math.max(1, Math.round((v / total) * width));
|
|
121
|
+
cells.push(`${bg(SERIES[i % SERIES.length])} ${' '.repeat(Math.max(0, w - 1))}${RESET}`);
|
|
122
|
+
});
|
|
123
|
+
out.push(cells.join(''));
|
|
124
|
+
vs.forEach((v, i) => {
|
|
125
|
+
const pct = ((v / total) * 100).toFixed(1);
|
|
126
|
+
out.push(`${fg(SERIES[i % SERIES.length])}●${RESET} ${ls[i]} ${DIM}${v}${RESET} ${DIM}(${pct}%)${RESET}`);
|
|
127
|
+
});
|
|
128
|
+
return out.join('\n');
|
|
129
|
+
}
|
|
130
|
+
if (kind === 'line' || kind === 'area') {
|
|
131
|
+
const max = Math.max(...vs);
|
|
132
|
+
const min = Math.min(...vs);
|
|
133
|
+
const span = max - min || 1;
|
|
134
|
+
const spark = vs.map((v) => {
|
|
135
|
+
const idx = Math.round(((v - min) / span) * (BLOCKS.length - 1));
|
|
136
|
+
return BLOCKS[Math.max(0, Math.min(BLOCKS.length - 1, idx))];
|
|
137
|
+
}).join('');
|
|
138
|
+
out.push(`${fg(SERIES[0])}${spark}${RESET}`);
|
|
139
|
+
out.push(`${DIM}${min} → ${max}${RESET}`);
|
|
140
|
+
vs.forEach((v, i) => out.push(`${DIM}${ls[i].padEnd(8)}${RESET}${v}`));
|
|
141
|
+
return out.join('\n');
|
|
142
|
+
}
|
|
143
|
+
// bar / hbar / gauge:横着的条,条长按最大值归一
|
|
144
|
+
const max = Math.max(...vs);
|
|
145
|
+
const width = 28;
|
|
146
|
+
const labelW = Math.max(...ls.map((l) => l.length));
|
|
147
|
+
vs.forEach((v, i) => {
|
|
148
|
+
const w = Math.max(1, Math.round((v / (max || 1)) * width));
|
|
149
|
+
out.push(`${ls[i].padEnd(labelW)} ${fg(SERIES[i % SERIES.length])}${'█'.repeat(w)}${RESET} ${DIM}${v}${RESET}`);
|
|
150
|
+
});
|
|
151
|
+
return out.join('\n');
|
|
152
|
+
}
|