lists. misc.

This commit is contained in:
Christopher Jeffrey 2013-06-01 02:06:04 -05:00
parent 7e85f82426
commit 4fe0e98704
2 changed files with 169 additions and 29 deletions

View File

@ -12,40 +12,77 @@ screen = new blessed.Screen({
program: program
});
/*
screen.append(new blessed.Box({
screen: screen,
parent: screen,
fg: 3,
bg: 5,
fg: 4,
bg: -1,
border: {
type: 'ascii',
fg: 1
fg: -1,
bg: -1
},
content: 'Hello world!',
//width: 30,
//height: 15,
width: '50%',
height: '50%',
top: 'center',
left: 'center'
}));
screen.children[0].append(new blessed.Box({
screen: screen,
parent: screen.children[0],
fg: 4,
bg: 3,
border: {
type: 'bg',
fg: 0,
bg: 1,
ch: '/'
},
content: 'Foobar',
width: '50%',
height: '50%',
top: 'center',
left: 'center'
}));
*/
screen.append(new blessed.List({
screen: screen,
parent: screen,
fg: 4,
bg: -1,
border: {
type: 'ascii',
fg: -1,
bg: -1
},
width: '50%',
height: '50%',
top: 'center',
left: 'center',
selectedBg: 2,
items: [
{ content: 'one' },
{ content: 'two' },
{ content: 'three' },
{ content: 'four' }
]
}));
program.on('keypress', function(ch, key) {
if (key.name === 'escape' || key.name === 'q') {
exit(0);
}
});
program.clear();
program.alternateBuffer();
program.hideCursor();
function exit(c) {
program.disableMouse();
program.clear();
program.showCursor();
program.normalBuffer();
return process.exit(c || 0);
return process.exit(0);
}
});
program.alternateBuffer();
program.hideCursor();
screen.render();

View File

