|
13 | 13 | let encoding = '' |
14 | 14 | let stdin = '' |
15 | 15 | let child |
| 16 | + let output = [] |
16 | 17 |
|
17 | 18 | function _getEnv() { |
18 | 19 | return env.split(' ').reduce((env, clause) => { |
|
26 | 27 |
|
27 | 28 | function spawn() { |
28 | 29 | child = null |
| 30 | + output = [] |
29 | 31 | const command = new Command(cmd, [...args, script], { |
30 | 32 | cwd: cwd || null, |
31 | 33 | env: _getEnv(), |
32 | | - encoding, |
| 34 | + encoding: encoding || null |
33 | 35 | }) |
34 | 36 |
|
35 | 37 | command.on('close', (data) => { |
|
40 | 42 | }) |
41 | 43 | command.on('error', (error) => onMessage(`command error: "${error}"`)) |
42 | 44 |
|
43 | | - command.stdout.on('data', (line) => onMessage(`command stdout: "${line}"`)) |
44 | | - command.stderr.on('data', (line) => onMessage(`command stderr: "${line}"`)) |
| 45 | + function onOutput(line, kind) { |
| 46 | + onMessage(`command ${kind}: "${line}"`) |
| 47 | +
|
| 48 | + if (line.endsWith('\n')) { |
| 49 | + line = line.substring(0, line.length - 1) |
| 50 | + } |
| 51 | +
|
| 52 | + const last = output[output.length - 1] |
| 53 | + if (last && last.endsWith('\r')) { |
| 54 | + output = [...output.slice(0, output.length - 1), line] |
| 55 | + } else { |
| 56 | + output = [...output, line] |
| 57 | + } |
| 58 | + } |
| 59 | +
|
| 60 | + command.stdout.on('data', (line) => onOutput(line, 'stdout')) |
| 61 | + command.stderr.on('data', (line) => onOutput(line, 'stderr')) |
45 | 62 |
|
46 | 63 | command |
47 | 64 | .spawn() |
|
90 | 107 | </div> |
91 | 108 | <div class="flex children:grow gap-1"> |
92 | 109 | <button class="btn" on:click={spawn}>Run</button> |
93 | | - <button class="btn" on:click={kill}>Kill</button> |
| 110 | + {#if child} |
| 111 | + <button class="btn" on:click={kill}>Kill</button> |
| 112 | + {/if} |
94 | 113 | </div> |
95 | 114 | {#if child} |
96 | 115 | <br /> |
97 | 116 | <input class="input" placeholder="write to stdin" bind:value={stdin} /> |
98 | 117 | <button class="btn" on:click={writeToStdin}>Write</button> |
99 | 118 | {/if} |
| 119 | + |
| 120 | + <div> |
| 121 | + {#each output as l} |
| 122 | + <p>{l}</p> |
| 123 | + {/each} |
| 124 | + </div> |
100 | 125 | </div> |
0 commit comments