nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.46k stars 279 forks source link

Printing to console with readline's input half completed? #4186

Closed andrewmoise closed 3 months ago

andrewmoise commented 1 year ago

Details

So, if I have a line of input half-typed at my readline prompt, and output goes to the console, it breaks my input line in the middle, prints the output, and leaves me on a blank line. It seems to me like it'd be better to clear the half-line of input, print the output, and then redraw the prompt and half-line down below the output line, so I can keep going with my input line. No?

I get that we can't do anything if some random text comes in over stdout, but I would propose that it'd be better to offer a function that developers can call to print an output message while the user's in the middle of typing text, that doesn't ruin the user's input line. I'd be happy to make up a PR if this is something people are open to.

Node.js version

{
  node: '20.3.0',
  acorn: '8.8.2',
  ada: '2.5.0',
  ares: '1.19.1',
  base64: '0.5.0',
  brotli: '1.0.9',
  cjs_module_lexer: '1.2.2',
  cldr: '42.0',
  icu: '72.1',
  llhttp: '8.1.0',
  modules: '115',
  napi: '9',
  nghttp2: '1.53.0',
  openssl: '1.1.1u',
  simdutf: '3.2.12',
  tz: '2022e',
  undici: '5.22.1',
  unicode: '15.0',
  uv: '1.45.0',
  uvwasi: '0.0.18',
  v8: '11.3.244.8-node.9',
  zlib: '1.2.11'
}

Example code

class ConsoleListener {
    constructor() {
    this.readlineInterface = readline.createInterface({
        input: process.stdin,
        output: process.stdout,
        prompt: '> ',
    });
    }

    startListening() {
    this.readlineInterface.prompt();
    this.readlineInterface.on('line', (line) => {
        const command = line.trim();
        this.executeCommand(command);

        this.readlineInterface.prompt();
    });

    setInterval(this.debugMessage.bind(this), 5000);
    }

    executeCommand(command) {
    console.log("We received a command!");
    }

    debugMessage() {
    console.log('Hello!');
    }
}

Operating system

Mac OS, node via homebrew

Scope

runtime

Module and version

No response

prettydiff commented 1 year ago

It sounds like you desire some missing features and it also sounds like those missing features are things you should write.

How should Node know if a given bit of input is complete or not? As a stream process.stdin will pass along whatever it receives character by character irrespective of that those characters are. You need to write the logic to determine if that input qualifies as complete enough to issue a child process command.

github-actions[bot] commented 4 months ago

It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.

github-actions[bot] commented 3 months ago

It seems there has been no activity on this issue for a while, and it is being closed. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.