Figlet, a beautiful way to print ASCII Arts in Javascript

Views: 414   

Wherever you see ASCII Arts they immediately grab your attention. Be it in application consoles while you are booting the application or in the browser consoles. Following are some of the examples of it.

Material UI browser console

Spring Boot logo on console

Figlet is a NPM library that helps you creating ASCII Arts in NodeJS applications. You can install it from npm registry by running the following command.

npm install figlet --save

Following code shows you how to use the library with different available options.

const figlet = require("figlet");

figlet('Hello World!', function(err, data) {
    if (err) {
        console.log('Something went wrong...', err);
    }else{
        console.log(data);
    }
});

Output:

  _   _      _ _        __        __         _     _ _ _ 
 | | | | ___| | | ___   \ \      / /__  _ __| | __| | | |
 | |_| |/ _ \ | |/ _ \   \ \ /\ / / _ \| '__| |/ _` | | |
 |  _  |  __/ | | (_) |   \ V  V / (_) | |  | | (_| |_|_|
 |_| |_|\___|_|_|\___/     \_/\_/ \___/|_|  |_|\__,_(_|_)

We can also pass options to the library where we can specify a different font, horizontalLayout and verticalLayout options.
Following example shows how to print the same Hello World using a different font.

const figlet = require("figlet");

figlet('Hello World!', {font: 'Crawford'}, function(err, data) {
    if (err) {
        console.log('Something went wrong...', err);
    }else{
        console.log(data);
    }
});

Output:

 __ __    ___  _      _       ___       __    __   ___   ____   _      ___    __ 
|  T  T  /  _]| T    | T     /   \     |  T__T  T /   \ |    \ | T    |   \  |  T
|  l  | /  [_ | |    | |    Y     Y    |  |  |  |Y     Y|  D  )| |    |    \ |  |
|  _  |Y    _]| l___ | l___ |  O  |    |  |  |  ||  O  ||    / | l___ |  D  Y|__j
|  |  ||   [_ |     T|     T|     |    l  `  '  !|     ||    \ |     T|     | __ 
|  |  ||     T|     ||     |l     !     \      / l     !|  .  Y|     ||     ||  T
l__j__jl_____jl_____jl_____j \___/       \_/\_/   \___/ l__j\_jl_____jl_____jl__j

We can also install the library globally and use the figlet from the command prompt.
Following is the command you need run to install it globally

npm install -g figlet-cli

Once installed you can start printing the ASCII fonts using the below command.

figlet -f "Crawford" "Hello World!"
On By