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

@@ -43,6 +43,11 @@ const props = defineProps({
type: String, type: String,
default: 'ssh', default: 'ssh',
}, },
/**消息处理函数 */
processMessages: {
type: Function,
default: undefined,
},
}); });
/**终端输入DOM节点实例对象 */ /**终端输入DOM节点实例对象 */
@@ -153,32 +158,49 @@ function wsMessage(res: Record<string, any>) {
} }
if (!requestId) return; if (!requestId) return;
if (terminal.value != null) { if (terminal.value != null) {
// 查找的开始输出标记 let text = '';
const parts: string[] = data.split('\u001b[?2004l\r'); // 处理消息
if (parts.length > 0) { if (props.processMessages) {
let text = parts[parts.length - 1]; text = props.processMessages(data);
// 找到最后输出标记 }else{
let lestIndex = text.lastIndexOf('\u001b[?2004h\u001b]0;'); text = processMessage(data);
if (lestIndex !== -1) { }
text = text.substring(0, lestIndex); // 无消息是则不输出
} if (text === '') {
if (text === '' || text === '\r\n' || text.startsWith('^C\r\n')) {
return;
}
// 是否还有最后输出标记
lestIndex = text.lastIndexOf('\u001b[?2004h');
if (lestIndex !== -1) {
text = text.substring(0, lestIndex);
}
// console.log({ parts, text });
terminal.value.write(text);
return; return;
} }
// 无标记 terminal.value.write(text);
terminal.value.write(data);
} }
} }
/**终端消息处理*/
function processMessage(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\r\n')) {
return '';
}
// 是否还有最后输出标记
lestIndex = text.lastIndexOf('\u001b[?2004h');
if (lestIndex !== -1) {
text = text.substring(0, lestIndex);
}
// console.log({ parts, text });
return text;
}
return data;
}
onMounted(() => { onMounted(() => {
if (props.neType && props.neId) { if (props.neType && props.neId) {
// 建立链接 // 建立链接

View File

@@ -121,6 +121,38 @@ function fnConnect() {
fnResend(); 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>) { function fnMessage(res: Record<string, any>) {
const { code, requestId, data } = res; const { code, requestId, data } = res;
@@ -345,10 +377,14 @@ onBeforeUnmount(() => {});
<a-auto-complete <a-auto-complete
v-model:value="state.data.command" v-model:value="state.data.command"
:disabled="state.running" :disabled="state.running"
:options="[
{ value: '-help' },
{ value: '-i 1 -c 4 8.8.8.8' },
]"
:dropdown-match-select-width="500" :dropdown-match-select-width="500"
style="width: 100%" 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> </a-auto-complete>
</div> </div>
@@ -363,6 +399,7 @@ onBeforeUnmount(() => {});
:ne-id="state.params.neId" :ne-id="state.params.neId"
:rows="state.params.rows" :rows="state.params.rows"
:cols="state.params.cols" :cols="state.params.cols"
:process-messages="fnProcessMessage"
style="height: 400px" style="height: 400px"
@connect="fnConnect" @connect="fnConnect"
@message="fnMessage" @message="fnMessage"