add padding option.

This commit is contained in:
Christopher Jeffrey 2013-06-13 19:02:02 -05:00
parent 05a8c9897b
commit 6669d199ef
3 changed files with 25 additions and 8 deletions

View File

@ -108,7 +108,7 @@ program.getCursor(function(err, data) {
### Example
This will render a box with ascii borders containing the text 'Hello world!',
centered horizontally and vertically.
perfectly centered horizontally and vertically.
``` js
var blessed = require('blessed')
@ -122,16 +122,16 @@ screen.append(new blessed.Box({
height: '50%',
border: {
type: 'ascii',
fg: 1
fg: 'white'
},
fg: 3,
bg: 5,
fg: 'white',
bg: 'magenta',
content: 'Hello world!',
}));
screen.on('keypress', function(ch, key) {
if (key.name === 'escape') {
process.exit(0);
return process.exit(0);
}
});
@ -233,6 +233,7 @@ The base element.
- **label** - a simple text label for the element.
- **align** - text alignment: `left`, `center`, or `right`.
- **shrink** - shrink/flex/grow to content width during render.
- **padding** - amount of padding on the inside of the element.
##### Properties:
@ -458,7 +459,7 @@ When an element is created, it can be given coordinates in its constructor:
var box = new blessed.Box({
left: 'center',
top: 'center',
bg: 3,
bg: 'yellow',
width: '50%',
height: '50%'
});

View File

@ -580,6 +580,7 @@ function Element(options) {
this.fixed = options.fixed || false;
this.align = options.align || 'left';
this.shrink = options.shrink;
this.padding = options.padding || 0;
this.border = options.border;
if (this.border) {
this.border.type = this.border.type || 'bg';
@ -1075,6 +1076,11 @@ Box.prototype.render = function(stop) {
if (this.border) yi_++, yl--, xi_++, xl--;
if (this.padding) {
yi_ += this.padding, yl -= this.padding;
xi_ += this.padding, xl -= this.padding;
}
outer:
for (yi = yi_; yi < yl; yi++) {
if (!lines[yi]) break;
@ -1083,7 +1089,8 @@ outer:
if (!cell) break;
if (this.shrink && !content[ci] && yi === yi_) {
xl = xi + 1 - 1;
// Need to subtract 1 and padding for below.
xl = xi + 1 - 1 - this.padding;
break outer;
}
@ -1123,8 +1130,16 @@ outer:
}
}
// This seems redundant, but we need to draw the
// border second because of the `shrink` option.
if (this.border) yi_--, yl++, xi_--, xl++;
if (this.padding) {
yi_ -= this.padding, yl += this.padding;
xi_ -= this.padding, xl += this.padding;
}
if (this.border) {
yi_--, yl++, xi_--, xl++;
yi = yi_;
for (xi = xi_; xi < xl; xi++) {
if (!lines[yi]) break;

View File

@ -136,6 +136,7 @@ var lorem = require('fs').readFileSync(__dirname + '/git.diff', 'utf8');
//lorem = lorem.replace(/\x1b[^m]*m/g, '');
var stext = new blessed.ScrollableText({
//padding: 1,
mouse: true,
content: lorem,
fg: 'blue',