improve Log element.

This commit is contained in:
Christopher Jeffrey 2015-03-31 01:31:43 -07:00
parent 6154859110
commit a266a869b7
3 changed files with 93 additions and 11 deletions

View File

@ -1027,15 +1027,21 @@ A log permanently scrolled to the bottom.
##### Options: ##### Options:
- inherits all from ScrollableText. - inherits all from ScrollableText.
- __scrollback__ - amount of scrollback allowed. default: Infinity.
- __scrollOnInput__ - scroll to bottom on input even if the user has scrolled
up. default: false.
##### Properties: ##### Properties:
- inherits all from ScrollableText. - inherits all from ScrollableText.
- __scrollback__ - amount of scrollback allowed. default: Infinity.
- __scrollOnInput__ - scroll to bottom on input even if the user has scrolled
up. default: false.
##### Events: ##### Events:
- inherits all from ScrollableText. - inherits all from ScrollableText.
- __line__ - emitted on a log line. passes in line. - __log__ - emitted on a log line. passes in line.
##### Methods: ##### Methods:

View File

@ -11,6 +11,7 @@
var EventEmitter = require('./events').EventEmitter var EventEmitter = require('./events').EventEmitter
, assert = require('assert') , assert = require('assert')
, path = require('path') , path = require('path')
, util = require('util')
, fs = require('fs'); , fs = require('fs');
var colors = require('./colors') var colors = require('./colors')
@ -4003,6 +4004,7 @@ Element.prototype.shiftLine = function(n) {
}; };
Element.prototype.pushLine = function(line) { Element.prototype.pushLine = function(line) {
if (!this.content) return this.setLine(0, line);
return this.insertLine(this._clines.fake.length, line); return this.insertLine(this._clines.fake.length, line);
}; };
@ -4423,12 +4425,11 @@ function ScrollableText(options) {
return new ScrollableText(options); return new ScrollableText(options);
} }
options = options || {}; options = options || {};
options.scrollable = true;
options.alwaysScroll = true; options.alwaysScroll = true;
Box.call(this, options); ScrollableBox.call(this, options);
} }
ScrollableText.prototype.__proto__ = Box.prototype; ScrollableText.prototype.__proto__ = ScrollableBox.prototype;
ScrollableText.prototype.type = 'scrollable-text'; ScrollableText.prototype.type = 'scrollable-text';
@ -6779,11 +6780,19 @@ function Log(options) {
ScrollableText.call(this, options); ScrollableText.call(this, options);
this.scrollback = options.scrollback != null
? options.scrollback
: Infinity;
this.scrollOnInput = options.scrollOnInput;
this.on('set content', function() { this.on('set content', function() {
nextTick(function() { if (!self._userScrolled || self.scrollOnInput) {
self.setScrollPerc(100); nextTick(function() {
self.screen.render(); self.setScrollPerc(100);
}); self._userScrolled = false;
self.screen.render();
});
}
}); });
} }
@ -6792,9 +6801,29 @@ Log.prototype.__proto__ = ScrollableText.prototype;
Log.prototype.type = 'log'; Log.prototype.type = 'log';
Log.prototype.log = Log.prototype.log =
Log.prototype.add = function(text) { Log.prototype.add = function() {
this.emit('line', text); var args = Array.prototype.slice.call(arguments);
return this.pushLine(text); if (typeof args[0] === 'object') {
args[0] = util.inspect(args[0], true, 20, true);
}
var text = util.format.apply(util, args);
this.emit('log', text);
var ret = this.pushLine(text);
if (this._clines.fake.length > this.scrollback) {
this.shiftLine(0, (this.scrollback / 3) | 0);
}
return ret;
};
Log.prototype._scroll = Log.prototype.scroll;
Log.prototype.scroll = function(offset, always) {
if (offset === 0) return this._scroll(offset, always);
this._userScrolled = true;
var ret = this._scroll(offset, always);
if (this.getScrollPerc() === 100) {
this._userScrolled = false;
}
return ret;
}; };
/** /**

47
test/widget-log.js Normal file
View File

@ -0,0 +1,47 @@
var blessed = require('../')
, screen;
screen = blessed.screen({
dump: __dirname + '/logs/logger.log',
smartCSR: true,
autoPadding: false
});
var logger = blessed.log({
parent: screen,
top: 'center',
left: 'center',
width: '50%',
height: '50%',
border: 'line',
tags: true,
keys: true,
vi: true,
mouse: true,
scrollback: 100,
scrollbar: {
ch: ' ',
track: {
bg: 'yellow'
},
style: {
inverse: true
}
}
});
logger.focus();
setInterval(function() {
logger.log('Hello {green-fg}world{/}: {bold}%s{/bold}.', Date.now().toString(36));
if (Math.random() < 0.30) {
logger.log({foo:{bar:{baz:true}}});
}
screen.render();
}, 1000).unref();
screen.key('q', function() {
return process.exit(0);
});
screen.render();