Skip to content

Commit 46484eb

Browse files
committed
fix: terminal anicode show
1 parent 3520526 commit 46484eb

File tree

3 files changed

+78
-7
lines changed

3 files changed

+78
-7
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"vite-plugin-top-level-await": "^1.3.0",
2525
"vite-plugin-wasm": "^3.2.2",
2626
"vue": "^3.2.47",
27-
"vue-router": "^4.1.6"
27+
"vue-router": "^4.1.6",
28+
"xterm": "^5.3.0"
2829
},
2930
"devDependencies": {
3031
"@types/chai": "^4.3.4",

pnpm-lock.yaml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/pages/index.vue

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ import { memberList } from "@/constant";
1818
import createMonaco, { PlMonaco } from "@pivot-lang/create-monaco";
1919
import CodeBlock from "@/components/codeBlock.vue";
2020
import { cp } from "fs";
21+
import { AnsiUp } from "ansi_up";
22+
import Convert from "ansi-to-html";
23+
import "xterm/css/xterm.css";
24+
import { Terminal } from "xterm";
25+
import { Unicode11Addon } from "@xterm/addon-unicode11";
26+
import { CanvasAddon } from "@xterm/addon-canvas";
27+
import { WebglAddon } from "@xterm/addon-webgl";
28+
29+
import { FitAddon } from "@xterm/addon-fit";
2130
const tabVal = ref("hello world");
2231
const tabList = basicCode.map((item) => item.title);
2332
let monaco: PlMonaco;
@@ -27,6 +36,24 @@ function gotoEmail() {
2736
window.location.href = "mailto:[email protected]";
2837
}
2938
39+
const ansiUp = new AnsiUp();
40+
const convert = new Convert();
41+
const terminal = new Terminal({
42+
convertEol: true,
43+
disableStdin: true, //是否应禁用输入
44+
fontSize: 14,
45+
theme: {
46+
foreground: "#ECECEC", //字体
47+
background: "#000000", //背景色
48+
},
49+
});
50+
51+
function handleOutput(re: string) {
52+
runResult.value = re[re.length - 1] === "\n" ? re.slice(0, -1) : re;
53+
terminal.reset();
54+
terminal.write(runResult.value);
55+
}
56+
3057
onMounted(async () => {
3158
monaco = createMonaco(
3259
document.getElementById("container")!,
@@ -36,6 +63,10 @@ onMounted(async () => {
3663
monaco.editor.onDidChangeModelContent(() => {
3764
code.value = monaco.editor.getModel()!.getValue();
3865
});
66+
const canvasAddon = new CanvasAddon();
67+
terminal.loadAddon(canvasAddon);
68+
terminal.open(document.querySelector(".code-block") as HTMLElement);
69+
terminal.resize(44, 17);
3970
});
4071
watch(
4172
() => tabVal.value,
@@ -60,11 +91,8 @@ watch(
6091
<div class="code-now-container">
6192
<div class="code-box">
6293
<TabList
63-
@updateVal="(val:string) => (tabVal = val)"
64-
@updateOutput="
65-
(re) =>
66-
(runResult = re[re.length - 1] === '\n' ? re.slice(0, -1) : re)
67-
"
94+
@updateVal="(val) => (tabVal = val)"
95+
@updateOutput="handleOutput"
6896
:tablist="tabList"
6997
:val="tabVal"
7098
:code="code"
@@ -75,7 +103,7 @@ watch(
75103
</div>
76104
<div class="execution">
77105
<div class="execution-title">Execution</div>
78-
<CodeBlock class="code-block" :code="runResult"></CodeBlock>
106+
<div class="code-block"></div>
79107
</div>
80108
</div>
81109
</div>
@@ -277,5 +305,40 @@ watch(
277305
margin: 50px auto;
278306
}
279307
}
308+
309+
.code-block {
310+
white-space: pre-wrap;
311+
padding: 10px 0 0 20px;
312+
border-radius: 10px;
313+
color: aliceblue;
314+
width: fit-content;
315+
box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
316+
border: 5px #666 solid;
317+
}
318+
319+
.xterm-helpers {
320+
visibility: hidden;
321+
height: 0;
322+
}
323+
.xterm-cursor {
324+
display: none !important;
325+
}
326+
327+
.xterm .xterm-viewport {
328+
&::-webkit-scrollbar {
329+
height: 5px;
330+
width: 5px;
331+
}
332+
&::-webkit-scrollbar-thumb {
333+
background-color: #666;
334+
box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
335+
&:hover {
336+
background-color: #999; // 轨道颜色
337+
}
338+
}
339+
&::-webkit-scrollbar-track {
340+
background-color: #222; // 轨道颜色
341+
}
342+
}
280343
}
281344
</style>

0 commit comments

Comments
 (0)