question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add ability to allow custom enquirer editors

See original GitHub issue

I’m currently using this function inspired by (copied from) the prompt method:

   function createCustomPrompt(task, Prompt, options) {
     task.prompt = true;
     let buffer = Buffer.alloc(64);
  
     const outputStream = through((data) => {
       buffer += data;
  
       // eslint-disable-next-line no-control-regex
       const deleteMultiLineRegexp = new RegExp(/.*(\u001b\[[0-9]*G|\u0007).*/m);          12
       if (deleteMultiLineRegexp.test(buffer.toString())) {
         buffer = Buffer.alloc(64);
        } else {
          task.output = buffer.toString();
        }
      });

      Object.assign(options, { stdout: outputStream });
      return new Prompt(options);
    }

With for example the enquirer-editor like this:

import EditorPrompt from 'enquirer-editor';
import { Manager } from 'listr2';

const manager = new Manager();
manager.add({
  title: 'Editor',
  task: async (_, task) => {
    const prompt = createCustomPrompt(task, EditorPrompt, { type: 'editor', message: 'Please Fill' });
    const message = await prompt.run();
  }
})
manager.runAll();

This works quite well, I was just wondering, @cenk1cenk2 do you think there’s a way to incorporate this into listr2 somehow? Or maybe make the prompt method from TaskWrapper more versatile to accept custom prompt classes?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
cenk1cenk2commented, Mar 31, 2020

So this seems like a good enough solution for me what do you think?

You pass the class of the EditorPrompt directly to the type section and pass the options in the same way.

Unfortunately, there is no type checking for the options since it can be anything but that can be done manually I preassume with as.

manager.add([
   {
     title: 'Custom prompt test',
     task: async (ctx, task): Promise<void> => {
       ctx.testInput = await task.prompt(EditorPrompt, {
         message: 'Please write a short bio of at least 3 lines',
         initial: 'Start writing!',
         validate: (response): boolean | string => {
           if (response.split('\n').length < 4) {
             return 'The bio must be at least 3 lines.'
           }
           return true
         }
       })
     }
   },
   {
     title: 'Dump',
     task: (ctx, task): void => {
       task.output = ctx.testInput
     },
     persistentOutput: true
   }
 ])

 try {
   ctx = await manager.runAll()
 // eslint-disable-next-line no-empty
 } catch (e){

 }
0reactions
cenk1cenk2commented, Mar 31, 2020

I merged it with v.1.3.7. But just tested basic functionality with the example.ts again, needs more testing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Enquirer - GitHub
Extensible - Easily create and use custom prompts by extending Enquirer's built-in prompts. Pluggable - Add advanced features to Enquirer using plugins.
Read more >
enquirer-editor - npm
A prompt built with enquirer that spawns a temporary editor using the preferred editor ( $VISUAL or $EDITOR ); it will default to...
Read more >
Build a JavaScript Command Line Interface (CLI) with Node.js
Finally, let's utilize Inquirer's checkbox “widget” to list the files. Insert the following code in lib/inquirer.js : askIgnoreFiles: (filelist) ...
Read more >
Who will emerge as the National Enquirer's editor after its sale?
Now that the National Enquirer, the troubled supermarket tabloid, has a deal to be sold for $100 million, the guessing game is on...
Read more >
Ronan Farrow: National Enquirer shredded secret Trump ...
During the first week of November 2016, the book alleges, Dylan Howard, who was editor in chief of the National Enquirer at the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found