fix: 终端命令显示查看组件输出命令到首行
This commit is contained in:
@@ -43,6 +43,11 @@ const props = defineProps({
|
||||
type: String,
|
||||
default: 'ssh',
|
||||
},
|
||||
/**消息处理函数 */
|
||||
processMessages: {
|
||||
type: Function,
|
||||
default: undefined,
|
||||
},
|
||||
});
|
||||
|
||||
/**终端输入DOM节点实例对象 */
|
||||
@@ -153,32 +158,49 @@ function wsMessage(res: Record<string, any>) {
|
||||
}
|
||||
if (!requestId) return;
|
||||
if (terminal.value != null) {
|
||||
// 查找的开始输出标记
|
||||
const parts: string[] = data.split('\u001b[?2004l\r');
|
||||
if (parts.length > 0) {
|
||||
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 });
|
||||
terminal.value.write(text);
|
||||
let text = '';
|
||||
// 处理消息
|
||||
if (props.processMessages) {
|
||||
text = props.processMessages(data);
|
||||
}else{
|
||||
text = processMessage(data);
|
||||
}
|
||||
// 无消息是则不输出
|
||||
if (text === '') {
|
||||
return;
|
||||
}
|
||||
// 无标记
|
||||
terminal.value.write(data);
|
||||
terminal.value.write(text);
|
||||
}
|
||||
}
|
||||
|
||||
/**终端消息处理*/
|
||||
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(() => {
|
||||
if (props.neType && props.neId) {
|
||||
// 建立链接
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user