UNPKG

file-prompt

Version:

An interactive prompt for selecting files from a directory.

1 lines 3.02 kB
"use strict";function _interopRequireDefault(e){return e&&e.__esModule?e:{"default":e}}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _possibleConstructorReturn(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function _inherits(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}var _createClass=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}();Object.defineProperty(exports,"__esModule",{value:!0});var _chalk=require("chalk"),_chalk2=_interopRequireDefault(_chalk),_column=require("./util/column"),_column2=_interopRequireDefault(_column),_component=require("./component"),_component2=_interopRequireDefault(_component),ITEMS_PER_ROW=4,MAX_COLUMN_LENGTH=20,Menu=function(e){function t(e){return _classCallCheck(this,t),_possibleConstructorReturn(this,Object.getPrototypeOf(t).call(this,e))}return _inherits(t,e),_createClass(t,[{key:"getDefaultProps",value:function(){return{options:[],stdout:process.stdout,stdin:process.stdin}}},{key:"getInitialState",value:function(){var e=this.props.options;return{options:e||[],ids:e?e.map(function(e){return e.id}):[]}}},{key:"filter",value:function(e){var t=arguments.length<=1||void 0===arguments[1]?this:arguments[1];return this.options().filter(e,t)}},{key:"getChoiceById",value:function(e){if(!this.hasId(e))throw new Error('Menu.getChoiceById: Could not find chocie by id "'+e+'"');return this.options()[this.state.ids.indexOf(e)]}},{key:"getIdByName",value:function(e){var t=this.filter(function(t){return t.name.startsWith(e)});return 1!==t.length?[]:t.map(function(e){return e.id})}},{key:"hasId",value:function(e){return this.state.ids.indexOf(Number(e))>-1}},{key:"ids",value:function(){return this.state.ids}},{key:"map",value:function(e){var t=arguments.length<=1||void 0===arguments[1]?this:arguments[1];return this.options().map(e,t)}},{key:"options",value:function(){return this.state.options}},{key:"setOptions",value:function(e){this.setState({options:e,ids:e.map(function(e){return e.id})})}},{key:"renderLabel",value:function(e){return""+_chalk2["default"].magenta.bold(e.slice(0,1))+e.slice(1)}},{key:"renderOption",value:function(e,t){var n=(t+1)%ITEMS_PER_ROW===0,r=" "+e.id+": "+this.renderLabel(e.label);return r=(0,_column2["default"])(r,MAX_COLUMN_LENGTH),r+" "+(n?"\n":"")}},{key:"render",value:function(){return this.state.options.map(this.renderOption,this).join("")}}]),t}(_component2["default"]);exports["default"]=Menu;