John Pavek
John Pavek

Reputation: 2665

Can I work interactively with the command line?

As part of a joke that went too far, I'm trying to make black jack in Deno. I'd like to not bring the web into this, but rather use an interactive prompt. I've gone through the STD Library, but I can only seem to find file IO. I was secretly hoping that prompt would work to keep this similar to the web, but no luck.

I've done this previously in Node with Readline, Example:

export const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
//...
rl.question(`name? `, (name: string) => {
   // other code
});

How can I accept user input in the command line after the application is already running?

Upvotes: 1

Views: 713

Answers (1)

Marcos Casagrande
Marcos Casagrande

Reputation: 40414

import { readLines } from 'https://deno.land/std/io/buffer.ts';

async function question() {
   console.log(question)
   // Listen to stdin input, once a new line is entered return
   for await(const line of readLines(Deno.stdin)) {
      return line;
   }
}

const answer = await question('Name?: ');
console.log(answer);

const answer2 = await question('Age?: ');
console.log(answer2);

Upvotes: 1

Related Questions