my xterm terminal just having basic black UI it is lacking logic , pls look into it

155 Views Asked by At

I have tried many things but still it is not able to build up the logic of a terminal. I have used the xterm-for-react package as well but it was still giving the same black UI only

const Xterm = () => {
  const termRef = useRef(null);

  useEffect(() => {
    const initTerminal = () => {
      const terminal = new Terminal();
      const fitAddon = new FitAddon();
      terminal.loadAddon(fitAddon);
      terminal.open(termRef.current);
      fitAddon.fit();

      terminal.onKey((e) => {
        const printable = !e.domEvent.altKey && !e.domEvent.altGraphKey && !e.domEvent.ctrlKey && !e.domEvent.metaKey;

        if (e.domEvent.key === 13) {
          terminal.write('\r\n');
        } else if (e.domEvent.key=== 8) {
          // Do something when backspace is pressed
        } else if (printable) {
          terminal.write(e.key);
        }
      });
    };

    initTerminal();
  }, []);

  return <div ref={termRef}></div>;
};

export default Xterm;

1

There are 1 best solutions below

1
ivenoidea On

xtermjs is only a visualization tool it doesn't have a built-in terminal logic. Check this in xtermjs official github page:

Xterm.js is not a terminal application that you can download and use on your computer. Xterm.js is not bash. Xterm.js can be connected to processes like bash and let you interact with them (provide input, receive output).

If you want to run processes like bash, vim etc. you should run a terminal process in the background and connect it with your js code. You can check this github project if you want to use it with React.