Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@clack/core

Package Overview
Dependencies
Maintainers
5
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@clack/core - npm Package Compare versions

Comparing version
1.4.0
to
1.4.1
+6
-0
CHANGELOG.md
# @clack/core
## 1.4.1
### Patch Changes
- [#548](https://github.com/bombshell-dev/clack/pull/548) [`2356e97`](https://github.com/bombshell-dev/clack/commit/2356e97c1f46007ead55133c3a26910404ef1cfb) Thanks [@43081j](https://github.com/43081j)! - Remove sourcemaps and enable pretty-ish build output.
## 1.4.0

@@ -4,0 +10,0 @@

+972
-17

@@ -1,17 +0,972 @@

import{styleText as v}from"node:util";import{stdout as x,stdin as D}from"node:process";import*as b from"node:readline";import G from"node:readline";import{wrapAnsi as M}from"fast-wrap-ansi";import{cursor as p,erase as V}from"sisteransi";import{ReadStream as O}from"node:tty";function f(r,t,s){if(!s.some(o=>!o.disabled))return r;const e=r+t,i=Math.max(s.length-1,0),n=e<0?i:e>i?0:e;return s[n].disabled?f(n,t<0?-1:1,s):n}function I(r,t,s,e){const i=e.split(`
`);let n=0,o=r;for(const a of i){if(o<=a.length)break;o-=a.length+1,n++}for(n=Math.max(0,Math.min(i.length-1,n+s)),o=Math.min(o,i[n].length)+t;o<0&&n>0;)n--,o+=i[n].length+1;for(;o>i[n].length&&n<i.length-1;)o-=i[n].length+1,n++;o=Math.max(0,Math.min(i[n].length,o));let u=0;for(let a=0;a<n;a++)u+=i[a].length+1;return u+o}const K=["up","down","left","right","space","enter","cancel"],j=["January","February","March","April","May","June","July","August","September","October","November","December"],h={actions:new Set(K),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:[...j],messages:{required:"Please enter a valid date",invalidMonth:"There are only 12 months in a year",invalidDay:(r,t)=>`There are only ${r} days in ${t}`,afterMin:r=>`Date must be on or after ${r.toISOString().slice(0,10)}`,beforeMax:r=>`Date must be on or before ${r.toISOString().slice(0,10)}`}}};function z(r){if(r.aliases!==void 0){const t=r.aliases;for(const s in t){if(!Object.hasOwn(t,s))continue;const e=t[s];h.actions.has(e)&&(h.aliases.has(s)||h.aliases.set(s,e))}}if(r.messages!==void 0){const t=r.messages;t.cancel!==void 0&&(h.messages.cancel=t.cancel),t.error!==void 0&&(h.messages.error=t.error)}if(r.withGuide!==void 0&&(h.withGuide=r.withGuide!==!1),r.date!==void 0){const t=r.date;t.monthNames!==void 0&&(h.date.monthNames=[...t.monthNames]),t.messages!==void 0&&(t.messages.required!==void 0&&(h.date.messages.required=t.messages.required),t.messages.invalidMonth!==void 0&&(h.date.messages.invalidMonth=t.messages.invalidMonth),t.messages.invalidDay!==void 0&&(h.date.messages.invalidDay=t.messages.invalidDay),t.messages.afterMin!==void 0&&(h.date.messages.afterMin=t.messages.afterMin),t.messages.beforeMax!==void 0&&(h.date.messages.beforeMax=t.messages.beforeMax))}}function C(r,t){if(typeof r=="string")return h.aliases.get(r)===t;for(const s of r)if(s!==void 0&&C(s,t))return!0;return!1}function Y(r,t){if(r===t)return;const s=r.split(`
`),e=t.split(`
`),i=Math.max(s.length,e.length),n=[];for(let o=0;o<i;o++)s[o]!==e[o]&&n.push(o);return{lines:n,numLinesBefore:s.length,numLinesAfter:e.length,numLines:i}}const q=globalThis.process.platform.startsWith("win"),k=Symbol("clack:cancel");function R(r){return r===k}function w(r,t){const s=r;s.isTTY&&s.setRawMode(t)}function W({input:r=D,output:t=x,overwrite:s=!0,hideCursor:e=!0}={}){const i=b.createInterface({input:r,output:t,prompt:"",tabSize:1});b.emitKeypressEvents(r,i),r instanceof O&&r.isTTY&&r.setRawMode(!0);const n=(o,{name:u,sequence:a})=>{const l=String(o);if(C([l,u,a],"cancel")){e&&t.write(p.show),process.exit(0);return}if(!s)return;const c=u==="return"?0:-1,y=u==="return"?-1:0;b.moveCursor(t,c,y,()=>{b.clearLine(t,1,()=>{r.once("keypress",n)})})};return e&&t.write(p.hide),r.once("keypress",n),()=>{r.off("keypress",n),e&&t.write(p.show),r instanceof O&&r.isTTY&&!q&&r.setRawMode(!1),i.terminal=!1,i.close()}}const A=r=>"columns"in r&&typeof r.columns=="number"?r.columns:80,L=r=>"rows"in r&&typeof r.rows=="number"?r.rows:20;function B(r,t,s,e=s,i=s,n){const o=A(r??x);return M(t,o-s.length,{hard:!0,trim:!1}).split(`
`).map((u,a,l)=>{const c=n?n(u,a):u;return a===0?`${e}${c}`:a===l.length-1?`${i}${c}`:`${s}${c}`}).join(`
`)}function P(r,t){if("~standard"in r){const s=r["~standard"].validate(t);if(s instanceof Promise)throw new TypeError("Schema validation must be synchronous. Update `validate()` and remove any asynchronous logic.");return s.issues?.at(0)?.message}return r(t)}class m{input;output;_abortSignal;rl;opts;_render;_track=!1;_prevFrame="";_subscribers=new Map;_cursor=0;state="initial";error="";value;userInput="";constructor(t,s=!0){const{input:e=D,output:i=x,render:n,signal:o,...u}=t;this.opts=u,this.onKeypress=this.onKeypress.bind(this),this.close=this.close.bind(this),this.render=this.render.bind(this),this._render=n.bind(this),this._track=s,this._abortSignal=o,this.input=e,this.output=i}unsubscribe(){this._subscribers.clear()}setSubscriber(t,s){const e=this._subscribers.get(t)??[];e.push(s),this._subscribers.set(t,e)}on(t,s){this.setSubscriber(t,{cb:s})}once(t,s){this.setSubscriber(t,{cb:s,once:!0})}emit(t,...s){const e=this._subscribers.get(t)??[],i=[];for(const n of e)n.cb(...s),n.once&&i.push(()=>e.splice(e.indexOf(n),1));for(const n of i)n()}prompt(){return new Promise(t=>{if(this._abortSignal){if(this._abortSignal.aborted)return this.state="cancel",this.close(),t(k);this._abortSignal.addEventListener("abort",()=>{this.state="cancel",this.close()},{once:!0})}this.rl=G.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),w(this.input,!0),this.output.on("resize",this.render),this.render(),this.once("submit",()=>{this.output.write(p.show),this.output.off("resize",this.render),w(this.input,!1),t(this.value)}),this.once("cancel",()=>{this.output.write(p.show),this.output.off("resize",this.render),w(this.input,!1),t(k)})})}_isActionKey(t,s){return t===" "}_shouldSubmit(t,s){return!0}_setValue(t){this.value=t,this.emit("value",this.value)}_setUserInput(t,s){this.userInput=t??"",this.emit("userInput",this.userInput),s&&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(t,s){if(this._track&&s.name!=="return"&&(s.name&&this._isActionKey(t,s)&&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"),s?.name&&(!this._track&&h.aliases.has(s.name)&&this.emit("cursor",h.aliases.get(s.name)),h.actions.has(s.name)&&this.emit("cursor",s.name)),t&&(t.toLowerCase()==="y"||t.toLowerCase()==="n")&&this.emit("confirm",t.toLowerCase()==="y"),this.emit("key",t,s),s?.name==="return"&&this._shouldSubmit(t,s)){if(this.opts.validate){const e=P(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")}C([t,s?.name,s?.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(`
`),w(this.input,!1),this.rl?.close(),this.rl=void 0,this.emit(`${this.state}`,this.value),this.unsubscribe()}restoreCursor(){const t=M(this._prevFrame,process.stdout.columns,{hard:!0,trim:!1}).split(`
`).length-1;this.output.write(p.move(-999,t*-1))}render(){const t=M(this._render(this)??"",process.stdout.columns,{hard:!0,trim:!1});if(t!==this._prevFrame){if(this.state==="initial")this.output.write(p.hide);else{const s=Y(this._prevFrame,t),e=L(this.output);if(this.restoreCursor(),s){const i=Math.max(0,s.numLinesAfter-e),n=Math.max(0,s.numLinesBefore-e);let o=s.lines.find(u=>u>=i);if(o===void 0){this._prevFrame=t;return}if(s.lines.length===1){this.output.write(p.move(0,o-n)),this.output.write(V.lines(1));const u=t.split(`
`);this.output.write(u[o]),this._prevFrame=t,this.output.write(p.move(0,u.length-o-1));return}else if(s.lines.length>1){if(i<n)o=i;else{const a=o-n;a>0&&this.output.write(p.move(0,a))}this.output.write(V.down());const u=t.split(`
`).slice(o);this.output.write(u.join(`
`)),this._prevFrame=t;return}}this.output.write(V.down())}this.output.write(t),this.state==="initial"&&(this.state="active"),this._prevFrame=t}}}function J(r,t){if(r===void 0||t.length===0)return 0;const s=t.findIndex(e=>e.value===r);return s!==-1?s:0}function H(r,t){return(t.label??String(t.value)).toLowerCase().includes(r.toLowerCase())}function Q(r,t){if(t)return r?t:t[0]}let X=class extends m{filteredOptions;multiple;isNavigating=!1;selectedValues=[];focusedValue;#s=0;#r="";#t;#n;#u;get cursor(){return this.#s}get userInputWithCursor(){if(!this.userInput)return v(["inverse","hidden"],"_");if(this._cursor>=this.userInput.length)return`${this.userInput}\u2588`;const t=this.userInput.slice(0,this._cursor),[s,...e]=this.userInput.slice(this._cursor);return`${t}${v("inverse",s)}${e.join("")}`}get options(){return typeof this.#n=="function"?this.#n():this.#n}constructor(t){super(t),this.#n=t.options,this.#u=t.placeholder;const s=this.options;this.filteredOptions=[...s],this.multiple=t.multiple===!0,this.#t=typeof t.options=="function"?t.filter:t.filter??H;let e;if(t.initialValue&&Array.isArray(t.initialValue)?this.multiple?e=t.initialValue:e=t.initialValue.slice(0,1):!this.multiple&&this.options.length>0&&(e=[this.options[0].value]),e)for(const i of e){const n=s.findIndex(o=>o.value===i);n!==-1&&(this.toggleSelected(i),this.#s=n)}this.focusedValue=this.options[this.#s]?.value,this.on("key",(i,n)=>this.#e(i,n)),this.on("userInput",i=>this.#i(i))}_isActionKey(t,s){return t===" "||this.multiple&&this.isNavigating&&s.name==="space"&&t!==void 0&&t!==""}#e(t,s){const e=s.name==="up",i=s.name==="down",n=s.name==="return",o=this.userInput===""||this.userInput===" ",u=this.#u,a=this.options,l=u!==void 0&&u!==""&&a.some(c=>!c.disabled&&(this.#t?this.#t(u,c):!0));if(s.name==="tab"&&o&&l){this.userInput===" "&&this._clearUserInput(),this._setUserInput(u,!0),this.isNavigating=!1;return}e||i?(this.#s=f(this.#s,e?-1:1,this.filteredOptions),this.focusedValue=this.filteredOptions[this.#s]?.value,this.multiple||(this.selectedValues=[this.focusedValue]),this.isNavigating=!0):n?this.value=Q(this.multiple,this.selectedValues):this.multiple?this.focusedValue!==void 0&&(s.name==="tab"||this.isNavigating&&s.name==="space")?this.toggleSelected(this.focusedValue):this.isNavigating=!1:(this.focusedValue&&(this.selectedValues=[this.focusedValue]),this.isNavigating=!1)}deselectAll(){this.selectedValues=[]}toggleSelected(t){this.filteredOptions.length!==0&&(this.multiple?this.selectedValues.includes(t)?this.selectedValues=this.selectedValues.filter(s=>s!==t):this.selectedValues=[...this.selectedValues,t]:this.selectedValues=[t])}#i(t){if(t!==this.#r){this.#r=t;const s=this.options;t&&this.#t?this.filteredOptions=s.filter(n=>this.#t?.(t,n)):this.filteredOptions=[...s];const e=J(this.focusedValue,this.filteredOptions);this.#s=f(e,0,this.filteredOptions);const i=this.filteredOptions[this.#s];i&&!i.disabled?this.focusedValue=i.value:this.focusedValue=void 0,this.multiple||(this.focusedValue!==void 0?this.toggleSelected(this.focusedValue):this.deselectAll())}}};class Z extends m{get cursor(){return this.value?0:1}get _value(){return this.cursor===0}constructor(t){super(t,!1),this.value=!!t.initialValue,this.on("userInput",()=>{this.value=this._value}),this.on("confirm",s=>{this.output.write(p.move(0,-1)),this.value=s,this.state="submit",this.close()}),this.on("cursor",()=>{this.value=!this.value})}}const tt={Y:{type:"year",len:4},M:{type:"month",len:2},D:{type:"day",len:2}};function F(r){return[...r].map(t=>tt[t])}function st(r){const t=new Intl.DateTimeFormat(r,{year:"numeric",month:"2-digit",day:"2-digit"}).formatToParts(new Date(2e3,0,15)),s=[];let e="/";for(const i of t)i.type==="literal"?e=i.value.trim()||i.value:(i.type==="year"||i.type==="month"||i.type==="day")&&s.push({type:i.type,len:i.type==="year"?4:2});return{segments:s,separator:e}}function $(r){return Number.parseInt((r||"0").replace(/_/g,"0"),10)||0}function S(r){return{year:$(r.year),month:$(r.month),day:$(r.day)}}function U(r,t){return new Date(r||2001,t||1,0).getDate()}function N(r){const{year:t,month:s,day:e}=S(r);if(!t||t<0||t>9999||!s||s<1||s>12||!e||e<1)return;const i=new Date(Date.UTC(t,s-1,e));if(!(i.getUTCFullYear()!==t||i.getUTCMonth()!==s-1||i.getUTCDate()!==e))return{year:t,month:s,day:e}}function E(r){const t=N(r);return t?new Date(Date.UTC(t.year,t.month-1,t.day)):void 0}function et(r,t,s,e){const i=s?{year:s.getUTCFullYear(),month:s.getUTCMonth()+1,day:s.getUTCDate()}:null,n=e?{year:e.getUTCFullYear(),month:e.getUTCMonth()+1,day:e.getUTCDate()}:null;return r==="year"?{min:i?.year??1,max:n?.year??9999}:r==="month"?{min:i&&t.year===i.year?i.month:1,max:n&&t.year===n.year?n.month:12}:{min:i&&t.year===i.year&&t.month===i.month?i.day:1,max:n&&t.year===n.year&&t.month===n.month?n.day:U(t.year,t.month)}}class it extends m{#s;#r;#t;#n;#u;#e={segmentIndex:0,positionInSegment:0};#i=!0;#o=null;inlineError="";get segmentCursor(){return{...this.#e}}get segmentValues(){return{...this.#t}}get segments(){return this.#s}get separator(){return this.#r}get formattedValue(){return this.#c(this.#t)}#c(t){return this.#s.map(s=>t[s.type]).join(this.#r)}#a(){this._setUserInput(this.#c(this.#t)),this._setValue(E(this.#t)??void 0)}constructor(t){const s=t.format?{segments:F(t.format),separator:t.separator??"/"}:st(t.locale),e=t.separator??s.separator,i=t.format?F(t.format):s.segments,n=t.initialValue??t.defaultValue,o=n?{year:String(n.getUTCFullYear()).padStart(4,"0"),month:String(n.getUTCMonth()+1).padStart(2,"0"),day:String(n.getUTCDate()).padStart(2,"0")}:{year:"____",month:"__",day:"__"},u=i.map(a=>o[a.type]).join(e);super({...t,initialUserInput:u},!1),this.#s=i,this.#r=e,this.#t=o,this.#n=t.minDate,this.#u=t.maxDate,this.#a(),this.on("cursor",a=>this.#d(a)),this.on("key",(a,l)=>this.#f(a,l)),this.on("finalize",()=>this.#g(t))}#h(){const t=Math.max(0,Math.min(this.#e.segmentIndex,this.#s.length-1)),s=this.#s[t];if(s)return this.#e.positionInSegment=Math.max(0,Math.min(this.#e.positionInSegment,s.len-1)),{segment:s,index:t}}#l(t){this.inlineError="",this.#o=null;const s=this.#h();s&&(this.#e.segmentIndex=Math.max(0,Math.min(this.#s.length-1,s.index+t)),this.#e.positionInSegment=0,this.#i=!0)}#p(t){const s=this.#h();if(!s)return;const{segment:e}=s,i=this.#t[e.type],n=!i||i.replace(/_/g,"")==="",o=Number.parseInt((i||"0").replace(/_/g,"0"),10)||0,u=et(e.type,S(this.#t),this.#n,this.#u);let a;n?a=t===1?u.min:u.max:a=Math.max(Math.min(u.max,o+t),u.min),this.#t={...this.#t,[e.type]:a.toString().padStart(e.len,"0")},this.#i=!0,this.#o=null,this.#a()}#d(t){if(t)switch(t){case"right":return this.#l(1);case"left":return this.#l(-1);case"up":return this.#p(1);case"down":return this.#p(-1)}}#f(t,s){if(s?.name==="backspace"||s?.sequence==="\x7F"||s?.sequence==="\b"||t==="\x7F"||t==="\b"){this.inlineError="";const e=this.#h();if(!e)return;if(!this.#t[e.segment.type].replace(/_/g,"")){this.#l(-1);return}this.#t[e.segment.type]="_".repeat(e.segment.len),this.#i=!0,this.#e.positionInSegment=0,this.#a();return}if(s?.name==="tab"){this.inlineError="";const e=this.#h();if(!e)return;const i=s.shift?-1:1,n=e.index+i;n>=0&&n<this.#s.length&&(this.#e.segmentIndex=n,this.#e.positionInSegment=0,this.#i=!0);return}if(t&&/^[0-9]$/.test(t)){const e=this.#h();if(!e)return;const{segment:i}=e,n=!this.#t[i.type].replace(/_/g,"");if(this.#i&&this.#o!==null&&!n){const d=this.#o+t,g={...this.#t,[i.type]:d},_=this.#m(g,i);if(_){this.inlineError=_,this.#o=null,this.#i=!1;return}this.inlineError="",this.#t[i.type]=d,this.#o=null,this.#i=!1,this.#a(),e.index<this.#s.length-1&&(this.#e.segmentIndex=e.index+1,this.#e.positionInSegment=0,this.#i=!0);return}this.#i&&!n&&(this.#t[i.type]="_".repeat(i.len),this.#e.positionInSegment=0),this.#i=!1,this.#o=null;const o=this.#t[i.type],u=o.indexOf("_"),a=u>=0?u:Math.min(this.#e.positionInSegment,i.len-1);if(a<0||a>=i.len)return;let l=o.slice(0,a)+t+o.slice(a+1),c=!1;if(a===0&&o==="__"&&(i.type==="month"||i.type==="day")){const d=Number.parseInt(t,10);l=`0${t}`,c=d<=(i.type==="month"?1:2)}if(i.type==="year"&&(l=(o.replace(/_/g,"")+t).padStart(i.len,"_")),!l.includes("_")){const d={...this.#t,[i.type]:l},g=this.#m(d,i);if(g){this.inlineError=g;return}}this.inlineError="",this.#t[i.type]=l;const y=l.includes("_")?void 0:N(this.#t);if(y){const{year:d,month:g}=y,_=U(d,g);this.#t={year:String(Math.max(0,Math.min(9999,d))).padStart(4,"0"),month:String(Math.max(1,Math.min(12,g))).padStart(2,"0"),day:String(Math.max(1,Math.min(_,y.day))).padStart(2,"0")}}this.#a();const T=l.indexOf("_");c?(this.#i=!0,this.#o=t):T>=0?this.#e.positionInSegment=T:u>=0&&e.index<this.#s.length-1?(this.#e.segmentIndex=e.index+1,this.#e.positionInSegment=0,this.#i=!0):this.#e.positionInSegment=Math.min(a+1,i.len-1)}}#m(t,s){const{month:e,day:i}=S(t);if(s.type==="month"&&(e<0||e>12))return h.date.messages.invalidMonth;if(s.type==="day"&&(i<0||i>31))return h.date.messages.invalidDay(31,"any month")}#g(t){const{year:s,month:e,day:i}=S(this.#t);if(s&&e&&i){const n=U(s,e);this.#t={...this.#t,day:String(Math.min(i,n)).padStart(2,"0")}}this.value=E(this.#t)??t.defaultValue??void 0}}let rt=class extends m{options;cursor=0;#s;getGroupItems(t){return this.options.filter(s=>s.group===t)}isGroupSelected(t){const s=this.getGroupItems(t),e=this.value;return e===void 0?!1:s.every(i=>e.includes(i.value))}toggleValue(){const t=this.options[this.cursor];if(this.value===void 0&&(this.value=[]),t.group===!0){const s=t.value,e=this.getGroupItems(s);this.isGroupSelected(s)?this.value=this.value.filter(i=>e.findIndex(n=>n.value===i)===-1):this.value=[...this.value,...e.map(i=>i.value)],this.value=Array.from(new Set(this.value))}else{const s=this.value.includes(t.value);this.value=s?this.value.filter(e=>e!==t.value):[...this.value,t.value]}}constructor(t){super(t,!1);const{options:s}=t;this.#s=t.selectableGroups!==!1,this.options=Object.entries(s).flatMap(([e,i])=>[{value:e,group:!0,label:e},...i.map(n=>({...n,group:e}))]),this.value=[...t.initialValues??[]],this.cursor=Math.max(this.options.findIndex(({value:e})=>e===t.cursorAt),this.#s?0:1),this.on("cursor",e=>{switch(e){case"left":case"up":{this.cursor=this.cursor===0?this.options.length-1:this.cursor-1;const i=this.options[this.cursor]?.group===!0;!this.#s&&i&&(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;const i=this.options[this.cursor]?.group===!0;!this.#s&&i&&(this.cursor=this.cursor===this.options.length-1?0:this.cursor+1);break}case"space":this.toggleValue();break}})}};const nt=new Set(["up","down","left","right"]);class ot extends m{#s=!1;#r;focused="editor";get userInputWithCursor(){if(this.state==="submit")return this.userInput;const t=this.userInput;if(this.cursor>=t.length)return`${t}\u2588`;const s=t.slice(0,this.cursor),e=t[this.cursor],i=t.slice(this.cursor+1);return e===`
`?`${s}\u2588
${i}`:`${s}${v("inverse",e)}${i}`}get cursor(){return this._cursor}#t(t){if(this.userInput.length===0){this._setUserInput(t);return}this._setUserInput(this.userInput.slice(0,this.cursor)+t+this.userInput.slice(this.cursor))}#n(t){const s=this.value??"";switch(t){case"up":this._cursor=I(this._cursor,0,-1,s);return;case"down":this._cursor=I(this._cursor,0,1,s);return;case"left":this._cursor=I(this._cursor,-1,0,s);return;case"right":this._cursor=I(this._cursor,1,0,s);return}}_shouldSubmit(t,s){if(this.#r)return this.focused==="submit"?!0:(this.#t(`
`),this._cursor++,!1);const e=this.#s;return this.#s=!0,e?(this.userInput[this.cursor-1]===`
`&&(this._setUserInput(this.userInput.slice(0,this.cursor-1)+this.userInput.slice(this.cursor)),this._cursor--),!0):(this.#t(`
`),this._cursor++,!1)}constructor(t){super(t,!1),this.#r=t.showSubmit??!1,this.on("key",(s,e)=>{if(e?.name&&nt.has(e.name)){this.#n(e.name);return}if(s===" "&&this.#r){this.focused=this.focused==="editor"?"submit":"editor";return}if(e?.name!=="return"){if(this.#s=!1,e?.name==="backspace"&&this.cursor>0){this._setUserInput(this.userInput.slice(0,this.cursor-1)+this.userInput.slice(this.cursor)),this._cursor--;return}if(e?.name==="delete"&&this.cursor<this.userInput.length){this._setUserInput(this.userInput.slice(0,this.cursor)+this.userInput.slice(this.cursor+1));return}s&&(this.#r&&this.focused==="submit"&&(this.focused="editor"),this.#t(s??""),this._cursor++)}}),this.on("userInput",s=>{this._setValue(s)}),this.on("finalize",()=>{this.value||(this.value=t.defaultValue),this.value===void 0&&(this.value="")})}}let ut=class extends m{options;cursor=0;get _value(){return this.options[this.cursor].value}get _enabledOptions(){return this.options.filter(t=>t.disabled!==!0)}toggleAll(){const t=this._enabledOptions,s=this.value!==void 0&&this.value.length===t.length;this.value=s?[]:t.map(e=>e.value)}toggleInvert(){const t=this.value;if(!t)return;const s=this._enabledOptions.filter(e=>!t.includes(e.value));this.value=s.map(e=>e.value)}toggleValue(){this.value===void 0&&(this.value=[]);const t=this.value.includes(this._value);this.value=t?this.value.filter(s=>s!==this._value):[...this.value,this._value]}constructor(t){super(t,!1),this.options=t.options,this.value=[...t.initialValues??[]];const s=Math.max(this.options.findIndex(({value:e})=>e===t.cursorAt),0);this.cursor=this.options[s].disabled?f(s,1,this.options):s,this.on("key",(e,i)=>{i.name==="a"&&this.toggleAll(),i.name==="i"&&this.toggleInvert()}),this.on("cursor",e=>{switch(e){case"left":case"up":this.cursor=f(this.cursor,-1,this.options);break;case"down":case"right":this.cursor=f(this.cursor,1,this.options);break;case"space":this.toggleValue();break}})}};class at extends m{_mask="\u2022";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;const t=this.userInput;if(this.cursor>=t.length)return`${this.masked}${v(["inverse","hidden"],"_")}`;const s=this.masked,e=s.slice(0,this.cursor),i=s.slice(this.cursor);return`${e}${v("inverse",i[0])}${i.slice(1)}`}clear(){this._clearUserInput()}constructor({mask:t,...s}){super(s),this._mask=t??"\u2022",this.on("userInput",e=>{this._setValue(e)})}}class ht extends m{options;cursor=0;get _selectedValue(){return this.options[this.cursor]}changeValue(){this.value=this._selectedValue.value}constructor(t){super(t,!1),this.options=t.options;const s=this.options.findIndex(({value:i})=>i===t.initialValue),e=s===-1?0:s;this.cursor=this.options[e].disabled?f(e,1,this.options):e,this.changeValue(),this.on("cursor",i=>{switch(i){case"left":case"up":this.cursor=f(this.cursor,-1,this.options);break;case"down":case"right":this.cursor=f(this.cursor,1,this.options);break}this.changeValue()})}}class lt extends m{options;cursor=0;constructor(t){super(t,!1),this.options=t.options;const s=t.caseSensitive===!0,e=this.options.map(({value:[i]})=>s?i:i?.toLowerCase());this.cursor=Math.max(e.indexOf(t.initialValue),0),this.on("key",i=>{if(!i)return;const n=s?i:i.toLowerCase();if(!e.includes(n))return;const o=this.options.find(({value:[u]})=>s?u===n:u?.toLowerCase()===n);o&&(this.value=o.value,this.state="submit",this.emit("submit"))})}}class ct extends m{get userInputWithCursor(){if(this.state==="submit")return this.userInput;const t=this.userInput;if(this.cursor>=t.length)return`${this.userInput}\u2588`;const s=t.slice(0,this.cursor),[e,...i]=t.slice(this.cursor);return`${s}${v("inverse",e)}${i.join("")}`}get cursor(){return this._cursor}constructor(t){super({...t,initialUserInput:t.initialUserInput??t.initialValue}),this.on("userInput",s=>{this._setValue(s)}),this.on("finalize",()=>{this.value||(this.value=t.defaultValue),this.value===void 0&&(this.value="")})}}export{X as AutocompletePrompt,Z as ConfirmPrompt,it as DatePrompt,rt as GroupMultiSelectPrompt,ot as MultiLinePrompt,ut as MultiSelectPrompt,at as PasswordPrompt,m as Prompt,lt as SelectKeyPrompt,ht as SelectPrompt,ct as TextPrompt,W as block,A as getColumns,L as getRows,R as isCancel,P as runValidation,h as settings,z as updateSettings,B as wrapTextWithPrefix};
//# sourceMappingURL=index.mjs.map
import { styleText } from 'node:util';
import { stdout, stdin } from 'node:process';
import * as l from 'node:readline';
import l__default from 'node:readline';
import { wrapAnsi } from 'fast-wrap-ansi';
import { cursor, erase } from 'sisteransi';
import { ReadStream } from 'node:tty';
function findCursor(s, o, l) {
if (!l.some((r) => !r.disabled))
return s;
const t = s + o, n = Math.max(l.length - 1, 0), e = t < 0 ? n : t > n ? 0 : t;
return l[e].disabled ? findCursor(e, o < 0 ? -1 : 1, l) : e;
}
function findTextCursor(s, o, l, i) {
const t = i.split(`
`);
let n = 0, e = s;
for (const r of t) {
if (e <= r.length)
break;
e -= r.length + 1, n++;
}
for (n = Math.max(0, Math.min(t.length - 1, n + l)), e = Math.min(e, t[n].length) + o; e < 0 && n > 0; )
n--, e += t[n].length + 1;
for (; e > t[n].length && n < t.length - 1; )
e -= t[n].length + 1, n++;
e = Math.max(0, Math.min(t[n].length, e));
let h = 0;
for (let r = 0; r < n; r++)
h += t[r].length + 1;
return h + e;
}
const a$2 = ["up", "down", "left", "right", "space", "enter", "cancel"], t = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
const settings = {
actions: new Set(a$2),
aliases: /* @__PURE__ */ new Map([
// vim support
["k", "up"],
["j", "down"],
["h", "left"],
["l", "right"],
["", "cancel"],
// opinionated defaults!
["escape", "cancel"]
]),
messages: {
cancel: "Canceled",
error: "Something went wrong"
},
withGuide: true,
date: {
monthNames: [...t],
messages: {
required: "Please enter a valid date",
invalidMonth: "There are only 12 months in a year",
invalidDay: (n, e) => `There are only ${n} days in ${e}`,
afterMin: (n) => `Date must be on or after ${n.toISOString().slice(0, 10)}`,
beforeMax: (n) => `Date must be on or before ${n.toISOString().slice(0, 10)}`
}
}
};
function updateSettings(n) {
if (n.aliases !== void 0) {
const e = n.aliases;
for (const s in e) {
if (!Object.hasOwn(e, s)) continue;
const i = e[s];
settings.actions.has(i) && (settings.aliases.has(s) || settings.aliases.set(s, i));
}
}
if (n.messages !== void 0) {
const e = n.messages;
e.cancel !== void 0 && (settings.messages.cancel = e.cancel), e.error !== void 0 && (settings.messages.error = e.error);
}
if (n.withGuide !== void 0 && (settings.withGuide = n.withGuide !== false), n.date !== void 0) {
const e = n.date;
e.monthNames !== void 0 && (settings.date.monthNames = [...e.monthNames]), e.messages !== void 0 && (e.messages.required !== void 0 && (settings.date.messages.required = e.messages.required), e.messages.invalidMonth !== void 0 && (settings.date.messages.invalidMonth = e.messages.invalidMonth), e.messages.invalidDay !== void 0 && (settings.date.messages.invalidDay = e.messages.invalidDay), e.messages.afterMin !== void 0 && (settings.date.messages.afterMin = e.messages.afterMin), e.messages.beforeMax !== void 0 && (settings.date.messages.beforeMax = e.messages.beforeMax));
}
}
function isActionKey(n, e) {
if (typeof n == "string")
return settings.aliases.get(n) === e;
for (const s of n)
if (s !== void 0 && isActionKey(s, e))
return true;
return false;
}
function diffLines(i, s) {
if (i === s) return;
const e = i.split(`
`), t = s.split(`
`), r = Math.max(e.length, t.length), f = [];
for (let n = 0; n < r; n++)
e[n] !== t[n] && f.push(n);
return {
lines: f,
numLinesBefore: e.length,
numLinesAfter: t.length,
numLines: r
};
}
const R = globalThis.process.platform.startsWith("win");
const CANCEL_SYMBOL = Symbol("clack:cancel");
function isCancel(e) {
return e === CANCEL_SYMBOL;
}
function setRawMode(e, r) {
const o = e;
o.isTTY && o.setRawMode(r);
}
function block({
input: e = stdin,
output: r = stdout,
overwrite: o = true,
hideCursor: t = true
} = {}) {
const s = l.createInterface({
input: e,
output: r,
prompt: "",
tabSize: 1
});
l.emitKeypressEvents(e, s), e instanceof ReadStream && e.isTTY && e.setRawMode(true);
const n = (f, { name: a, sequence: p }) => {
const c = String(f);
if (isActionKey([c, a, p], "cancel")) {
t && r.write(cursor.show), process.exit(0);
return;
}
if (!o) return;
const i = a === "return" ? 0 : -1, m = a === "return" ? -1 : 0;
l.moveCursor(r, i, m, () => {
l.clearLine(r, 1, () => {
e.once("keypress", n);
});
});
};
return t && r.write(cursor.hide), e.once("keypress", n), () => {
e.off("keypress", n), t && r.write(cursor.show), e instanceof ReadStream && e.isTTY && !R && e.setRawMode(false), s.terminal = false, s.close();
};
}
const getColumns = (e) => "columns" in e && typeof e.columns == "number" ? e.columns : 80, getRows = (e) => "rows" in e && typeof e.rows == "number" ? e.rows : 20;
function wrapTextWithPrefix(e, r, o, t = o, s = o, n) {
const f = getColumns(e ?? stdout);
return wrapAnsi(r, f - o.length, {
hard: true,
trim: false
}).split(`
`).map((c, i, m) => {
const d = n ? n(c, i) : c;
return i === 0 ? `${t}${d}` : i === m.length - 1 ? `${s}${d}` : `${o}${d}`;
}).join(`
`);
}
function runValidation(e, n) {
if ("~standard" in e) {
const a = e["~standard"].validate(n);
if (a instanceof Promise)
throw new TypeError(
"Schema validation must be synchronous. Update `validate()` and remove any asynchronous logic."
);
return a.issues?.at(0)?.message;
}
return e(n);
}
class V {
input;
output;
_abortSignal;
rl;
opts;
_render;
_track = false;
_prevFrame = "";
_subscribers = /* @__PURE__ */ new Map();
_cursor = 0;
state = "initial";
error = "";
value;
userInput = "";
constructor(t, e = true) {
const { input: i = stdin, output: n = stdout, render: s, signal: r, ...o } = t;
this.opts = o, this.onKeypress = this.onKeypress.bind(this), this.close = this.close.bind(this), this.render = this.render.bind(this), this._render = s.bind(this), this._track = e, this._abortSignal = r, this.input = i, this.output = n;
}
/**
* Unsubscribe all listeners
*/
unsubscribe() {
this._subscribers.clear();
}
/**
* Set a subscriber with opts
* @param event - The event name
*/
setSubscriber(t, e) {
const i = this._subscribers.get(t) ?? [];
i.push(e), this._subscribers.set(t, i);
}
/**
* Subscribe to an event
* @param event - The event name
* @param cb - The callback
*/
on(t, e) {
this.setSubscriber(t, { cb: e });
}
/**
* Subscribe to an event once
* @param event - The event name
* @param cb - The callback
*/
once(t, e) {
this.setSubscriber(t, { cb: e, once: true });
}
/**
* Emit an event with data
* @param event - The event name
* @param data - The data to pass to the callback
*/
emit(t, ...e) {
const i = this._subscribers.get(t) ?? [], n = [];
for (const s of i)
s.cb(...e), s.once && n.push(() => i.splice(i.indexOf(s), 1));
for (const s of n)
s();
}
prompt() {
return new Promise((t) => {
if (this._abortSignal) {
if (this._abortSignal.aborted)
return this.state = "cancel", this.close(), t(CANCEL_SYMBOL);
this._abortSignal.addEventListener(
"abort",
() => {
this.state = "cancel", this.close();
},
{ once: true }
);
}
this.rl = l__default.createInterface({
input: this.input,
tabSize: 2,
prompt: "",
escapeCodeTimeout: 50,
terminal: true
}), this.rl.prompt(), this.opts.initialUserInput !== void 0 && this._setUserInput(this.opts.initialUserInput, true), this.input.on("keypress", this.onKeypress), setRawMode(this.input, true), this.output.on("resize", this.render), this.render(), this.once("submit", () => {
this.output.write(cursor.show), this.output.off("resize", this.render), setRawMode(this.input, false), t(this.value);
}), this.once("cancel", () => {
this.output.write(cursor.show), this.output.off("resize", this.render), setRawMode(this.input, false), t(CANCEL_SYMBOL);
});
});
}
_isActionKey(t, e) {
return t === " ";
}
_shouldSubmit(t, e) {
return true;
}
_setValue(t) {
this.value = t, this.emit("value", this.value);
}
_setUserInput(t, e) {
this.userInput = t ?? "", this.emit("userInput", this.userInput), e && this._track && this.rl && (this.rl.write(this.userInput), this._cursor = this.rl.cursor);
}
_clearUserInput() {
this.rl?.write(null, { ctrl: true, name: "u" }), this._setUserInput("");
}
onKeypress(t, e) {
if (this._track && e.name !== "return" && (e.name && this._isActionKey(t, e) && this.rl?.write(null, { ctrl: true, name: "h" }), this._cursor = this.rl?.cursor ?? 0, this._setUserInput(this.rl?.line)), this.state === "error" && (this.state = "active"), e?.name && (!this._track && settings.aliases.has(e.name) && this.emit("cursor", settings.aliases.get(e.name)), settings.actions.has(e.name) && this.emit("cursor", e.name)), t && (t.toLowerCase() === "y" || t.toLowerCase() === "n") && this.emit("confirm", t.toLowerCase() === "y"), this.emit("key", t, e), e?.name === "return" && this._shouldSubmit(t, e)) {
if (this.opts.validate) {
const i = runValidation(this.opts.validate, this.value);
i && (this.error = i instanceof Error ? i.message : i, this.state = "error", this.rl?.write(this.userInput));
}
this.state !== "error" && (this.state = "submit");
}
isActionKey([t, e?.name, e?.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(`
`), setRawMode(this.input, false), this.rl?.close(), this.rl = void 0, this.emit(`${this.state}`, this.value), this.unsubscribe();
}
restoreCursor() {
const t = wrapAnsi(this._prevFrame, process.stdout.columns, { hard: true, trim: false }).split(`
`).length - 1;
this.output.write(cursor.move(-999, t * -1));
}
render() {
const t = wrapAnsi(this._render(this) ?? "", process.stdout.columns, {
hard: true,
trim: false
});
if (t !== this._prevFrame) {
if (this.state === "initial")
this.output.write(cursor.hide);
else {
const e = diffLines(this._prevFrame, t), i = getRows(this.output);
if (this.restoreCursor(), e) {
const n = Math.max(0, e.numLinesAfter - i), s = Math.max(0, e.numLinesBefore - i);
let r = e.lines.find((o) => o >= n);
if (r === void 0) {
this._prevFrame = t;
return;
}
if (e.lines.length === 1) {
this.output.write(cursor.move(0, r - s)), this.output.write(erase.lines(1));
const o = t.split(`
`);
this.output.write(o[r]), this._prevFrame = t, this.output.write(cursor.move(0, o.length - r - 1));
return;
} else if (e.lines.length > 1) {
if (n < s)
r = n;
else {
const h = r - s;
h > 0 && this.output.write(cursor.move(0, h));
}
this.output.write(erase.down());
const f = t.split(`
`).slice(r);
this.output.write(f.join(`
`)), this._prevFrame = t;
return;
}
}
this.output.write(erase.down());
}
this.output.write(t), this.state === "initial" && (this.state = "active"), this._prevFrame = t;
}
}
}
function p$1(l, e) {
if (l === void 0 || e.length === 0)
return 0;
const i = e.findIndex((s) => s.value === l);
return i !== -1 ? i : 0;
}
function g(l, e) {
return (e.label ?? String(e.value)).toLowerCase().includes(l.toLowerCase());
}
function m(l, e) {
if (e)
return l ? e : e[0];
}
let T$1 = class T extends V {
filteredOptions;
multiple;
isNavigating = false;
selectedValues = [];
focusedValue;
#e = 0;
#s = "";
#t;
#i;
#n;
get cursor() {
return this.#e;
}
get userInputWithCursor() {
if (!this.userInput)
return styleText(["inverse", "hidden"], "_");
if (this._cursor >= this.userInput.length)
return `${this.userInput}\u2588`;
const e = this.userInput.slice(0, this._cursor), [t, ...i] = this.userInput.slice(this._cursor);
return `${e}${styleText("inverse", t)}${i.join("")}`;
}
get options() {
return typeof this.#i == "function" ? this.#i() : this.#i;
}
constructor(e) {
super(e), this.#i = e.options, this.#n = e.placeholder;
const t = this.options;
this.filteredOptions = [...t], this.multiple = e.multiple === true, this.#t = typeof e.options == "function" ? e.filter : e.filter ?? g;
let i;
if (e.initialValue && Array.isArray(e.initialValue) ? this.multiple ? i = e.initialValue : i = e.initialValue.slice(0, 1) : !this.multiple && this.options.length > 0 && (i = [this.options[0].value]), i)
for (const s of i) {
const n = t.findIndex((o) => o.value === s);
n !== -1 && (this.toggleSelected(s), this.#e = n);
}
this.focusedValue = this.options[this.#e]?.value, this.on("key", (s, n) => this.#l(s, n)), this.on("userInput", (s) => this.#u(s));
}
_isActionKey(e, t) {
return e === " " || this.multiple && this.isNavigating && t.name === "space" && e !== void 0 && e !== "";
}
#l(e, t) {
const i = t.name === "up", s = t.name === "down", n = t.name === "return", o = this.userInput === "" || this.userInput === " ", u = this.#n, h = this.options, f = u !== void 0 && u !== "" && h.some(
(r) => !r.disabled && (this.#t ? this.#t(u, r) : true)
);
if (t.name === "tab" && o && f) {
this.userInput === " " && this._clearUserInput(), this._setUserInput(u, true), this.isNavigating = false;
return;
}
i || s ? (this.#e = findCursor(this.#e, i ? -1 : 1, this.filteredOptions), this.focusedValue = this.filteredOptions[this.#e]?.value, this.multiple || (this.selectedValues = [this.focusedValue]), this.isNavigating = true) : n ? this.value = m(this.multiple, this.selectedValues) : this.multiple ? this.focusedValue !== void 0 && (t.name === "tab" || this.isNavigating && t.name === "space") ? this.toggleSelected(this.focusedValue) : this.isNavigating = false : (this.focusedValue && (this.selectedValues = [this.focusedValue]), this.isNavigating = false);
}
deselectAll() {
this.selectedValues = [];
}
toggleSelected(e) {
this.filteredOptions.length !== 0 && (this.multiple ? this.selectedValues.includes(e) ? this.selectedValues = this.selectedValues.filter((t) => t !== e) : this.selectedValues = [...this.selectedValues, e] : this.selectedValues = [e]);
}
#u(e) {
if (e !== this.#s) {
this.#s = e;
const t = this.options;
e && this.#t ? this.filteredOptions = t.filter((n) => this.#t?.(e, n)) : this.filteredOptions = [...t];
const i = p$1(this.focusedValue, this.filteredOptions);
this.#e = findCursor(i, 0, this.filteredOptions);
const s = this.filteredOptions[this.#e];
s && !s.disabled ? this.focusedValue = s.value : this.focusedValue = void 0, this.multiple || (this.focusedValue !== void 0 ? this.toggleSelected(this.focusedValue) : this.deselectAll());
}
}
};
class r extends V {
get cursor() {
return this.value ? 0 : 1;
}
get _value() {
return this.cursor === 0;
}
constructor(t) {
super(t, false), this.value = !!t.initialValue, this.on("userInput", () => {
this.value = this._value;
}), this.on("confirm", (i) => {
this.output.write(cursor.move(0, -1)), this.value = i, this.state = "submit", this.close();
}), this.on("cursor", () => {
this.value = !this.value;
});
}
}
const _ = {
Y: { type: "year", len: 4 },
M: { type: "month", len: 2 },
D: { type: "day", len: 2 }
};
function M(r) {
return [...r].map((t) => _[t]);
}
function P(r) {
const i = new Intl.DateTimeFormat(r, {
year: "numeric",
month: "2-digit",
day: "2-digit"
}).formatToParts(new Date(2e3, 0, 15)), s = [];
let n = "/";
for (const e of i)
e.type === "literal" ? n = e.value.trim() || e.value : (e.type === "year" || e.type === "month" || e.type === "day") && s.push({ type: e.type, len: e.type === "year" ? 4 : 2 });
return { segments: s, separator: n };
}
function p(r) {
return Number.parseInt((r || "0").replace(/_/g, "0"), 10) || 0;
}
function f(r) {
return {
year: p(r.year),
month: p(r.month),
day: p(r.day)
};
}
function c(r, t) {
return new Date(r || 2001, t || 1, 0).getDate();
}
function b(r) {
const { year: t, month: i, day: s } = f(r);
if (!t || t < 0 || t > 9999 || !i || i < 1 || i > 12 || !s || s < 1) return;
const n = new Date(Date.UTC(t, i - 1, s));
if (!(n.getUTCFullYear() !== t || n.getUTCMonth() !== i - 1 || n.getUTCDate() !== s))
return { year: t, month: i, day: s };
}
function C(r) {
const t = b(r);
return t ? new Date(Date.UTC(t.year, t.month - 1, t.day)) : void 0;
}
function T(r, t, i, s) {
const n = i ? {
year: i.getUTCFullYear(),
month: i.getUTCMonth() + 1,
day: i.getUTCDate()
} : null, e = s ? {
year: s.getUTCFullYear(),
month: s.getUTCMonth() + 1,
day: s.getUTCDate()
} : null;
return r === "year" ? { min: n?.year ?? 1, max: e?.year ?? 9999 } : r === "month" ? {
min: n && t.year === n.year ? n.month : 1,
max: e && t.year === e.year ? e.month : 12
} : {
min: n && t.year === n.year && t.month === n.month ? n.day : 1,
max: e && t.year === e.year && t.month === e.month ? e.day : c(t.year, t.month)
};
}
class U extends V {
#i;
#o;
#t;
#h;
#u;
#e = { segmentIndex: 0, positionInSegment: 0 };
#n = true;
#s = null;
inlineError = "";
get segmentCursor() {
return { ...this.#e };
}
get segmentValues() {
return { ...this.#t };
}
get segments() {
return this.#i;
}
get separator() {
return this.#o;
}
get formattedValue() {
return this.#l(this.#t);
}
#l(t) {
return this.#i.map((i) => t[i.type]).join(this.#o);
}
#r() {
this._setUserInput(this.#l(this.#t)), this._setValue(C(this.#t) ?? void 0);
}
constructor(t) {
const i = t.format ? { segments: M(t.format), separator: t.separator ?? "/" } : P(t.locale), s = t.separator ?? i.separator, n = t.format ? M(t.format) : i.segments, e = t.initialValue ?? t.defaultValue, m = e ? {
year: String(e.getUTCFullYear()).padStart(4, "0"),
month: String(e.getUTCMonth() + 1).padStart(2, "0"),
day: String(e.getUTCDate()).padStart(2, "0")
} : { year: "____", month: "__", day: "__" }, o = n.map((a) => m[a.type]).join(s);
super({ ...t, initialUserInput: o }, false), this.#i = n, this.#o = s, this.#t = m, this.#h = t.minDate, this.#u = t.maxDate, this.#r(), this.on("cursor", (a) => this.#f(a)), this.on("key", (a, u) => this.#y(a, u)), this.on("finalize", () => this.#p(t));
}
#a() {
const t = Math.max(0, Math.min(this.#e.segmentIndex, this.#i.length - 1)), i = this.#i[t];
if (i)
return this.#e.positionInSegment = Math.max(
0,
Math.min(this.#e.positionInSegment, i.len - 1)
), { segment: i, index: t };
}
#m(t) {
this.inlineError = "", this.#s = null;
const i = this.#a();
i && (this.#e.segmentIndex = Math.max(
0,
Math.min(this.#i.length - 1, i.index + t)
), this.#e.positionInSegment = 0, this.#n = true);
}
#d(t) {
const i = this.#a();
if (!i) return;
const { segment: s } = i, n = this.#t[s.type], e = !n || n.replace(/_/g, "") === "", m = Number.parseInt((n || "0").replace(/_/g, "0"), 10) || 0, o = T(
s.type,
f(this.#t),
this.#h,
this.#u
);
let a;
e ? a = t === 1 ? o.min : o.max : a = Math.max(Math.min(o.max, m + t), o.min), this.#t = {
...this.#t,
[s.type]: a.toString().padStart(s.len, "0")
}, this.#n = true, this.#s = null, this.#r();
}
#f(t) {
if (t)
switch (t) {
case "right":
return this.#m(1);
case "left":
return this.#m(-1);
case "up":
return this.#d(1);
case "down":
return this.#d(-1);
}
}
#y(t, i) {
if (i?.name === "backspace" || i?.sequence === "\x7F" || i?.sequence === "\b" || t === "\x7F" || t === "\b") {
this.inlineError = "";
const n = this.#a();
if (!n) return;
if (!this.#t[n.segment.type].replace(/_/g, "")) {
this.#m(-1);
return;
}
this.#t[n.segment.type] = "_".repeat(n.segment.len), this.#n = true, this.#e.positionInSegment = 0, this.#r();
return;
}
if (i?.name === "tab") {
this.inlineError = "";
const n = this.#a();
if (!n) return;
const e = i.shift ? -1 : 1, m = n.index + e;
m >= 0 && m < this.#i.length && (this.#e.segmentIndex = m, this.#e.positionInSegment = 0, this.#n = true);
return;
}
if (t && /^[0-9]$/.test(t)) {
const n = this.#a();
if (!n) return;
const { segment: e } = n, m = !this.#t[e.type].replace(/_/g, "");
if (this.#n && this.#s !== null && !m) {
const h = this.#s + t, d = { ...this.#t, [e.type]: h }, g = this.#g(d, e);
if (g) {
this.inlineError = g, this.#s = null, this.#n = false;
return;
}
this.inlineError = "", this.#t[e.type] = h, this.#s = null, this.#n = false, this.#r(), n.index < this.#i.length - 1 && (this.#e.segmentIndex = n.index + 1, this.#e.positionInSegment = 0, this.#n = true);
return;
}
this.#n && !m && (this.#t[e.type] = "_".repeat(e.len), this.#e.positionInSegment = 0), this.#n = false, this.#s = null;
const o = this.#t[e.type], a = o.indexOf("_"), u = a >= 0 ? a : Math.min(this.#e.positionInSegment, e.len - 1);
if (u < 0 || u >= e.len) return;
let l = o.slice(0, u) + t + o.slice(u + 1), D = false;
if (u === 0 && o === "__" && (e.type === "month" || e.type === "day")) {
const h = Number.parseInt(t, 10);
l = `0${t}`, D = h <= (e.type === "month" ? 1 : 2);
}
if (e.type === "year" && (l = (o.replace(/_/g, "") + t).padStart(e.len, "_")), !l.includes("_")) {
const h = { ...this.#t, [e.type]: l }, d = this.#g(h, e);
if (d) {
this.inlineError = d;
return;
}
}
this.inlineError = "", this.#t[e.type] = l;
const y = l.includes("_") ? void 0 : b(this.#t);
if (y) {
const { year: h, month: d } = y, g = c(h, d);
this.#t = {
year: String(Math.max(0, Math.min(9999, h))).padStart(4, "0"),
month: String(Math.max(1, Math.min(12, d))).padStart(2, "0"),
day: String(Math.max(1, Math.min(g, y.day))).padStart(2, "0")
};
}
this.#r();
const S = l.indexOf("_");
D ? (this.#n = true, this.#s = t) : S >= 0 ? this.#e.positionInSegment = S : a >= 0 && n.index < this.#i.length - 1 ? (this.#e.segmentIndex = n.index + 1, this.#e.positionInSegment = 0, this.#n = true) : this.#e.positionInSegment = Math.min(u + 1, e.len - 1);
}
}
#g(t, i) {
const { month: s, day: n } = f(t);
if (i.type === "month" && (s < 0 || s > 12))
return settings.date.messages.invalidMonth;
if (i.type === "day" && (n < 0 || n > 31))
return settings.date.messages.invalidDay(31, "any month");
}
#p(t) {
const { year: i, month: s, day: n } = f(this.#t);
if (i && s && n) {
const e = c(i, s);
this.#t = {
...this.#t,
day: String(Math.min(n, e)).padStart(2, "0")
};
}
this.value = C(this.#t) ?? t.defaultValue ?? void 0;
}
}
let u$1 = class u extends V {
options;
cursor = 0;
#t;
getGroupItems(t) {
return this.options.filter((r) => r.group === t);
}
isGroupSelected(t) {
const r = this.getGroupItems(t), e = this.value;
return e === void 0 ? false : r.every((s) => e.includes(s.value));
}
toggleValue() {
const t = this.options[this.cursor];
if (this.value === void 0 && (this.value = []), t.group === true) {
const r = t.value, e = this.getGroupItems(r);
this.isGroupSelected(r) ? this.value = this.value.filter(
(s) => e.findIndex((i) => i.value === s) === -1
) : this.value = [...this.value, ...e.map((s) => s.value)], this.value = Array.from(new Set(this.value));
} else {
const r = this.value.includes(t.value);
this.value = r ? this.value.filter((e) => e !== t.value) : [...this.value, t.value];
}
}
constructor(t) {
super(t, false);
const { options: r } = t;
this.#t = t.selectableGroups !== false, this.options = Object.entries(r).flatMap(([e, s]) => [
{ value: e, group: true, label: e },
...s.map((i) => ({ ...i, group: e }))
]), this.value = [...t.initialValues ?? []], this.cursor = Math.max(
this.options.findIndex(({ value: e }) => e === t.cursorAt),
this.#t ? 0 : 1
), this.on("cursor", (e) => {
switch (e) {
case "left":
case "up": {
this.cursor = this.cursor === 0 ? this.options.length - 1 : this.cursor - 1;
const s = this.options[this.cursor]?.group === true;
!this.#t && s && (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;
const s = this.options[this.cursor]?.group === true;
!this.#t && s && (this.cursor = this.cursor === this.options.length - 1 ? 0 : this.cursor + 1);
break;
}
case "space":
this.toggleValue();
break;
}
});
}
};
const o$1 = /* @__PURE__ */ new Set(["up", "down", "left", "right"]);
class h extends V {
#s = false;
#t;
focused = "editor";
get userInputWithCursor() {
if (this.state === "submit")
return this.userInput;
const t = this.userInput;
if (this.cursor >= t.length)
return `${t}\u2588`;
const s = t.slice(0, this.cursor), r = t[this.cursor], e = t.slice(this.cursor + 1);
return r === `
` ? `${s}\u2588
${e}` : `${s}${styleText("inverse", r)}${e}`;
}
get cursor() {
return this._cursor;
}
#r(t) {
if (this.userInput.length === 0) {
this._setUserInput(t);
return;
}
this._setUserInput(
this.userInput.slice(0, this.cursor) + t + this.userInput.slice(this.cursor)
);
}
#i(t) {
const s = this.value ?? "";
switch (t) {
case "up":
this._cursor = findTextCursor(this._cursor, 0, -1, s);
return;
case "down":
this._cursor = findTextCursor(this._cursor, 0, 1, s);
return;
case "left":
this._cursor = findTextCursor(this._cursor, -1, 0, s);
return;
case "right":
this._cursor = findTextCursor(this._cursor, 1, 0, s);
return;
}
}
_shouldSubmit(t, s) {
if (this.#t)
return this.focused === "submit" ? true : (this.#r(`
`), this._cursor++, false);
const r = this.#s;
return this.#s = true, r ? (this.userInput[this.cursor - 1] === `
` && (this._setUserInput(
this.userInput.slice(0, this.cursor - 1) + this.userInput.slice(this.cursor)
), this._cursor--), true) : (this.#r(`
`), this._cursor++, false);
}
constructor(t) {
super(t, false), this.#t = t.showSubmit ?? false, this.on("key", (s, r) => {
if (r?.name && o$1.has(r.name)) {
this.#i(r.name);
return;
}
if (s === " " && this.#t) {
this.focused = this.focused === "editor" ? "submit" : "editor";
return;
}
if (r?.name !== "return") {
if (this.#s = false, r?.name === "backspace" && this.cursor > 0) {
this._setUserInput(
this.userInput.slice(0, this.cursor - 1) + this.userInput.slice(this.cursor)
), this._cursor--;
return;
}
if (r?.name === "delete" && this.cursor < this.userInput.length) {
this._setUserInput(
this.userInput.slice(0, this.cursor) + this.userInput.slice(this.cursor + 1)
);
return;
}
s && (this.#t && this.focused === "submit" && (this.focused = "editor"), this.#r(s ?? ""), this._cursor++);
}
}), this.on("userInput", (s) => {
this._setValue(s);
}), this.on("finalize", () => {
this.value || (this.value = t.defaultValue), this.value === void 0 && (this.value = "");
});
}
}
let a$1 = class a extends V {
options;
cursor = 0;
get _value() {
return this.options[this.cursor].value;
}
get _enabledOptions() {
return this.options.filter((e) => e.disabled !== true);
}
toggleAll() {
const e = this._enabledOptions, i = this.value !== void 0 && this.value.length === e.length;
this.value = i ? [] : e.map((t) => t.value);
}
toggleInvert() {
const e = this.value;
if (!e)
return;
const i = this._enabledOptions.filter((t) => !e.includes(t.value));
this.value = i.map((t) => t.value);
}
toggleValue() {
this.value === void 0 && (this.value = []);
const e = this.value.includes(this._value);
this.value = e ? this.value.filter((i) => i !== this._value) : [...this.value, this._value];
}
constructor(e) {
super(e, false), this.options = e.options, this.value = [...e.initialValues ?? []];
const i = Math.max(
this.options.findIndex(({ value: t }) => t === e.cursorAt),
0
);
this.cursor = this.options[i].disabled ? findCursor(i, 1, this.options) : i, this.on("key", (t, l) => {
l.name === "a" && this.toggleAll(), l.name === "i" && this.toggleInvert();
}), this.on("cursor", (t) => {
switch (t) {
case "left":
case "up":
this.cursor = findCursor(this.cursor, -1, this.options);
break;
case "down":
case "right":
this.cursor = findCursor(this.cursor, 1, this.options);
break;
case "space":
this.toggleValue();
break;
}
});
}
};
class o extends V {
_mask = "\u2022";
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;
const t = this.userInput;
if (this.cursor >= t.length)
return `${this.masked}${styleText(["inverse", "hidden"], "_")}`;
const s = this.masked, r = s.slice(0, this.cursor), e = s.slice(this.cursor);
return `${r}${styleText("inverse", e[0])}${e.slice(1)}`;
}
clear() {
this._clearUserInput();
}
constructor({ mask: t, ...s }) {
super(s), this._mask = t ?? "\u2022", this.on("userInput", (r) => {
this._setValue(r);
});
}
}
class a extends V {
options;
cursor = 0;
get _selectedValue() {
return this.options[this.cursor];
}
changeValue() {
this.value = this._selectedValue.value;
}
constructor(t) {
super(t, false), this.options = t.options;
const i = this.options.findIndex(({ value: s }) => s === t.initialValue), e = i === -1 ? 0 : i;
this.cursor = this.options[e].disabled ? findCursor(e, 1, this.options) : e, this.changeValue(), this.on("cursor", (s) => {
switch (s) {
case "left":
case "up":
this.cursor = findCursor(this.cursor, -1, this.options);
break;
case "down":
case "right":
this.cursor = findCursor(this.cursor, 1, this.options);
break;
}
this.changeValue();
});
}
}
class u extends V {
options;
cursor = 0;
constructor(t) {
super(t, false), this.options = t.options;
const s = t.caseSensitive === true, i = this.options.map(({ value: [e] }) => s ? e : e?.toLowerCase());
this.cursor = Math.max(i.indexOf(t.initialValue), 0), this.on("key", (e) => {
if (!e)
return;
const o = s ? e : e.toLowerCase();
if (!i.includes(o))
return;
const n = this.options.find(({ value: [r] }) => s ? r === o : r?.toLowerCase() === o);
n && (this.value = n.value, this.state = "submit", this.emit("submit"));
});
}
}
class n extends V {
get userInputWithCursor() {
if (this.state === "submit")
return this.userInput;
const t = this.userInput;
if (this.cursor >= t.length)
return `${this.userInput}\u2588`;
const e = t.slice(0, this.cursor), [s, ...r] = t.slice(this.cursor);
return `${e}${styleText("inverse", s)}${r.join("")}`;
}
get cursor() {
return this._cursor;
}
constructor(t) {
super({
...t,
initialUserInput: t.initialUserInput ?? t.initialValue
}), this.on("userInput", (e) => {
this._setValue(e);
}), this.on("finalize", () => {
this.value || (this.value = t.defaultValue), this.value === void 0 && (this.value = "");
});
}
}
export { T$1 as AutocompletePrompt, r as ConfirmPrompt, U as DatePrompt, u$1 as GroupMultiSelectPrompt, h as MultiLinePrompt, a$1 as MultiSelectPrompt, o as PasswordPrompt, V as Prompt, u as SelectKeyPrompt, a as SelectPrompt, n as TextPrompt, block, getColumns, getRows, isCancel, runValidation, settings, updateSettings, wrapTextWithPrefix };
+1
-1
{
"name": "@clack/core",
"version": "1.4.0",
"version": "1.4.1",
"type": "module",

@@ -5,0 +5,0 @@ "main": "./dist/index.mjs",

Sorry, the diff of this file is too big to display