How to handle new line in xterm.js while writing data into the terminal?

4.3k views Asked by At

I am trying to output string with newlines in xterm.js but it's showcasing the output in a weird format. I need the output to be printed as it would in an actual terminal.

import { Terminal } from 'xterm';

    let terminal = new Terminal();
    terminal.loadAddon(new FitAddon());
    let data = "kumar\nkanhaiya"
    this.setState({showOutput: true},() => {
        terminal.open(document.getElementById('xterm'));
        terminal.writeUtf8(data)
    })

    <div id="xterm"></div>

I have attached the images for reference to the issue I am facing.

The newline should make the other part of text start from beginning as in an actual terminal. But it leaves some space on the left.

https://i.stack.imgur.com/IOcpW.png

2

There are 2 answers

0
afsal On

Replace the LF('\n') charecters with CRLF('\r\n') from the data

let data = "kumar\r\nkanhaiya"
0
abhinav gajurel On

Try this

let terminal = new Terminal({convertEol: true});

It will set the cursor to the beginning of the next line with every new line.