6

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

  • 1
    You need CRLF (`'\r\n'`) as line separator, `'\n'` only moves the cursor one line down but not to the beginning. – jerch Jan 02 '21 at 21:33

2 Answers2

17

Try this

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

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

abhinav gajurel
  • 421
  • 5
  • 11
6

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

let data = "kumar\r\nkanhaiya"
afsal
  • 69
  • 4