UNPKG

eve

Version:

Filesystem-first framework for durable backend AI agents that run anywhere.

20 lines • 28.6 kB
import{styleText as e}from"node:util";import{stdin as t,stdout as n}from"node:process";import*as r from"node:readline";import i from"node:readline";import{ReadStream as a}from"node:tty";var o=(e,t)=>()=>(t||(e((t={exports:{}}).exports,t),e=null),t.exports);const s=(()=>{let e=/[\uD800-\uDBFF][\uDC00-\uDFFF]/g;return t=>{let n=0;for(e.lastIndex=0;e.test(t);)n+=1;return t.length-n}})(),c=e=>e===12288||e>=65281&&e<=65376||e>=65504&&e<=65510,l=e=>e===8987||e===9001||e>=12272&&e<=12287||e>=12289&&e<=12350||e>=12441&&e<=12543||e>=12549&&e<=12591||e>=12593&&e<=12686||e>=12688&&e<=12771||e>=12783&&e<=12830||e>=12832&&e<=12871||e>=12880&&e<=19903||e>=65040&&e<=65049||e>=65072&&e<=65106||e>=65108&&e<=65126||e>=65128&&e<=65131||e>=127488&&e<=127490||e>=127504&&e<=127547||e>=127552&&e<=127560||e>=131072&&e<=196605||e>=196608&&e<=262141,u=/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]|\u001b\]8;[^;]*;.*?(?:\u0007|\u001b\u005c)/y,d=/[\x00-\x08\x0A-\x1F\x7F-\x9F]{1,1000}/y,f=/(?:(?![\uFF61-\uFF9F\uFF00-\uFFEF])[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\p{Script=Tangut}]){1,1000}/uy,p=/\t{1,1000}/y,m=/[\u{1F1E6}-\u{1F1FF}]{2}|\u{1F3F4}[\u{E0061}-\u{E007A}]{2}[\u{E0030}-\u{E0039}\u{E0061}-\u{E007A}]{1,3}\u{E007F}|(?:\p{Emoji}\uFE0F\u20E3?|\p{Emoji_Modifier_Base}\p{Emoji_Modifier}?|\p{Emoji_Presentation})(?:\u200D(?:\p{Emoji_Modifier_Base}\p{Emoji_Modifier}?|\p{Emoji_Presentation}|\p{Emoji}\uFE0F\u20E3?))*/uy,ee=/(?:[\x20-\x7E\xA0-\xFF](?!\uFE0F)){1,1000}/y,te=/\p{M}+/gu,h={limit:1/0,ellipsis:``},g=(e,t={},n={})=>{let r=t.limit??1/0,i=t.ellipsis??``,a=t?.ellipsisWidth??(i?g(i,h,n).width:0),o=n.controlWidth??0,_=n.tabWidth??8,v=n.emojiWidth??2,y=n.regularWidth??1,b=n.wideWidth??2,x=[[ee,y],[u,0],[d,o],[p,_],[m,v],[f,b]],S=0,C=0,w=e.length,T=0,E=!1,D=w,O=Math.max(0,r-a),k=0,A=0,j=0,M=0;outer:for(;;){if(A>k||C>=w&&C>S){let t=e.slice(k,A)||e.slice(S,C);T=0;for(let e of t.replaceAll(te,``)){let t=e.codePointAt(0)||0;if(M=c(t)?2:l(t)?b:y,j+M>O&&(D=Math.min(D,Math.max(k,S)+T)),j+M>r){E=!0;break outer}T+=e.length,j+=M}k=A=0}if(C>=w)break outer;for(let t=0,n=x.length;t<n;t++){let[n,i]=x[t];if(n.lastIndex=C,n.test(e)){if(T=n===f?s(e.slice(C,n.lastIndex)):n===m?1:n.lastIndex-C,M=T*i,j+M>O&&(D=Math.min(D,C+Math.floor((O-j)/i))),j+M>r){E=!0;break outer}j+=M,k=S,A=C,C=S=n.lastIndex;continue outer}}C+=1}return{width:E?O:j,index:E?D:w,truncated:E,ellipsed:E&&r>=a}},_={limit:1/0,ellipsis:``,ellipsisWidth:0},v=(e,t={})=>g(e,_,t).width,y=`]8;;`,b=RegExp(`(?:\\[(?<code>\\d+)m|\\${y}(?<uri>.*))`,`y`),x=e=>{if(e>=30&&e<=37||e>=90&&e<=97)return 39;if(e>=40&&e<=47||e>=100&&e<=107)return 49;if(e===1||e===2)return 22;if(e===3)return 23;if(e===4)return 24;if(e===7)return 27;if(e===8)return 28;if(e===9)return 29;if(e===0)return 0},S=e=>`[${e}m`,C=e=>`${y}${e}`,w=(e,t,n)=>{let r=t[Symbol.iterator](),i=!1,a=!1,o=e.at(-1),s=o===void 0?0:v(o),c=r.next(),l=r.next(),u=0;for(;!c.done;){let o=c.value,d=v(o);s+d<=n?e[e.length-1]+=o:(e.push(o),s=0),(o===`\x1B`||o===`›`)&&(i=!0,a=t.startsWith(y,u+1)),i?a?o===`\x07`&&(i=!1,a=!1):o===`m`&&(i=!1):(s+=d,s===n&&!l.done&&(e.push(``),s=0)),c=l,l=r.next(),u+=o.length}o=e.at(-1),!s&&o!==void 0&&o.length&&e.length>1&&(e[e.length-2]+=e.pop())},T=e=>{let t=e.split(` `),n=t.length;for(;n&&!v(t[n-1]);)n--;return n===t.length?e:t.slice(0,n).join(` `)+t.slice(n).join(``)},E=(e,t,n={})=>{if(n.trim!==!1&&e.trim()===``)return``;let r=``,i,a,o=e.split(` `),s=[``],c=0;for(let e=0;e<o.length;e++){let r=o[e];if(n.trim!==!1){let e=s.at(-1)??``,t=e.trimStart();e.length!==t.length&&(s[s.length-1]=t,c=v(t))}e!==0&&(c>=t&&(n.wordWrap===!1||n.trim===!1)&&(s.push(``),c=0),(c||n.trim===!1)&&(s[s.length-1]+=` `,c++));let i=v(r);if(n.hard&&i>t){let e=t-c,n=1+Math.floor((i-e-1)/t);Math.floor((i-1)/t)<n&&s.push(``),w(s,r,t),c=v(s.at(-1)??``);continue}if(c+i>t&&c&&i){if(n.wordWrap===!1&&c<t){w(s,r,t),c=v(s.at(-1)??``);continue}s.push(``),c=0}if(c+i>t&&n.wordWrap===!1){w(s,r,t),c=v(s.at(-1)??``);continue}s[s.length-1]+=r,c+=i}n.trim!==!1&&(s=s.map(e=>T(e)));let l=s.join(` `),u=!1;for(let e=0;e<l.length;e++){let t=l[e];if(r+=t,u)u=!1;else if(u=t>=`\ud800`&&t<=`\udbff`,u)continue;if(t===`\x1B`||t===`›`){b.lastIndex=e+1;let t=b.exec(l)?.groups;if(t?.code!==void 0){let e=Number.parseFloat(t.code);i=e===39?void 0:e}else t?.uri!==void 0&&(a=t.uri.length===0?void 0:t.uri)}if(l[e+1]===` `){a&&(r+=C(``));let e=i?x(i):void 0;i&&e&&(r+=S(e))}else t===` `&&(i&&x(i)&&(r+=S(i)),a&&(r+=C(a)))}return r},D=/\r?\n/;function O(e,t,n){return String(e).normalize().split(D).map(e=>E(e,t,n)).join(` `)}var k=o(((e,t)=>{let n={to(e,t){return t?`[${t+1};${e+1}H`:`[${e+1}G`},move(e,t){let n=``;return e<0?n+=`[${-e}D`:e>0&&(n+=`[${e}C`),t<0?n+=`[${-t}A`:t>0&&(n+=`[${t}B`),n},up:(e=1)=>`[${e}A`,down:(e=1)=>`[${e}B`,forward:(e=1)=>`[${e}C`,backward:(e=1)=>`[${e}D`,nextLine:(e=1)=>`\x1B[E`.repeat(e),prevLine:(e=1)=>`\x1B[F`.repeat(e),left:`\x1B[G`,hide:`\x1B[?25l`,show:`\x1B[?25h`,save:`\x1B7`,restore:`\x1B8`};t.exports={cursor:n,scroll:{up:(e=1)=>`\x1B[S`.repeat(e),down:(e=1)=>`\x1B[T`.repeat(e)},erase:{screen:`\x1B[2J`,up:(e=1)=>`\x1B[1J`.repeat(e),down:(e=1)=>`\x1B[J`.repeat(e),line:`\x1B[2K`,lineEnd:`\x1B[K`,lineStart:`\x1B[1K`,lines(e){let t=``;for(let r=0;r<e;r++)t+=this.line+(r<e-1?n.up():``);return e&&(t+=n.left),t}},beep:`\x07`}}))();function A(e,t,n){if(!n.some(e=>!e.disabled))return e;let r=e+t,i=Math.max(n.length-1,0),a=r<0?i:r>i?0:r;return n[a].disabled?A(a,t<0?-1:1,n):a}function j(e,t,n,r){let i=r.split(` `),a=0,o=e;for(let e of i){if(o<=e.length)break;o-=e.length+1,a++}for(a=Math.max(0,Math.min(i.length-1,a+n)),o=Math.min(o,i[a].length)+t;o<0&&a>0;)a--,o+=i[a].length+1;for(;o>i[a].length&&a<i.length-1;)o-=i[a].length+1,a++;o=Math.max(0,Math.min(i[a].length,o));let s=0;for(let e=0;e<a;e++)s+=i[e].length+1;return s+o}const M={actions:new Set([`up`,`down`,`left`,`right`,`space`,`enter`,`cancel`]),aliases:new Map([[`k`,`up`],[`j`,`down`],[`h`,`left`],[`l`,`right`],[``,`cancel`],[`escape`,`cancel`]]),messages:{cancel:`Canceled`,error:`Something went wrong`},withGuide:!0,date:{monthNames:[`January`,`February`,`March`,`April`,`May`,`June`,`July`,`August`,`September`,`October`,`November`,`December`],messages:{required:`Please enter a valid date`,invalidMonth:`There are only 12 months in a year`,invalidDay:(e,t)=>`There are only ${e} days in ${t}`,afterMin:e=>`Date must be on or after ${e.toISOString().slice(0,10)}`,beforeMax:e=>`Date must be on or before ${e.toISOString().slice(0,10)}`}}};function ne(e){if(e.aliases!==void 0){let t=e.aliases;for(let e in t){if(!Object.hasOwn(t,e))continue;let n=t[e];M.actions.has(n)&&(M.aliases.has(e)||M.aliases.set(e,n))}}if(e.messages!==void 0){let t=e.messages;t.cancel!==void 0&&(M.messages.cancel=t.cancel),t.error!==void 0&&(M.messages.error=t.error)}if(e.withGuide!==void 0&&(M.withGuide=e.withGuide!==!1),e.date!==void 0){let t=e.date;t.monthNames!==void 0&&(M.date.monthNames=[...t.monthNames]),t.messages!==void 0&&(t.messages.required!==void 0&&(M.date.messages.required=t.messages.required),t.messages.invalidMonth!==void 0&&(M.date.messages.invalidMonth=t.messages.invalidMonth),t.messages.invalidDay!==void 0&&(M.date.messages.invalidDay=t.messages.invalidDay),t.messages.afterMin!==void 0&&(M.date.messages.afterMin=t.messages.afterMin),t.messages.beforeMax!==void 0&&(M.date.messages.beforeMax=t.messages.beforeMax))}}function N(e,t){if(typeof e==`string`)return M.aliases.get(e)===t;for(let n of e)if(n!==void 0&&N(n,t))return!0;return!1}function P(e,t){if(e===t)return;let n=e.split(` `),r=t.split(` `),i=Math.max(n.length,r.length),a=[];for(let e=0;e<i;e++)n[e]!==r[e]&&a.push(e);return{lines:a,numLinesBefore:n.length,numLinesAfter:r.length,numLines:i}}const re=globalThis.process.platform.startsWith(`win`),F=Symbol(`clack:cancel`);function I(e){return e===F}function L(e,t){let n=e;n.isTTY&&n.setRawMode(t)}function ie({input:e=t,output:i=n,overwrite:o=!0,hideCursor:s=!0}={}){let c=r.createInterface({input:e,output:i,prompt:``,tabSize:1});r.emitKeypressEvents(e,c),e instanceof a&&e.isTTY&&e.setRawMode(!0);let l=(t,{name:n,sequence:a})=>{if(N([String(t),n,a],`cancel`)){s&&i.write(k.cursor.show),process.exit(0);return}if(!o)return;let c=n===`return`?0:-1,u=n===`return`?-1:0;r.moveCursor(i,c,u,()=>{r.clearLine(i,1,()=>{e.once(`keypress`,l)})})};return s&&i.write(k.cursor.hide),e.once(`keypress`,l),()=>{e.off(`keypress`,l),s&&i.write(k.cursor.show),e instanceof a&&e.isTTY&&!re&&e.setRawMode(!1),c.terminal=!1,c.close()}}const R=e=>`columns`in e&&typeof e.columns==`number`?e.columns:80,z=e=>`rows`in e&&typeof e.rows==`number`?e.rows:20;function B(e,t,r,i=r,a=r,o){return O(t,R(e??n)-r.length,{hard:!0,trim:!1}).split(` `).map((e,t,n)=>{let s=o?o(e,t):e;return t===0?`${i}${s}`:t===n.length-1?`${a}${s}`:`${r}${s}`}).join(` `)}let V=class{input;output;_abortSignal;rl;opts;_render;_track=!1;_prevFrame=``;_subscribers=new Map;_cursor=0;state=`initial`;error=``;value;userInput=``;constructor(e,r=!0){let{input:i=t,output:a=n,render:o,signal:s,...c}=e;this.opts=c,this.onKeypress=this.onKeypress.bind(this),this.close=this.close.bind(this),this.render=this.render.bind(this),this._render=o.bind(this),this._track=r,this._abortSignal=s,this.input=i,this.output=a}unsubscribe(){this._subscribers.clear()}setSubscriber(e,t){let n=this._subscribers.get(e)??[];n.push(t),this._subscribers.set(e,n)}on(e,t){this.setSubscriber(e,{cb:t})}once(e,t){this.setSubscriber(e,{cb:t,once:!0})}emit(e,...t){let n=this._subscribers.get(e)??[],r=[];for(let e of n)e.cb(...t),e.once&&r.push(()=>n.splice(n.indexOf(e),1));for(let e of r)e()}prompt(){return new Promise(e=>{if(this._abortSignal){if(this._abortSignal.aborted)return this.state=`cancel`,this.close(),e(F);this._abortSignal.addEventListener(`abort`,()=>{this.state=`cancel`,this.close()},{once:!0})}this.rl=i.createInterface({input:this.input,tabSize:2,prompt:``,escapeCodeTimeout:50,terminal:!0}),this.rl.prompt(),this.opts.initialUserInput!==void 0&&this._setUserInput(this.opts.initialUserInput,!0),this.input.on(`keypress`,this.onKeypress),L(this.input,!0),this.output.on(`resize`,this.render),this.render(),this.once(`submit`,()=>{this.output.write(k.cursor.show),this.output.off(`resize`,this.render),L(this.input,!1),e(this.value)}),this.once(`cancel`,()=>{this.output.write(k.cursor.show),this.output.off(`resize`,this.render),L(this.input,!1),e(F)})})}_isActionKey(e,t){return e===` `}_shouldSubmit(e,t){return!0}_setValue(e){this.value=e,this.emit(`value`,this.value)}_setUserInput(e,t){this.userInput=e??``,this.emit(`userInput`,this.userInput),t&&this._track&&this.rl&&(this.rl.write(this.userInput),this._cursor=this.rl.cursor)}_clearUserInput(){this.rl?.write(null,{ctrl:!0,name:`u`}),this._setUserInput(``)}onKeypress(e,t){if(this._track&&t.name!==`return`&&(t.name&&this._isActionKey(e,t)&&this.rl?.write(null,{ctrl:!0,name:`h`}),this._cursor=this.rl?.cursor??0,this._setUserInput(this.rl?.line)),this.state===`error`&&(this.state=`active`),t?.name&&(!this._track&&M.aliases.has(t.name)&&this.emit(`cursor`,M.aliases.get(t.name)),M.actions.has(t.name)&&this.emit(`cursor`,t.name)),e&&(e.toLowerCase()===`y`||e.toLowerCase()===`n`)&&this.emit(`confirm`,e.toLowerCase()===`y`),this.emit(`key`,e?.toLowerCase(),t),t?.name===`return`&&this._shouldSubmit(e,t)){if(this.opts.validate){let e=this.opts.validate(this.value);e&&(this.error=e instanceof Error?e.message:e,this.state=`error`,this.rl?.write(this.userInput))}this.state!==`error`&&(this.state=`submit`)}N([e,t?.name,t?.sequence],`cancel`)&&(this.state=`cancel`),(this.state===`submit`||this.state===`cancel`)&&this.emit(`finalize`),this.render(),(this.state===`submit`||this.state===`cancel`)&&this.close()}close(){this.input.unpipe(),this.input.removeListener(`keypress`,this.onKeypress),this.output.write(` `),L(this.input,!1),this.rl?.close(),this.rl=void 0,this.emit(`${this.state}`,this.value),this.unsubscribe()}restoreCursor(){let e=O(this._prevFrame,process.stdout.columns,{hard:!0,trim:!1}).split(` `).length-1;this.output.write(k.cursor.move(-999,e*-1))}render(){let e=O(this._render(this)??``,process.stdout.columns,{hard:!0,trim:!1});if(e!==this._prevFrame){if(this.state===`initial`)this.output.write(k.cursor.hide);else{let t=P(this._prevFrame,e),n=z(this.output);if(this.restoreCursor(),t){let r=Math.max(0,t.numLinesAfter-n),i=Math.max(0,t.numLinesBefore-n),a=t.lines.find(e=>e>=r);if(a===void 0){this._prevFrame=e;return}if(t.lines.length===1){this.output.write(k.cursor.move(0,a-i)),this.output.write(k.erase.lines(1));let t=e.split(` `);this.output.write(t[a]),this._prevFrame=e,this.output.write(k.cursor.move(0,t.length-a-1));return}if(t.lines.length>1){if(r<i)a=r;else{let e=a-i;e>0&&this.output.write(k.cursor.move(0,e))}this.output.write(k.erase.down());let t=e.split(` `).slice(a);this.output.write(t.join(` `)),this._prevFrame=e;return}}this.output.write(k.erase.down())}this.output.write(e),this.state===`initial`&&(this.state=`active`),this._prevFrame=e}}};function H(e,t){if(e===void 0||t.length===0)return 0;let n=t.findIndex(t=>t.value===e);return n===-1?0:n}function U(e,t){return(t.label??String(t.value)).toLowerCase().includes(e.toLowerCase())}function W(e,t){if(t)return e?t:t[0]}let G=class extends V{filteredOptions;multiple;isNavigating=!1;selectedValues=[];focusedValue;#e=0;#t=``;#n;#r;#i;get cursor(){return this.#e}get userInputWithCursor(){if(!this.userInput)return e([`inverse`,`hidden`],`_`);if(this._cursor>=this.userInput.length)return`${this.userInput}\u2588`;let t=this.userInput.slice(0,this._cursor),[n,...r]=this.userInput.slice(this._cursor);return`${t}${e(`inverse`,n)}${r.join(``)}`}get options(){return typeof this.#r==`function`?this.#r():this.#r}constructor(e){super(e),this.#r=e.options,this.#i=e.placeholder;let t=this.options;this.filteredOptions=[...t],this.multiple=e.multiple===!0,this.#n=typeof e.options==`function`?e.filter:e.filter??U;let n;if(e.initialValue&&Array.isArray(e.initialValue)?n=this.multiple?e.initialValue:e.initialValue.slice(0,1):!this.multiple&&this.options.length>0&&(n=[this.options[0].value]),n)for(let e of n){let n=t.findIndex(t=>t.value===e);n!==-1&&(this.toggleSelected(e),this.#e=n)}this.focusedValue=this.options[this.#e]?.value,this.on(`key`,(e,t)=>this.#a(e,t)),this.on(`userInput`,e=>this.#o(e))}_isActionKey(e,t){return e===` `||this.multiple&&this.isNavigating&&t.name===`space`&&e!==void 0&&e!==``}#a(e,t){let n=t.name===`up`,r=t.name===`down`,i=t.name===`return`,a=this.userInput===``||this.userInput===` `,o=this.#i,s=this.options,c=o!==void 0&&o!==``&&s.some(e=>!e.disabled&&(!this.#n||this.#n(o,e)));if(t.name===`tab`&&a&&c){this.userInput===` `&&this._clearUserInput(),this._setUserInput(o,!0),this.isNavigating=!1;return}n||r?(this.#e=A(this.#e,n?-1:1,this.filteredOptions),this.focusedValue=this.filteredOptions[this.#e]?.value,this.multiple||(this.selectedValues=[this.focusedValue]),this.isNavigating=!0):i?this.value=W(this.multiple,this.selectedValues):this.multiple?this.focusedValue!==void 0&&(t.name===`tab`||this.isNavigating&&t.name===`space`)?this.toggleSelected(this.focusedValue):this.isNavigating=!1:(this.focusedValue&&(this.selectedValues=[this.focusedValue]),this.isNavigating=!1)}deselectAll(){this.selectedValues=[]}toggleSelected(e){this.filteredOptions.length!==0&&(this.selectedValues=this.multiple?this.selectedValues.includes(e)?this.selectedValues.filter(t=>t!==e):[...this.selectedValues,e]:[e])}#o(e){if(e!==this.#t){this.#t=e;let t=this.options;this.filteredOptions=e&&this.#n?t.filter(t=>this.#n?.(e,t)):[...t];let n=H(this.focusedValue,this.filteredOptions);this.#e=A(n,0,this.filteredOptions);let r=this.filteredOptions[this.#e];this.focusedValue=r&&!r.disabled?r.value:void 0,this.multiple||(this.focusedValue===void 0?this.deselectAll():this.toggleSelected(this.focusedValue))}}};var K=class extends V{get cursor(){return+!this.value}get _value(){return this.cursor===0}constructor(e){super(e,!1),this.value=!!e.initialValue,this.on(`userInput`,()=>{this.value=this._value}),this.on(`confirm`,e=>{this.output.write(k.cursor.move(0,-1)),this.value=e,this.state=`submit`,this.close()}),this.on(`cursor`,()=>{this.value=!this.value})}};const q={Y:{type:`year`,len:4},M:{type:`month`,len:2},D:{type:`day`,len:2}};function J(e){return[...e].map(e=>q[e])}function ae(e){let t=new Intl.DateTimeFormat(e,{year:`numeric`,month:`2-digit`,day:`2-digit`}).formatToParts(new Date(2e3,0,15)),n=[],r=`/`;for(let e of t)e.type===`literal`?r=e.value.trim()||e.value:(e.type===`year`||e.type===`month`||e.type===`day`)&&n.push({type:e.type,len:e.type===`year`?4:2});return{segments:n,separator:r}}function Y(e){return Number.parseInt((e||`0`).replace(/_/g,`0`),10)||0}function X(e){return{year:Y(e.year),month:Y(e.month),day:Y(e.day)}}function Z(e,t){return new Date(e||2001,t||1,0).getDate()}function Q(e){let{year:t,month:n,day:r}=X(e);if(!t||t<0||t>9999||!n||n<1||n>12||!r||r<1)return;let i=new Date(Date.UTC(t,n-1,r));if(i.getUTCFullYear()===t&&i.getUTCMonth()===n-1&&i.getUTCDate()===r)return{year:t,month:n,day:r}}function $(e){let t=Q(e);return t?new Date(Date.UTC(t.year,t.month-1,t.day)):void 0}function oe(e,t,n,r){let i=n?{year:n.getUTCFullYear(),month:n.getUTCMonth()+1,day:n.getUTCDate()}:null,a=r?{year:r.getUTCFullYear(),month:r.getUTCMonth()+1,day:r.getUTCDate()}:null;return e===`year`?{min:i?.year??1,max:a?.year??9999}:e===`month`?{min:i&&t.year===i.year?i.month:1,max:a&&t.year===a.year?a.month:12}:{min:i&&t.year===i.year&&t.month===i.month?i.day:1,max:a&&t.year===a.year&&t.month===a.month?a.day:Z(t.year,t.month)}}var se=class extends V{#e;#t;#n;#r;#i;#a={segmentIndex:0,positionInSegment:0};#o=!0;#s=null;inlineError=``;get segmentCursor(){return{...this.#a}}get segmentValues(){return{...this.#n}}get segments(){return this.#e}get separator(){return this.#t}get formattedValue(){return this.#c(this.#n)}#c(e){return this.#e.map(t=>e[t.type]).join(this.#t)}#l(){this._setUserInput(this.#c(this.#n)),this._setValue($(this.#n)??void 0)}constructor(e){let t=e.format?{segments:J(e.format),separator:e.separator??`/`}:ae(e.locale),n=e.separator??t.separator,r=e.format?J(e.format):t.segments,i=e.initialValue??e.defaultValue,a=i?{year:String(i.getUTCFullYear()).padStart(4,`0`),month:String(i.getUTCMonth()+1).padStart(2,`0`),day:String(i.getUTCDate()).padStart(2,`0`)}:{year:`____`,month:`__`,day:`__`},o=r.map(e=>a[e.type]).join(n);super({...e,initialUserInput:o},!1),this.#e=r,this.#t=n,this.#n=a,this.#r=e.minDate,this.#i=e.maxDate,this.#l(),this.on(`cursor`,e=>this.#p(e)),this.on(`key`,(e,t)=>this.#m(e,t)),this.on(`finalize`,()=>this.#g(e))}#u(){let e=Math.max(0,Math.min(this.#a.segmentIndex,this.#e.length-1)),t=this.#e[e];if(t)return this.#a.positionInSegment=Math.max(0,Math.min(this.#a.positionInSegment,t.len-1)),{segment:t,index:e}}#d(e){this.inlineError=``,this.#s=null;let t=this.#u();t&&(this.#a.segmentIndex=Math.max(0,Math.min(this.#e.length-1,t.index+e)),this.#a.positionInSegment=0,this.#o=!0)}#f(e){let t=this.#u();if(!t)return;let{segment:n}=t,r=this.#n[n.type],i=!r||r.replace(/_/g,``)===``,a=Number.parseInt((r||`0`).replace(/_/g,`0`),10)||0,o=oe(n.type,X(this.#n),this.#r,this.#i),s;s=i?e===1?o.min:o.max:Math.max(Math.min(o.max,a+e),o.min),this.#n={...this.#n,[n.type]:s.toString().padStart(n.len,`0`)},this.#o=!0,this.#s=null,this.#l()}#p(e){if(e)switch(e){case`right`:return this.#d(1);case`left`:return this.#d(-1);case`up`:return this.#f(1);case`down`:return this.#f(-1)}}#m(e,t){if(t?.name===`backspace`||t?.sequence===``||t?.sequence===`\b`||e===``||e===`\b`){this.inlineError=``;let e=this.#u();if(!e)return;if(!this.#n[e.segment.type].replace(/_/g,``)){this.#d(-1);return}this.#n[e.segment.type]=`_`.repeat(e.segment.len),this.#o=!0,this.#a.positionInSegment=0,this.#l();return}if(t?.name===`tab`){this.inlineError=``;let e=this.#u();if(!e)return;let n=t.shift?-1:1,r=e.index+n;r>=0&&r<this.#e.length&&(this.#a.segmentIndex=r,this.#a.positionInSegment=0,this.#o=!0);return}if(e&&/^[0-9]$/.test(e)){let t=this.#u();if(!t)return;let{segment:n}=t,r=!this.#n[n.type].replace(/_/g,``);if(this.#o&&this.#s!==null&&!r){let r=this.#s+e,i={...this.#n,[n.type]:r},a=this.#h(i,n);if(a){this.inlineError=a,this.#s=null,this.#o=!1;return}this.inlineError=``,this.#n[n.type]=r,this.#s=null,this.#o=!1,this.#l(),t.index<this.#e.length-1&&(this.#a.segmentIndex=t.index+1,this.#a.positionInSegment=0,this.#o=!0);return}this.#o&&!r&&(this.#n[n.type]=`_`.repeat(n.len),this.#a.positionInSegment=0),this.#o=!1,this.#s=null;let i=this.#n[n.type],a=i.indexOf(`_`),o=a>=0?a:Math.min(this.#a.positionInSegment,n.len-1);if(o<0||o>=n.len)return;let s=i.slice(0,o)+e+i.slice(o+1),c=!1;if(o===0&&i===`__`&&(n.type===`month`||n.type===`day`)){let t=Number.parseInt(e,10);s=`0${e}`,c=t<=(n.type===`month`?1:2)}if(n.type===`year`&&(s=(i.replace(/_/g,``)+e).padStart(n.len,`_`)),!s.includes(`_`)){let e={...this.#n,[n.type]:s},t=this.#h(e,n);if(t){this.inlineError=t;return}}this.inlineError=``,this.#n[n.type]=s;let l=s.includes(`_`)?void 0:Q(this.#n);if(l){let{year:e,month:t}=l,n=Z(e,t);this.#n={year:String(Math.max(0,Math.min(9999,e))).padStart(4,`0`),month:String(Math.max(1,Math.min(12,t))).padStart(2,`0`),day:String(Math.max(1,Math.min(n,l.day))).padStart(2,`0`)}}this.#l();let u=s.indexOf(`_`);c?(this.#o=!0,this.#s=e):u>=0?this.#a.positionInSegment=u:a>=0&&t.index<this.#e.length-1?(this.#a.segmentIndex=t.index+1,this.#a.positionInSegment=0,this.#o=!0):this.#a.positionInSegment=Math.min(o+1,n.len-1)}}#h(e,t){let{month:n,day:r}=X(e);if(t.type===`month`&&(n<0||n>12))return M.date.messages.invalidMonth;if(t.type===`day`&&(r<0||r>31))return M.date.messages.invalidDay(31,`any month`)}#g(e){let{year:t,month:n,day:r}=X(this.#n);if(t&&n&&r){let e=Z(t,n);this.#n={...this.#n,day:String(Math.min(r,e)).padStart(2,`0`)}}this.value=$(this.#n)??e.defaultValue??void 0}},ce=class extends V{options;cursor=0;#e;getGroupItems(e){return this.options.filter(t=>t.group===e)}isGroupSelected(e){let t=this.getGroupItems(e),n=this.value;return n!==void 0&&t.every(e=>n.includes(e.value))}toggleValue(){let e=this.options[this.cursor];if(this.value===void 0&&(this.value=[]),e.group===!0){let t=e.value,n=this.getGroupItems(t);this.value=this.isGroupSelected(t)?this.value.filter(e=>n.findIndex(t=>t.value===e)===-1):[...this.value,...n.map(e=>e.value)],this.value=Array.from(new Set(this.value))}else{let t=this.value.includes(e.value);this.value=t?this.value.filter(t=>t!==e.value):[...this.value,e.value]}}constructor(e){super(e,!1);let{options:t}=e;this.#e=e.selectableGroups!==!1,this.options=Object.entries(t).flatMap(([e,t])=>[{value:e,group:!0,label:e},...t.map(t=>({...t,group:e}))]),this.value=[...e.initialValues??[]],this.cursor=Math.max(this.options.findIndex(({value:t})=>t===e.cursorAt),+!this.#e),this.on(`cursor`,e=>{switch(e){case`left`:case`up`:{this.cursor=this.cursor===0?this.options.length-1:this.cursor-1;let e=this.options[this.cursor]?.group===!0;!this.#e&&e&&(this.cursor=this.cursor===0?this.options.length-1:this.cursor-1);break}case`down`:case`right`:{this.cursor=this.cursor===this.options.length-1?0:this.cursor+1;let e=this.options[this.cursor]?.group===!0;!this.#e&&e&&(this.cursor=this.cursor===this.options.length-1?0:this.cursor+1);break}case`space`:this.toggleValue()}})}},le=class extends V{#e=!1;#t;focused=`editor`;get userInputWithCursor(){if(this.state===`submit`)return this.userInput;let t=this.userInput;if(this.cursor>=t.length)return`${t}\u2588`;let n=t.slice(0,this.cursor),r=t[this.cursor],i=t.slice(this.cursor+1);return r===` `?`${n}\u2588 ${i}`:`${n}${e(`inverse`,r)}${i}`}get cursor(){return this._cursor}#n(e){if(this.userInput.length===0){this._setUserInput(e);return}this._setUserInput(this.userInput.slice(0,this.cursor)+e+this.userInput.slice(this.cursor))}#r(e){let t=this.value??``;switch(e){case`up`:this._cursor=j(this._cursor,0,-1,t);return;case`down`:this._cursor=j(this._cursor,0,1,t);return;case`left`:this._cursor=j(this._cursor,-1,0,t);return;case`right`:this._cursor=j(this._cursor,1,0,t);return}}_shouldSubmit(e,t){if(this.#t)return this.focused===`submit`||(this.#n(` `),this._cursor++,!1);let n=this.#e;return this.#e=!0,n?(this.userInput[this.cursor-1]===` `&&(this._setUserInput(this.userInput.slice(0,this.cursor-1)+this.userInput.slice(this.cursor)),this._cursor--),!0):(this.#n(` `),this._cursor++,!1)}constructor(e){super(e,!1),this.#t=e.showSubmit??!1,this.on(`key`,(e,t)=>{if(t?.name&&M.actions.has(t.name)){this.#r(t.name);return}if(e===` `&&this.#t){this.focused=this.focused===`editor`?`submit`:`editor`;return}if(t?.name!==`return`){if(this.#e=!1,t?.name===`backspace`&&this.cursor>0){this._setUserInput(this.userInput.slice(0,this.cursor-1)+this.userInput.slice(this.cursor)),this._cursor--;return}if(t?.name===`delete`&&this.cursor<this.userInput.length){this._setUserInput(this.userInput.slice(0,this.cursor)+this.userInput.slice(this.cursor+1));return}e&&(this.#t&&this.focused===`submit`&&(this.focused=`editor`),this.#n(e??``),this._cursor++)}}),this.on(`userInput`,e=>{this._setValue(e)}),this.on(`finalize`,()=>{this.value||=e.defaultValue,this.value===void 0&&(this.value=``)})}};let ue=class extends V{options;cursor=0;get _value(){return this.options[this.cursor].value}get _enabledOptions(){return this.options.filter(e=>e.disabled!==!0)}toggleAll(){let e=this._enabledOptions,t=this.value!==void 0&&this.value.length===e.length;this.value=t?[]:e.map(e=>e.value)}toggleInvert(){let e=this.value;if(!e)return;let t=this._enabledOptions.filter(t=>!e.includes(t.value));this.value=t.map(e=>e.value)}toggleValue(){this.value===void 0&&(this.value=[]);let e=this.value.includes(this._value);this.value=e?this.value.filter(e=>e!==this._value):[...this.value,this._value]}constructor(e){super(e,!1),this.options=e.options,this.value=[...e.initialValues??[]];let t=Math.max(this.options.findIndex(({value:t})=>t===e.cursorAt),0);this.cursor=this.options[t].disabled?A(t,1,this.options):t,this.on(`key`,e=>{e===`a`&&this.toggleAll(),e===`i`&&this.toggleInvert()}),this.on(`cursor`,e=>{switch(e){case`left`:case`up`:this.cursor=A(this.cursor,-1,this.options);break;case`down`:case`right`:this.cursor=A(this.cursor,1,this.options);break;case`space`:this.toggleValue()}})}};var de=class extends V{_mask=`•`;get cursor(){return this._cursor}get masked(){return this.userInput.replaceAll(/./g,this._mask)}get userInputWithCursor(){if(this.state===`submit`||this.state===`cancel`)return this.masked;let t=this.userInput;if(this.cursor>=t.length)return`${this.masked}${e([`inverse`,`hidden`],`_`)}`;let n=this.masked,r=n.slice(0,this.cursor),i=n.slice(this.cursor);return`${r}${e(`inverse`,i[0])}${i.slice(1)}`}clear(){this._clearUserInput()}constructor({mask:e,...t}){super(t),this._mask=e??`•`,this.on(`userInput`,e=>{this._setValue(e)})}},fe=class extends V{options;cursor=0;get _selectedValue(){return this.options[this.cursor]}changeValue(){this.value=this._selectedValue.value}constructor(e){super(e,!1),this.options=e.options;let t=this.options.findIndex(({value:t})=>t===e.initialValue),n=t===-1?0:t;this.cursor=this.options[n].disabled?A(n,1,this.options):n,this.changeValue(),this.on(`cursor`,e=>{switch(e){case`left`:case`up`:this.cursor=A(this.cursor,-1,this.options);break;case`down`:case`right`:this.cursor=A(this.cursor,1,this.options)}this.changeValue()})}},pe=class extends V{options;cursor=0;constructor(e){super(e,!1),this.options=e.options;let t=e.caseSensitive===!0,n=this.options.map(({value:[e]})=>t?e:e?.toLowerCase());this.cursor=Math.max(n.indexOf(e.initialValue),0),this.on(`key`,(e,r)=>{if(!e)return;let i=t&&r.shift?e.toUpperCase():e;if(!n.includes(i))return;let a=this.options.find(({value:[n]})=>t?n===i:n?.toLowerCase()===e);a&&(this.value=a.value,this.state=`submit`,this.emit(`submit`))})}},me=class extends V{get userInputWithCursor(){if(this.state===`submit`)return this.userInput;let t=this.userInput;if(this.cursor>=t.length)return`${this.userInput}\u2588`;let n=t.slice(0,this.cursor),[r,...i]=t.slice(this.cursor);return`${n}${e(`inverse`,r)}${i.join(``)}`}get cursor(){return this._cursor}constructor(e){super({...e,initialUserInput:e.initialUserInput??e.initialValue}),this.on(`userInput`,e=>{this._setValue(e)}),this.on(`finalize`,()=>{this.value||=e.defaultValue,this.value===void 0&&(this.value=``)})}};export{G as AutocompletePrompt,K as ConfirmPrompt,se as DatePrompt,ce as GroupMultiSelectPrompt,le as MultiLinePrompt,ue as MultiSelectPrompt,de as PasswordPrompt,V as Prompt,pe as SelectKeyPrompt,fe as SelectPrompt,me as TextPrompt,ie as block,R as getColumns,z as getRows,I as isCancel,M as settings,ne as updateSettings,B as wrapTextWithPrefix};