Skip to content

Commit a193572

Browse files
committed
[vue3] fix pushMessage接口传入数组时无法解析ansi格式 #112
1 parent a137a20 commit a193572

File tree

3 files changed

+37
-29
lines changed

3 files changed

+37
-29
lines changed

src/Terminal.vue

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ const _endExecCallBack = () => {
10971097
}
10981098
10991099
const _filterMessageType = (message: Message) => {
1100-
const valid = message.type && /^(normal|html|code|table|json)$/.test(message.type)
1100+
const valid = message.type && /^(normal|html|code|table|json|ansi)$/.test(message.type)
11011101
if (!valid) {
11021102
console.debug(`Invalid terminal message type: ${message.type}, the default type normal will be used`)
11031103
message.type = 'normal'
@@ -1134,18 +1134,6 @@ const _pushMessage = (message: Message | Array<Message> | string) => {
11341134
return;
11351135
}
11361136
1137-
if (typeof message === 'string') {
1138-
message = {
1139-
type: 'normal',
1140-
content: message as string
1141-
}
1142-
}
1143-
1144-
if (message.type === 'ansi') {
1145-
message.type = 'html'
1146-
message.content = _parseANSI(message.content as string)
1147-
}
1148-
11491137
_pushMessage0(message)
11501138
_jumpToBottom()
11511139
@@ -1156,10 +1144,22 @@ const _pushMessage = (message: Message | Array<Message> | string) => {
11561144
}
11571145
}
11581146
1159-
const _pushMessage0 = (message: Message, checkSize: boolean = false) => {
1160-
_filterMessageType(message)
1161-
if (message.type !== 'cmdLine' && props.pushMessageBefore) {
1162-
props.pushMessageBefore(message, getName())
1147+
const _pushMessage0 = (message: Message | string, checkSize: boolean = false) => {
1148+
if (typeof message === 'string') {
1149+
message = {
1150+
type: 'normal',
1151+
content: message as string
1152+
}
1153+
} else {
1154+
_filterMessageType(message)
1155+
if (message.type === 'ansi') {
1156+
message.type = 'html'
1157+
message.content = _parseANSI(message.content as string)
1158+
}
1159+
1160+
if (message.type !== 'cmdLine' && props.pushMessageBefore) {
1161+
props.pushMessageBefore(message, getName())
1162+
}
11631163
}
11641164
11651165
let terminalLogLength = terminalLog.value.length

src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import './css/style.css'
44
import 'vue-json-viewer/style.css'
55
import type {App} from 'vue'
66
import TerminalStore from "./common/store"
7-
import TerminalApi, {setOptions} from "./common/api"
8-
import {configHighlight, configCodemirror, configTheme} from "./common/api"
7+
import TerminalApi, {configCodemirror, configHighlight, configTheme, setOptions} from "./common/api"
98
import Terminal from "./Terminal.vue"
109
import {Options, TerminalAsk, TerminalFlash} from "./types"
1110

test/App.vue

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import {Terminal, TerminalApi, TerminalAsk, TerminalElementInfo} from '~/index'
99
import {Command, FailedFunc, Message, SuccessFunc} from "~/types";
1010
import {reactive, ref} from "vue";
11+
1112
const commandStore: Array<Command> = [
1213
{
1314
key: "COMMAND CONFIG"
@@ -28,16 +29,24 @@ const commandStore: Array<Command> = [
2829
key: "Test CONFIG",
2930
}
3031
]
31-
const initLog = reactive([{
32-
type: 'normal',
33-
content: "Terminal Initializing ..."
34-
}, {
35-
type: 'normal',
36-
content: "Current login time: " + new Date().toLocaleString()
37-
}, {
38-
type: 'normal',
39-
content: "Welcome to vue web terminal! If you are using for the first time, you can use the <span class='t-cmd-key'>help</span> command to learn. Thanks for your star support: <a class='t-a' target='_blank' href='https://github.com/tzfun/vue-web-terminal'>https://github.com/tzfun/vue-web-terminal</a>"
40-
}])
32+
const initLog = reactive([
33+
{
34+
type: 'normal',
35+
content: "Terminal Initializing ..."
36+
},
37+
{
38+
type: 'normal',
39+
content: "Current login time: " + new Date().toLocaleString()
40+
},
41+
{
42+
type: 'ansi',
43+
content: '\x1B[1;34mThis are some blue text.\x1B[0m\n\x1B[30;43mThis is a line of text with a background color.\x1B[0m\n\x1B[92;5mThis is blink text.\x1B[0m'
44+
},
45+
{
46+
type: 'normal',
47+
content: "Welcome to vue web terminal! If you are using for the first time, you can use the <span class='t-cmd-key'>help</span> command to learn. Thanks for your star support: <a class='t-a' target='_blank' href='https://github.com/tzfun/vue-web-terminal'>https://github.com/tzfun/vue-web-terminal</a>"
48+
}
49+
])
4150
const testInputValue = ref("")
4251
4352
const terminals = ref<Array<any>>([

0 commit comments

Comments
 (0)