local-echo icon indicating copy to clipboard operation
local-echo copied to clipboard

Feature Request: Compatibility With Node's `process.std{in,out}`

Open ilyaigpetrov opened this issue 6 years ago • 0 comments

Reason

Make it possible to create terminal programs that run both in a browser and a terminal.

Wanted Changes

Something like:

// ./lib/LocalEchoController
...
handleTermData(data) { // data may be Buffer!
  data = data.toString(); // Against https://eslint.org/docs/rules/no-param-reassign.
...

Test

import LocalEchoController from 'local-echo';

(async () => {

  const window = globalThis.window;
  const term = !window
    ? (() => {

        process.stdin.setRawMode(true);
        return process.stdin;
      })()
    : await new Promise((resolve) => {
        const term = new Terminal({
          // rendererType: 'dom',
        });
        term.open(document.getElementById('terminal'));
        resolve(term);
      });

  term.write('Hello from \x1B[1;3;31mxterm.js\x1B[0m $ ');

  term.on('data', (data) => {

    // In the "raw" mode we must handle ctrl+c manually.
    if(data.toString().charAt(0) === '\x03') {
      (window ? console.log('We can\'t close the window.') : process.exit());
    }
  });
  // Create a local echo controller
  const controller = new LocalEchoController(term);
  while(true) {
    await controller.read("~$ ")
      .then(input => controller.println(`User entered: ${input}`))
      .catch(error => console.log(`Error reading: ${error}`));
  }

})();

ilyaigpetrov avatar Jun 10 '19 05:06 ilyaigpetrov