@@ -2,21 +2,20 @@ import React, { useEffect, useRef, useContext } from "react";
2
2
import { Terminal , ITerminalOptions } from "xterm" ;
3
3
import { FitAddon } from "xterm-addon-fit" ;
4
4
import { ITerminalColor } from "../../utils/types" ;
5
- import styles from "./XTerminal.module.css" ;
6
- import "xterm/css/xterm.css" ;
7
5
import { AppContext } from "../context" ;
8
6
import debounce from "../../utils/debounce" ;
9
-
10
- let term : Terminal ;
11
- let fitAddon : FitAddon ;
7
+ import styles from "./XTerminal.module.css" ;
8
+ import "xterm/css/xterm.css" ;
12
9
13
10
const XTerminal : React . FC = ( ) => {
14
11
const { username, io } = useContext ( AppContext ) ;
15
12
const validCommmands = [ "ls" , "cat" , "whoami" ] ;
13
+ const command = useRef < string > ( "" ) ;
14
+ const terminalEl = useRef ( ) as React . MutableRefObject < HTMLInputElement > ;
15
+ const terminalRef = useRef ( ) as React . MutableRefObject < HTMLDivElement > ;
16
16
17
- const promptValue = `${
18
- ITerminalColor . Cyan + username + ITerminalColor . Purple
19
- } @ProdexOne${ ITerminalColor . Reset } $ `;
17
+ let term : Terminal ;
18
+ let fitAddon : FitAddon ;
20
19
21
20
const TerminalOptions : ITerminalOptions = {
22
21
fontFamily : "Consolas" ,
@@ -26,24 +25,20 @@ const XTerminal: React.FC = () => {
26
25
cursorStyle : "bar" ,
27
26
theme : { background : "#002B36" } ,
28
27
} ;
29
- const command = useRef < string > ( "" ) ;
30
- // const Size = useRef<number>();
31
- let terminalEl = useRef ( ) as React . MutableRefObject < HTMLInputElement > ; // as React.MutableRefObject<HTMLInputElement>
28
+
29
+ const promptValue = `${
30
+ ITerminalColor . Cyan + username + ITerminalColor . Purple
31
+ } @ProdexOne${ ITerminalColor . Reset } $ `;
32
+
32
33
function prompt ( ) {
33
34
term . write ( promptValue ) ;
34
35
}
35
- // init func
36
36
37
37
const initTerminal = ( ) => {
38
38
term = new Terminal ( TerminalOptions ) ;
39
39
fitAddon = new FitAddon ( ) ;
40
40
term . loadAddon ( fitAddon ) ;
41
-
42
41
term . open ( terminalEl . current ) ;
43
- // setInterval(() => {
44
- // console.log("DS");
45
- // fitAddon.fit();
46
- // }, 3000);
47
42
fitAddon . fit ( ) ;
48
43
term . writeln (
49
44
"Welcome to " +
@@ -59,31 +54,24 @@ const XTerminal: React.FC = () => {
59
54
term . writeln ( "Type some keys and commands to play around.\n" ) ;
60
55
prompt ( ) ;
61
56
62
- // term.write("Hello from \x1B[1;3;31mxterm.js\x1B[0m $ ");
63
- // term.onResize((e) => console.log("Resize : ", e));
64
57
term . onKey ( ( e ) => {
65
- // console.log(e);
66
-
67
58
switch ( e . domEvent . key ) {
68
59
case "Enter" :
69
60
term . writeln ( "" ) ;
70
61
if ( ! command . current ) return prompt ( ) ;
71
- //send data
72
62
io . current ?. emit ( "command" , command . current ) ;
73
-
74
63
command . current = "" ;
75
-
76
64
break ;
65
+
77
66
case "Backspace" :
78
67
if ( command . current ) {
79
68
term . write ( "\b \b" ) ;
80
69
command . current = command . current . slice ( 0 , - 1 ) ;
81
70
}
82
71
break ;
72
+
83
73
default :
84
74
command . current += e . key ;
85
- // console.log(command);
86
- // term.write(e.key);
87
75
if ( validCommmands . includes ( command . current ) ) {
88
76
term . write (
89
77
"\b \b" . repeat ( command . current . length - 1 ) +
@@ -96,63 +84,44 @@ const XTerminal: React.FC = () => {
96
84
}
97
85
}
98
86
} ) ;
99
-
100
- // var printable = !ev!!.altKey && !ev!!.ctrlKey && !ev!!.metaKey;
101
87
} ;
102
88
103
- // const [height, setHeight] = useState();
104
- const tref = useRef ( ) as React . MutableRefObject < HTMLDivElement > ;
105
- // const fnd = (fn, time) => {
106
- // let tm;
107
- // clearTimeout(tm);
108
- // tm = setTimeout(() => {
109
- // fn();
110
- // }, time);
111
- // };
112
89
useEffect ( ( ) => {
113
90
initTerminal ( ) ;
114
- // @ts -ignore
115
- io ?. current . on ( "command_response" , ( res ) => {
91
+ io ?. current ! . on ( "command_response" , ( res ) => {
116
92
term . writeln ( res ) ;
117
93
prompt ( ) ;
118
94
} ) ;
119
95
// eslint-disable-next-line react-hooks/exhaustive-deps
120
96
} , [ ] ) ;
121
- useEffect ( ( ) => {
122
- const observeEl = tref . current ;
123
97
98
+ useEffect ( ( ) => {
99
+ const observeEl = terminalRef . current ;
124
100
const resizeObserver = new ResizeObserver (
125
101
debounce (
126
102
( ) => {
127
- if ( tref . current ) fitAddon . fit ( ) ;
128
- // console.log("el", observeEl, "----", tref.current);
103
+ if ( terminalRef . current ) fitAddon . fit ( ) ;
129
104
} ,
130
105
500 ,
131
106
null
132
107
)
133
108
) ;
109
+
134
110
resizeObserver . observe ( observeEl ) ;
135
111
136
112
return ( ) => {
137
113
resizeObserver . unobserve ( observeEl ) ;
138
- console . log ( "unobs" ) ;
139
114
} ;
140
115
// eslint-disable-next-line react-hooks/exhaustive-deps
141
116
} , [ ] ) ;
142
- // useEffect(() => {
143
- // console.log(fitAddon);
144
- // fitAddon?.fit();
145
- // eslint-disable-next-line react-hooks/exhaustive-deps
146
- // }, [dimensions]);
147
117
148
118
return (
149
119
< div className = { styles . terminal__container } >
150
120
< div className = { styles . terminal__bar } >
151
121
< div className = "t-left" > Terminal Session of { username } </ div >
152
-
153
122
< div className = "t-right" > 🚀</ div >
154
123
</ div >
155
- < div className = { styles . terminal__wrapper } ref = { tref } >
124
+ < div className = { styles . terminal__wrapper } ref = { terminalRef } >
156
125
< div className = { styles . terminal } ref = { terminalEl } > </ div >
157
126
</ div >
158
127
</ div >
0 commit comments