pi-straw-boss 0.80.7 → 0.80.8
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/dist/server/{chunk-576U3FLU.mjs → chunk-4PGRA77L.mjs} +4 -2
- package/dist/server/cli.mjs +585 -119
- package/dist/server/main.mjs +464 -418
- package/package.json +1 -1
- package/prompts/head-run.md +1 -1
- package/prompts/main.md +1 -1
- package/prompts/tower-run.md +1 -1
- package/src/auto.ts +3 -3
- package/src/autotext.ts +1 -1
- package/src/index.ts +2 -1
- package/src/net.ts +1 -1
- package/src/runallow.ts +4 -4
- package/src/runmode.ts +19 -3
- package/src/server/autotower.ts +1 -1
- package/src/server/headcmds.ts +3 -3
- package/src/server/main.ts +11 -4
- package/src/server/runs.ts +36 -8
- package/src/server/standing.ts +2 -2
- package/src/server/towers.ts +10 -9
- package/src/server/towerstore.ts +2 -0
- package/src/server/waitcard.ts +2 -1
- package/src/server/waitpress.ts +41 -3
- package/src/tools.ts +3 -2
- package/src/towerprompt/cardview.ts +414 -0
- package/src/towerprompt/look.ts +6 -0
- package/src/towerprompt/messages.ts +4 -0
- package/src/towerprompt/prompt.ts +122 -11
- package/src/towerprompt/serversource.ts +8 -5
- package/src/towerprompt/source.ts +6 -1
- package/src/waitask.ts +175 -0
- package/src/waitcmd.ts +61 -32
- package/src/waitview.ts +12 -7
package/dist/server/cli.mjs
CHANGED
|
@@ -89,7 +89,7 @@ import {
|
|
|
89
89
|
writeBoard,
|
|
90
90
|
writeJson,
|
|
91
91
|
writerAlive
|
|
92
|
-
} from "./chunk-
|
|
92
|
+
} from "./chunk-4PGRA77L.mjs";
|
|
93
93
|
|
|
94
94
|
// src/server/cli.ts
|
|
95
95
|
import { existsSync as existsSync2, realpathSync as realpathSync3 } from "node:fs";
|
|
@@ -106,9 +106,9 @@ function integrationPath(output) {
|
|
|
106
106
|
const line = output.split(/\r?\n/).find((l) => l.startsWith("pi:"));
|
|
107
107
|
return line?.match(/\(([^()]*)\)\s*$/)?.[1]?.trim() || void 0;
|
|
108
108
|
}
|
|
109
|
-
async function ensureIntegration(run2,
|
|
109
|
+
async function ensureIntegration(run2, within3 = (ms) => ms) {
|
|
110
110
|
const status2 = async () => {
|
|
111
|
-
const output = await run2(["integration", "status"],
|
|
111
|
+
const output = await run2(["integration", "status"], within3(1e4)), state = integrationState(output);
|
|
112
112
|
if (state === void 0) throw new Error("`herdr integration status` has no pi line");
|
|
113
113
|
return { state, path: integrationPath(output) };
|
|
114
114
|
};
|
|
@@ -116,7 +116,7 @@ async function ensureIntegration(run2, within2 = (ms) => ms) {
|
|
|
116
116
|
if (before.state === "current") return "current";
|
|
117
117
|
if (!before.path) throw new Error("`herdr integration status` names no file for the pi integration");
|
|
118
118
|
mkdirSync(dirname(before.path), { recursive: true });
|
|
119
|
-
await run2(["integration", "install", "pi"],
|
|
119
|
+
await run2(["integration", "install", "pi"], within3(3e4));
|
|
120
120
|
const after = (await status2()).state;
|
|
121
121
|
if (after !== "current") throw new Error(`Herdr's pi integration is still ${after} after \`herdr integration install pi\``);
|
|
122
122
|
return "installed";
|
|
@@ -508,68 +508,6 @@ function notices(flags, io) {
|
|
|
508
508
|
// src/towerprompt/prompt.ts
|
|
509
509
|
import { clearLine, createInterface, cursorTo } from "node:readline";
|
|
510
510
|
|
|
511
|
-
// src/towerprompt/look.ts
|
|
512
|
-
var PLAIN = { color: false, ascii: true, clear: false };
|
|
513
|
-
var on = (value) => /^(1|true|yes|on)$/i.test(value ?? "");
|
|
514
|
-
var off = (value) => /^(0|false|no|off)$/i.test(value ?? "");
|
|
515
|
-
function lookOf(env, terminal) {
|
|
516
|
-
const ascii = on(env.STRAW_BOSS_ASCII) || !terminal && !off(env.STRAW_BOSS_ASCII);
|
|
517
|
-
if (!terminal) return { color: false, ascii, clear: false };
|
|
518
|
-
const dumb = env.TERM === "dumb";
|
|
519
|
-
return { color: !dumb && !env.NO_COLOR, ascii, clear: !dumb };
|
|
520
|
-
}
|
|
521
|
-
var TOWER = "\u{F043B}";
|
|
522
|
-
var CROWN = "\u{F01A5}";
|
|
523
|
-
var RESET = "\x1B[0m";
|
|
524
|
-
var CLEAR = "\x1B[H\x1B[2J";
|
|
525
|
-
var SGR = { bold: "1", dim: "2", accent: "36", ok: "32", warn: "33", bad: "31", run: "34;1" };
|
|
526
|
-
var KINDS = {
|
|
527
|
-
info: { glyph: "\u25D0", ascii: "*", style: "accent" },
|
|
528
|
-
ok: { glyph: "\u2713", ascii: "+", style: "ok" },
|
|
529
|
-
warn: { glyph: "!", ascii: "!", style: "warn" },
|
|
530
|
-
bad: { glyph: "\u2717", ascii: "x", style: "bad" },
|
|
531
|
-
ask: { glyph: "?", ascii: "?", style: "warn" }
|
|
532
|
-
};
|
|
533
|
-
var LEAVE = { glyph: "\u21A9", ascii: "<" };
|
|
534
|
-
var STATES = {
|
|
535
|
-
standing: { glyph: "\u25CF", ascii: "o", style: "ok" },
|
|
536
|
-
waking: { glyph: "\u25D0", ascii: "~", style: "warn" },
|
|
537
|
-
running: { glyph: "\u25B6", ascii: ">", style: "run" },
|
|
538
|
-
closing: { glyph: "\u25D1", ascii: "~", style: "warn" },
|
|
539
|
-
held: { glyph: "\u25A0", ascii: "!", style: "bad" }
|
|
540
|
-
};
|
|
541
|
-
var UNKNOWN_STATE = { glyph: "\u25CF", ascii: "?", style: "dim" };
|
|
542
|
-
var cells = (text) => [...text].reduce((n, ch) => n + (ch.codePointAt(0) >= 11904 ? 2 : 1), 0);
|
|
543
|
-
function draw(look) {
|
|
544
|
-
const paint = (style, text) => look.color && text ? `\x1B[${style.map((s) => SGR[s]).join(";")}m${text}${RESET}` : text;
|
|
545
|
-
const mark = (glyph, ascii) => look.ascii ? ascii : glyph;
|
|
546
|
-
return {
|
|
547
|
-
/** ` tower · Alpha`, bold; the head tower's title carries the crown. */
|
|
548
|
-
title: (text, head = false) => `${paint(["accent"], mark(head ? CROWN : TOWER, "#"))} ${paint(["bold"], look.ascii ? text.replace(" \xB7 ", " - ") : text)}`,
|
|
549
|
-
/** `● standing by`: the dot in the colour of the state, the words dim for a tower that stands by and plain otherwise. */
|
|
550
|
-
state: (state, words) => {
|
|
551
|
-
const s = (Object.hasOwn(STATES, state) ? STATES[state] : void 0) ?? UNKNOWN_STATE;
|
|
552
|
-
return `${paint([s.style], mark(s.glyph, s.ascii))} ${state === "standing" ? paint(["dim"], words) : words}`;
|
|
553
|
-
},
|
|
554
|
-
/** A line between the states. */
|
|
555
|
-
note: (kind, text) => {
|
|
556
|
-
const k = KINDS[kind];
|
|
557
|
-
return `${paint([k.style], mark(k.glyph, k.ascii))} ${text}`;
|
|
558
|
-
},
|
|
559
|
-
/** The line that says the boss took the pane. */
|
|
560
|
-
leave: (text) => `${paint(["dim"], mark(LEAVE.glyph, LEAVE.ascii))} ${paint(["dim"], text)}`,
|
|
561
|
-
/** The key rows under the state, the keys in the accent colour and in one column, the words dim. */
|
|
562
|
-
hints: (rows) => {
|
|
563
|
-
const keys = rows.map((r) => look.ascii ? r.plain : r.keys), width = Math.max(...keys.map(cells));
|
|
564
|
-
return rows.map((r, i) => ` ${paint(["accent"], keys[i])}${" ".repeat(width - cells(keys[i]))} ${paint(["dim"], r.text)}`);
|
|
565
|
-
},
|
|
566
|
-
/** The string readline writes before the line the boss types. */
|
|
567
|
-
prompt: () => look.ascii ? "tower> " : paint(["accent", "bold"], `${TOWER} \u276F `),
|
|
568
|
-
/** The prompt string of the pick mode (`wait ❯`; `wait> ` in ASCII, whatever the language: nothing outside ASCII is drawn). */
|
|
569
|
-
pickPrompt: (word) => look.ascii ? "wait> " : paint(["accent", "bold"], `${word} \u276F `)
|
|
570
|
-
};
|
|
571
|
-
}
|
|
572
|
-
|
|
573
511
|
// src/waitview.ts
|
|
574
512
|
function rowsOf(towers) {
|
|
575
513
|
return towers.flatMap((t) => tidy({ rev: 0, entries: t.waits.map((x) => ({ ...x, kind: "wait" })), proposals: [] }).entries.map((wait) => ({ tower: { ...t, label: cleanLabel(t.label) }, wait })));
|
|
@@ -602,6 +540,8 @@ var WORDS = {
|
|
|
602
540
|
done: "I'm done",
|
|
603
541
|
skip: "Skip",
|
|
604
542
|
back: "Back",
|
|
543
|
+
next: "Next card",
|
|
544
|
+
all: "All cards",
|
|
605
545
|
restate: (tower) => `Ask ${tower} to rewrite it as a card`,
|
|
606
546
|
secretStart: (name) => `Start entering (asks ${name} to open the entry box)`,
|
|
607
547
|
secretDone: "I entered it",
|
|
@@ -650,6 +590,8 @@ var WORDS = {
|
|
|
650
590
|
done: "\u6211\u505A\u5B8C\u4E86",
|
|
651
591
|
skip: "\u7565\u904E",
|
|
652
592
|
back: "\u8FD4\u56DE",
|
|
593
|
+
next: "\u4E0B\u4E00\u5F35",
|
|
594
|
+
all: "\u5168\u90E8\u5361\u7247",
|
|
653
595
|
restate: (tower) => `\u8ACB${tower}\u91CD\u5BEB\u6210\u5361\u7247`,
|
|
654
596
|
secretStart: (name) => `\u958B\u59CB\u8F38\u5165\uFF08\u8ACB ${name} \u958B\u555F\u8F38\u5165\u6846\uFF09`,
|
|
655
597
|
secretDone: "\u6211\u8F38\u5165\u597D\u4E86",
|
|
@@ -704,7 +646,7 @@ function pressedWord(lang, e) {
|
|
|
704
646
|
const m = memoryWords(lang), p = e.press;
|
|
705
647
|
return p.act === "choose" ? m.didChose(e.card?.options?.[p.option ?? -1]?.label ?? "?") : p.act === "decline" ? m.didDecline : p.act === "restate" ? m.didRestate : m.didDone;
|
|
706
648
|
}
|
|
707
|
-
function actsOf(lang, auto, tower, e) {
|
|
649
|
+
function actsOf(lang, auto, tower, e, nav = {}) {
|
|
708
650
|
const w = WORDS[lang], m = memoryWords(lang), c = e.card, who = c?.from?.name ?? w.raiserFallback, tw = towerWord(lang, tower);
|
|
709
651
|
const pressed = e.press !== void 0 && e.press.act !== "secret";
|
|
710
652
|
const press = (label, json, what) => ({ label, act: { kind: "press", json: { op: "press", id: e.id, ...json }, what } });
|
|
@@ -718,8 +660,471 @@ function actsOf(lang, auto, tower, e) {
|
|
|
718
660
|
} else own.push(press(w.done, { act: "done" }, m.didDone));
|
|
719
661
|
own.push({ label: w.skip, act: { kind: "skip" } });
|
|
720
662
|
}
|
|
721
|
-
const there = c?.from?.tab && !c.from.gone ? c.from : void 0;
|
|
722
|
-
return [...own, { label: w.reply(tw), act: { kind: "reply" } }, ...there ? [{ label: w.switchTo(who), act: { kind: "switch", from: there } }] : [], { label: w.back, act: { kind: "back" } }];
|
|
663
|
+
const there = c?.from?.tab && !c.from.gone && !nav.ask ? c.from : void 0;
|
|
664
|
+
return [...own, { label: w.reply(tw), act: { kind: "reply" } }, ...there ? [{ label: w.switchTo(who), act: { kind: "switch", from: there } }] : [], ...nav.next ? [{ label: w.next, act: { kind: "next" } }] : [], ...nav.ask ? [] : [{ label: nav.all ? w.all : w.back, act: { kind: "back" } }]];
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
// src/towerprompt/waitblock.ts
|
|
668
|
+
var BLOCK_MAX = 5;
|
|
669
|
+
var CONTROLS = /[\u0000-\u001f\u007f-\u009f\u200b-\u200f\u2028-\u202e\u2060-\u206f\ufeff]/g;
|
|
670
|
+
var plain2 = (value, max = 200) => String(value ?? "").replace(CONTROLS, "").slice(0, max);
|
|
671
|
+
var WORDS2 = {
|
|
672
|
+
en: {
|
|
673
|
+
title: (n) => `waits for you (${n})`,
|
|
674
|
+
more: (n, command) => `${n} more: ${command}`,
|
|
675
|
+
from: (who) => `from ${who}`,
|
|
676
|
+
parked: "parked",
|
|
677
|
+
type: { action: "action", choice: "choice", secret: "secret", legacy: "old" },
|
|
678
|
+
press: (command) => `${command} press a wait card`
|
|
679
|
+
},
|
|
680
|
+
"zh-TW": {
|
|
681
|
+
title: (n) => `\u7B49\u4F60\u8655\u7406\uFF08${n}\uFF09`,
|
|
682
|
+
more: (n, command) => `\u9084\u6709 ${n} \u5F35\uFF1A${command}`,
|
|
683
|
+
from: (who) => `\u7531 ${who}`,
|
|
684
|
+
parked: "\u5DF2\u66AB\u653E",
|
|
685
|
+
type: { action: "\u52D5\u4F5C", choice: "\u9078\u64C7", secret: "\u5BC6\u9470", legacy: "\u820A\u5F0F" },
|
|
686
|
+
press: (command) => `${command} \u8655\u7406\u7B49\u4F60\u7684\u5361\u7247`
|
|
687
|
+
}
|
|
688
|
+
};
|
|
689
|
+
function waitBlock(rows, locale, command) {
|
|
690
|
+
if (!rows.length) return [];
|
|
691
|
+
const w = WORDS2[locale];
|
|
692
|
+
const lines = rows.slice(0, BLOCK_MAX).map((r) => {
|
|
693
|
+
const parts = [plain2(r.do, 120), ...r.from ? [w.from(plain2(r.from, 60))] : [], ...r.parked ? [w.parked] : []];
|
|
694
|
+
return ` ${plain2(r.id, 12)} ${w.type[plain2(r.type, 20)] ?? plain2(r.type, 20)} \xB7 ${parts.join(" \xB7 ")}`;
|
|
695
|
+
});
|
|
696
|
+
return [w.title(rows.length), ...lines, ...rows.length > BLOCK_MAX ? [` ${w.more(rows.length - BLOCK_MAX, command)}`] : []];
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
// src/towerprompt/cardview.ts
|
|
700
|
+
var QUIET_MS = 6e4;
|
|
701
|
+
var VIEW = {
|
|
702
|
+
en: {
|
|
703
|
+
head: (i, n) => `Waits for you ${i}/${n}`,
|
|
704
|
+
type: { action: "action", choice: "choice", secret: "secret", legacy: "old" },
|
|
705
|
+
from: (who) => `from ${who}`,
|
|
706
|
+
parked: "parked",
|
|
707
|
+
keys: (many, ascii) => ascii ? `Up/Down or 1-9 marks, Enter sends${many ? ", Left/Right other card" : ""}, Esc: message line` : `\u2191\u2193 or a number marks \xB7 Enter sends${many ? " \xB7 \u2190\u2192 other card" : ""} \xB7 Esc: message line`,
|
|
708
|
+
released: "the keys are the message line's; /wait brings the cards back",
|
|
709
|
+
reason: "Reason for skipping (optional): Enter goes on, Esc cancels",
|
|
710
|
+
confirm: (body) => `Skip this? ${body} Enter confirms, Esc cancels`,
|
|
711
|
+
reply: (tower) => `Reply to ${tower}: Enter sends, an empty line or Esc cancels`,
|
|
712
|
+
fields: { reason: "reason", confirm: "skip", reply: "reply" },
|
|
713
|
+
elsewhere: (id) => `${id} was answered elsewhere.`,
|
|
714
|
+
left: "No wait card is left for you.",
|
|
715
|
+
pasted: "a paste is not a pick; nothing was pressed",
|
|
716
|
+
read: (why) => `The cards could not be read: ${why}`,
|
|
717
|
+
markRefused: (why) => `The cards need this pane marked as local-only, and it could not be (${why}); nothing was pressed. /wait tries again.`
|
|
718
|
+
},
|
|
719
|
+
"zh-TW": {
|
|
720
|
+
head: (i, n) => `\u7B49\u4F60\u8655\u7406 ${i}/${n}`,
|
|
721
|
+
type: { action: "\u52D5\u4F5C", choice: "\u9078\u64C7", secret: "\u5BC6\u9470", legacy: "\u820A\u5F0F" },
|
|
722
|
+
from: (who) => `\u7531 ${who}`,
|
|
723
|
+
parked: "\u5DF2\u66AB\u653E",
|
|
724
|
+
keys: (many, ascii) => ascii ? `Up/Down \u6216\u6578\u5B57\u9078\u64C7, Enter \u9001\u51FA${many ? ", Left/Right \u63DB\u5F35" : ""}, Esc \u56DE\u5230\u8F38\u5165\u5217` : `\u2191\u2193 \u6216\u6578\u5B57\u9078\u64C7 \xB7 Enter \u9001\u51FA${many ? " \xB7 \u2190\u2192 \u63DB\u5F35" : ""} \xB7 Esc \u56DE\u5230\u8F38\u5165\u5217`,
|
|
725
|
+
released: "\u6309\u9375\u56DE\u5230\u8F38\u5165\u5217\uFF1B/wait \u53EF\u56DE\u5230\u5361\u7247",
|
|
726
|
+
reason: "\u7565\u904E\u7684\u539F\u56E0\uFF08\u9078\u586B\uFF09\uFF1AEnter \u7E7C\u7E8C\uFF0CEsc \u53D6\u6D88",
|
|
727
|
+
confirm: (body) => `\u78BA\u5B9A\u7565\u904E\uFF1F${body}Enter \u78BA\u5B9A\u7565\u904E\uFF0CEsc \u53D6\u6D88`,
|
|
728
|
+
reply: (tower) => `\u56DE\u8986${tower}\uFF1AEnter \u9001\u51FA\uFF0C\u7A7A\u884C\u6216 Esc \u53D6\u6D88`,
|
|
729
|
+
fields: { reason: "\u539F\u56E0", confirm: "\u7565\u904E", reply: "\u56DE\u8986" },
|
|
730
|
+
elsewhere: (id) => `${id} \u5DF2\u5728\u5225\u8655\u8655\u7406\u3002`,
|
|
731
|
+
left: "\u6C92\u6709\u7B49\u4F60\u8655\u7406\u7684\u5361\u7247\u4E86\u3002",
|
|
732
|
+
pasted: "\u8CBC\u4E0A\u7684\u6587\u5B57\u4E0D\u7B97\u9078\u64C7\uFF0C\u6C92\u6709\u6309\u4E0B\u4EFB\u4F55\u6771\u897F",
|
|
733
|
+
read: (why) => `\u8B80\u4E0D\u5230\u5361\u7247\uFF1A${why}`,
|
|
734
|
+
markRefused: (why) => `\u5361\u7247\u9700\u8981\u628A\u9019\u500B\u7A97\u683C\u6A19\u6210\u53EA\u6709\u672C\u6A5F\u80FD\u64CD\u4F5C\uFF0C\u4F46\u6A19\u4E0D\u8D77\u4F86\uFF08${why}\uFF09\uFF1B\u6C92\u6709\u6309\u4E0B\u4EFB\u4F55\u6771\u897F\u3002/wait \u53EF\u518D\u8A66\u4E00\u6B21\u3002`
|
|
735
|
+
}
|
|
736
|
+
};
|
|
737
|
+
var FIELD_PLAIN = VIEW.en.fields;
|
|
738
|
+
var WIDE = [4352, 4447, 8986, 8987, 9001, 9002, 9193, 9196, 9200, 9200, 9203, 9203, 9725, 9726, 9748, 9749, 9800, 9811, 9855, 9855, 9875, 9875, 9889, 9889, 9898, 9899, 9917, 9918, 9924, 9925, 9934, 9934, 9940, 9940, 9962, 9962, 9970, 9971, 9973, 9973, 9978, 9978, 9981, 9981, 9989, 9989, 9994, 9995, 10024, 10024, 10060, 10060, 10062, 10062, 10067, 10069, 10071, 10071, 10133, 10135, 10160, 10160, 10175, 10175, 11035, 11036, 11088, 11088, 11093, 11093, 11904, 12350, 12353, 13311, 13312, 19903, 19968, 42191, 43360, 43391, 44032, 55203, 63744, 64255, 65040, 65049, 65072, 65135, 65281, 65376, 65504, 65510, 94176, 101119, 110592, 111359, 126980, 126980, 127183, 127183, 127374, 127374, 127377, 127386, 127488, 127569, 127744, 128591, 128640, 128767, 129280, 129535, 129648, 129791, 131072, 262141];
|
|
739
|
+
var ZERO = [768, 879, 6832, 6911, 7616, 7679, 8205, 8205, 8400, 8447, 65024, 65039, 65056, 65071, 917760, 917999];
|
|
740
|
+
var within2 = (ranges, code) => {
|
|
741
|
+
for (let i = 0; i < ranges.length; i += 2) if (code >= ranges[i] && code <= ranges[i + 1]) return true;
|
|
742
|
+
return false;
|
|
743
|
+
};
|
|
744
|
+
function cellsOf(text) {
|
|
745
|
+
let n = 0;
|
|
746
|
+
for (const ch of text) {
|
|
747
|
+
const code = ch.codePointAt(0);
|
|
748
|
+
n += within2(ZERO, code) ? 0 : within2(WIDE, code) ? 2 : 1;
|
|
749
|
+
}
|
|
750
|
+
return n;
|
|
751
|
+
}
|
|
752
|
+
function fit(text, max) {
|
|
753
|
+
if (cellsOf(text) <= max) return text;
|
|
754
|
+
let kept = "", n = 0;
|
|
755
|
+
for (const ch of text) {
|
|
756
|
+
const w = cellsOf(ch);
|
|
757
|
+
if (n + w > max - 1) break;
|
|
758
|
+
kept += ch;
|
|
759
|
+
n += w;
|
|
760
|
+
}
|
|
761
|
+
return `${kept}\u2026`;
|
|
762
|
+
}
|
|
763
|
+
var safe = (text) => text.split("\n").map((l) => plain2(l, 4e3));
|
|
764
|
+
var keyOf = (r) => `${r.tower.workspace}
|
|
765
|
+
${r.wait.id}`;
|
|
766
|
+
var pending = (w) => w.state !== "answered" && (!w.press || w.press.act === "secret");
|
|
767
|
+
var CARD_KEYS = ["up", "down", "left", "right", "escape"];
|
|
768
|
+
function createCardView(io) {
|
|
769
|
+
const w = VIEW[io.locale], v = WORDS[io.locale], m = memoryWords(io.locale), source = io.source;
|
|
770
|
+
let rows = [], auto = false, at = 0, mark, step = "card", reason = "";
|
|
771
|
+
let cur, drawn = "";
|
|
772
|
+
let manual = false, released = false;
|
|
773
|
+
let engaged = false, lastKey = -Infinity, release, closed;
|
|
774
|
+
let sending = false;
|
|
775
|
+
let opening = false;
|
|
776
|
+
const settled = /* @__PURE__ */ new Set();
|
|
777
|
+
let busy = Promise.resolve();
|
|
778
|
+
const run2 = (fn, otherwise) => {
|
|
779
|
+
const next = busy.then(fn).catch((cause) => (out(cause.message, "bad"), otherwise));
|
|
780
|
+
busy = next;
|
|
781
|
+
return next;
|
|
782
|
+
};
|
|
783
|
+
const out = (text, kind = "plain") => void safe(text).forEach((line, i) => io.say(line, i === 0 ? kind : "plain"));
|
|
784
|
+
const row = () => cur === void 0 ? void 0 : rows[at];
|
|
785
|
+
const acts = (r) => actsOf(io.locale, auto, r.tower, r.wait).filter((a) => a.act.kind === "press" || a.act.kind === "skip" || a.act.kind === "reply");
|
|
786
|
+
const stateOf = (r) => `${keyOf(r)}
|
|
787
|
+
${r.wait.press?.act ?? ""}
|
|
788
|
+
${auto}`;
|
|
789
|
+
const region = () => {
|
|
790
|
+
const r = row();
|
|
791
|
+
if (!r) return [];
|
|
792
|
+
const max = Math.max(20, io.columns() - 2);
|
|
793
|
+
const list = acts(r).map((a, i) => io.paint(fit(` ${i === mark ? io.pointer : " "} ${i + 1}. ${plain2(a.label, 400)}`, max), i === mark ? "marked" : "plain"));
|
|
794
|
+
return [...list, io.paint(fit(` ${released ? w.released : w.keys(rows.length > 1, io.ascii)}`, max), "hint")];
|
|
795
|
+
};
|
|
796
|
+
const show = (i) => {
|
|
797
|
+
at = i;
|
|
798
|
+
const r = rows[i], c = r.wait.card, ms = Math.max(0, io.now() - Date.parse(r.wait.at ?? ""));
|
|
799
|
+
cur = keyOf(r);
|
|
800
|
+
drawn = stateOf(r);
|
|
801
|
+
mark = void 0;
|
|
802
|
+
dropField();
|
|
803
|
+
io.fresh();
|
|
804
|
+
const head = [w.head(i + 1, rows.length), `${v.tag(r.tower.label).trim()} ${r.wait.id} ${w.type[c?.type ?? "legacy"] ?? c?.type}`, ...c?.from?.name ? [w.from(c.from.name)] : [], ...Number.isFinite(ms) ? [v.ago(ms)] : [], ...r.wait.parked ? [w.parked] : []];
|
|
805
|
+
const line = (k, value) => ` ${k}: ${value}`;
|
|
806
|
+
const body = !c ? [` ${r.wait.text}`, ` ${v.legacy}`] : [
|
|
807
|
+
...c.happened ? [line(v.happened, c.happened)] : [],
|
|
808
|
+
line(v.do, c.do),
|
|
809
|
+
...c.url ? [line(v.where, c.url)] : [],
|
|
810
|
+
...c.command ? [` ${v.run}:`, ...c.command.split("\n").map((l) => ` ${l}`)] : [],
|
|
811
|
+
...c.after ? [line(v.after, c.after)] : [],
|
|
812
|
+
...c.secret ? [line(v.secret, c.secret.name)] : [],
|
|
813
|
+
...auto && c.type === "secret" && !r.wait.press?.secretAsked ? [` ${v.autoSecret}`] : []
|
|
814
|
+
];
|
|
815
|
+
out(head.join(" \xB7 "), "ask");
|
|
816
|
+
for (const l of body) out(l);
|
|
817
|
+
io.rows(region());
|
|
818
|
+
};
|
|
819
|
+
function dropField() {
|
|
820
|
+
if (step === "card") return;
|
|
821
|
+
step = "card";
|
|
822
|
+
reason = "";
|
|
823
|
+
io.field();
|
|
824
|
+
}
|
|
825
|
+
const engage = async () => {
|
|
826
|
+
if (engaged) return true;
|
|
827
|
+
const held = new Promise((resolve4) => release = resolve4);
|
|
828
|
+
try {
|
|
829
|
+
await new Promise((ok, bad) => void (closed = source.here(() => (ok(), held)).catch(bad)));
|
|
830
|
+
} catch (cause) {
|
|
831
|
+
release = void 0;
|
|
832
|
+
closed = void 0;
|
|
833
|
+
released = true;
|
|
834
|
+
out(w.markRefused(cause.message), "bad");
|
|
835
|
+
io.rows(region());
|
|
836
|
+
return false;
|
|
837
|
+
}
|
|
838
|
+
engaged = true;
|
|
839
|
+
return true;
|
|
840
|
+
};
|
|
841
|
+
const disengage = async () => {
|
|
842
|
+
if (!engaged) return;
|
|
843
|
+
engaged = false;
|
|
844
|
+
release?.();
|
|
845
|
+
release = void 0;
|
|
846
|
+
await closed?.catch(() => {
|
|
847
|
+
});
|
|
848
|
+
closed = void 0;
|
|
849
|
+
io.idle();
|
|
850
|
+
};
|
|
851
|
+
const close = async (line) => {
|
|
852
|
+
cur = void 0;
|
|
853
|
+
rows = [];
|
|
854
|
+
mark = void 0;
|
|
855
|
+
dropField();
|
|
856
|
+
io.fresh();
|
|
857
|
+
released = manual;
|
|
858
|
+
if (line) out(line, "info");
|
|
859
|
+
await disengage();
|
|
860
|
+
};
|
|
861
|
+
const letGo = async (line) => {
|
|
862
|
+
released = true;
|
|
863
|
+
mark = void 0;
|
|
864
|
+
dropField();
|
|
865
|
+
if (line) out(line, "warn");
|
|
866
|
+
await disengage();
|
|
867
|
+
io.rows(region());
|
|
868
|
+
};
|
|
869
|
+
const sync = async (open = false) => {
|
|
870
|
+
let next;
|
|
871
|
+
try {
|
|
872
|
+
const got = await source.waits();
|
|
873
|
+
auto = got.auto;
|
|
874
|
+
next = rowsOf(got.towers).filter((r) => pending(r.wait));
|
|
875
|
+
} catch (cause) {
|
|
876
|
+
if (open) out(w.read(cause.message), "bad");
|
|
877
|
+
return;
|
|
878
|
+
}
|
|
879
|
+
const was = cur;
|
|
880
|
+
rows = next;
|
|
881
|
+
if (was === void 0) {
|
|
882
|
+
if (!open && (manual || released || !rows.some((r) => r.tower.own))) return void (rows = []);
|
|
883
|
+
if (!rows.length) return void (open ? out(v.none, "info") : void 0);
|
|
884
|
+
return show(0);
|
|
885
|
+
}
|
|
886
|
+
const i = rows.findIndex((r) => keyOf(r) === was);
|
|
887
|
+
if (i >= 0) return stateOf(rows[i]) === drawn ? void (at = i) : show(i);
|
|
888
|
+
io.fresh();
|
|
889
|
+
if (!settled.has(was)) out(w.elsewhere(was.split("\n")[1]), "info");
|
|
890
|
+
if (!rows.length) return close(w.left);
|
|
891
|
+
show(at >= rows.length ? 0 : at);
|
|
892
|
+
};
|
|
893
|
+
const send = async (r, what) => {
|
|
894
|
+
const key = keyOf(r);
|
|
895
|
+
sending = true;
|
|
896
|
+
let refused = false;
|
|
897
|
+
try {
|
|
898
|
+
out(await what(), "ok");
|
|
899
|
+
} catch (cause) {
|
|
900
|
+
refused = true;
|
|
901
|
+
out(cause.message, "bad");
|
|
902
|
+
}
|
|
903
|
+
settled.add(key);
|
|
904
|
+
lastKey = io.now();
|
|
905
|
+
await sync();
|
|
906
|
+
sending = false;
|
|
907
|
+
if (refused && cur === key) rows.length > 1 ? show((at + 1) % rows.length) : (mark = void 0, io.rows(region()));
|
|
908
|
+
};
|
|
909
|
+
const press = (r, json, what) => send(r, async () => {
|
|
910
|
+
const got = await source.press(r.tower.workspace, json);
|
|
911
|
+
if (got.status !== 200) throw new Error(String(got.body?.reason ?? got.body?.error ?? got.status));
|
|
912
|
+
const name = r.wait.card?.from?.name ?? "", status2 = got.body?.press?.main?.status;
|
|
913
|
+
return m.pressed(r.wait.id, what, !name || !status2 ? "" : status2 === "delivered" || status2 === "queued" ? m.told(name) : m.notReached(name, status2));
|
|
914
|
+
});
|
|
915
|
+
const ask = (next, hint) => {
|
|
916
|
+
step = next;
|
|
917
|
+
out(hint, "ask");
|
|
918
|
+
io.field({ word: w.fields[next], plain: FIELD_PLAIN[next] });
|
|
919
|
+
};
|
|
920
|
+
const back = () => {
|
|
921
|
+
dropField();
|
|
922
|
+
mark = void 0;
|
|
923
|
+
io.rows(region());
|
|
924
|
+
};
|
|
925
|
+
return {
|
|
926
|
+
/** Whether the view shows the cards itself (it has a card on screen and the keys, or would draw one on its own); else the prompt prints the block of the digest, as where keys are not reported. */
|
|
927
|
+
shows: () => !released && (cur !== void 0 || !manual),
|
|
928
|
+
/** Whether the boss is at the cards (the mark is open): what is on the line is then never their words to the tower. */
|
|
929
|
+
engaged: () => engaged,
|
|
930
|
+
/** A field has the message line: its keys are text, and Esc and Enter are the view's. */
|
|
931
|
+
field: () => cur !== void 0 && step !== "card",
|
|
932
|
+
/** Whether this key, typed alone on an empty message line, is the view's: an arrow, Esc, or the number of a row. */
|
|
933
|
+
takes(name) {
|
|
934
|
+
const r = row();
|
|
935
|
+
if (!r || released || step !== "card") return false;
|
|
936
|
+
return CARD_KEYS.includes(name) || /^[1-9]$/.test(name) && Number(name) <= acts(r).length;
|
|
937
|
+
},
|
|
938
|
+
/** A card key: an arrow or a digit marks a row (the first one opens the pane's mark), left and right draw another card, Esc gives the keys back. Nothing here presses. */
|
|
939
|
+
key(name) {
|
|
940
|
+
if (!engaged && name !== "escape") opening = true;
|
|
941
|
+
return run2(async () => {
|
|
942
|
+
try {
|
|
943
|
+
if (!row() || released || step !== "card") return;
|
|
944
|
+
if (name === "escape") return await letGo();
|
|
945
|
+
if (!await engage()) return;
|
|
946
|
+
} finally {
|
|
947
|
+
opening = false;
|
|
948
|
+
}
|
|
949
|
+
const r = row();
|
|
950
|
+
if (!r) return;
|
|
951
|
+
lastKey = io.now();
|
|
952
|
+
const n = acts(r).length;
|
|
953
|
+
if (name === "left" || name === "right") return rows.length > 1 ? show((at + (name === "right" ? 1 : rows.length - 1)) % rows.length) : void 0;
|
|
954
|
+
if (/^\d$/.test(name) && Number(name) > n) return;
|
|
955
|
+
mark = name === "up" ? ((mark ?? n) + n - 1) % n : name === "down" ? ((mark ?? -1) + 1) % n : Number(name) - 1;
|
|
956
|
+
io.rows(region());
|
|
957
|
+
}, void 0);
|
|
958
|
+
},
|
|
959
|
+
/** The pane's mark is being opened: an Enter that arrives now is told to `entered` as `early`. */
|
|
960
|
+
opening: () => opening,
|
|
961
|
+
/** A key in a field: the boss is still answering. */
|
|
962
|
+
touch() {
|
|
963
|
+
lastKey = io.now();
|
|
964
|
+
},
|
|
965
|
+
/** Esc in a field: back to the card, nothing pressed. */
|
|
966
|
+
escape: () => run2(async () => void (step !== "card" && back()), void 0),
|
|
967
|
+
/** A typed key left text on the message line while a card had the keys: the boss is writing to the tower, so the mark of a row goes and the view is no longer engaged. (Pasted text leaves the mark: its Enter then ends the view, `entered`.) */
|
|
968
|
+
typed: () => cur !== void 0 && step === "card" && (engaged || mark !== void 0) ? run2(async () => {
|
|
969
|
+
if (step !== "card") return;
|
|
970
|
+
mark = void 0;
|
|
971
|
+
await disengage();
|
|
972
|
+
io.rows(region());
|
|
973
|
+
}, void 0) : void 0,
|
|
974
|
+
/**
|
|
975
|
+
* Enter: what was entered (`lines`, more than one or `pasted` for a paste). True when it was the view's: a marked row is pressed (or its field opened), a field's text is taken. False when it is the message line's: no card has the
|
|
976
|
+
* keys, or no row is marked (an empty Enter then wakes the tower, as ever). An Enter that came while a press was on its way, or while the pane's mark was still opening (`early`), is dropped: it is no press and no wake.
|
|
977
|
+
*/
|
|
978
|
+
entered(lines, pasted, early = false) {
|
|
979
|
+
const during = sending || early;
|
|
980
|
+
return run2(async () => {
|
|
981
|
+
const r = row();
|
|
982
|
+
if (!r || released) return false;
|
|
983
|
+
const text = lines.join("\n").trim();
|
|
984
|
+
if (step === "card") {
|
|
985
|
+
if (during) return true;
|
|
986
|
+
if (mark === void 0 || !engaged) return false;
|
|
987
|
+
if (pasted || text !== "") return letGo(w.pasted).then(() => true);
|
|
988
|
+
const act = acts(r)[mark]?.act;
|
|
989
|
+
if (!act) return false;
|
|
990
|
+
lastKey = io.now();
|
|
991
|
+
if (act.kind === "press") await press(r, act.json, act.what);
|
|
992
|
+
else if (act.kind === "skip") ask("reason", w.reason);
|
|
993
|
+
else if (act.kind === "reply") ask("reply", w.reply(towerWord(io.locale, r.tower)));
|
|
994
|
+
return true;
|
|
995
|
+
}
|
|
996
|
+
lastKey = io.now();
|
|
997
|
+
if (step === "reason") {
|
|
998
|
+
reason = lines.map((l) => l.trim()).filter(Boolean).join(" ");
|
|
999
|
+
ask("confirm", w.confirm(v.skipBody(towerWord(io.locale, r.tower), r.wait.card?.from?.name)));
|
|
1000
|
+
} else if (step === "confirm") {
|
|
1001
|
+
if (pasted) return letGo(w.pasted).then(() => true);
|
|
1002
|
+
const why = reason;
|
|
1003
|
+
if (text !== "" && !/^y(es)?$/i.test(text)) return back(), true;
|
|
1004
|
+
dropField();
|
|
1005
|
+
await press(r, { op: "press", id: r.wait.id, act: "decline", ...why ? { reason: why } : {} }, m.didDecline);
|
|
1006
|
+
} else if (text === "") back();
|
|
1007
|
+
else {
|
|
1008
|
+
dropField();
|
|
1009
|
+
await send(r, async () => v.replied2(towerWord(io.locale, r.tower), (await source.reply(r.tower, text, r.wait.id)).answered ?? "", r.wait.id));
|
|
1010
|
+
}
|
|
1011
|
+
return true;
|
|
1012
|
+
}, true);
|
|
1013
|
+
},
|
|
1014
|
+
/** The device has no boss at it (the poll says so): no card is shown on its own. */
|
|
1015
|
+
attend(attended) {
|
|
1016
|
+
manual = !attended;
|
|
1017
|
+
if (manual && cur === void 0) released = true;
|
|
1018
|
+
},
|
|
1019
|
+
/** The digest of a poll changed, or the boss is answering: the cards are read again. */
|
|
1020
|
+
sync: () => run2(() => sync(), void 0),
|
|
1021
|
+
/** `/wait`: the view takes the keys again, from the first card. */
|
|
1022
|
+
focus: () => run2(async () => {
|
|
1023
|
+
released = false;
|
|
1024
|
+
cur = void 0;
|
|
1025
|
+
io.fresh();
|
|
1026
|
+
await sync(true);
|
|
1027
|
+
if (cur === void 0) released = manual, await disengage();
|
|
1028
|
+
}, void 0),
|
|
1029
|
+
/**
|
|
1030
|
+
* Whether the boss is answering, for the poll: at the cards, with a card key in the last `QUIET_MS`. Past that the view is no longer engaged (the mark is dropped, a field and a mark with it), and the next card key engages again.
|
|
1031
|
+
*/
|
|
1032
|
+
answering() {
|
|
1033
|
+
if (!engaged) return false;
|
|
1034
|
+
if (io.now() - lastKey <= QUIET_MS) return true;
|
|
1035
|
+
void run2(async () => {
|
|
1036
|
+
if (!engaged || io.now() - lastKey <= QUIET_MS) return;
|
|
1037
|
+
mark = void 0;
|
|
1038
|
+
dropField();
|
|
1039
|
+
await disengage();
|
|
1040
|
+
io.rows(region());
|
|
1041
|
+
}, void 0);
|
|
1042
|
+
return false;
|
|
1043
|
+
},
|
|
1044
|
+
/** The prompt posts the boss's words: they are no longer at the cards. */
|
|
1045
|
+
wrote: () => run2(async () => {
|
|
1046
|
+
if (!engaged && mark === void 0) return;
|
|
1047
|
+
mark = void 0;
|
|
1048
|
+
await disengage();
|
|
1049
|
+
}, void 0),
|
|
1050
|
+
/** The view ends without a word (the prompt leaves, or a run starts): the mark is dropped. */
|
|
1051
|
+
stop: () => run2(async () => {
|
|
1052
|
+
cur = void 0;
|
|
1053
|
+
rows = [];
|
|
1054
|
+
step = "card";
|
|
1055
|
+
await disengage();
|
|
1056
|
+
}, void 0),
|
|
1057
|
+
/** An operation that is under way (a press whose answer is not in yet) is waited for, so its result is printed before the prompt leaves. */
|
|
1058
|
+
whenIdle: () => busy
|
|
1059
|
+
};
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
// src/towerprompt/look.ts
|
|
1063
|
+
var PLAIN = { color: false, ascii: true, clear: false };
|
|
1064
|
+
var on = (value) => /^(1|true|yes|on)$/i.test(value ?? "");
|
|
1065
|
+
var off = (value) => /^(0|false|no|off)$/i.test(value ?? "");
|
|
1066
|
+
function lookOf(env, terminal) {
|
|
1067
|
+
const ascii = on(env.STRAW_BOSS_ASCII) || !terminal && !off(env.STRAW_BOSS_ASCII);
|
|
1068
|
+
if (!terminal) return { color: false, ascii, clear: false };
|
|
1069
|
+
const dumb = env.TERM === "dumb";
|
|
1070
|
+
return { color: !dumb && !env.NO_COLOR, ascii, clear: !dumb };
|
|
1071
|
+
}
|
|
1072
|
+
var TOWER = "\u{F043B}";
|
|
1073
|
+
var CROWN = "\u{F01A5}";
|
|
1074
|
+
var RESET = "\x1B[0m";
|
|
1075
|
+
var CLEAR = "\x1B[H\x1B[2J";
|
|
1076
|
+
var SGR = { bold: "1", dim: "2", accent: "36", ok: "32", warn: "33", bad: "31", run: "34;1" };
|
|
1077
|
+
var KINDS = {
|
|
1078
|
+
info: { glyph: "\u25D0", ascii: "*", style: "accent" },
|
|
1079
|
+
ok: { glyph: "\u2713", ascii: "+", style: "ok" },
|
|
1080
|
+
warn: { glyph: "!", ascii: "!", style: "warn" },
|
|
1081
|
+
bad: { glyph: "\u2717", ascii: "x", style: "bad" },
|
|
1082
|
+
ask: { glyph: "?", ascii: "?", style: "warn" }
|
|
1083
|
+
};
|
|
1084
|
+
var LEAVE = { glyph: "\u21A9", ascii: "<" };
|
|
1085
|
+
var STATES = {
|
|
1086
|
+
standing: { glyph: "\u25CF", ascii: "o", style: "ok" },
|
|
1087
|
+
waking: { glyph: "\u25D0", ascii: "~", style: "warn" },
|
|
1088
|
+
running: { glyph: "\u25B6", ascii: ">", style: "run" },
|
|
1089
|
+
closing: { glyph: "\u25D1", ascii: "~", style: "warn" },
|
|
1090
|
+
held: { glyph: "\u25A0", ascii: "!", style: "bad" }
|
|
1091
|
+
};
|
|
1092
|
+
var UNKNOWN_STATE = { glyph: "\u25CF", ascii: "?", style: "dim" };
|
|
1093
|
+
var cells = (text) => [...text].reduce((n, ch) => n + (ch.codePointAt(0) >= 11904 ? 2 : 1), 0);
|
|
1094
|
+
function draw(look) {
|
|
1095
|
+
const paint = (style, text) => look.color && text ? `\x1B[${style.map((s) => SGR[s]).join(";")}m${text}${RESET}` : text;
|
|
1096
|
+
const mark = (glyph, ascii) => look.ascii ? ascii : glyph;
|
|
1097
|
+
return {
|
|
1098
|
+
/** ` tower · Alpha`, bold; the head tower's title carries the crown. */
|
|
1099
|
+
title: (text, head = false) => `${paint(["accent"], mark(head ? CROWN : TOWER, "#"))} ${paint(["bold"], look.ascii ? text.replace(" \xB7 ", " - ") : text)}`,
|
|
1100
|
+
/** `● standing by`: the dot in the colour of the state, the words dim for a tower that stands by and plain otherwise. */
|
|
1101
|
+
state: (state, words) => {
|
|
1102
|
+
const s = (Object.hasOwn(STATES, state) ? STATES[state] : void 0) ?? UNKNOWN_STATE;
|
|
1103
|
+
return `${paint([s.style], mark(s.glyph, s.ascii))} ${state === "standing" ? paint(["dim"], words) : words}`;
|
|
1104
|
+
},
|
|
1105
|
+
/** A line between the states. */
|
|
1106
|
+
note: (kind, text) => {
|
|
1107
|
+
const k = KINDS[kind];
|
|
1108
|
+
return `${paint([k.style], mark(k.glyph, k.ascii))} ${text}`;
|
|
1109
|
+
},
|
|
1110
|
+
/** The line that says the boss took the pane. */
|
|
1111
|
+
leave: (text) => `${paint(["dim"], mark(LEAVE.glyph, LEAVE.ascii))} ${paint(["dim"], text)}`,
|
|
1112
|
+
/** The key rows under the state, the keys in the accent colour and in one column, the words dim. */
|
|
1113
|
+
hints: (rows) => {
|
|
1114
|
+
const keys = rows.map((r) => look.ascii ? r.plain : r.keys), width = Math.max(...keys.map(cells));
|
|
1115
|
+
return rows.map((r, i) => ` ${paint(["accent"], keys[i])}${" ".repeat(width - cells(keys[i]))} ${paint(["dim"], r.text)}`);
|
|
1116
|
+
},
|
|
1117
|
+
/** The string readline writes before the line the boss types. */
|
|
1118
|
+
prompt: () => look.ascii ? "tower> " : paint(["accent", "bold"], `${TOWER} \u276F `),
|
|
1119
|
+
/** The prompt string of the pick mode (`wait ❯`; `wait> ` in ASCII, whatever the language: nothing outside ASCII is drawn). */
|
|
1120
|
+
pickPrompt: (word) => look.ascii ? "wait> " : paint(["accent", "bold"], `${word} \u276F `),
|
|
1121
|
+
/** The prompt string of a field of the card view (`原因 ❯`; in ASCII its English word, `reason> `). */
|
|
1122
|
+
fieldPrompt: (word, plain3) => look.ascii ? `${plain3}> ` : paint(["accent", "bold"], `${word} \u276F `),
|
|
1123
|
+
/** The mark before the row the boss has marked in the card view. */
|
|
1124
|
+
pointer: () => look.ascii ? ">" : "\u276F",
|
|
1125
|
+
/** A row of the card view: the marked one in the accent colour, the key hint dim. */
|
|
1126
|
+
row: (text, kind) => kind === "marked" ? paint(["accent", "bold"], text) : kind === "hint" ? paint(["dim"], text) : text
|
|
1127
|
+
};
|
|
723
1128
|
}
|
|
724
1129
|
|
|
725
1130
|
// src/towerprompt/pick.ts
|
|
@@ -755,12 +1160,12 @@ var PICK = {
|
|
|
755
1160
|
unreplied: (tower, answered, id) => `\u5DF2\u4EA4\u7D66${tower}\u3002${answered === id ? `${id} \u5DF2\u6A19\u70BA\u56DE\u8986\u3002` : `${id} \u6C92\u6709\u6A19\u70BA\u56DE\u8986\uFF1A\u8A18\u61B6\u88E1\u5DF2\u7D93\u6C92\u6709\u9019\u500B\u7B49\u4F60\u7684\u7B49\u5F85\u3002`}`
|
|
756
1161
|
}
|
|
757
1162
|
};
|
|
758
|
-
var
|
|
1163
|
+
var safe2 = (text) => text.split("\n").map((l) => l.replace(/[\u0000-\u001f\u007f-\u009f\u200b-\u200f\u2028-\u202e\u2060-\u206f\ufeff]/g, ""));
|
|
759
1164
|
function createPick(io) {
|
|
760
1165
|
const w = PICK[io.locale], v = WORDS[io.locale], m = memoryWords(io.locale);
|
|
761
1166
|
const source = io.source;
|
|
762
1167
|
let state = "off", rows = [], auto = false, step = { at: "list" }, release, closed, busy = Promise.resolve();
|
|
763
|
-
const out = (text, kind = "plain") => void
|
|
1168
|
+
const out = (text, kind = "plain") => void safe2(text).forEach((line, i) => io.say(line, i === 0 ? kind : "plain"));
|
|
764
1169
|
const numbered = (labels) => labels.forEach((label, i) => out(` ${i + 1}. ${label}`));
|
|
765
1170
|
const open = async () => {
|
|
766
1171
|
const held = new Promise((resolve4) => release = resolve4);
|
|
@@ -914,38 +1319,6 @@ function createPick(io) {
|
|
|
914
1319
|
};
|
|
915
1320
|
}
|
|
916
1321
|
|
|
917
|
-
// src/towerprompt/waitblock.ts
|
|
918
|
-
var BLOCK_MAX = 5;
|
|
919
|
-
var CONTROLS = /[\u0000-\u001f\u007f-\u009f\u200b-\u200f\u2028-\u202e\u2060-\u206f\ufeff]/g;
|
|
920
|
-
var plain2 = (value, max = 200) => String(value ?? "").replace(CONTROLS, "").slice(0, max);
|
|
921
|
-
var WORDS2 = {
|
|
922
|
-
en: {
|
|
923
|
-
title: (n) => `waits for you (${n})`,
|
|
924
|
-
more: (n, command) => `${n} more: ${command}`,
|
|
925
|
-
from: (who) => `from ${who}`,
|
|
926
|
-
parked: "parked",
|
|
927
|
-
type: { action: "action", choice: "choice", secret: "secret", legacy: "old" },
|
|
928
|
-
press: (command) => `${command} press a wait card`
|
|
929
|
-
},
|
|
930
|
-
"zh-TW": {
|
|
931
|
-
title: (n) => `\u7B49\u4F60\u8655\u7406\uFF08${n}\uFF09`,
|
|
932
|
-
more: (n, command) => `\u9084\u6709 ${n} \u5F35\uFF1A${command}`,
|
|
933
|
-
from: (who) => `\u7531 ${who}`,
|
|
934
|
-
parked: "\u5DF2\u66AB\u653E",
|
|
935
|
-
type: { action: "\u52D5\u4F5C", choice: "\u9078\u64C7", secret: "\u5BC6\u9470", legacy: "\u820A\u5F0F" },
|
|
936
|
-
press: (command) => `${command} \u8655\u7406\u7B49\u4F60\u7684\u5361\u7247`
|
|
937
|
-
}
|
|
938
|
-
};
|
|
939
|
-
function waitBlock(rows, locale, command) {
|
|
940
|
-
if (!rows.length) return [];
|
|
941
|
-
const w = WORDS2[locale];
|
|
942
|
-
const lines = rows.slice(0, BLOCK_MAX).map((r) => {
|
|
943
|
-
const parts = [plain2(r.do, 120), ...r.from ? [w.from(plain2(r.from, 60))] : [], ...r.parked ? [w.parked] : []];
|
|
944
|
-
return ` ${plain2(r.id, 12)} ${w.type[plain2(r.type, 20)] ?? plain2(r.type, 20)} \xB7 ${parts.join(" \xB7 ")}`;
|
|
945
|
-
});
|
|
946
|
-
return [w.title(rows.length), ...lines, ...rows.length > BLOCK_MAX ? [` ${w.more(rows.length - BLOCK_MAX, command)}`] : []];
|
|
947
|
-
}
|
|
948
|
-
|
|
949
1322
|
// src/towerprompt/prompt.ts
|
|
950
1323
|
var POLL_MS = 2e3;
|
|
951
1324
|
var WAKING_POLL_MS = 250;
|
|
@@ -977,6 +1350,10 @@ async function runPrompt(o) {
|
|
|
977
1350
|
const terminal = o.output.isTTY === true;
|
|
978
1351
|
const look = o.look ?? (o.env ? lookOf(o.env, terminal) : PLAIN), d = draw({ ...look, color: look.color && terminal, clear: look.clear && terminal });
|
|
979
1352
|
let begun = false;
|
|
1353
|
+
let region = [];
|
|
1354
|
+
let lone = false, lineBefore = "";
|
|
1355
|
+
let polling = false, again = false, ready = false, syncFirst = false;
|
|
1356
|
+
let early = false;
|
|
980
1357
|
let shownWaits, sawPaste = false;
|
|
981
1358
|
let rl, closed = false, finished = false, posting = false, silent = false, shown, shownLabel, wokeAt;
|
|
982
1359
|
let timer, batch = [], batchTimer, pasteTimer, pasting = false;
|
|
@@ -994,15 +1371,61 @@ async function runPrompt(o) {
|
|
|
994
1371
|
if (terminal && rl && !closed && !finished) rl.prompt(true);
|
|
995
1372
|
}
|
|
996
1373
|
});
|
|
997
|
-
const say = (line,
|
|
1374
|
+
const say = (line, again2 = true) => {
|
|
1375
|
+
const live = again2 && terminal && rl !== void 0 && !closed && !finished;
|
|
1376
|
+
if (live && region.length && rl.getCursorPos().rows === 0) {
|
|
1377
|
+
o.output.write(`\x1B[${region.length}A\r\x1B[2K${line}
|
|
1378
|
+
${region.map((l) => `\x1B[2K${l}
|
|
1379
|
+
`).join("")}`);
|
|
1380
|
+
return rl.prompt(true);
|
|
1381
|
+
}
|
|
1382
|
+
region = [];
|
|
998
1383
|
if (terminal) {
|
|
999
1384
|
cursorTo(o.output, 0);
|
|
1000
1385
|
clearLine(o.output, 0);
|
|
1001
1386
|
}
|
|
1002
1387
|
o.output.write(`${line}
|
|
1003
1388
|
`);
|
|
1004
|
-
if (
|
|
1389
|
+
if (live) rl.prompt(true);
|
|
1390
|
+
};
|
|
1391
|
+
const drawRows = (lines) => {
|
|
1392
|
+
if (!lines.length) return void (region = []);
|
|
1393
|
+
if (region.length === lines.length && rl && !closed && !finished && rl.getCursorPos().rows === 0) {
|
|
1394
|
+
o.output.write(`\x1B[${lines.length}A\r${lines.map((l) => `\x1B[2K${l}
|
|
1395
|
+
`).join("")}`);
|
|
1396
|
+
rl.prompt(true);
|
|
1397
|
+
} else {
|
|
1398
|
+
region = [];
|
|
1399
|
+
for (const line of lines) say(line);
|
|
1400
|
+
}
|
|
1401
|
+
region = terminal && rl && !closed && !finished ? lines : [];
|
|
1005
1402
|
};
|
|
1403
|
+
const soon = () => {
|
|
1404
|
+
if (finished) return;
|
|
1405
|
+
if (polling) return void (again = true);
|
|
1406
|
+
if (timer) clock.cancel(timer);
|
|
1407
|
+
timer = clock.after(() => void poll(), 0);
|
|
1408
|
+
};
|
|
1409
|
+
const keys = terminal && o.input.isTTY === true && o.env?.TERM !== "dumb" && (o.platform ?? process.platform) !== "win32";
|
|
1410
|
+
const cards = keys && source.pick ? createCardView({
|
|
1411
|
+
source: source.pick,
|
|
1412
|
+
locale: o.locale,
|
|
1413
|
+
ascii: look.ascii,
|
|
1414
|
+
pointer: d.pointer(),
|
|
1415
|
+
paint: d.row,
|
|
1416
|
+
now: () => clock.now(),
|
|
1417
|
+
columns: () => o.output.columns ?? 80,
|
|
1418
|
+
say: (line, kind = "plain") => say(kind === "plain" ? line : d.note(kind, line)),
|
|
1419
|
+
rows: drawRows,
|
|
1420
|
+
fresh: () => void (region = []),
|
|
1421
|
+
field: (f) => {
|
|
1422
|
+
if (!rl || closed || finished) return;
|
|
1423
|
+
rl.setPrompt(f ? d.fieldPrompt(f.word, f.plain) : d.prompt());
|
|
1424
|
+
if (!f && rl.line !== "") rl.write(null, { ctrl: true, name: "e" }), rl.write(null, { ctrl: true, name: "u" });
|
|
1425
|
+
rl.prompt(true);
|
|
1426
|
+
},
|
|
1427
|
+
idle: soon
|
|
1428
|
+
}) : void 0;
|
|
1006
1429
|
const begin = () => {
|
|
1007
1430
|
if (begun) return;
|
|
1008
1431
|
begun = true;
|
|
@@ -1017,12 +1440,13 @@ async function runPrompt(o) {
|
|
|
1017
1440
|
rl?.close();
|
|
1018
1441
|
try {
|
|
1019
1442
|
await pick.stop();
|
|
1443
|
+
await cards?.stop();
|
|
1020
1444
|
await after();
|
|
1021
1445
|
} catch {
|
|
1022
1446
|
}
|
|
1023
1447
|
finish(0);
|
|
1024
1448
|
};
|
|
1025
|
-
const lineOf = (a) => ({ state: clean(a.state), words: text.state({ state: clean(a.state), queued: Number.isFinite(a.queued) ? a.queued : 0 }) });
|
|
1449
|
+
const lineOf = (a) => ({ state: clean(a.state), words: `${text.state({ state: clean(a.state), queued: Number.isFinite(a.queued) ? a.queued : 0 })}${a.holding === true ? text.holding : ""}` });
|
|
1026
1450
|
const handle = async (polled) => {
|
|
1027
1451
|
if (polled.kind === "gone") {
|
|
1028
1452
|
say(d.note("warn", text.noTower), false);
|
|
@@ -1042,8 +1466,9 @@ async function runPrompt(o) {
|
|
|
1042
1466
|
silent = false;
|
|
1043
1467
|
if (polled.answer.leave) {
|
|
1044
1468
|
if (posting) return true;
|
|
1045
|
-
const picking = pick.active();
|
|
1046
|
-
if (
|
|
1469
|
+
const picking = pick.active() || (cards?.engaged() ?? false);
|
|
1470
|
+
if (pick.active()) await pick.whenIdle();
|
|
1471
|
+
await cards?.whenIdle();
|
|
1047
1472
|
const entered = [...batch, rl?.line ?? ""];
|
|
1048
1473
|
if (entered.at(-1) === "" && entered.length > 1) entered.pop();
|
|
1049
1474
|
const words = picking || commandOf(entered) !== void 0 ? "" : entered.join("\n").trim();
|
|
@@ -1081,7 +1506,14 @@ ${now.words}`;
|
|
|
1081
1506
|
say(d.state(now.state, now.words));
|
|
1082
1507
|
}
|
|
1083
1508
|
const waits = Array.isArray(polled.answer.waits) ? polled.answer.waits : [], key = JSON.stringify(waits);
|
|
1084
|
-
|
|
1509
|
+
cards?.attend(polled.answer.unattended !== true);
|
|
1510
|
+
if (cards?.shows()) {
|
|
1511
|
+
if (key !== shownWaits || cards.engaged()) {
|
|
1512
|
+
shownWaits = key;
|
|
1513
|
+
if (ready) void cards.sync();
|
|
1514
|
+
else syncFirst = true;
|
|
1515
|
+
}
|
|
1516
|
+
} else if (key !== shownWaits && !pick.active()) {
|
|
1085
1517
|
const first2 = shownWaits === void 0;
|
|
1086
1518
|
shownWaits = key;
|
|
1087
1519
|
if (waits.length) waitBlock(waits, o.locale, "/wait").forEach((l, i) => say(i === 0 ? d.note("ask", l) : l));
|
|
@@ -1091,9 +1523,15 @@ ${now.words}`;
|
|
|
1091
1523
|
};
|
|
1092
1524
|
const poll = async () => {
|
|
1093
1525
|
if (finished) return;
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1526
|
+
polling = true;
|
|
1527
|
+
try {
|
|
1528
|
+
const polled = await source.poll(cards?.answering() ?? false).catch((cause) => ({ kind: "wait", why: cause.message }));
|
|
1529
|
+
if (finished || !await handle(polled)) return;
|
|
1530
|
+
} finally {
|
|
1531
|
+
polling = false;
|
|
1532
|
+
}
|
|
1533
|
+
timer = clock.after(() => void poll(), again ? 0 : nextPoll());
|
|
1534
|
+
again = false;
|
|
1097
1535
|
};
|
|
1098
1536
|
const nextPoll = () => wokeAt !== void 0 ? wakingMs : pollMs;
|
|
1099
1537
|
const flush = async () => {
|
|
@@ -1102,14 +1540,19 @@ ${now.words}`;
|
|
|
1102
1540
|
const pasted = sawPaste || lines.length > 1;
|
|
1103
1541
|
sawPaste = false;
|
|
1104
1542
|
if (pick.active()) return void (pasted ? await pick.paste() : await pick.line(lines[0] ?? ""));
|
|
1543
|
+
const wasEarly = early;
|
|
1544
|
+
early = false;
|
|
1545
|
+
if (cards && await cards.entered(lines, pasted, wasEarly)) return;
|
|
1546
|
+
if (finished) return;
|
|
1105
1547
|
const word = commandOf(lines);
|
|
1106
1548
|
if (word !== void 0) {
|
|
1107
1549
|
if (word === "/new") return void await startOver();
|
|
1108
1550
|
if (word === "/quit") return leave();
|
|
1109
|
-
if (/^\/(?:boss )?wait$/.test(word.replace(/\s+/g, " "))) return void await pick.start();
|
|
1551
|
+
if (/^\/(?:boss )?wait$/.test(word.replace(/\s+/g, " "))) return void (cards ? await cards.focus() : await pick.start());
|
|
1110
1552
|
return void say(d.note("ask", text.unknownCommand(clean(word.split(/\s+/)[0], 40))));
|
|
1111
1553
|
}
|
|
1112
1554
|
const message = lines.join("\n").trim() || NO_MESSAGE;
|
|
1555
|
+
void cards?.wrote();
|
|
1113
1556
|
posting = true;
|
|
1114
1557
|
try {
|
|
1115
1558
|
await source.post(message);
|
|
@@ -1158,10 +1601,12 @@ ${now.words}`;
|
|
|
1158
1601
|
const joins = pasting || near && (typed ? unfinished : true);
|
|
1159
1602
|
if (!joins) chunks++;
|
|
1160
1603
|
if (decoded !== "") unfinished = typed && !joins ? false : !/[\r\n]$/.test(decoded);
|
|
1604
|
+
lone = typed && !joins;
|
|
1605
|
+
lineBefore = rl?.line ?? "";
|
|
1161
1606
|
lastDataAt = at;
|
|
1162
1607
|
if (pasting) armPasteCap();
|
|
1163
1608
|
});
|
|
1164
|
-
rl = createInterface({ input: o.input, output: o.output, prompt: d.prompt(), terminal });
|
|
1609
|
+
rl = createInterface({ input: o.input, output: o.output, prompt: d.prompt(), terminal, ...o.escapeMs !== void 0 ? { escapeCodeTimeout: o.escapeMs } : {} });
|
|
1165
1610
|
const armFlush = () => {
|
|
1166
1611
|
clearTimeout(batchTimer);
|
|
1167
1612
|
if (pasting) return;
|
|
@@ -1176,8 +1621,11 @@ ${now.words}`;
|
|
|
1176
1621
|
clearTimeout(pasteTimer);
|
|
1177
1622
|
pasteTimer = setTimeout(endPaste, pasteCapMs);
|
|
1178
1623
|
};
|
|
1624
|
+
o.output.on("resize", () => void (region = []));
|
|
1179
1625
|
rl.on("line", (raw) => {
|
|
1626
|
+
region = [];
|
|
1180
1627
|
if (finished || posting) return;
|
|
1628
|
+
if (cards?.opening()) early = true;
|
|
1181
1629
|
batch.push(raw);
|
|
1182
1630
|
lineChunk = chunks;
|
|
1183
1631
|
armFlush();
|
|
@@ -1190,7 +1638,21 @@ ${now.words}`;
|
|
|
1190
1638
|
clearTimeout(batchTimer);
|
|
1191
1639
|
armPasteCap();
|
|
1192
1640
|
} else if (key?.name === PASTE_END) endPaste();
|
|
1641
|
+
else if (cards && !pick.active()) cardKey(key);
|
|
1193
1642
|
});
|
|
1643
|
+
const cardKey = (key) => {
|
|
1644
|
+
const line = () => rl?.line ?? "";
|
|
1645
|
+
if (key?.ctrl && key.name === "l") region = [];
|
|
1646
|
+
const name = key?.name === "escape" ? "escape" : key?.ctrl || key?.meta ? "" : /^\d$/.test(key?.sequence ?? "") ? key.sequence : key?.name ?? "";
|
|
1647
|
+
if (!lone || pasting) return;
|
|
1648
|
+
if (cards.field()) return void (name === "escape" ? cards.escape() : cards.touch());
|
|
1649
|
+
if (lineBefore === "" && cards.takes(name)) {
|
|
1650
|
+
if (name === "up" && line() !== "") rl.write(null, { name: "down" });
|
|
1651
|
+
if (line() !== "") rl.write(null, { ctrl: true, name: "u" });
|
|
1652
|
+
return void cards.key(name);
|
|
1653
|
+
}
|
|
1654
|
+
if (line() !== "") void cards.typed();
|
|
1655
|
+
};
|
|
1194
1656
|
const leave = () => {
|
|
1195
1657
|
if (finished) return;
|
|
1196
1658
|
batch = [];
|
|
@@ -1200,6 +1662,8 @@ ${now.words}`;
|
|
|
1200
1662
|
rl.on("close", () => void (closed = true, leave()));
|
|
1201
1663
|
rl.on("SIGINT", leave);
|
|
1202
1664
|
rl.prompt();
|
|
1665
|
+
ready = true;
|
|
1666
|
+
if (syncFirst && !finished) void cards?.sync();
|
|
1203
1667
|
if (!finished) timer = clock.after(() => void poll(), pollMs);
|
|
1204
1668
|
return done;
|
|
1205
1669
|
}
|
|
@@ -1283,6 +1747,7 @@ function serverSource(ctx, workspace, pid = process.pid, pane = process.env.HERD
|
|
|
1283
1747
|
return reply;
|
|
1284
1748
|
};
|
|
1285
1749
|
const here = createHereMark({ ask: (request) => ask(request, TELL_MS).then((reply) => ({ ok: reply.ok === true, ...reply }), (cause) => ({ ok: false, error: cause instanceof Absent ? "no server" : cause.message })), pane, every });
|
|
1750
|
+
let head = false;
|
|
1286
1751
|
const pick = {
|
|
1287
1752
|
waits: async () => {
|
|
1288
1753
|
const reply = await sure({ cmd: "boss-waits", own: workspace });
|
|
@@ -1293,22 +1758,23 @@ function serverSource(ctx, workspace, pid = process.pid, pane = process.env.HERD
|
|
|
1293
1758
|
return { ...typeof reply.status === "number" ? { status: reply.status } : {}, ...typeof reply.body === "object" && reply.body !== null ? { body: reply.body } : {} };
|
|
1294
1759
|
},
|
|
1295
1760
|
reply: async (tower, text, answers2) => {
|
|
1296
|
-
const reply = await sure(tower.head ? { cmd: "head-event", text, answers: answers2 } : { cmd: "tower-event", workspace: tower.workspace, text, present: true, answers: answers2 });
|
|
1761
|
+
const reply = await sure(tower.head ? { cmd: "head-event", text, answers: answers2, ...head ? { door: "pane" } : {} } : { cmd: "tower-event", workspace: tower.workspace, text, present: true, answers: answers2, ...tower.workspace === workspace ? { door: "pane" } : {} });
|
|
1297
1762
|
return typeof reply.answered === "string" ? { answered: reply.answered } : {};
|
|
1298
1763
|
},
|
|
1299
1764
|
here
|
|
1300
1765
|
};
|
|
1301
1766
|
return {
|
|
1302
1767
|
pick,
|
|
1303
|
-
async poll() {
|
|
1768
|
+
async poll(answering = false) {
|
|
1304
1769
|
let reply;
|
|
1305
1770
|
try {
|
|
1306
|
-
reply = await ask({ cmd: "prompt-poll", workspace, pid }, POLL_MS3);
|
|
1771
|
+
reply = await ask({ cmd: "prompt-poll", workspace, pid, ...answering ? { answering: true } : {} }, POLL_MS3);
|
|
1307
1772
|
} catch (cause) {
|
|
1308
1773
|
return { kind: "wait", why: cause.message };
|
|
1309
1774
|
}
|
|
1310
1775
|
if (reply.ok === true && typeof reply.label === "string" && typeof reply.state === "string" && typeof reply.queued === "number" && typeof reply.leave === "boolean") {
|
|
1311
|
-
|
|
1776
|
+
head = reply.head === true;
|
|
1777
|
+
return { kind: "state", answer: { label: reply.label, state: reply.state, queued: reply.queued, leave: reply.leave, ...reply.head === true ? { head: true } : {}, ...Array.isArray(reply.waits) ? { waits: reply.waits } : {}, ...typeof reply.auto === "boolean" ? { auto: reply.auto } : {}, ...reply.holding === true ? { holding: true } : {}, ...reply.unattended === true ? { unattended: true } : {} } };
|
|
1312
1778
|
}
|
|
1313
1779
|
const error = String(reply.error ?? "no usable answer");
|
|
1314
1780
|
return reply.gone === true ? { kind: "gone", error } : { kind: "wait", why: error };
|
|
@@ -1316,7 +1782,7 @@ function serverSource(ctx, workspace, pid = process.pid, pane = process.env.HERD
|
|
|
1316
1782
|
async post(text) {
|
|
1317
1783
|
let reply;
|
|
1318
1784
|
try {
|
|
1319
|
-
reply = await ask({ cmd: "tower-event", workspace, text, present: true }, POST_MS);
|
|
1785
|
+
reply = await ask({ cmd: "tower-event", workspace, text, present: true, door: "pane" }, POST_MS);
|
|
1320
1786
|
} catch (cause) {
|
|
1321
1787
|
throw new Error(cause instanceof Unreachable ? `the sb server did not answer: ${cause.message}` : cause.message);
|
|
1322
1788
|
}
|