From 9bbe6a3a74a4ec418a3058520c6376f8f162e3d1 Mon Sep 17 00:00:00 2001 From: Rahul Patni Date: Mon, 12 Feb 2024 16:23:34 -0800 Subject: [PATCH] large prompt follows cli ux required interface --- ironfish-cli/src/commands/wallet/import.ts | 14 +++----------- ironfish-cli/src/utils/longPrompt.ts | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/ironfish-cli/src/commands/wallet/import.ts b/ironfish-cli/src/commands/wallet/import.ts index 4c1cd91350..1f90804e6a 100644 --- a/ironfish-cli/src/commands/wallet/import.ts +++ b/ironfish-cli/src/commands/wallet/import.ts @@ -150,16 +150,8 @@ export class ImportCommand extends IronfishCommand { } async importTTY(): Promise { - let userInput = ( - await largePrompt('Paste the output of wallet:export, or your spending key: ') - ).trim() - - while (userInput.length === 0) { - userInput = ( - await largePrompt('Paste the output of wallet:export, or your spending key: ') - ).trim() - } - - return userInput + return await largePrompt('Paste the output of wallet:export, or your spending key: ', { + required: true, + }) } } diff --git a/ironfish-cli/src/utils/longPrompt.ts b/ironfish-cli/src/utils/longPrompt.ts index bd50d9ad3c..fa989bce52 100644 --- a/ironfish-cli/src/utils/longPrompt.ts +++ b/ironfish-cli/src/utils/longPrompt.ts @@ -4,7 +4,7 @@ import readline from 'readline' // Most effective way to take in a large textual prompt input without affecting UX -export function largePrompt(question: string): Promise { +function fetchResponse(question: string): Promise { const rl = readline.createInterface({ input: process.stdin, output: process.stdout, @@ -17,3 +17,18 @@ export function largePrompt(question: string): Promise { }) }) } + +export async function largePrompt( + question: string, + options?: { + required?: boolean + }, +): Promise { + let userInput = (await fetchResponse(question)).trim() + + while (userInput.length === 0 && options?.required) { + userInput = (await fetchResponse(question)).trim() + } + + return userInput +}