@@ -24,32 +24,32 @@ const terminal = new Terminal({
24
24
cursorBlink: true , // 光标闪烁
25
25
cursorStyle: ' bar' ,
26
26
cursorInactiveStyle: ' underline' , // 光标样式
27
+ convertEol: true , // 回车换行
27
28
});
28
29
const fitAddon = new FitAddon ();
29
30
30
31
const terminalContainer = ref ();
31
32
32
- const enter = () => terminal .write (' \r\n ' );
33
- const backspace = () => terminal .write (' \b \b ' );
34
-
35
- const keyActions: { [key : string ]: (terminal : Terminal ) => void } = {
36
- enter ,
37
- backspace ,
33
+ const sequenceMap: { [key : string ]: string } = {
34
+ enter: ' \r\n ' ,
35
+ backspace: ' \b \b ' ,
38
36
};
39
37
const commands: Array <string > = [];
40
38
let command = ' ' ;
41
39
// Handle the key event
42
40
terminal .onKey (e => {
43
41
const code = e .domEvent .code .toLowerCase ();
44
42
45
- const keyAction = keyActions [code ];
43
+ const sequence = sequenceMap [code ];
46
44
47
- if (keyAction ) {
48
- keyAction (terminal );
49
- exec (command );
45
+ if (code === ' enter' ) {
50
46
commands .push (command );
47
+ terminal .write (sequence );
48
+ exec (command );
51
49
command = ' ' ;
52
- return ;
50
+ } else if (code === ' backspace' ) {
51
+ command = command .slice (0 , - 1 );
52
+ terminal .write (sequence );
53
53
} else {
54
54
terminal .write (e .key );
55
55
command += e .key ;
@@ -62,8 +62,12 @@ const exec = (command: string) => {
62
62
// eslint-disable-next-line
63
63
console .log (` exec res ${res } ` );
64
64
terminal .writeln (res as string );
65
+ terminal .writeln (' ' );
65
66
})
66
- .catch (e => terminal .writeln (e ));
67
+ .catch (e => {
68
+ terminal .writeln (e );
69
+ terminal .writeln (' ' );
70
+ });
67
71
};
68
72
69
73
onMounted (async () => {
@@ -72,10 +76,10 @@ onMounted(async () => {
72
76
// Attach the terminal to the container
73
77
terminal .open (terminalContainer .value );
74
78
fitAddon .fit ();
79
+ terminal .focus ();
75
80
76
81
// Example: Write text to the terminal
77
82
terminal .write (' Welcome to AnyTerm!\r\n ' );
78
-
79
83
// Optional: Add terminal handling logic, e.g., for executing commands
80
84
// terminal.onData((data: string) => {
81
85
// terminal.write(data);
0 commit comments