[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ilyaigpetrov opened this issue Jun 10, 2019 · 0 comments
Open

Comments

@ilyaigpetrov
Copy link
ilyaigpetrov commented Jun 10, 2019

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}`));
  }

})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant