xterm.js的深入学习
1、引入依赖
1 2 3 4 | import { Terminal } from 'xterm' import 'xterm/dist/xterm.css' |
2、 实例化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | let term = new Terminal({ rendererType: "canvas" , //渲染类型 rows: 40, //行数 convertEol: true , //启用时,光标将设置为下一行的开头 scrollback:10, //终端中的回滚量 disableStdin: false , //是否应禁用输入。 cursorStyle: 'underline' , //光标样式 cursorBlink: true , //光标闪烁 theme: { foreground: 'yellow' , //字体 background: '#060101' , //背景色 cursor: 'help' , //设置光标 } }) term.open(document.getElementById( 'app' )) |
具体的配置,可以参考xterm的配置
3、尝试
1 2 3 4 5 6 7 8 9 | term.writeln(`✔ Installed 1 packages ✔ Linked 0 latest versions ✔ Run 0 scripts Recently updated (since 2019-05-10): 1 packages (detail see file /Users/baolilei/Desktop/project/felab/xterm.js/fe-source-stage/src/xterm/node_modules/.recently_updates.txt) Today: → xterm@*(3.13.1) (01:15:03) ✔ All packages installed (1 packages installed from npm registry, used 1s(network 1s), speed 365.87kB/s, json 1(7.12kB), tarball 509.49kB)`) |
4、编写按键
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | // 版本4.9.0 <br>function runFakeTerminal () { if (term._initialized) { return } term._initialized = true term.prompt = () => { term.write( '\r\n$ ' ) } term.writeln( 'Welcome to xterm.js' ) term.writeln( 'This is a local terminal emulation, without a real terminal in the back-end.' ) term.writeln( 'Type some keys and commands to play around.' ) term.writeln( '' ) term.prompt() term.onKey(e => { console.log(e) const ev = e.domEvent const printable = !ev.altKey && !ev.altGraphKey && !ev.ctrlKey && !ev.metaKey if (ev.keyCode === 13) { term.prompt() } else if (ev.keyCode === 8) { // Do not delete the prompt if (term._core.buffer.x > 2) { term.write( '\b \b' ) } } else if (printable) { term.write(e.key) } }) } runFakeTerminal() <br><br><br> |
如果是旧版本的,是这样的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | function runFakeTerminal() { if (term._initialized) { return ; } term._initialized = true ; term.prompt = () => { term.write( '\r\n$ ' ); }; term.writeln( 'Welcome to xterm.js' ); term.writeln( 'This is a local terminal emulation, without a real terminal in the back-end.' ); term.writeln( 'Type some keys and commands to play around.' ); term.writeln( '' ); term.prompt(); term. on ( 'key' , function (key, ev) { const printable = !ev.altKey && !ev.altGraphKey && !ev.ctrlKey && !ev.metaKey; console.log(key,ev.keyCode); console.log(term._core.buffer.x); if (ev.keyCode === 13) { term.prompt(); } else if (ev.keyCode === 8) { // Do not delete the prompt if (term._core.buffer.x > 2) { term.write( '\b \b' ); } } else if (printable) { term.write(key); } }); term. on ( 'paste' , function (data) { term.write(data); }); } runFakeTerminal(); |
5、vue中使用demo如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | <template> <div id= "app" class = "app-box" >Hello</div> </template> <script> import { Terminal } from 'xterm' import 'xterm/css/xterm.css' export default { name: 'app' , mounted () { const term = new Terminal({ rendererType: 'canvas' , // 渲染类型 rows: 40, // 行数 convertEol: true , // 启用时,光标将设置为下一行的开头 scrollback: 10, // 终端中的回滚量 disableStdin: false , // 是否应禁用输入。 cursorStyle: 'underline' , // 光标样式 cursorBlink: true , // 光标闪烁 theme: { foreground: 'yellow' , // 字体 background: '#060101' , // 背景色 cursor: 'help' // 设置光标 } }) term.open(document.getElementById( 'app' )) function runFakeTerminal () { if (term._initialized) { return } term._initialized = true term.prompt = () => { term.write( '\r\n$ ' ) } term.writeln( 'Welcome to xterm.js' ) term.writeln( 'This is a local terminal emulation, without a real terminal in the back-end.' ) term.writeln( 'Type some keys and commands to play around.' ) term.writeln( '' ) term.prompt() term.onKey(e => { console.log(e) const ev = e.domEvent const printable = !ev.altKey && !ev.altGraphKey && !ev.ctrlKey && !ev.metaKey if (ev.keyCode === 13) { term.prompt() } else if (ev.keyCode === 8) { // Do not delete the prompt if (term._core.buffer.x > 2) { term.write( '\b \b' ) } } else if (printable) { term.write(e.key) } }) } runFakeTerminal() } } </script> <style lang= "scss" > html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; } </style> |
如果觉得文章不错,可以给小编发个红包给予鼓励.
你要觉得这篇文章比较好,记得点推荐!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」