Mantra official documentation

38-mantra-console-api-reference.md

A  A  A 

Mantra Console API Reference

Mantra Console API object is get as an Utils property:

const MantraConsole = Mantra.Utils.Console;

This object has a number of useful and common methods to manage console messages and prompts.

Because of the nature of Node.js, within the same application, the same process.stdin and process.stdout should be used in the application running in the same process.

Mantra.Utils.Console.error

error(msg, withDate = true)

Shows in the console an information message in red.

Param:

  • msg: string with the message to show.
  • withDate: if true, then the message includes the date time.

Mantra.Utils.Console.getReadlineInterface

Property that returns the ReadLine interface used by Mantra, equivalent to:

const readLineInterface = require("readline").
                            createInterface({input: process.stdin, output: process.stdout});

Mantra.Utils.Console.info

info(msg, withDate = true)

Shows in the console an information message in green.

Param:

  • msg: string with the message to show.
  • withDate: if true, then the message includes the date time.

Mantra.Utils.Console.newline

newLine()

Simply adds a new empty line to the console.

Mantra.Utils.Console.question

async question(msg, allowEmpty)

Prompts a question and returns the result typed by the user.

Params:

  • msg: question message to show.
  • allowEmpty: if true, then the methods accepts no entry as a result.

Mantra.Utils.Console.questionWithOpts

async questionWithOpts( msg, opts )

Prompts a question expecting a number for some options.

Params:

  • msg: question message to show.
  • opts: array with string messages to show with the options.

For instance:

const optionSelected = await Mantra.Utils.Console.questionWithOpts( "Choose option:", 
    ["Add item", "Delete Item", "Show all items"] );

Returns a zero-base number with de option typed by the user.

Mantra.Utils.Console.rawInfo

rawInfo(msg)

Show in the console the string message of the parameter. Equivalent to console.log() method.

Param:

  • msg: string with the message to show.

Mantra.Utils.Console.warning

warning(msg, withDate = true)

Shows in the console a warning message in orange.

Param:

  • msg: string with the message to show.
  • withDate: if true, then the message includes the date time.

To learn by example, go to Mantra demos and components sections of Mantra site.