Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Commit 2f06f3c

Browse files
committed
fix: AskingTerminal can drop chunks coming from madwizard over raw stdout
re: the ||'' in Ask.tsx, this is just being extra cautious
1 parent 2d73fe3 commit 2f06f3c

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

plugins/plugin-madwizard/components/src/Ask.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ export default class AskUI extends React.PureComponent<Props, State> {
215215
}
216216

217217
private justTheMessage(choice: Ask['prompt']['choices'][number]) {
218-
return stripAnsi(choice.message).replace(' ◄ prior choice', '')
218+
return stripAnsi(choice.message || '').replace(' ◄ prior choice', '')
219219
}
220220

221221
/** User has clicked on a simple list item */
@@ -291,7 +291,7 @@ export default class AskUI extends React.PureComponent<Props, State> {
291291
{message !== _.name && (
292292
<div>
293293
<Ansi noWrap="normal" className="sans-serif">
294-
{_.message.split(/\n/).slice(-2)[0]}
294+
{(_.message || '').split(/\n/).slice(-2)[0]}
295295
</Ansi>
296296
</div>
297297
)}
@@ -351,7 +351,7 @@ export default class AskUI extends React.PureComponent<Props, State> {
351351

352352
// options other than the "suggested" (i.e. prior choice)
353353
const others = ask.prompt.choices
354-
.filter(_ => _.name !== this.state?.userSelection && (!filter || pattern.test(_.message)))
354+
.filter(_ => _.name !== this.state?.userSelection && (!filter || pattern.test(_.message || '')))
355355
.map(_ => this._selectOptionForChoice(_))
356356

357357
// ugh, a bit of syntactic garbage here to make typescript
@@ -406,7 +406,7 @@ export default class AskUI extends React.PureComponent<Props, State> {
406406

407407
// is every message the same as the title?
408408
const isSimplistic = ask.prompt.choices.every(
409-
_ => _.name === stripAnsi(_.message).replace(' ◄ you selected this last time', '')
409+
_ => _.name === stripAnsi(_.message || '').replace(' ◄ prior choice', '')
410410
)
411411

412412
return (

plugins/plugin-madwizard/components/src/AskingTerminal.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ import SelectedProfileTerminal from './SelectedProfileTerminal'
2929

3030
import '../../web/scss/components/Allotment/_index.scss'
3131

32+
/** Be extra cautious parsing raw messages from madwizard */
33+
function tryToParseJson(str: string) {
34+
try {
35+
return JSON.parse(str)
36+
} catch (err) {
37+
console.error('Suspicious raw message from madwizard', str)
38+
return JSON.parse(stripAnsi(str))
39+
}
40+
}
41+
3242
export type AskingProps = {
3343
terminalProps: Pick<RestartableProps, 'tab' | 'REPL' | 'onExit' | 'searchable' | 'fontSizeAdjust'>
3444

@@ -132,7 +142,7 @@ export default class AskingTerminal extends React.PureComponent<Props, State> {
132142
const str = (this.leftover + line.slice(idx1 < 0 ? 0 : idx1, idx2)).slice(this.rawPrefix.length + 1)
133143
this.leftover = ''
134144
try {
135-
const msg = JSON.parse(stripAnsi(str)) as
145+
const msg = tryToParseJson(str) as
136146
| { type: 'qa-done' }
137147
| { type: 'all-done'; success: boolean }
138148
| { type: 'ask'; ask: Prompts.Prompt }
@@ -155,7 +165,9 @@ export default class AskingTerminal extends React.PureComponent<Props, State> {
155165
console.error('Error parsing line', idx1, idx2, line, '|||', str, err)
156166
}
157167
} else {
158-
this.leftover += idx1 === 0 ? line : line.slice(idx1)
168+
// we haven't reached the end of line, so stash what we have
169+
// so far in this.leftover
170+
this.leftover += idx1 <= 0 ? line : line.slice(idx1)
159171
}
160172
return null
161173
} else {

0 commit comments

Comments
 (0)