@@ -24,32 +24,32 @@ const terminal = new Terminal({
2424 cursorBlink: true , // 光标闪烁
2525 cursorStyle: ' bar' ,
2626 cursorInactiveStyle: ' underline' , // 光标样式
27+ convertEol: true , // 回车换行
2728});
2829const fitAddon = new FitAddon ();
2930
3031const terminalContainer = ref ();
3132
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 ' ,
3836};
3937const commands: Array <string > = [];
4038let command = ' ' ;
4139// Handle the key event
4240terminal .onKey (e => {
4341 const code = e .domEvent .code .toLowerCase ();
4442
45- const keyAction = keyActions [code ];
43+ const sequence = sequenceMap [code ];
4644
47- if (keyAction ) {
48- keyAction (terminal );
49- exec (command );
45+ if (code === ' enter' ) {
5046 commands .push (command );
47+ terminal .write (sequence );
48+ exec (command );
5149 command = ' ' ;
52- return ;
50+ } else if (code === ' backspace' ) {
51+ command = command .slice (0 , - 1 );
52+ terminal .write (sequence );
5353 } else {
5454 terminal .write (e .key );
5555 command += e .key ;
@@ -62,8 +62,12 @@ const exec = (command: string) => {
6262 // eslint-disable-next-line
6363 console .log (` exec res ${res } ` );
6464 terminal .writeln (res as string );
65+ terminal .writeln (' ' );
6566 })
66- .catch (e => terminal .writeln (e ));
67+ .catch (e => {
68+ terminal .writeln (e );
69+ terminal .writeln (' ' );
70+ });
6771};
6872
6973onMounted (async () => {
@@ -72,10 +76,10 @@ onMounted(async () => {
7276 // Attach the terminal to the container
7377 terminal .open (terminalContainer .value );
7478 fitAddon .fit ();
79+ terminal .focus ();
7580
7681 // Example: Write text to the terminal
7782 terminal .write (' Welcome to AnyTerm!\r\n ' );
78-
7983 // Optional: Add terminal handling logic, e.g., for executing commands
8084 // terminal.onData((data: string) => {
8185 // terminal.write(data);
0 commit comments