mayx-cli 0.0.0-stage → 1.0.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 +21 -0
- package/README.md +126 -2
- package/bin/mayx.js +23 -0
- package/package.json +30 -4
- package/src/ansi.js +62 -0
- package/src/canvas.js +352 -0
- package/src/cli.js +422 -0
- package/src/commands.js +303 -0
- package/src/data.js +408 -0
- package/src/format.js +170 -0
- package/src/markdown.js +267 -0
- package/src/open.js +56 -0
- package/src/tui.js +1117 -0
package/src/format.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 展示层:把数据格式化成 run 行,TUI 和 CLI 共用一套。
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const { C, runs, push, wrapRuns, runsFit, strWidth } = require('./canvas');
|
|
8
|
+
|
|
9
|
+
/* ============================ 文章元信息 ============================ */
|
|
10
|
+
|
|
11
|
+
function metaBlock(post, width, opts) {
|
|
12
|
+
const { compact = false } = opts || {};
|
|
13
|
+
const out = [];
|
|
14
|
+
|
|
15
|
+
// 标题过长时折行,续行按 "#12 " 的宽度缩进对齐
|
|
16
|
+
const prefix = `#${post.num} `;
|
|
17
|
+
const hang = ' '.repeat(strWidth(prefix));
|
|
18
|
+
const titleLines = wrapRuns([...runs(post.title, C.title)], Math.max(8, width - strWidth(prefix)));
|
|
19
|
+
titleLines.forEach((l, i) => {
|
|
20
|
+
out.push([...runs(i === 0 ? prefix : hang, C.date), ...l]);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const info = [];
|
|
24
|
+
push(info, post.date, C.date);
|
|
25
|
+
if (post.category) {
|
|
26
|
+
push(info, ' · ', C.dim);
|
|
27
|
+
push(info, post.category, C.tag);
|
|
28
|
+
}
|
|
29
|
+
if (!compact && post.tags.length) {
|
|
30
|
+
push(info, ' · ', C.dim);
|
|
31
|
+
push(info, post.tags.map((t) => `#${t}`).join(' '), C.tag);
|
|
32
|
+
}
|
|
33
|
+
out.push(info);
|
|
34
|
+
|
|
35
|
+
out.push([...runs('─'.repeat(Math.max(4, width)), C.border)]);
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/* ============================ 列表 ============================ */
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 终端表格。
|
|
43
|
+
* @param {Array} posts
|
|
44
|
+
* @param {number} width
|
|
45
|
+
* @param {{numWidth?: number, showTags?: boolean}} [opts]
|
|
46
|
+
*/
|
|
47
|
+
function table(posts, width, opts) {
|
|
48
|
+
const { showTags = false } = opts || {};
|
|
49
|
+
if (!posts.length) return [[...runs('(没有结果)', C.warn)]];
|
|
50
|
+
|
|
51
|
+
// 按显示宽度补齐(中文占两列,不能用 padEnd)
|
|
52
|
+
const padDisplay = (s, w) => {
|
|
53
|
+
const sw = strWidth(s);
|
|
54
|
+
return sw >= w ? s : s + ' '.repeat(w - sw);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const numW = String(Math.max(...posts.map((p) => p.num))).length;
|
|
58
|
+
const dateW = 10;
|
|
59
|
+
const gaps = 4;
|
|
60
|
+
const titleW = Math.max(10, width - numW - dateW - gaps);
|
|
61
|
+
|
|
62
|
+
const out = [];
|
|
63
|
+
// 表头
|
|
64
|
+
out.push([
|
|
65
|
+
...runs(' '.repeat(numW) + ' ', C.dim),
|
|
66
|
+
...runs(padDisplay('日期', dateW), C.dim),
|
|
67
|
+
...runs(' ', ''),
|
|
68
|
+
...runs('标题', C.dim),
|
|
69
|
+
]);
|
|
70
|
+
|
|
71
|
+
for (const p of posts) {
|
|
72
|
+
const row = [];
|
|
73
|
+
push(row, String(p.num).padStart(numW), C.date);
|
|
74
|
+
push(row, ' ', '');
|
|
75
|
+
push(row, p.date, C.date);
|
|
76
|
+
push(row, ' ', '');
|
|
77
|
+
|
|
78
|
+
let titleText = p.title;
|
|
79
|
+
let tagText = '';
|
|
80
|
+
if (showTags && p.tags.length) {
|
|
81
|
+
tagText = ' ' + p.tags.slice(0, 3).map((t) => `#${t}`).join(' ');
|
|
82
|
+
}
|
|
83
|
+
const tagW = strWidth(tagText);
|
|
84
|
+
const avail = Math.max(6, titleW - tagW);
|
|
85
|
+
if (strWidth(titleText) > avail) {
|
|
86
|
+
// 按列宽截断并加省略号
|
|
87
|
+
let acc = '';
|
|
88
|
+
let w = 0;
|
|
89
|
+
for (const ch of titleText) {
|
|
90
|
+
const cw = strWidth(ch);
|
|
91
|
+
if (w + cw > avail - 1) break;
|
|
92
|
+
acc += ch;
|
|
93
|
+
w += cw;
|
|
94
|
+
}
|
|
95
|
+
titleText = acc + '…';
|
|
96
|
+
}
|
|
97
|
+
push(row, titleText, C.strong);
|
|
98
|
+
if (tagText) push(row, tagText, C.tag);
|
|
99
|
+
out.push(row);
|
|
100
|
+
}
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/* ============================ 评论 ============================ */
|
|
105
|
+
|
|
106
|
+
function commentsBlock(result, width) {
|
|
107
|
+
const out = [];
|
|
108
|
+
if (!result.comments.length) {
|
|
109
|
+
out.push([...runs('📭 暂无评论', C.warn)]);
|
|
110
|
+
if (result.reason === 'NO_ISSUE') {
|
|
111
|
+
out.push([...runs('(这篇还没有对应的 GitHub Issue)', C.dim)]);
|
|
112
|
+
}
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
out.push([...runs(`共 ${result.comments.length} 条评论`, C.dim)]);
|
|
117
|
+
out.push([]);
|
|
118
|
+
|
|
119
|
+
result.comments.forEach((c, i) => {
|
|
120
|
+
const head = [];
|
|
121
|
+
push(head, `${i + 1}. `, C.date);
|
|
122
|
+
push(head, c.author, C.ok + C.bold);
|
|
123
|
+
if (c.date) push(head, ' ' + c.date.slice(0, 16).replace('T', ' '), C.dim);
|
|
124
|
+
out.push(head);
|
|
125
|
+
|
|
126
|
+
for (const line of String(c.body).split('\n')) {
|
|
127
|
+
const wrapped = wrapRuns([...runs(line, '')], Math.max(8, width - 3));
|
|
128
|
+
for (const l of wrapped) out.push([...runs(' '), ...l]);
|
|
129
|
+
}
|
|
130
|
+
out.push([]);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
if (result.issueUrl) {
|
|
134
|
+
out.push([...runs('原 Issue: ', C.dim), ...runs(result.issueUrl, C.link)]);
|
|
135
|
+
}
|
|
136
|
+
return out;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/* ============================ 关于 ============================ */
|
|
140
|
+
|
|
141
|
+
function aboutBlock(text, width) {
|
|
142
|
+
const out = [];
|
|
143
|
+
for (const line of String(text).split('\n')) {
|
|
144
|
+
const t = line.replace(/\t/g, ' ');
|
|
145
|
+
if (!t.trim()) {
|
|
146
|
+
out.push([]);
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
// /* SECTION */ 这类注释行加粗
|
|
150
|
+
if (/^\/\*.*\*\/$/.test(t.trim())) {
|
|
151
|
+
out.push([...runs(t.trim(), C.h3)]);
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
out.push(wrapRuns([...runs(t, '')], width)[0]);
|
|
155
|
+
}
|
|
156
|
+
while (out.length && out[out.length - 1].length === 0) out.pop();
|
|
157
|
+
return out;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/* ============================ 提示条 ============================ */
|
|
161
|
+
|
|
162
|
+
function hint(text) {
|
|
163
|
+
return [...runs(text, C.dim)];
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function errorLine(text) {
|
|
167
|
+
return [...runs('✗ ', C.err), ...runs(text, C.err)];
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
module.exports = { metaBlock, table, commentsBlock, aboutBlock, hint, errorLine };
|
package/src/markdown.js
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Markdown → 终端样式行。
|
|
5
|
+
*
|
|
6
|
+
* 输出的是 canvas 的 run 数组(Run[][]),已经按目标宽度折好行,
|
|
7
|
+
* 调用方只要拼到画布上就行。
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const { C, runs, push, wrapRuns, strWidth, charWidth } = require('./canvas');
|
|
11
|
+
|
|
12
|
+
/* ============================ 行内解析 ============================ */
|
|
13
|
+
|
|
14
|
+
// `code` / **bold** / *italic* / ~~del~~ / [text](url) / 
|
|
15
|
+
const INLINE_RE = /(`[^`\n]+`)|(\*\*[^*\n]+\*\*|__[^_\n]+__)|(\*[^*\n]+\*|_[^_\n]+_)|(~~[^~\n]+~~)|(!?\[[^\]\n]*\]\([^)\n]*\))/g;
|
|
16
|
+
|
|
17
|
+
function inlineRuns(text, base) {
|
|
18
|
+
const out = [];
|
|
19
|
+
const st = base || '';
|
|
20
|
+
let last = 0;
|
|
21
|
+
let m;
|
|
22
|
+
INLINE_RE.lastIndex = 0;
|
|
23
|
+
|
|
24
|
+
while ((m = INLINE_RE.exec(text)) !== null) {
|
|
25
|
+
if (m.index > last) push(out, text.slice(last, m.index), st);
|
|
26
|
+
const tok = m[0];
|
|
27
|
+
|
|
28
|
+
if (m[1]) {
|
|
29
|
+
push(out, tok.slice(1, -1), C.code);
|
|
30
|
+
} else if (m[2]) {
|
|
31
|
+
push(out, tok.slice(2, -2), st + C.bold);
|
|
32
|
+
} else if (m[3]) {
|
|
33
|
+
push(out, tok.slice(1, -1), st + C.italic);
|
|
34
|
+
} else if (m[4]) {
|
|
35
|
+
push(out, tok.slice(2, -2), st + C.strike + C.dim);
|
|
36
|
+
} else if (m[5]) {
|
|
37
|
+
const mm = /^(!?)\[([^\]]*)\]\(([^)]*)\)$/.exec(tok);
|
|
38
|
+
if (mm) {
|
|
39
|
+
const isImg = mm[1] === '!';
|
|
40
|
+
const label = mm[2] || (isImg ? '图片' : mm[3]);
|
|
41
|
+
if (isImg) {
|
|
42
|
+
push(out, `🖼 ${label}`, C.dim);
|
|
43
|
+
} else {
|
|
44
|
+
push(out, label, C.link);
|
|
45
|
+
}
|
|
46
|
+
} else {
|
|
47
|
+
push(out, tok, st);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
last = m.index + tok.length;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (last < text.length) push(out, text.slice(last), st);
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/* ============================ 定宽切片(代码块用) ============================ */
|
|
58
|
+
|
|
59
|
+
function sliceFixed(text, width) {
|
|
60
|
+
const lines = [];
|
|
61
|
+
let cur = '';
|
|
62
|
+
let w = 0;
|
|
63
|
+
for (const ch of text) {
|
|
64
|
+
const cw = charWidth(ch);
|
|
65
|
+
if (w + cw > width) {
|
|
66
|
+
lines.push(cur);
|
|
67
|
+
cur = '';
|
|
68
|
+
w = 0;
|
|
69
|
+
}
|
|
70
|
+
cur += ch;
|
|
71
|
+
w += cw;
|
|
72
|
+
}
|
|
73
|
+
lines.push(cur);
|
|
74
|
+
return lines;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/* ============================ 主渲染 ============================ */
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @param {string} md 原始 Markdown
|
|
81
|
+
* @param {number} width 可用列宽
|
|
82
|
+
* @param {{showFrontMatter?: boolean, maxLines?: number}} [opts]
|
|
83
|
+
* @returns {Array<Array>} Run[][]
|
|
84
|
+
*/
|
|
85
|
+
function render(md, width, opts) {
|
|
86
|
+
const { showFrontMatter = false, maxLines = 20000 } = opts || {};
|
|
87
|
+
const W = Math.max(8, width);
|
|
88
|
+
const out = [];
|
|
89
|
+
|
|
90
|
+
// 去掉 HTML 注释(如 <!--more-->)和多余的行内换行标签
|
|
91
|
+
const src = String(md || '')
|
|
92
|
+
.replace(/<!--[\s\S]*?-->/g, '')
|
|
93
|
+
.replace(/<br\s*\/?>/gi, '\n');
|
|
94
|
+
const lines = src.split('\n');
|
|
95
|
+
let inCode = false;
|
|
96
|
+
let inFront = false;
|
|
97
|
+
let codeLang = '';
|
|
98
|
+
let codeBuf = [];
|
|
99
|
+
let frontLines = [];
|
|
100
|
+
let i = 0;
|
|
101
|
+
|
|
102
|
+
const emit = (rs) => {
|
|
103
|
+
if (out.length < maxLines) out.push(rs);
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const flushCode = () => {
|
|
107
|
+
if (!codeBuf.length) return;
|
|
108
|
+
const inner = W - 3;
|
|
109
|
+
// 去掉代码块首尾的空行
|
|
110
|
+
while (codeBuf.length && codeBuf[0].trim() === '') codeBuf.shift();
|
|
111
|
+
while (codeBuf.length && codeBuf[codeBuf.length - 1].trim() === '') codeBuf.pop();
|
|
112
|
+
|
|
113
|
+
for (const raw of codeBuf) {
|
|
114
|
+
const stripped = raw.replace(/\t/g, ' ');
|
|
115
|
+
const parts = sliceFixed(stripped, Math.max(4, inner));
|
|
116
|
+
for (const part of parts) {
|
|
117
|
+
emit([...runs('│ ', C.dim), ...runs(part, C.codeBlock)]);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
codeBuf = [];
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
for (; i < lines.length; i++) {
|
|
124
|
+
const raw = lines[i];
|
|
125
|
+
const indentMatch = /^(\s*)/.exec(raw);
|
|
126
|
+
const indent = indentMatch[1].length;
|
|
127
|
+
const t = raw.trim();
|
|
128
|
+
|
|
129
|
+
/* ---- YAML front matter ---- */
|
|
130
|
+
if (i === 0 && /^---\s*$/.test(t)) {
|
|
131
|
+
inFront = true;
|
|
132
|
+
frontLines = [raw];
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (inFront) {
|
|
136
|
+
frontLines.push(raw);
|
|
137
|
+
if (/^---\s*$/.test(t)) {
|
|
138
|
+
inFront = false;
|
|
139
|
+
if (showFrontMatter) {
|
|
140
|
+
for (const l of frontLines) emit([...runs(l, C.dim)]);
|
|
141
|
+
emit([]);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/* ---- 代码围栏 ---- */
|
|
148
|
+
const fence = /^(```|~~~)\s*([\w+.-]*)/.exec(t);
|
|
149
|
+
if (fence) {
|
|
150
|
+
if (inCode) {
|
|
151
|
+
flushCode();
|
|
152
|
+
inCode = false;
|
|
153
|
+
codeLang = '';
|
|
154
|
+
} else {
|
|
155
|
+
inCode = true;
|
|
156
|
+
codeLang = fence[2] || '';
|
|
157
|
+
}
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (inCode) {
|
|
161
|
+
codeBuf.push(raw);
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/* ---- 空行 ---- */
|
|
166
|
+
if (t === '') {
|
|
167
|
+
// 连续空行只留一个
|
|
168
|
+
if (out.length && out[out.length - 1].length !== 0) emit([]);
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/* ---- 标题 ---- */
|
|
173
|
+
const h = /^(#{1,6})\s+(.*)$/.exec(t);
|
|
174
|
+
if (h) {
|
|
175
|
+
const level = h[1].length;
|
|
176
|
+
const style = [C.h1, C.h1, C.h2, C.h3, C.h4, C.h4][level - 1] || C.h4;
|
|
177
|
+
const body = h[2].replace(/\s+#+\s*$/, '');
|
|
178
|
+
const wrapped = wrapRuns([...runs(body, style)], W);
|
|
179
|
+
for (const l of wrapped) emit(l);
|
|
180
|
+
emit([]);
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/* ---- 分隔线 ---- */
|
|
185
|
+
if (/^(-{3,}|\*{3,}|_{3,})$/.test(t.replace(/\s+/g, ''))) {
|
|
186
|
+
emit([...runs('─'.repeat(W), C.border)]);
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/* ---- 引用 ---- */
|
|
191
|
+
if (/^>/.test(t)) {
|
|
192
|
+
let body = t.replace(/^>\s?/, '');
|
|
193
|
+
body = body.replace(/^>\s?/, '');
|
|
194
|
+
const style = C.quote;
|
|
195
|
+
const wrapped = wrapRuns(inlineRuns(body, style), W - 2);
|
|
196
|
+
for (const l of wrapped) emit([...runs('▏ ', C.border), ...l]);
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/* ---- 表格(原样保留,改色以便区分) ---- */
|
|
201
|
+
if (/^\|.*\|$/.test(t)) {
|
|
202
|
+
const wrapped = wrapRuns([...runs(t, C.dim)], W);
|
|
203
|
+
for (const l of wrapped) emit(l);
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/* ---- 无序列表 ---- */
|
|
208
|
+
const ul = /^[-*+]\s+(.*)$/.exec(t);
|
|
209
|
+
if (ul) {
|
|
210
|
+
const pad = ' '.repeat(Math.min(3, Math.floor(indent / 2)));
|
|
211
|
+
const bulletW = strWidth(pad) + 2;
|
|
212
|
+
const wrapped = wrapRuns(inlineRuns(ul[1], ''), W - bulletW);
|
|
213
|
+
wrapped.forEach((l, idx) => {
|
|
214
|
+
if (idx === 0) emit([...runs(`${pad}• `, C.ok), ...l]);
|
|
215
|
+
else emit([...runs(' '.repeat(bulletW)), ...l]);
|
|
216
|
+
});
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/* ---- 有序列表 ---- */
|
|
221
|
+
const ol = /^(\d+)[.)]\s+(.*)$/.exec(t);
|
|
222
|
+
if (ol) {
|
|
223
|
+
const pad = ' '.repeat(Math.min(3, Math.floor(indent / 2)));
|
|
224
|
+
const marker = `${pad}${ol[1]}. `;
|
|
225
|
+
const bulletW = strWidth(marker);
|
|
226
|
+
const wrapped = wrapRuns(inlineRuns(ol[2], ''), W - bulletW);
|
|
227
|
+
wrapped.forEach((l, idx) => {
|
|
228
|
+
if (idx === 0) emit([...runs(marker, C.date), ...l]);
|
|
229
|
+
else emit([...runs(' '.repeat(bulletW)), ...l]);
|
|
230
|
+
});
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/* ---- 任务列表 ---- */
|
|
235
|
+
const task = /^\[( |x|X)\]\s+(.*)$/.exec(t);
|
|
236
|
+
if (task) {
|
|
237
|
+
const checked = task[1].toLowerCase() === 'x';
|
|
238
|
+
const wrapped = wrapRuns(inlineRuns(task[2], ''), W - 4);
|
|
239
|
+
wrapped.forEach((l, idx) => {
|
|
240
|
+
if (idx === 0) emit([...runs(checked ? '☑ ' : '☐ ', C.ok), ...l]);
|
|
241
|
+
else emit([...runs(' '), ...l]);
|
|
242
|
+
});
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/* ---- 裸 HTML:直接去掉标签 ---- */
|
|
247
|
+
if (/^<[^>]+>/.test(t) && !/^<https?:/.test(t)) {
|
|
248
|
+
const stripped = t.replace(/<[^>]*>/g, '').trim();
|
|
249
|
+
if (!stripped) continue;
|
|
250
|
+
const wrapped = wrapRuns(inlineRuns(stripped, C.dim), W);
|
|
251
|
+
for (const l of wrapped) emit(l);
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/* ---- 普通段落:保留作者的手动换行 ---- */
|
|
256
|
+
const wrapped = wrapRuns(inlineRuns(t, ''), W);
|
|
257
|
+
for (const l of wrapped) emit(l);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (inCode) flushCode();
|
|
261
|
+
|
|
262
|
+
// 去掉尾部空行
|
|
263
|
+
while (out.length && out[out.length - 1].length === 0) out.pop();
|
|
264
|
+
return out;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
module.exports = { render, inlineRuns };
|
package/src/open.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { spawn } = require('child_process');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 用系统默认浏览器打开 URL。
|
|
7
|
+
* @returns {boolean} 是否成功发起调用
|
|
8
|
+
*/
|
|
9
|
+
function openInBrowser(url) {
|
|
10
|
+
if (!url) return false;
|
|
11
|
+
let cmd;
|
|
12
|
+
let args;
|
|
13
|
+
if (process.platform === 'darwin') {
|
|
14
|
+
cmd = 'open';
|
|
15
|
+
args = [url];
|
|
16
|
+
} else if (process.platform === 'win32') {
|
|
17
|
+
cmd = 'cmd';
|
|
18
|
+
args = ['/c', 'start', '""', url.replace(/&/g, '^&')];
|
|
19
|
+
} else {
|
|
20
|
+
cmd = 'xdg-open';
|
|
21
|
+
args = [url];
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
const child = spawn(cmd, args, { detached: true, stdio: 'ignore' });
|
|
25
|
+
child.on('error', () => {});
|
|
26
|
+
child.unref();
|
|
27
|
+
return true;
|
|
28
|
+
} catch (_) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** 复制文本到剪贴板(尽力而为) */
|
|
34
|
+
function copyToClipboard(text) {
|
|
35
|
+
const candidates =
|
|
36
|
+
process.platform === 'darwin'
|
|
37
|
+
? [['pbcopy', []]]
|
|
38
|
+
: process.platform === 'win32'
|
|
39
|
+
? [['clip', []]]
|
|
40
|
+
: [['wl-copy', []], ['xclip', ['-selection', 'clipboard']], ['xsel', ['--clipboard', '--input']]];
|
|
41
|
+
|
|
42
|
+
for (const [cmd, args] of candidates) {
|
|
43
|
+
try {
|
|
44
|
+
const child = spawn(cmd, args, { stdio: ['pipe', 'ignore', 'ignore'] });
|
|
45
|
+
child.on('error', () => {});
|
|
46
|
+
child.stdin.end(text);
|
|
47
|
+
child.unref();
|
|
48
|
+
return true;
|
|
49
|
+
} catch (_) {
|
|
50
|
+
/* 试下一个 */
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
module.exports = { openInBrowser, copyToClipboard };
|