import registry from 'registry'; import chalk from 'chalk'; import enquirer from 'enquirer'; export default class Enquirer { #session = null; constructor(session){ this.#session = session; } async prompt(){ const avatar = this.#session.user.avatar; const username = this.#session.username; try { const {text} = await enquirer.prompt({ type: 'input', name: 'text', message: username, }); if(text) await avatar.command(text); }catch{ process.exit(0) } await this.prompt(); } async prompt1(){ const username = this.#session.username; const avatar = this.#session.user.avatar; //console.log('await avatar.completions()',await avatar.completions()) const prompt = new enquirer.AutoComplete({ name: 'action', message: username, limit: 10, choices: await avatar.completions() }); const input = await prompt.run(); await avatar.command(input); // check is there are any other prompts//// await this.prompt(); } async data(data){ console.log(JSON.stringify(data, null, ' ')) } async print(data){ const username = this.#session.username; const avatar = this.#session.user.avatar; if(data.type == 'alert'){ if(data.kind == 'info'){ console.log(`${chalk.white.bgBlue('@'+username)}${chalk.blue('\uE0B0')} ${data.text}`); }else if(data.kind == 'warning'){ console.log(`${chalk.red.bgYellow('@'+username)}${chalk.yellow('\uE0B0')} ${data.text}`); }else if(data.kind == 'error'){ console.log(`${chalk.black.bgRed('@'+username)}${chalk.red('\uE0B0')} ${data.text}`); }else{ console.log(data.text); } }else{ console.log(data); } } async input(data){ const {name, message, initial} = Object.assign({name:'value', message:'', initial:''}, data); const prompt = new enquirer.Input({ message, initial, name, }); const value = await prompt.run(); return {[name]:value}; } async select(data){ const {name, message, choices} = Object.assign({name:'value', message:'', choices:[]}, data); // NOTE: structure of choices:[ // {name:'I feel great!', value='true'}, // {name:'I don\'t feel so good.', value='false'}, // ] const prompt = new enquirer.Select({ name, message, choices, }); const value = await prompt.run(); const chosen = choices.filter(i=>i.name === value).map(i=>i.value).pop() return {[name]:chosen}; } }