fix: 终端命令显示查看组件输出命令到首行

This commit is contained in:
TsMask
2024-11-05 17:33:23 +08:00
parent 8283523327
commit 23116db988
2 changed files with 81 additions and 22 deletions

View File

@@ -121,6 +121,38 @@ function fnConnect() {
fnResend();
}
/**终端消息处理*/
function fnProcessMessage(data: string): string {
// 查找的开始输出标记
const parts: string[] = data.split('\u001b[?2004l\r');
if (parts.length > 0) {
if (parts[0].startsWith('^C') || parts[0].startsWith('\r')) {
return '';
}
let text = parts[parts.length - 1];
// 找到最后输出标记
let lestIndex = text.lastIndexOf('\u001b[?2004h\u001b]0;');
if (lestIndex !== -1) {
text = text.substring(0, lestIndex);
}
if (text === '' || text === '\r\n' || text.startsWith('^C')) {
return '';
}
// 是否还有最后输出标记
lestIndex = text.lastIndexOf('\u001b[?2004h');
if (lestIndex !== -1) {
text = text.substring(0, lestIndex);
}
// console.log({ parts, text });
if (parts[0].startsWith('ping')) {
return parts[0] + '\r\n' + text;
}
return text;
}
return data;
}
/**终端消息监听*/
function fnMessage(res: Record<string, any>) {
const { code, requestId, data } = res;
@@ -345,10 +377,14 @@ onBeforeUnmount(() => {});
<a-auto-complete
v-model:value="state.data.command"
:disabled="state.running"
:options="[
{ value: '-help' },
{ value: '-i 1 -c 4 8.8.8.8' },
]"
:dropdown-match-select-width="500"
style="width: 100%"
>
<a-input addon-before="ping" placeholder="eg: -i 1 -c 4 8.8.8.8" />
<a-input addon-before="ping" placeholder="command" />
</a-auto-complete>
</div>
@@ -363,6 +399,7 @@ onBeforeUnmount(() => {});
:ne-id="state.params.neId"
:rows="state.params.rows"
:cols="state.params.cols"
:process-messages="fnProcessMessage"
style="height: 400px"
@connect="fnConnect"
@message="fnMessage"