@jacoobanderson/interface-creator
v1.0.9
Published
A module that helps in creating a terminal user interface
Downloads
3
Maintainers
Readme
Installation and Usage
This is a Node.js module that is installed using:
$ npm i @jacoobanderson/interface-creator
Import the module and initialize a new interface creator by writing:
import { InterfaceCreator } from '@jacoobanderson/interface-creator'
const ui = new InterfaceCreator()
You can then call the methods on the ui by:
const ui = new InterfaceCreator()
ui.setColor('menu', 'red')
Methods
This is a list of the different methods that are offered:
| Method | Description |
| ------------------- | ---------------------------------------------|
| createPrompt(string, function, color) | Creates a string/question and also prompts the user and allows for the user's input to be handled. string
is a string that shows before the user prompt.function
is a function that takes the input as a parameter and handles the input.color
is the color that the first parameter string should be. |
| addExitOption() | Adds an exit option in the main menu. |
| setMainMenu(menuOptions) | Sets the main menu view. menuOptions
is a object that should contain numbered keys starting from 1 with values set to the strings that wants to be shown in the menu. |
| assignMainMenuFunctionality(menuFunctionality) | Assigns the functionality needed for the main menu view menuFunctionality
is an object that should contain numbered keys starting from 1 and correlate to the menuOptions, the values should be functions which will be called when the correspondent menuOption is chosen. |
| createSubMenu(view, functionality) | This method is supposed to be used in another menu, i.e in the main menu as a value of the menuFunctionality object or in another submenu functionality object. view
is a object that should contain numbered keys starting from 1 with values set to the strings that wants to be shown in the submenu. functionality
is an object that should contain numbered keys starting from 1 and correlate to the view, the values should be functions which will be called when the correspondent view option is chosen. |
| addReturnToMenuOption() | Adds a return to menu option in sub menus or after main menu functionality has been called. |
| createForm(questions) | Used to create a form which supports both regular questions with written answers and multichoice questions. createForm needs to be put inside an async function, should be returned to extract the answers questions
is an array that contains strings for regular questions with written answers and an object with the question as the key and and array of multichoice answers as the value for multichoice questions. |
| start() | Is the method that initializes the user interface and is supposed to be put after all other methods in the code. This method is essential for it all to work. It can also be used as a method to return to the main menu if needed. |
| setColor(section, color) | Sets the color of a certain section in the user interface section
is the section that the color should apply to. color
is the color that the section should have. All the available colors and sections are shown down below. |
Available color sections
menu
The main menu.
exit
The exit message.
returnToMenu
The return to menu message.
form
The form.
Available colors
red
green
yellow
blue
cyan
Method examples
ui.createPrompt('What is your name?', (user) => console.log('Welcome, ' + user + '\n'), 'blue')
ui.setMainMenu({
1: 'Go to sub menu',
2: 'Divide values',
3: 'Multiply values'
})
const view = {
1: 'Add values',
2: 'Form',
3: 'Subtract values'
}
const functionality = {
1: () => addValues(1, 3),
2: async () => await functionWithCreateForm(),
3: () => subtractValues(23, 34)
}
ui.assignMainMenuFunctionality({
1: () => ui.createSubMenu(view, functionality),
2: () => divideValues(),
3: () => multiplyValues()
})
// Creates form and returns answers
async function form() {
return await ui.createForm([
'What is your name?',
'What is your cats name?',
// Question with multi choice answers.
{
'Which country do you live in?': ['Sweden', 'Norway', 'Denmark']
}
])
}
const data = await form()
// Shows the form data.
console.log(data)
ui.setColor('form', 'blue')