mirror of
https://github.com/embarklabs/neo-blessed.git
synced 2025-02-02 22:24:55 +00:00
improve Log element.
This commit is contained in:
parent
6154859110
commit
a266a869b7
@ -1027,15 +1027,21 @@ A log permanently scrolled to the bottom.
|
||||
##### Options:
|
||||
|
||||
- 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:
|
||||
|
||||
- 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:
|
||||
|
||||
- inherits all from ScrollableText.
|
||||
- __line__ - emitted on a log line. passes in line.
|
||||
- __log__ - emitted on a log line. passes in line.
|
||||
|
||||
##### Methods:
|
||||
|
||||
|
@ -11,6 +11,7 @@
|
||||
var EventEmitter = require('./events').EventEmitter
|
||||
, assert = require('assert')
|
||||
, path = require('path')
|
||||
, util = require('util')
|
||||
, fs = require('fs');
|
||||
|
||||
var colors = require('./colors')
|
||||
@ -4003,6 +4004,7 @@ Element.prototype.shiftLine = function(n) {
|
||||
};
|
||||
|
||||
Element.prototype.pushLine = function(line) {
|
||||
if (!this.content) return this.setLine(0, line);
|
||||
return this.insertLine(this._clines.fake.length, line);
|
||||
};
|
||||
|
||||
@ -4423,12 +4425,11 @@ function ScrollableText(options) {
|
||||
return new ScrollableText(options);
|
||||
}
|
||||
options = options || {};
|
||||
options.scrollable = 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';
|
||||
|
||||
@ -6779,11 +6780,19 @@ function Log(options) {
|
||||
|
||||
ScrollableText.call(this, options);
|
||||
|
||||
this.scrollback = options.scrollback != null
|
||||
? options.scrollback
|
||||
: Infinity;
|
||||
this.scrollOnInput = options.scrollOnInput;
|
||||
|
||||
this.on('set content', function() {
|
||||
nextTick(function() {
|
||||
self.setScrollPerc(100);
|
||||
self.screen.render();
|
||||
});
|
||||
if (!self._userScrolled || self.scrollOnInput) {
|
||||
nextTick(function() {
|
||||
self.setScrollPerc(100);
|
||||
self._userScrolled = false;
|
||||
self.screen.render();
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -6792,9 +6801,29 @@ Log.prototype.__proto__ = ScrollableText.prototype;
|
||||
Log.prototype.type = 'log';
|
||||
|
||||
Log.prototype.log =
|
||||
Log.prototype.add = function(text) {
|
||||
this.emit('line', text);
|
||||
return this.pushLine(text);
|
||||
Log.prototype.add = function() {
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
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
47
test/widget-log.js
Normal 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();
|
Loading…
x
Reference in New Issue
Block a user