Abstracts out the multiple choice prompt

This commit is contained in:
Ben 2025-02-25 14:37:28 +01:00
parent ef589cf085
commit b1e49eef87
No known key found for this signature in database
GPG Key ID: 0F16E812E736C24B
3 changed files with 51 additions and 4 deletions

View File

@ -92,7 +92,7 @@ export async function main() {
const uiService = new UiService();
const mainMenu = new MainMenu(uiService);
mainMenu.show();
await mainMenu.show();
return;
try {

View File

@ -1,5 +1,8 @@
import boxen from "boxen";
import chalk from "chalk";
import inquirer from "inquirer";
import { ASCII_ART } from "../constants/ascii.js";
function show(msg) {
console.log(msg);
@ -32,7 +35,7 @@ export class UiService {
);
};
showInfoMessage(message) {
showInfoMessage = (message) => {
show(
boxen(chalk.cyan(message), {
padding: 1,
@ -43,5 +46,34 @@ export class UiService {
titleAlignment: "center",
}),
);
}
};
showLogo = () => {
console.log("\n" + chalk.cyanBright(ASCII_ART));
};
askMultipleChoice = async (message, choices) => {
var counter = 1;
var promptChoices = [];
choices.forEach(function(choice) {
promptChoices.push(`${counter}. ${choice.label}`);
counter++;
});
const { choice } = await inquirer.prompt([
{
type: "list",
name: "choice",
message: message,
choices: promptChoices,
pageSize: counter - 1,
loop: true
}
]);
const selectStr = choice.split(".")[0];
const selectIndex = parseInt(selectStr) - 1;
await choices[selectIndex].action();
};
}

View File

@ -3,7 +3,22 @@ export class MainMenu {
this.ui = uiService;
}
show = () => {
show = async () => {
this.ui.showLogo();
this.ui.showInfoMessage("hello");
await this.ui.askMultipleChoice("Select an option",[
{
label: "optionOne",
action: async function() {
console.log("A!")
}
},{
label: "optionTwo",
action: async function() {
console.log("B!")
}
},
])
};
}