-
Details
Steps to reproduce<template>
<div id="terminal" ref="terminalRef"></div>
</template>
<script setup>
import { Terminal } from 'xterm'
import { FitAddon } from 'xterm-addon-fit'
import { AttachAddon } from 'xterm-addon-attach'
import 'xterm/css/xterm.css'
import { onMounted, ref } from 'vue'
const term = ref()
const ROWS = 40
const COLS = 10
const terminalRef = ref(null)
let socket
const initXterm = () => {
term.value = new Terminal({
allowProposedApi: true,
rendererType: 'canvas', //渲染类型
rows: ROWS, //行数
cols: COLS, // 不指定行数,自动回车后光标从下一行开始
convertEol: true, //启用时,光标将设置为下一行的开头
scrollback: 50, //终端中的回滚量
disableStdin: false, //是否应禁用输入
tabStopWidth: 4,
// cursorStyle: "underline", //光标样式
cursorBlink: true, //光标闪烁
lineHeight: 1.2,
fontSize: 12,
screenReaderMode: true,
fontFamily: "Monaco, Menlo, Consolas, 'Courier New', monospace",
theme: {
foreground: '#ECECEC', //字体
background: '#000000', //背景色
cursor: 'help', //设置光标
lineHeight: 10,
},
})
// 创建terminal实例
term.value.open(terminalRef.value)
// 换行并输入起始符 $
term.value.prompt = () => {
term.value.write('\r\n\x1b[33m$\x1b[0m ')
}
term.value.prompt()
// canvas背景全屏
const fitAddon = new FitAddon()
term.value.loadAddon(fitAddon)
fitAddon.fit()
window.addEventListener('resize', resizeScreen)
function resizeScreen() {
try {
// 窗口大小改变时,触发xterm的resize方法使自适应
fitAddon.fit()
} catch (e) {
console.log('e', e.message)
}
}
setTimeout(async () => {
runFakeTerminal()
socket = new WebSocket('ws://127.0.0.1:3000/terminals/51727')
socket.onopen = runRealTerminal
socket.onclose = runFakeTerminal
socket.onerror = runFakeTerminal
}, 1000)
}
function runRealTerminal() {
const attachAddon = new AttachAddon(socket)
term.value.loadAddon(attachAddon)
// term.value.focus()
}
const runFakeTerminal = () => {
if (term.value._initialized) return
// 初始化
term.value._initialized = true
term.value.writeln('')
term.value.writeln('=================================================')
term.value.writeln('Welcome to \x1b[1;32mGBD\x1b[0m.')
term.value.writeln('=================================================')
// 添加事件监听器,支持输入方法
term.value.onKey((e) => {
const ev = e.domEvent
socket.send(ev.code)
const printable = !ev.altKey && !ev.ctrlKey && !ev.metaKey
if (ev.code === 'Enter') {
term.value.prompt()
} else if (ev.code === 'Backspace') {
// back 删除的情况
if (term.value._core.buffer.x > 2) {
// term.value.write('\b \b')
}
} else if (printable) {
// term.value.write(e.key)
}
})
term.value.onData((key) => {
console.log(key, 'onData')
// 粘贴的情况
// if (key.length > 1) term.value.write(key)
})
}
onMounted(() => {
if (terminalRef.value) {
console.log(terminalRef.value)
initXterm()
}
})
</script>
<style lang="less" scoped>
#terminal {
width: 100%;
height: 100%;
}
</style>
|
Beta Was this translation helpful? Give feedback.
Answered by
Pancake-Q
Aug 14, 2023
Replies: 1 comment 10 replies
-
The cause of xx disableStdin |
Beta Was this translation helpful? Give feedback.
10 replies
Answer selected by
Tyriar
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The cause of xx disableStdin