Created
May 24, 2015 19:01
-
-
Save RodrigoEspinosa/94bf941ba8973eacf753 to your computer and use it in GitHub Desktop.
Example shown in http://recodes.co/create-blessed-custom-wigets/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Blessed = require('blessed'); | |
var screen = Blessed.screen({ | |
smartCSR: true, | |
useBCE: true | |
}); | |
var ChatHistory = function (options) { | |
if (!(this instanceof Blessed.Node)) { | |
return new ChatHistory(options); | |
} | |
options = options || {}; | |
options.shrink = true; | |
Blessed.Element.call(this, options); | |
}; | |
ChatHistory.prototype = Object.create(Blessed.Element.prototype); | |
ChatHistory.prototype.type = 'chatHistory'; | |
// Define a instance property called `messages` that will store the message list. | |
ChatHistory.prototype._messages = []; | |
/** | |
* Add a message to the history. | |
* @param {String} msg | |
*/ | |
ChatHistory.prototype.addMessage = function (msg) { | |
var self = this; | |
// Store the message into the widget list. | |
this._messages.push(msg); | |
}; | |
ChatHistory.prototype.render = function () { | |
// Set the widget content. | |
this.setContent(this._messages.join('\n')); | |
// Call the super. | |
return this._render(); | |
}; | |
// Create a new `ChatHistory` instance. | |
var chat = ChatHistory({ | |
height: '100%', | |
padding: 1, | |
tags: true, | |
width: '100%' | |
}); | |
// Display dummy messages. | |
chat.addMessage('People assume that time is a strict progression of cause to effect,'); | |
chat.addMessage('but {underline}actually{/underline} from a non-linear, non-subjective viewpoint -'); | |
chat.addMessage('it\'s more like a big ball of wibbly wobbly... time-y wimey... stuff.'); | |
// Append the chat to the screen. | |
screen.append(chat); | |
// Render the screen. | |
screen.render(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment