UNPKG

neo-neo-bblessed

Version:

A fork of neo-blessed (which is a fork of blessed) with bug fixes and maintenance.

3 lines (2 loc) 2.1 kB
var h=require("path"),u=require("fs"),d=require("../helpers"),v=require("./node"),g=require("./list");function f(e){var t=this;if(!(this instanceof v))return new f(e);e=e||{},e.parseTags=!0,g.call(this,e),this.cwd=e.cwd||process.cwd(),this.file=this.cwd,this.value=this.cwd,e.label&&~e.label.indexOf("%path")&&this._label.setContent(e.label.replace("%path",this.cwd)),this.on("select",function(i){var s=i.content.replace(/\{[^{}]+\}/g,"").replace(/@$/,""),n=h.resolve(t.cwd,s);return u.stat(n,function(o,a){if(o)return t.emit("error",o,n);t.file=n,t.value=n,a.isDirectory()?(t.emit("cd",n,t.cwd),t.cwd=n,e.label&&~e.label.indexOf("%path")&&t._label.setContent(e.label.replace("%path",n)),t.refresh()):t.emit("file",n)})})}f.prototype.__proto__=g.prototype,f.prototype.type="file-manager",f.prototype.refresh=function(e,t){t||(t=e,e=null);var i=this;return e?this.cwd=e:e=this.cwd,u.readdir(e,function(s,n){if(s&&s.code==="ENOENT")return i.cwd=e!==process.env.HOME?process.env.HOME:"/",i.refresh(t);if(s)return t?t(s):i.emit("error",s,e);var o=[],a=[];n.unshift(".."),n.forEach(function(r){var c=h.resolve(e,r),l;try{l=u.lstatSync(c)}catch(F){}l&&l.isDirectory()||r===".."?o.push({name:r,text:"{light-blue-fg}"+r+"{/light-blue-fg}/",dir:!0}):l&&l.isSymbolicLink()?a.push({name:r,text:"{light-cyan-fg}"+r+"{/light-cyan-fg}@",dir:!1}):a.push({name:r,text:r,dir:!1})}),o=d.asort(o),a=d.asort(a),n=o.concat(a).map(function(r){return r.text}),i.setItems(n),i.select(0),i.screen.render(),i.emit("refresh"),t&&t()})},f.prototype.pick=function(e,t){t||(t=e,e=null);var i=this,s=this.screen.focused===this,n=this.hidden,o,a;function r(){i.removeListener("file",o),i.removeListener("cancel",a),n&&i.hide(),s||i.screen.restoreFocus(),i.screen.render()}this.on("file",o=function(c){return r(),t(null,c)}),this.on("cancel",a=function(){return r(),t()}),this.refresh(e,function(c){if(c)return t(c);n&&i.show(),s||(i.screen.saveFocus(),i.focus()),i.screen.render()})},f.prototype.reset=function(e,t){t||(t=e,e=null),this.cwd=e||this.options.cwd,this.refresh(t)},module.exports=f; //# sourceMappingURL=filemanager.js.map