@ -88,7 +88,6 @@ function Screen(options) {
this.program.on('resize', function() {
self.alloc();
self.program.clear();
self.render();
});
}
@ -263,8 +262,21 @@ function Element(options) {
this.bold = options.bold ? 1 : 0;
this.underline = options.underline ? 2 : 0;
if (this.fg === -1) this.fg = exports.NORMAL;
if (this.bg === -1) this.bg = exports.NORMAL;
this.fixed = options.fixed || false;
this.border = options.border;
if (this.border) {
this.border.type = this.border.type || 'bg';
this.border.fg = this.border.fg || -1;
this.border.bg = this.border.bg || -1;
this.border.ch = this.border.ch || ' ';
this.border.bold = this.border.bold ? 1 : 0;
this.border.underline = this.border.underline ? 2 : 0;
if (this.border.fg === -1) this.border.fg = exports.NORMAL;
if (this.border.bg === -1) this.border.bg = exports.NORMAL;
}
this.children = options.children || [];
@ -398,6 +410,15 @@ Box.prototype.render = function() {
for (yi = this.top; yi < yl; yi++) {
for (xi = this.left; xi < xl; xi++) {
if (this.border && (yi === this.top || xi === this.left || yi === yl - 1 || xi === xl - 1)) {
/*
if (this.border && (
(yi === this.top && this.border.top !== false)
|| (xi === this.left && this.border.left !== false)
|| (yi === yl - 1 && this.border.bottom !== false)
|| (xi === xl - 1 && this.border.right !== false))) {
*/
attr = ((this.border.bold << 18) + (this.border.underline << 18)) | (this.border.fg << 9) | this.border.bg;
if (this.border.type === 'ascii') {
if (yi === this.top) {
@ -412,7 +433,7 @@ Box.prototype.render = function() {
ch = '│';
}
} else if (this.border.type === 'bg') {
ch = ' ';
ch = this.border.ch;
}
} else {
attr = ((this.bold << 18) + (this.underline << 18)) | (this.fg << 9) | this.bg;
@ -438,6 +459,7 @@ Box.prototype.render = function() {
function Text(options) {
Element.call(this, options);
this.full = options.full;
}
Text.prototype.__proto__ = Element.prototype;
@ -454,20 +476,38 @@ Text.prototype.render = function() {
, ci = 0
, cl = this.content.length;
if (this.full) {
this.position.width = '100%';
this.position.width = this.width - 4; // TODO: FIX
}
if (this.position.width) {
xl = this.width;
xl = xi + this.width;
}
if (this.position.height) {
yl = this.height;
yl = yi + this.height;
}
var ended = -1;
for (yi = this.top; yi < yl; yi++) {
for (xi = this.left; xi < xl; xi++) {
cell = lines[yi][xi];
attr = ((this.bold << 18) + (this.underline << 18)) | (this.fg << 9) | this.bg;
ch = this.content[ci++];
if (!ch) break;
if (!ch) {
if (this.full) {
if (ended === -1) ended = yi;
if (yi === ended) {
ch = ' ';
} else {
break;
}
} else {
break;
}
}
if (attr !== cell[0] || ch !== cell[1]) {
lines[yi][xi][0] = attr;
lines[yi][xi][1] = ch;
@ -515,16 +555,20 @@ ScrollableBox.prototype.render = function() {
function List(options) {
ScrollableBox.call(this, options);
this.children = [];
this.children = options.children || [];
this.selected = 0;
this.selectedBg = options.selectedBg;
this.selectedFg = options.selectedFg;
this.selectedBold = options.selectedBold;
this.selectedUnderline = options.selectedUnderline;
this.selectedBg = options.selectedBg || -1;
this.selectedFg = options.selectedFg || -1;
this.selectedBold = options.selectedBold ? 1 : 0;
this.selectedUnderline = options.selectedUnderline ? 2 : 0;
if (options.children) {
options.children.forEach(this.add.bind(this));
if (this.selectedBg === -1) this.selectedBg = exports.NORMAL;
if (this.selectedFg === -1) this.selectedFg = exports.NORMAL;
if (options.items) {
options.items.forEach(this.add.bind(this));
this.select(this.selected);
}
}
@ -534,9 +578,11 @@ List.prototype.add = function(item) {
this.append(new Text({
screen: this.screen,
parent: this,
top: this.children.length,
content: item.content,
height: 1
top: this.children.length + 1,
left: 2,
full: true
//height: 1 // TODO: FIX
}));
};
@ -605,6 +651,62 @@ function Button(options) {
Button.prototype.__proto__ = Input.prototype;
/**
* ProgressBar
*/
function ProgressBar(options) {
Input.call(this, options);
this.filled = options.filled || 0;
if (typeof this.filled === 'string') {
this.filled = +this.filled.slice(0, -1);
}
this.ch = options.ch || ' ';
this.barFg = options.barFg || -1;
this.barBg = options.barBg || -1;
if (this.barFg === -1) this.barFg = exports.NORMAL;
if (this.barBg === -1) this.barBg = exports.NORMAL;
}
ProgressBar.prototype.__proto__ = Input.prototype;
ProgressBar.prototype._render = ProgressBar.prototype.render;
ProgressBar.prototype.render = function() {
this._render();
var x, y;
var h = this.height
, w = this.width * (this.filled / 100)
, l = this.left
, t = this.top;
for (y = t; y < t + h; y++) {
for (x = l; x < l + w; x++) {
attr = ((this.bold << 18) + (this.underline << 18)) | (this.barFg << 9) | this.barBg;
ch = this.ch;
cell = lines[yi][xi];
if (attr !== cell[0] || ch !== cell[1]) {
lines[yi][xi][0] = attr;
lines[yi][xi][1] = ch;
lines[yi].dirty = true;
}
}
}
};
ProgressBar.prototype.progress = function(filled) {
this.filled += filled;
if (this.filled < 0) this.filled = 0;
else if (this.filled > 100) this.filled = 100;
};
/**
* Constants
*/
exports.NORMAL = 0x1ff;
/**
* Expose
*/
@ -617,3 +719,4 @@ exports.List = List;
exports.Input = Input;
exports.Textbox = Textbox;
exports.Button = Button;
exports.ProgressBar = ProgressBar;