refactor shrink so is possible.

This commit is contained in:
Christopher Jeffrey 2015-07-22 05:36:25 -07:00
parent 18f3dfcc99
commit 80f91fad5a
6 changed files with 58 additions and 28 deletions

View File

@ -84,6 +84,9 @@ ANSIImage.prototype.setImage = function(file) {
var width = this.position.width;
var height = this.position.height;
if (width === 'shrink') width = null;
if (height === 'shrink') height = null;
if (width != null) {
width = this.width;
}

View File

@ -99,13 +99,17 @@ BigText.prototype.setContent = function(content) {
};
BigText.prototype.render = function() {
if (this.position.width == null || this._shrinkWidth) {
if (this.position.width === 'shrink'
|| this.position.width == null
|| this._shrinkWidth) {
// if (this.width - this.iwidth < this.ratio.width * this.text.length + 1) {
this.position.width = this.ratio.width * this.text.length + 1;
this._shrinkWidth = true;
// }
}
if (this.position.height == null || this._shrinkHeight) {
if (this.position.height === 'shrink'
|| this.position.height == null
|| this._shrinkHeight) {
// if (this.height - this.iheight < this.ratio.height + 0) {
this.position.height = this.ratio.height + 0;
this._shrinkHeight = true;

View File

@ -13,7 +13,8 @@ var assert = require('assert');
var colors = require('../colors')
, unicode = require('../unicode');
var nextTick = global.setImmediate || process.nextTick.bind(process);
var nextTick = global.setImmediate || process.nextTick.bind(process)
, String = global.String;
var helpers = require('../helpers');
@ -59,17 +60,6 @@ function Element(options) {
height: options.height
};
if (options.position.width === 'shrink'
|| options.position.height === 'shrink') {
if (options.position.width === 'shrink') {
delete options.position.width;
}
if (options.position.height === 'shrink') {
delete options.position.height;
}
options.shrink = true;
}
this.position = options.position;
this.noOverflow = options.noOverflow;
@ -1062,6 +1052,8 @@ Element.prototype._getWidth = function(get) {
, left
, expr;
if (width == 'shrink') width = null;
if (typeof width === 'string') {
if (width === 'half') width = '50%';
expr = width.split(/(?=\+|-)/);
@ -1111,6 +1103,8 @@ Element.prototype._getHeight = function(get) {
, top
, expr;
if (height == 'shrink') height = null;
if (typeof height === 'string') {
if (height === 'half') height = '50%';
expr = height.split(/(?=\+|-)/);
@ -1481,6 +1475,35 @@ Element.prototype.__defineSetter__('bottom', function(val) {
return this.rbottom = val;
});
// Legacy `shrink` property:
Element.prototype.__defineSetter__('shrink', function(value) {
if (value) {
if (this.position.width == null) {
this.position.width = 'shrink';
this._widthShrunk = true;
}
if (this.position.height == null) {
this.position.height = 'shrink';
this._heightShrunk = true;
}
} else {
if (this._widthShrunk) {
this.position.width = null;
this._widthShrunk = false;
}
if (this._heightShrink) {
this.position.height = null;
this._heightShrunk = false;
}
}
return value;
});
// Legacy `shrink` property:
Element.prototype.__defineGetter__('shrink', function() {
return this.position.width === 'shrink' || this.position.height === 'shrink';
});
/**
* Rendering - here be dragons
*/
@ -1550,7 +1573,7 @@ Element.prototype._getShrinkBox = function(xi, xl, yi, yl, get) {
//this.shrink = true;
}
if (this.position.width == null
if (this.position.width === 'shrink'
&& (this.position.left == null
|| this.position.right == null)) {
if (this.position.left == null && this.position.right != null) {
@ -1580,7 +1603,7 @@ Element.prototype._getShrinkBox = function(xi, xl, yi, yl, get) {
}
}
if (this.position.height == null
if (this.position.height === 'shrink'
&& (this.position.top == null
|| this.position.bottom == null)
&& (!this.scrollable || this._isList)) {
@ -1615,7 +1638,7 @@ Element.prototype._getShrinkContent = function(xi, xl, yi, yl, get) {
var h = this._clines.length
, w = this._clines.mwidth || 1;
if (this.position.width == null
if (this.position.width === 'shrink'
&& (this.position.left == null
|| this.position.right == null)) {
if (this.position.left == null && this.position.right != null) {
@ -1625,7 +1648,7 @@ Element.prototype._getShrinkContent = function(xi, xl, yi, yl, get) {
}
}
if (this.position.height == null
if (this.position.height === 'shrink'
&& (this.position.top == null
|| this.position.bottom == null)
&& (!this.scrollable || this._isList)) {

View File

@ -26,17 +26,17 @@ function Layout(options) {
options = options || {};
if ((options.width == null
&& (options.left == null && options.right == null))
|| (options.height == null
&& (options.top == null && options.bottom == null))) {
throw new Error('`Layout` must have a width and height!');
}
options.layout = options.layout || 'inline';
Element.call(this, options);
if (((this.position.width == null || this.position.width === 'shrink')
&& (this.position.left == null && this.position.right == null))
|| ((this.position.height == null || this.position.height === 'shrink')
&& (this.position.top == null && this.position.bottom == null))) {
throw new Error('`Layout` must have a width and height!');
}
if (options.renderer) {
this.renderer = options.renderer;
}

View File

@ -192,7 +192,7 @@ OverlayImage.prototype.setImage = function(img, callback) {
}
if (self.shrink || self.options.autofit) {
delete self.shrink;
self.shrink = false;
delete self.options.shrink;
self.options.autofit = true;
return self.imageSize(function(err, size) {

View File

@ -77,10 +77,10 @@ Table.prototype._calculateMaxes = function() {
// width appears to be less than total if it's a percentage or left/right
// combination.
if (this.width < total) {
delete this.position.width;
this.position.width = 'shrink';
}
if (this.position.width != null) {
if (this.position.width !== 'shrink' && this.position.width != null) {
var missing = this.width - total;
var w = missing / maxes.length | 0;
var wr = missing % maxes.length;