neo-neo-bblessed
Version:
A fork of neo-blessed (which is a fork of blessed) with bug fixes and maintenance.
17 lines (16 loc) • 7.65 kB
JavaScript
var _=require("fs"),p=require("child_process"),g=require("../helpers"),S=require("./node"),w=require("./box");function n(e){var i=this;if(!(this instanceof S))return new n(e);if(e=e||{},w.call(this,e),e.w3m&&(n.w3mdisplay=e.w3m),n.hasW3MDisplay==null){if(_.existsSync(n.w3mdisplay))n.hasW3MDisplay=!0;else if(e.search!==!1){var t=g.findFile("/usr","w3mimgdisplay")||g.findFile("/lib","w3mimgdisplay")||g.findFile("/bin","w3mimgdisplay");t?(n.hasW3MDisplay=!0,n.w3mdisplay=t):n.hasW3MDisplay=!1}}this.on("hide",function(){i._lastFile=i.file,i.clearImage()}),this.on("show",function(){i._lastFile&&i.setImage(i._lastFile)}),this.on("detach",function(){i._lastFile=i.file,i.clearImage()}),this.on("attach",function(){i._lastFile&&i.setImage(i._lastFile)}),this.onScreenEvent("resize",function(){i._needsRatio=!0}),this.onScreenEvent("render",function(){i.screen.program.flush(),i._noImage||i.setImage(i.file)}),(this.options.file||this.options.img)&&this.setImage(this.options.file||this.options.img)}n.prototype.__proto__=w.prototype,n.prototype.type="overlayimage",n.w3mdisplay="/usr/lib/w3m/w3mimgdisplay",n.prototype.spawn=function(e,i,t,r){var o=require("child_process").spawn,s;return t=t||{},s=o(e,i,t),s.on("error",function(a){if(r)return r(a)}),s.on("exit",function(a){if(r)return a!==0?r(new Error("Exit Code: "+a)):r(null,a===0)}),s},n.prototype.setImage=function(e,i){var t=this;if(this._settingImage){this._queue=this._queue||[],this._queue.push([e,i]);return}this._settingImage=!0;var r=function(){t._settingImage=!1,t._queue=t._queue||[];var o=t._queue.shift();o&&t.setImage(o[0],o[1])};return n.hasW3MDisplay===!1?(r(),i?i(new Error("W3M Image Display not available.")):void 0):e?(this.file=e,this.getPixelRatio(function(o,s){return o?(r(),i?i(o):void 0):t.renderImage(e,s,function(a,h){if(a)return r(),i?i(a):void 0;if(t.shrink||t.options.autofit)return delete t.shrink,delete t.options.shrink,t.options.autofit=!0,t.imageSize(function(f,u){return f?(r(),i?i(f):void 0):t._lastSize&&s.tw===t._lastSize.tw&&s.th===t._lastSize.th&&u.width===t._lastSize.width&&u.height===t._lastSize.height&&t.aleft===t._lastSize.aleft&&t.atop===t._lastSize.atop?(r(),i?i(null,h):void 0):(t._lastSize={tw:s.tw,th:s.th,width:u.width,height:u.height,aleft:t.aleft,atop:t.atop},t.position.width=u.width/s.tw|0,t.position.height=u.height/s.th|0,t._noImage=!0,t.screen.render(),t._noImage=!1,r(),t.renderImage(e,s,i))});if(r(),!!i)return i(null,h)})})):(r(),i?i(new Error("No image.")):void 0)},n.prototype.renderImage=function(e,i,t){var r=this;if(p.execSync){t=t||function(a,h){return h};try{return t(null,this.renderImageSync(e,i))}catch(a){return t(a)}}if(n.hasW3MDisplay===!1)return t?t(new Error("W3M Image Display not available.")):void 0;if(!i)return t?t(new Error("No ratio.")):void 0;var o=r.file,s=r._lastSize;return r.clearImage(function(a){if(a)return t(a);r.file=o,r._lastSize=s;var h={stdio:"pipe",env:process.env,cwd:process.env.HOME},f=r.spawn(n.w3mdisplay,[],h,function(d,I){if(t)return d?t(d):t(null,I)}),u=r.width*i.tw|0,m=r.height*i.th|0,l=r.aleft*i.tw|0,y=r.atop*i.th|0,v="0;1;"+l+";"+y+";"+u+";"+m+";;;;;"+e+`
4;
3;
`;r._props={aleft:l,atop:y,width:u,height:m},f.stdin.write(v),f.stdin.end()})},n.prototype.clearImage=function(e){if(p.execSync){e=e||function(f,u){return u};try{return e(null,this.clearImageSync())}catch(f){return e(f)}}if(n.hasW3MDisplay===!1)return e?e(new Error("W3M Image Display not available.")):void 0;if(!this._props)return e?e(null):void 0;var i={stdio:"pipe",env:process.env,cwd:process.env.HOME},t=this.spawn(n.w3mdisplay,[],i,function(f,u){if(e)return f?e(f):e(null,u)}),r=this._props.width+2,o=this._props.height+2,s=this._props.aleft,a=this._props.atop;this._drag&&(s-=10,a-=10,r+=10,o+=10);var h="6;"+s+";"+a+";"+r+";"+o+`
4;
3;
`;delete this.file,delete this._props,delete this._lastSize,t.stdin.write(h),t.stdin.end()},n.prototype.imageSize=function(e){var i=this.file;if(p.execSync){e=e||function(a,h){return h};try{return e(null,this.imageSizeSync())}catch(a){return e(a)}}if(n.hasW3MDisplay===!1)return e?e(new Error("W3M Image Display not available.")):void 0;if(!i)return e?e(new Error("No image.")):void 0;var t={stdio:"pipe",env:process.env,cwd:process.env.HOME},r=this.spawn(n.w3mdisplay,[],t),o="";r.stdout.setEncoding("utf8"),r.stdout.on("data",function(a){o+=a}),r.on("error",function(a){if(e)return e(a)}),r.on("exit",function(){if(e){var a=o.trim().split(/\s+/);return e(null,{raw:o.trim(),width:+a[0],height:+a[1]})}});var s="5;"+i+`
`;r.stdin.write(s),r.stdin.end()},n.prototype.termSize=function(e){var i=this;if(p.execSync){e=e||function(s,a){return a};try{return e(null,this.termSizeSync())}catch(s){return e(s)}}if(n.hasW3MDisplay===!1)return e?e(new Error("W3M Image Display not available.")):void 0;var t={stdio:"pipe",env:process.env,cwd:process.env.HOME},r=this.spawn(n.w3mdisplay,["-test"],t),o="";r.stdout.setEncoding("utf8"),r.stdout.on("data",function(s){o+=s}),r.on("error",function(s){if(e)return e(s)}),r.on("exit",function(){if(e){if(!o.trim())return i.termSize(e);var s=o.trim().split(/\s+/);return e(null,{raw:o.trim(),width:+s[0],height:+s[1]})}}),r.stdin.end()},n.prototype.getPixelRatio=function(e){var i=this;if(p.execSync){e=e||function(t,r){return r};try{return e(null,this.getPixelRatioSync())}catch(t){return e(t)}}return this._ratio&&!this._needsRatio?e(null,this._ratio):this.termSize(function(t,r){return t?e(t):(i._ratio={tw:r.width/i.screen.width,th:r.height/i.screen.height},i._needsRatio=!1,e(null,i._ratio))})},n.prototype.renderImageSync=function(e,i){if(n.hasW3MDisplay===!1)throw new Error("W3M Image Display not available.");if(!i)throw new Error("No ratio.");var t=this.file,r=this._lastSize;this.clearImageSync(),this.file=t,this._lastSize=r;var o=this.width*i.tw|0,s=this.height*i.th|0,a=this.aleft*i.tw|0,h=this.atop*i.th|0,f="0;1;"+a+";"+h+";"+o+";"+s+";;;;;"+e+`
4;
3;
`;this._props={aleft:a,atop:h,width:o,height:s};try{p.execFileSync(n.w3mdisplay,[],{env:process.env,encoding:"utf8",input:f,timeout:1e3})}catch(u){}return!0},n.prototype.clearImageSync=function(){if(n.hasW3MDisplay===!1)throw new Error("W3M Image Display not available.");if(!this._props)return!1;var e=this._props.width+2,i=this._props.height+2,t=this._props.aleft,r=this._props.atop;this._drag&&(t-=10,r-=10,e+=10,i+=10);var o="6;"+t+";"+r+";"+e+";"+i+`
4;
3;
`;delete this.file,delete this._props,delete this._lastSize;try{p.execFileSync(n.w3mdisplay,[],{env:process.env,encoding:"utf8",input:o,timeout:1e3})}catch(s){}return!0},n.prototype.imageSizeSync=function(){var e=this.file;if(n.hasW3MDisplay===!1)throw new Error("W3M Image Display not available.");if(!e)throw new Error("No image.");var i="",t="5;"+e+`
`;try{i=p.execFileSync(n.w3mdisplay,[],{env:process.env,encoding:"utf8",input:t,timeout:1e3})}catch(o){}var r=i.trim().split(/\s+/);return{raw:i.trim(),width:+r[0],height:+r[1]}},n.prototype.termSizeSync=function(e,i){if(n.hasW3MDisplay===!1)throw new Error("W3M Image Display not available.");var t="";try{t=p.execFileSync(n.w3mdisplay,["-test"],{env:process.env,encoding:"utf8",timeout:1e3})}catch(o){}if(!t.trim()){if(i=i||0,++i===5)throw new Error("Term size not determined.");return this.termSizeSync(e,i)}var r=t.trim().split(/\s+/);return{raw:t.trim(),width:+r[0],height:+r[1]}},n.prototype.getPixelRatioSync=function(){if(this._ratio&&!this._needsRatio)return this._ratio;this._needsRatio=!1;var e=this.termSizeSync();return this._ratio={tw:e.width/this.screen.width,th:e.height/this.screen.height},this._ratio},n.prototype.displayImage=function(e){return this.screen.displayImage(this.file,e)},module.exports=n;
//# sourceMappingURL=overlayimage.js.map