-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: seven <zilisheng1996@gmail.com>
- Loading branch information
Showing
4 changed files
with
80 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<template> | ||
<div> | ||
<div ref="terminalContainer"></div> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import '@xterm/xterm/css/xterm.css'; | ||
import { Terminal } from '@xterm/xterm'; | ||
import { FitAddon } from 'xterm-addon-fit'; | ||
const terminal = new Terminal({ | ||
cursorBlink: true, // 光标闪烁 | ||
cursorStyle: 'bar', | ||
cursorInactiveStyle: 'underline', // 光标样式 | ||
}); | ||
const fitAddon = new FitAddon(); | ||
const terminalContainer = ref(); | ||
// Handle the key event | ||
terminal.onKey(e => { | ||
const code = e.domEvent.code.toLowerCase(); | ||
const enter = () => terminal.write('\r\n'); | ||
const backspace = () => terminal.write('\b \b'); | ||
const write = (key: string) => terminal.write(key); | ||
const keyActions: { [key: string]: () => void } = { | ||
enter, | ||
backspace, | ||
}; | ||
(keyActions[code] || (() => write(e.key)))(); | ||
}); | ||
onMounted(() => { | ||
terminal.loadAddon(fitAddon); | ||
// Attach the terminal to the container | ||
terminal.open(terminalContainer.value); | ||
fitAddon.fit(); | ||
// Example: Write text to the terminal | ||
terminal.write('Welcome to the Vue 3 + xterm.js example!\r\n'); | ||
// Optional: Add terminal handling logic, e.g., for executing commands | ||
// terminal.onData((data: string) => { | ||
// terminal.write(data); | ||
// console.log('terminal on data', data); | ||
// }); | ||
}); | ||
</script> | ||
|
||
<style lang="scss" scoped></style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters