mirror of
https://github.com/embarklabs/neo-blessed.git
synced 2025-01-10 19:16:20 +00:00
text alignment. first step to deprecate Text.
This commit is contained in:
parent
bca271fbe6
commit
d9a0b49bce
@ -228,6 +228,7 @@ The base element.
|
||||
- **input** - element is focusable and can receive key input.
|
||||
- **hidden** - whether the element is hidden.
|
||||
- **label** - a simple text label for the element.
|
||||
- **align** - text alignment: `left`, `center`, or `right`.
|
||||
|
||||
##### Properties:
|
||||
|
||||
@ -286,6 +287,7 @@ An element similar to Box, but geared towards rendering simple text elements.
|
||||
- inherits all from Element.
|
||||
- **fill** - fill the entire line with chosen bg until parent bg ends, even if
|
||||
there is not enough text to fill the entire width.
|
||||
- **align** - text alignment: `left`, `center`, or `right`.
|
||||
|
||||
Inherits all options, properties, events, and methods from Element.
|
||||
|
||||
|
@ -572,6 +572,7 @@ function Element(options) {
|
||||
this.invisible = options.invisible ? 16 : 0;
|
||||
|
||||
this.fixed = options.fixed || false;
|
||||
this.align = options.align || 'left';
|
||||
this.border = options.border;
|
||||
if (this.border) {
|
||||
this.border.type = this.border.type || 'bg';
|
||||
@ -980,7 +981,8 @@ Box.prototype.render = function(stop) {
|
||||
if (this.hidden) return;
|
||||
|
||||
var lines = this.screen.lines
|
||||
, xi = this.left
|
||||
, xi_ = this.left
|
||||
, xi
|
||||
, xl = this.screen.cols - this.right
|
||||
, yi = this.top
|
||||
, yl = this.screen.rows - this.bottom
|
||||
@ -995,7 +997,7 @@ Box.prototype.render = function(stop) {
|
||||
, c;
|
||||
|
||||
if (this.position.width) {
|
||||
xl = xi + this.width;
|
||||
xl = xi_ + this.width;
|
||||
}
|
||||
|
||||
if (this.position.height) {
|
||||
@ -1007,9 +1009,7 @@ Box.prototype.render = function(stop) {
|
||||
, visible = this.parent.height - (this.parent.border ? 2 : 0);
|
||||
|
||||
yi -= this.parent.childBase;
|
||||
// if (noOverflow) ...
|
||||
yl = Math.min(yl, this.screen.rows - this.parent.bottom);
|
||||
// yl -= this.parent.childBase; // necessary here, but not necessary in Text.render for some reason.
|
||||
yl = Math.min(yl, this.screen.rows - this.parent.bottom - (this.parent.border ? 1 : 0));
|
||||
|
||||
if (rtop - this.parent.childBase < 0) {
|
||||
return;
|
||||
@ -1019,8 +1019,23 @@ Box.prototype.render = function(stop) {
|
||||
}
|
||||
}
|
||||
|
||||
if (this.align === 'center' || this.align === 'right') {
|
||||
var width = this.width - (this.border ? 2 : 0);
|
||||
var ncontent = content.split('\n')[0].replace(/\x1b\[[^m]*m/g, '');
|
||||
if (ncontent.length < width) {
|
||||
var s = width - 1 - ncontent.length - 1;
|
||||
if (this.align === 'center') {
|
||||
//xi_ += s / 2 | 0;
|
||||
content = Array(s / 2 | 0).join(' ') + content;
|
||||
} else {
|
||||
//xi_ += s;
|
||||
content = Array(s).join(' ') + content;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var ret = {
|
||||
xi: xi,
|
||||
xi: xi_,
|
||||
xl: xl,
|
||||
yi: yi,
|
||||
yl: yl
|
||||
@ -1051,7 +1066,7 @@ Box.prototype.render = function(stop) {
|
||||
|
||||
for (; yi < yl; yi++) {
|
||||
if (!lines[yi]) break;
|
||||
for (xi = this.left; xi < xl; xi++) {
|
||||
for (xi = xi_; xi < xl; xi++) {
|
||||
cell = lines[yi][xi];
|
||||
if (!cell) break;
|
||||
|
||||
@ -1131,6 +1146,7 @@ Box.prototype.render = function(stop) {
|
||||
function Text(options) {
|
||||
Element.call(this, options);
|
||||
this.full = options.full;
|
||||
this.align = options.align || 'left';
|
||||
}
|
||||
|
||||
Text.prototype.__proto__ = Element.prototype;
|
||||
@ -1143,7 +1159,8 @@ Text.prototype.render = function(stop) {
|
||||
if (this.hidden) return;
|
||||
|
||||
var lines = this.screen.lines
|
||||
, xi = this.left
|
||||
, xi_ = this.left
|
||||
, xi
|
||||
, xl = this.screen.cols - this.right
|
||||
, yi = this.top
|
||||
, yl = this.screen.rows - this.bottom
|
||||
@ -1158,7 +1175,7 @@ Text.prototype.render = function(stop) {
|
||||
, c;
|
||||
|
||||
if (this.position.width) {
|
||||
xl = xi + this.width;
|
||||
xl = xi_ + this.width;
|
||||
}
|
||||
|
||||
if (this.position.height) {
|
||||
@ -1166,7 +1183,7 @@ Text.prototype.render = function(stop) {
|
||||
}
|
||||
|
||||
if (this.full) {
|
||||
xl = xi + this.parent.width - (this.parent.border ? 2 : 0) - this.rleft - this.rright;
|
||||
//xl -= this.parent.border ? 2 : 0;
|
||||
}
|
||||
|
||||
if (this.parent.childBase != null && ~this.parent.items.indexOf(this)) {
|
||||
@ -1174,8 +1191,7 @@ Text.prototype.render = function(stop) {
|
||||
, visible = this.parent.height - (this.parent.border ? 2 : 0);
|
||||
|
||||
yi -= this.parent.childBase;
|
||||
// if (noOverflow) ...
|
||||
yl = Math.min(yl, this.screen.rows - this.parent.bottom);
|
||||
yl = Math.min(yl, this.screen.rows - this.parent.bottom - (this.parent.border ? 1 : 0));
|
||||
|
||||
if (rtop - this.parent.childBase < 0) {
|
||||
return;
|
||||
@ -1185,8 +1201,23 @@ Text.prototype.render = function(stop) {
|
||||
}
|
||||
}
|
||||
|
||||
if (this.align === 'center' || this.align === 'right') {
|
||||
var width = this.width - (this.border ? 2 : 0);
|
||||
var ncontent = content.split('\n')[0].replace(/\x1b\[[^m]*m/g, '');
|
||||
if (ncontent.length < width) {
|
||||
var s = width - 1 - ncontent.length - 1;
|
||||
if (this.align === 'center') {
|
||||
//xi_ += s / 2 | 0;
|
||||
content = Array(s / 2 | 0).join(' ') + content;
|
||||
} else {
|
||||
//xi_ += s;
|
||||
content = Array(s).join(' ') + content;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var ret = {
|
||||
xi: xi,
|
||||
xi: xi_,
|
||||
xl: xl,
|
||||
yi: yi,
|
||||
yl: yl
|
||||
@ -1199,7 +1230,7 @@ Text.prototype.render = function(stop) {
|
||||
|
||||
for (; yi < yl; yi++) {
|
||||
if (!lines[yi]) break;
|
||||
for (xi = this.left; xi < xl; xi++) {
|
||||
for (xi = xi_; xi < xl; xi++) {
|
||||
cell = lines[yi][xi];
|
||||
if (!cell) break;
|
||||
|
||||
@ -1384,14 +1415,16 @@ List.prototype.add = function(item) {
|
||||
var self = this;
|
||||
|
||||
// TODO: Use box here and get rid of text.
|
||||
var item = new Text({
|
||||
var item = new Box({
|
||||
screen: this.screen,
|
||||
parent: this,
|
||||
fg: this.fg,
|
||||
bg: this.bg,
|
||||
content: item.content || item,
|
||||
align: this.align || 'left',
|
||||
top: this.items.length + (this.border ? 1 : 0),
|
||||
left: (this.border ? 1 : 0) + 1,
|
||||
right: (this.border ? 1 : 0) + 1,
|
||||
full: true,
|
||||
height: 1
|
||||
});
|
||||
|
@ -8,7 +8,9 @@ var screen = new blessed.Screen({
|
||||
screen.append(new blessed.Text({
|
||||
top: 0,
|
||||
left: 2,
|
||||
content: 'Welcome to my program'
|
||||
width: '100%',
|
||||
content: 'Welcome to my program',
|
||||
align: 'center'
|
||||
}));
|
||||
|
||||
screen.append(new blessed.Line({
|
||||
@ -56,6 +58,7 @@ screen.children[0].append(new blessed.Box({
|
||||
*/
|
||||
|
||||
var list = new blessed.List({
|
||||
align: 'center',
|
||||
mouse: true,
|
||||
fg: 'blue',
|
||||
bg: 'default',
|
||||
|
Loading…
x
Reference in New Issue
Block a user