From 80f91fad5aa22ea35239e061863498cdcfde1ff1 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 22 Jul 2015 05:36:25 -0700 Subject: [PATCH] refactor shrink so is possible. --- lib/widgets/ansiimage.js | 3 ++ lib/widgets/bigtext.js | 8 ++++-- lib/widgets/element.js | 55 ++++++++++++++++++++++++++----------- lib/widgets/layout.js | 14 +++++----- lib/widgets/overlayimage.js | 2 +- lib/widgets/table.js | 4 +-- 6 files changed, 58 insertions(+), 28 deletions(-) diff --git a/lib/widgets/ansiimage.js b/lib/widgets/ansiimage.js index 4578724..51afc1e 100644 --- a/lib/widgets/ansiimage.js +++ b/lib/widgets/ansiimage.js @@ -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; } diff --git a/lib/widgets/bigtext.js b/lib/widgets/bigtext.js index c7fbefa..09ed97a 100644 --- a/lib/widgets/bigtext.js +++ b/lib/widgets/bigtext.js @@ -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; diff --git a/lib/widgets/element.js b/lib/widgets/element.js index ace5c04..1f630e6 100644 --- a/lib/widgets/element.js +++ b/lib/widgets/element.js @@ -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)) { diff --git a/lib/widgets/layout.js b/lib/widgets/layout.js index 4dc52db..f761864 100644 --- a/lib/widgets/layout.js +++ b/lib/widgets/layout.js @@ -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; } diff --git a/lib/widgets/overlayimage.js b/lib/widgets/overlayimage.js index 1631011..fa63e4f 100644 --- a/lib/widgets/overlayimage.js +++ b/lib/widgets/overlayimage.js @@ -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) { diff --git a/lib/widgets/table.js b/lib/widgets/table.js index 6db4983..b7941a6 100644 --- a/lib/widgets/table.js +++ b/lib/widgets/table.js @@ -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;