/ blessed

Being Blessed

Blessed is a high-level terminal interface library built in node.js that allows us to create impressive terminal applications easily.

You may want a simple application where the user inputs data and gets data, or something more complicated where the user interacts with the data.

Just npm install it:

$ npm i blessed

Getting started with Blessed

// Require the Blessed API. 
var Blessed = require('blessed'); 

// Initialize the screen widget.
var screen = Blessed.screen({
	// Example of optional settings:
	smartCSR: true,
	useBCE: true,
	cursor: {
		artificial: true,
		blink: true,
		shape: 'underline'
	},
	log: `${__dirname}/application.log`,
	debug: true,
	dockBorders: true
});

// Specify the title of the application.
screen.title = 'La pizza de Don Cangrejo.';

screen is your top-level component (or widget). It will be the oldest ancestor of all the widgets that you can append to the application, internally Blessed will be creating a tree with all the widgets.

Appending components

Blessed counts with an extensive amount of Widgets. There are prompts, inputs, buttons progress bars, file managers, loaders, and the list goes on and on.

This is how you append widgets:

// Creating a textarea on the bottom of the screen.
var input = Blessed.textarea({ 
	bottom: 0,        
	height: 3,        
	inputOnFocus: true,
	padding: {        
		top: 1,
		left: 2
	},
	style: {          
		fg: '#787878',
		bg: '#454545',  

		focus: {        
			fg: '#f6f6f6',
			bg: '#353535' 
		}
	}
});

// Append the widget to the screen.
screen.append(input);

// Render the screen.
screen.render();

This will create a functional input in the screen, lets see now how we can handle the events for the widgets.

Handling events

Events are all around Blessed, you can receive and handle: keys, clicks, scrolls, pre-render, render, and post-render events. Even events like terminal re-sizing!

// Quit on `q`, or `Control-C` when the focus is on the screen.
screen.key(['q', 'C-c'], function(ch, key) {
	process.exit(0);
});

// Focus on `escape` or `i` when focus is on the screen.
screen.key(['escape', 'i'], function() {
	// Set the focus on the input.
	input.focus();
});
    
// If box is focused, handle `Control+s`.
input.key('C-s', function(ch, key) {
	var message = this.getValue();
	// Send the message somehow.
	this.clearValue();
	screen.render();
});  

Examples

โœ‹ Slap

slap

Sublime-like terminal-based text editor.

GitterCLI

gitter-cli-example-1

An extremely simple Gitter client for your terminal

Continue with blessed-contrib

Blessed-contrib is a blessed extension that adds even more widgets to the blessed library.

blessed-contrib

Slides