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.1.0
to
1.2.0
+12
-0
CHANGELOG.md
# @clack/core
## 1.2.0
### Minor Changes
- 9786226: Externalize `fast-string-width` and `fast-wrap-ansi` to avoid double dependencies
- 090902c: Adds `date` prompt with `format` support (YMD, MDY, DMY)
### Patch Changes
- bdf89a5: Adds `placeholder` option to `autocomplete`. When the placeholder is set and the input is empty, pressing `tab` will set the value to `placeholder`.
- 417b451: Only apply autocomplete default filter if it has been explicitly set or if options is not a getter.
## 1.1.0

@@ -4,0 +16,0 @@

+71
-2

@@ -15,2 +15,12 @@ import { Key } from 'node:readline';

withGuide: boolean;
date: {
monthNames: string[];
messages: {
invalidMonth: string;
required: string;
invalidDay: (days: number, month: string) => string;
afterMin: (min: Date) => string;
beforeMax: (max: Date) => string;
};
};
}

@@ -43,2 +53,21 @@ declare const settings: InternalClackSettings;

withGuide?: boolean;
/**
* Date prompt localization
*/
date?: {
/** Month names for validation messages (January, February, ...) */
monthNames?: string[];
messages?: {
/** Shown when date is missing */
required?: string;
/** Shown when month > 12 */
invalidMonth?: string;
/** (days, monthName) => message for invalid day */
invalidDay?: (days: number, month: string) => string;
/** (min) => message when date is before minDate */
afterMin?: (min: Date) => string;
/** (max) => message when date is after maxDate */
beforeMax?: (max: Date) => string;
};
};
}

@@ -143,2 +172,9 @@ declare function updateSettings(updates: ClackSettings): void;

multiple?: boolean;
/**
* When set (non-empty), pressing Tab with no input fills the field with this value
* and runs the normal filter/selection logic so the user can confirm with Enter.
* Tab only fills the input when the placeholder matches at least one option under
* the prompt's filter (so the value remains selectable).
*/
placeholder?: string;
}

@@ -172,2 +208,35 @@ declare class AutocompletePrompt<T extends OptionLike$1> extends Prompt<T['value'] | T['value'][]> {

interface SegmentConfig {
type: 'year' | 'month' | 'day';
len: number;
}
interface DateParts {
year: string;
month: string;
day: string;
}
type DateFormat = 'YMD' | 'MDY' | 'DMY';
interface DateOptions extends PromptOptions<Date, DatePrompt> {
format?: DateFormat;
locale?: string;
separator?: string;
defaultValue?: Date;
initialValue?: Date;
minDate?: Date;
maxDate?: Date;
}
declare class DatePrompt extends Prompt<Date> {
#private;
inlineError: string;
get segmentCursor(): {
segmentIndex: number;
positionInSegment: number;
};
get segmentValues(): DateParts;
get segments(): readonly SegmentConfig[];
get separator(): string;
get formattedValue(): string;
constructor(opts: DateOptions);
}
interface GroupMultiSelectOptions<T extends {

@@ -283,3 +352,3 @@ value: any;

export { AutocompletePrompt, ConfirmPrompt, GroupMultiSelectPrompt, MultiSelectPrompt, PasswordPrompt, Prompt, SelectKeyPrompt, SelectPrompt, TextPrompt, block, getColumns, getRows, isCancel, settings, updateSettings, wrapTextWithPrefix };
export type { AutocompleteOptions, ClackSettings, ConfirmOptions, GroupMultiSelectOptions, MultiSelectOptions, PasswordOptions, PromptOptions, SelectKeyOptions, SelectOptions, ClackState as State, TextOptions };
export { AutocompletePrompt, ConfirmPrompt, DatePrompt, GroupMultiSelectPrompt, MultiSelectPrompt, PasswordPrompt, Prompt, SelectKeyPrompt, SelectPrompt, TextPrompt, block, getColumns, getRows, isCancel, settings, updateSettings, wrapTextWithPrefix };
export type { AutocompleteOptions, ClackSettings, ConfirmOptions, DateFormat, DateOptions, DateParts, GroupMultiSelectOptions, MultiSelectOptions, PasswordOptions, PromptOptions, SelectKeyOptions, SelectOptions, ClackState as State, TextOptions };
+10
-17

@@ -1,18 +0,11 @@

import{styleText as D}from"node:util";import{stdout as R,stdin as q}from"node:process";import*as k from"node:readline";import ot from"node:readline";import{cursor as I,erase as N}from"sisteransi";import{ReadStream as J}from"node:tty";function x(t,e,s){if(!s.some(u=>!u.disabled))return t;const i=t+e,r=Math.max(s.length-1,0),n=i<0?r:i>r?0:i;return s[n].disabled?x(n,e<0?-1:1,s):n}const at=t=>t===161||t===164||t===167||t===168||t===170||t===173||t===174||t>=176&&t<=180||t>=182&&t<=186||t>=188&&t<=191||t===198||t===208||t===215||t===216||t>=222&&t<=225||t===230||t>=232&&t<=234||t===236||t===237||t===240||t===242||t===243||t>=247&&t<=250||t===252||t===254||t===257||t===273||t===275||t===283||t===294||t===295||t===299||t>=305&&t<=307||t===312||t>=319&&t<=322||t===324||t>=328&&t<=331||t===333||t===338||t===339||t===358||t===359||t===363||t===462||t===464||t===466||t===468||t===470||t===472||t===474||t===476||t===593||t===609||t===708||t===711||t>=713&&t<=715||t===717||t===720||t>=728&&t<=731||t===733||t===735||t>=768&&t<=879||t>=913&&t<=929||t>=931&&t<=937||t>=945&&t<=961||t>=963&&t<=969||t===1025||t>=1040&&t<=1103||t===1105||t===8208||t>=8211&&t<=8214||t===8216||t===8217||t===8220||t===8221||t>=8224&&t<=8226||t>=8228&&t<=8231||t===8240||t===8242||t===8243||t===8245||t===8251||t===8254||t===8308||t===8319||t>=8321&&t<=8324||t===8364||t===8451||t===8453||t===8457||t===8467||t===8470||t===8481||t===8482||t===8486||t===8491||t===8531||t===8532||t>=8539&&t<=8542||t>=8544&&t<=8555||t>=8560&&t<=8569||t===8585||t>=8592&&t<=8601||t===8632||t===8633||t===8658||t===8660||t===8679||t===8704||t===8706||t===8707||t===8711||t===8712||t===8715||t===8719||t===8721||t===8725||t===8730||t>=8733&&t<=8736||t===8739||t===8741||t>=8743&&t<=8748||t===8750||t>=8756&&t<=8759||t===8764||t===8765||t===8776||t===8780||t===8786||t===8800||t===8801||t>=8804&&t<=8807||t===8810||t===8811||t===8814||t===8815||t===8834||t===8835||t===8838||t===8839||t===8853||t===8857||t===8869||t===8895||t===8978||t>=9312&&t<=9449||t>=9451&&t<=9547||t>=9552&&t<=9587||t>=9600&&t<=9615||t>=9618&&t<=9621||t===9632||t===9633||t>=9635&&t<=9641||t===9650||t===9651||t===9654||t===9655||t===9660||t===9661||t===9664||t===9665||t>=9670&&t<=9672||t===9675||t>=9678&&t<=9681||t>=9698&&t<=9701||t===9711||t===9733||t===9734||t===9737||t===9742||t===9743||t===9756||t===9758||t===9792||t===9794||t===9824||t===9825||t>=9827&&t<=9829||t>=9831&&t<=9834||t===9836||t===9837||t===9839||t===9886||t===9887||t===9919||t>=9926&&t<=9933||t>=9935&&t<=9939||t>=9941&&t<=9953||t===9955||t===9960||t===9961||t>=9963&&t<=9969||t===9972||t>=9974&&t<=9977||t===9979||t===9980||t===9982||t===9983||t===10045||t>=10102&&t<=10111||t>=11094&&t<=11097||t>=12872&&t<=12879||t>=57344&&t<=63743||t>=65024&&t<=65039||t===65533||t>=127232&&t<=127242||t>=127248&&t<=127277||t>=127280&&t<=127337||t>=127344&&t<=127373||t===127375||t===127376||t>=127387&&t<=127404||t>=917760&&t<=917999||t>=983040&&t<=1048573||t>=1048576&&t<=1114109,lt=t=>t===12288||t>=65281&&t<=65376||t>=65504&&t<=65510,ht=t=>t>=4352&&t<=4447||t===8986||t===8987||t===9001||t===9002||t>=9193&&t<=9196||t===9200||t===9203||t===9725||t===9726||t===9748||t===9749||t>=9800&&t<=9811||t===9855||t===9875||t===9889||t===9898||t===9899||t===9917||t===9918||t===9924||t===9925||t===9934||t===9940||t===9962||t===9970||t===9971||t===9973||t===9978||t===9981||t===9989||t===9994||t===9995||t===10024||t===10060||t===10062||t>=10067&&t<=10069||t===10071||t>=10133&&t<=10135||t===10160||t===10175||t===11035||t===11036||t===11088||t===11093||t>=11904&&t<=11929||t>=11931&&t<=12019||t>=12032&&t<=12245||t>=12272&&t<=12287||t>=12289&&t<=12350||t>=12353&&t<=12438||t>=12441&&t<=12543||t>=12549&&t<=12591||t>=12593&&t<=12686||t>=12688&&t<=12771||t>=12783&&t<=12830||t>=12832&&t<=12871||t>=12880&&t<=19903||t>=19968&&t<=42124||t>=42128&&t<=42182||t>=43360&&t<=43388||t>=44032&&t<=55203||t>=63744&&t<=64255||t>=65040&&t<=65049||t>=65072&&t<=65106||t>=65108&&t<=65126||t>=65128&&t<=65131||t>=94176&&t<=94180||t===94192||t===94193||t>=94208&&t<=100343||t>=100352&&t<=101589||t>=101632&&t<=101640||t>=110576&&t<=110579||t>=110581&&t<=110587||t===110589||t===110590||t>=110592&&t<=110882||t===110898||t>=110928&&t<=110930||t===110933||t>=110948&&t<=110951||t>=110960&&t<=111355||t===126980||t===127183||t===127374||t>=127377&&t<=127386||t>=127488&&t<=127490||t>=127504&&t<=127547||t>=127552&&t<=127560||t===127568||t===127569||t>=127584&&t<=127589||t>=127744&&t<=127776||t>=127789&&t<=127797||t>=127799&&t<=127868||t>=127870&&t<=127891||t>=127904&&t<=127946||t>=127951&&t<=127955||t>=127968&&t<=127984||t===127988||t>=127992&&t<=128062||t===128064||t>=128066&&t<=128252||t>=128255&&t<=128317||t>=128331&&t<=128334||t>=128336&&t<=128359||t===128378||t===128405||t===128406||t===128420||t>=128507&&t<=128591||t>=128640&&t<=128709||t===128716||t>=128720&&t<=128722||t>=128725&&t<=128727||t>=128732&&t<=128735||t===128747||t===128748||t>=128756&&t<=128764||t>=128992&&t<=129003||t===129008||t>=129292&&t<=129338||t>=129340&&t<=129349||t>=129351&&t<=129535||t>=129648&&t<=129660||t>=129664&&t<=129672||t>=129680&&t<=129725||t>=129727&&t<=129733||t>=129742&&t<=129755||t>=129760&&t<=129768||t>=129776&&t<=129784||t>=131072&&t<=196605||t>=196608&&t<=262141,O=/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/y,y=/[\x00-\x08\x0A-\x1F\x7F-\x9F]{1,1000}/y,L=/\t{1,1000}/y,P=/[\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?))*/yu,M=/(?:[\x20-\x7E\xA0-\xFF](?!\uFE0F)){1,1000}/y,ct=/\p{M}+/gu,ft={limit:1/0,ellipsis:""},X=(t,e={},s={})=>{const i=e.limit??1/0,r=e.ellipsis??"",n=e?.ellipsisWidth??(r?X(r,ft,s).width:0),u=s.ansiWidth??0,a=s.controlWidth??0,l=s.tabWidth??8,E=s.ambiguousWidth??1,g=s.emojiWidth??2,m=s.fullWidthWidth??2,A=s.regularWidth??1,V=s.wideWidth??2;let h=0,o=0,p=t.length,v=0,F=!1,d=p,b=Math.max(0,i-n),C=0,w=0,c=0,f=0;t:for(;;){if(w>C||o>=p&&o>h){const ut=t.slice(C,w)||t.slice(h,o);v=0;for(const Y of ut.replaceAll(ct,"")){const $=Y.codePointAt(0)||0;if(lt($)?f=m:ht($)?f=V:E!==A&&at($)?f=E:f=A,c+f>b&&(d=Math.min(d,Math.max(C,h)+v)),c+f>i){F=!0;break t}v+=Y.length,c+=f}C=w=0}if(o>=p)break;if(M.lastIndex=o,M.test(t)){if(v=M.lastIndex-o,f=v*A,c+f>b&&(d=Math.min(d,o+Math.floor((b-c)/A))),c+f>i){F=!0;break}c+=f,C=h,w=o,o=h=M.lastIndex;continue}if(O.lastIndex=o,O.test(t)){if(c+u>b&&(d=Math.min(d,o)),c+u>i){F=!0;break}c+=u,C=h,w=o,o=h=O.lastIndex;continue}if(y.lastIndex=o,y.test(t)){if(v=y.lastIndex-o,f=v*a,c+f>b&&(d=Math.min(d,o+Math.floor((b-c)/a))),c+f>i){F=!0;break}c+=f,C=h,w=o,o=h=y.lastIndex;continue}if(L.lastIndex=o,L.test(t)){if(v=L.lastIndex-o,f=v*l,c+f>b&&(d=Math.min(d,o+Math.floor((b-c)/l))),c+f>i){F=!0;break}c+=f,C=h,w=o,o=h=L.lastIndex;continue}if(P.lastIndex=o,P.test(t)){if(c+g>b&&(d=Math.min(d,o)),c+g>i){F=!0;break}c+=g,C=h,w=o,o=h=P.lastIndex;continue}o+=1}return{width:F?b:c,index:F?d:p,truncated:F,ellipsed:F&&i>=n}},pt={limit:1/0,ellipsis:"",ellipsisWidth:0},S=(t,e={})=>X(t,pt,e).width,T="\x1B",Z="\x9B",Ft=39,j="\x07",Q="[",dt="]",tt="m",U=`${dt}8;;`,et=new RegExp(`(?:\\${Q}(?<code>\\d+)m|\\${U}(?<uri>.*)${j})`,"y"),mt=t=>{if(t>=30&&t<=37||t>=90&&t<=97)return 39;if(t>=40&&t<=47||t>=100&&t<=107)return 49;if(t===1||t===2)return 22;if(t===3)return 23;if(t===4)return 24;if(t===7)return 27;if(t===8)return 28;if(t===9)return 29;if(t===0)return 0},st=t=>`${T}${Q}${t}${tt}`,it=t=>`${T}${U}${t}${j}`,gt=t=>t.map(e=>S(e)),G=(t,e,s)=>{const i=e[Symbol.iterator]();let r=!1,n=!1,u=t.at(-1),a=u===void 0?0:S(u),l=i.next(),E=i.next(),g=0;for(;!l.done;){const m=l.value,A=S(m);a+A<=s?t[t.length-1]+=m:(t.push(m),a=0),(m===T||m===Z)&&(r=!0,n=e.startsWith(U,g+1)),r?n?m===j&&(r=!1,n=!1):m===tt&&(r=!1):(a+=A,a===s&&!E.done&&(t.push(""),a=0)),l=E,E=i.next(),g+=m.length}u=t.at(-1),!a&&u!==void 0&&u.length>0&&t.length>1&&(t[t.length-2]+=t.pop())},vt=t=>{const e=t.split(" ");let s=e.length;for(;s>0&&!(S(e[s-1])>0);)s--;return s===e.length?t:e.slice(0,s).join(" ")+e.slice(s).join("")},Et=(t,e,s={})=>{if(s.trim!==!1&&t.trim()==="")return"";let i="",r,n;const u=t.split(" "),a=gt(u);let l=[""];for(const[h,o]of u.entries()){s.trim!==!1&&(l[l.length-1]=(l.at(-1)??"").trimStart());let p=S(l.at(-1)??"");if(h!==0&&(p>=e&&(s.wordWrap===!1||s.trim===!1)&&(l.push(""),p=0),(p>0||s.trim===!1)&&(l[l.length-1]+=" ",p++)),s.hard&&a[h]>e){const v=e-p,F=1+Math.floor((a[h]-v-1)/e);Math.floor((a[h]-1)/e)<F&&l.push(""),G(l,o,e);continue}if(p+a[h]>e&&p>0&&a[h]>0){if(s.wordWrap===!1&&p<e){G(l,o,e);continue}l.push("")}if(p+a[h]>e&&s.wordWrap===!1){G(l,o,e);continue}l[l.length-1]+=o}s.trim!==!1&&(l=l.map(h=>vt(h)));const E=l.join(`
`),g=E[Symbol.iterator]();let m=g.next(),A=g.next(),V=0;for(;!m.done;){const h=m.value,o=A.value;if(i+=h,h===T||h===Z){et.lastIndex=V+1;const F=et.exec(E)?.groups;if(F?.code!==void 0){const d=Number.parseFloat(F.code);r=d===Ft?void 0:d}else F?.uri!==void 0&&(n=F.uri.length===0?void 0:F.uri)}const p=r?mt(r):void 0;o===`
`?(n&&(i+=it("")),r&&p&&(i+=st(p))):h===`
`&&(r&&p&&(i+=st(r)),n&&(i+=it(n))),V+=h.length,m=A,A=g.next()}return i};function K(t,e,s){return String(t).normalize().replaceAll(`\r
`,`
`).split(`
`).map(i=>Et(i,e,s)).join(`
`)}const At=["up","down","left","right","space","enter","cancel"],_={actions:new Set(At),aliases:new Map([["k","up"],["j","down"],["h","left"],["l","right"],["","cancel"],["escape","cancel"]]),messages:{cancel:"Canceled",error:"Something went wrong"},withGuide:!0};function It(t){if(t.aliases!==void 0){const e=t.aliases;for(const s in e){if(!Object.hasOwn(e,s))continue;const i=e[s];_.actions.has(i)&&(_.aliases.has(s)||_.aliases.set(s,i))}}if(t.messages!==void 0){const e=t.messages;e.cancel!==void 0&&(_.messages.cancel=e.cancel),e.error!==void 0&&(_.messages.error=e.error)}t.withGuide!==void 0&&(_.withGuide=t.withGuide!==!1)}function H(t,e){if(typeof t=="string")return _.aliases.get(t)===e;for(const s of t)if(s!==void 0&&H(s,e))return!0;return!1}function _t(t,e){if(t===e)return;const s=t.split(`
`),i=e.split(`
`),r=Math.max(s.length,i.length),n=[];for(let u=0;u<r;u++)s[u]!==i[u]&&n.push(u);return{lines:n,numLinesBefore:s.length,numLinesAfter:i.length,numLines:r}}const bt=globalThis.process.platform.startsWith("win"),z=Symbol("clack:cancel");function Ct(t){return t===z}function W(t,e){const s=t;s.isTTY&&s.setRawMode(e)}function xt({input:t=q,output:e=R,overwrite:s=!0,hideCursor:i=!0}={}){const r=k.createInterface({input:t,output:e,prompt:"",tabSize:1});k.emitKeypressEvents(t,r),t instanceof J&&t.isTTY&&t.setRawMode(!0);const n=(u,{name:a,sequence:l})=>{const E=String(u);if(H([E,a,l],"cancel")){i&&e.write(I.show),process.exit(0);return}if(!s)return;const g=a==="return"?0:-1,m=a==="return"?-1:0;k.moveCursor(e,g,m,()=>{k.clearLine(e,1,()=>{t.once("keypress",n)})})};return i&&e.write(I.hide),t.once("keypress",n),()=>{t.off("keypress",n),i&&e.write(I.show),t instanceof J&&t.isTTY&&!bt&&t.setRawMode(!1),r.terminal=!1,r.close()}}const rt=t=>"columns"in t&&typeof t.columns=="number"?t.columns:80,nt=t=>"rows"in t&&typeof t.rows=="number"?t.rows:20;function Bt(t,e,s,i=s){const r=rt(t??R);return K(e,r-s.length,{hard:!0,trim:!1}).split(`
`).map((n,u)=>`${u===0?i:s}${n}`).join(`
`)}class B{input;output;_abortSignal;rl;opts;_render;_track=!1;_prevFrame="";_subscribers=new Map;_cursor=0;state="initial";error="";value;userInput="";constructor(e,s=!0){const{input:i=q,output:r=R,render:n,signal:u,...a}=e;this.opts=a,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=u,this.input=i,this.output=r}unsubscribe(){this._subscribers.clear()}setSubscriber(e,s){const i=this._subscribers.get(e)??[];i.push(s),this._subscribers.set(e,i)}on(e,s){this.setSubscriber(e,{cb:s})}once(e,s){this.setSubscriber(e,{cb:s,once:!0})}emit(e,...s){const i=this._subscribers.get(e)??[],r=[];for(const n of i)n.cb(...s),n.once&&r.push(()=>i.splice(i.indexOf(n),1));for(const n of r)n()}prompt(){return new Promise(e=>{if(this._abortSignal){if(this._abortSignal.aborted)return this.state="cancel",this.close(),e(z);this._abortSignal.addEventListener("abort",()=>{this.state="cancel",this.close()},{once:!0})}this.rl=ot.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(I.show),this.output.off("resize",this.render),W(this.input,!1),e(this.value)}),this.once("cancel",()=>{this.output.write(I.show),this.output.off("resize",this.render),W(this.input,!1),e(z)})})}_isActionKey(e,s){return e===" "}_setValue(e){this.value=e,this.emit("value",this.value)}_setUserInput(e,s){this.userInput=e??"",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(e,s){if(this._track&&s.name!=="return"&&(s.name&&this._isActionKey(e,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&&_.aliases.has(s.name)&&this.emit("cursor",_.aliases.get(s.name)),_.actions.has(s.name)&&this.emit("cursor",s.name)),e&&(e.toLowerCase()==="y"||e.toLowerCase()==="n")&&this.emit("confirm",e.toLowerCase()==="y"),this.emit("key",e?.toLowerCase(),s),s?.name==="return"){if(this.opts.validate){const i=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")}H([e,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 e=K(this._prevFrame,process.stdout.columns,{hard:!0,trim:!1}).split(`
`).length-1;this.output.write(I.move(-999,e*-1))}render(){const e=K(this._render(this)??"",process.stdout.columns,{hard:!0,trim:!1});if(e!==this._prevFrame){if(this.state==="initial")this.output.write(I.hide);else{const s=_t(this._prevFrame,e),i=nt(this.output);if(this.restoreCursor(),s){const r=Math.max(0,s.numLinesAfter-i),n=Math.max(0,s.numLinesBefore-i);let u=s.lines.find(a=>a>=r);if(u===void 0){this._prevFrame=e;return}if(s.lines.length===1){this.output.write(I.move(0,u-n)),this.output.write(N.lines(1));const a=e.split(`
`);this.output.write(a[u]),this._prevFrame=e,this.output.write(I.move(0,a.length-u-1));return}else if(s.lines.length>1){if(r<n)u=r;else{const l=u-n;l>0&&this.output.write(I.move(0,l))}this.output.write(N.down());const a=e.split(`
`).slice(u);this.output.write(a.join(`
`)),this._prevFrame=e;return}}this.output.write(N.down())}this.output.write(e),this.state==="initial"&&(this.state="active"),this._prevFrame=e}}}function wt(t,e){if(t===void 0||e.length===0)return 0;const s=e.findIndex(i=>i.value===t);return s!==-1?s:0}function Dt(t,e){return(e.label??String(e.value)).toLowerCase().includes(t.toLowerCase())}function St(t,e){if(e)return t?e:e[0]}class Vt extends B{filteredOptions;multiple;isNavigating=!1;selectedValues=[];focusedValue;#t=0;#s="";#i;#e;get cursor(){return this.#t}get userInputWithCursor(){if(!this.userInput)return D(["inverse","hidden"],"_");if(this._cursor>=this.userInput.length)return`${this.userInput}\u2588`;const e=this.userInput.slice(0,this._cursor),[s,...i]=this.userInput.slice(this._cursor);return`${e}${D("inverse",s)}${i.join("")}`}get options(){return typeof this.#e=="function"?this.#e():this.#e}constructor(e){super(e),this.#e=e.options;const s=this.options;this.filteredOptions=[...s],this.multiple=e.multiple===!0,this.#i=e.filter??Dt;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 r of i){const n=s.findIndex(u=>u.value===r);n!==-1&&(this.toggleSelected(r),this.#t=n)}this.focusedValue=this.options[this.#t]?.value,this.on("key",(r,n)=>this.#r(r,n)),this.on("userInput",r=>this.#n(r))}_isActionKey(e,s){return e===" "||this.multiple&&this.isNavigating&&s.name==="space"&&e!==void 0&&e!==""}#r(e,s){const i=s.name==="up",r=s.name==="down",n=s.name==="return";i||r?(this.#t=x(this.#t,i?-1:1,this.filteredOptions),this.focusedValue=this.filteredOptions[this.#t]?.value,this.multiple||(this.selectedValues=[this.focusedValue]),this.isNavigating=!0):n?this.value=St(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(e){this.filteredOptions.length!==0&&(this.multiple?this.selectedValues.includes(e)?this.selectedValues=this.selectedValues.filter(s=>s!==e):this.selectedValues=[...this.selectedValues,e]:this.selectedValues=[e])}#n(e){if(e!==this.#s){this.#s=e;const s=this.options;e?this.filteredOptions=s.filter(n=>this.#i(e,n)):this.filteredOptions=[...s];const i=wt(this.focusedValue,this.filteredOptions);this.#t=x(i,0,this.filteredOptions);const r=this.filteredOptions[this.#t];r&&!r.disabled?this.focusedValue=r.value:this.focusedValue=void 0,this.multiple||(this.focusedValue!==void 0?this.toggleSelected(this.focusedValue):this.deselectAll())}}}class kt extends B{get cursor(){return this.value?0:1}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",s=>{this.output.write(I.move(0,-1)),this.value=s,this.state="submit",this.close()}),this.on("cursor",()=>{this.value=!this.value})}}class yt extends B{options;cursor=0;#t;getGroupItems(e){return this.options.filter(s=>s.group===e)}isGroupSelected(e){const s=this.getGroupItems(e),i=this.value;return i===void 0?!1:s.every(r=>i.includes(r.value))}toggleValue(){const e=this.options[this.cursor];if(this.value===void 0&&(this.value=[]),e.group===!0){const s=e.value,i=this.getGroupItems(s);this.isGroupSelected(s)?this.value=this.value.filter(r=>i.findIndex(n=>n.value===r)===-1):this.value=[...this.value,...i.map(r=>r.value)],this.value=Array.from(new Set(this.value))}else{const s=this.value.includes(e.value);this.value=s?this.value.filter(i=>i!==e.value):[...this.value,e.value]}}constructor(e){super(e,!1);const{options:s}=e;this.#t=e.selectableGroups!==!1,this.options=Object.entries(s).flatMap(([i,r])=>[{value:i,group:!0,label:i},...r.map(n=>({...n,group:i}))]),this.value=[...e.initialValues??[]],this.cursor=Math.max(this.options.findIndex(({value:i})=>i===e.cursorAt),this.#t?0:1),this.on("cursor",i=>{switch(i){case"left":case"up":{this.cursor=this.cursor===0?this.options.length-1:this.cursor-1;const r=this.options[this.cursor]?.group===!0;!this.#t&&r&&(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 r=this.options[this.cursor]?.group===!0;!this.#t&&r&&(this.cursor=this.cursor===this.options.length-1?0:this.cursor+1);break}case"space":this.toggleValue();break}})}}let Lt=class extends B{options;cursor=0;get _value(){return this.options[this.cursor].value}get _enabledOptions(){return this.options.filter(e=>e.disabled!==!0)}toggleAll(){const e=this._enabledOptions,s=this.value!==void 0&&this.value.length===e.length;this.value=s?[]:e.map(i=>i.value)}toggleInvert(){const e=this.value;if(!e)return;const s=this._enabledOptions.filter(i=>!e.includes(i.value));this.value=s.map(i=>i.value)}toggleValue(){this.value===void 0&&(this.value=[]);const e=this.value.includes(this._value);this.value=e?this.value.filter(s=>s!==this._value):[...this.value,this._value]}constructor(e){super(e,!1),this.options=e.options,this.value=[...e.initialValues??[]];const s=Math.max(this.options.findIndex(({value:i})=>i===e.cursorAt),0);this.cursor=this.options[s].disabled?x(s,1,this.options):s,this.on("key",i=>{i==="a"&&this.toggleAll(),i==="i"&&this.toggleInvert()}),this.on("cursor",i=>{switch(i){case"left":case"up":this.cursor=x(this.cursor,-1,this.options);break;case"down":case"right":this.cursor=x(this.cursor,1,this.options);break;case"space":this.toggleValue();break}})}};class Mt extends B{_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 e=this.userInput;if(this.cursor>=e.length)return`${this.masked}${D(["inverse","hidden"],"_")}`;const s=this.masked,i=s.slice(0,this.cursor),r=s.slice(this.cursor);return`${i}${D("inverse",r[0])}${r.slice(1)}`}clear(){this._clearUserInput()}constructor({mask:e,...s}){super(s),this._mask=e??"\u2022",this.on("userInput",i=>{this._setValue(i)})}}class Tt extends B{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;const s=this.options.findIndex(({value:r})=>r===e.initialValue),i=s===-1?0:s;this.cursor=this.options[i].disabled?x(i,1,this.options):i,this.changeValue(),this.on("cursor",r=>{switch(r){case"left":case"up":this.cursor=x(this.cursor,-1,this.options);break;case"down":case"right":this.cursor=x(this.cursor,1,this.options);break}this.changeValue()})}}class Wt extends B{options;cursor=0;constructor(e){super(e,!1),this.options=e.options;const s=e.caseSensitive===!0,i=this.options.map(({value:[r]})=>s?r:r?.toLowerCase());this.cursor=Math.max(i.indexOf(e.initialValue),0),this.on("key",(r,n)=>{if(!r)return;const u=s&&n.shift?r.toUpperCase():r;if(!i.includes(u))return;const a=this.options.find(({value:[l]})=>s?l===u:l?.toLowerCase()===r);a&&(this.value=a.value,this.state="submit",this.emit("submit"))})}}class $t extends B{get userInputWithCursor(){if(this.state==="submit")return this.userInput;const e=this.userInput;if(this.cursor>=e.length)return`${this.userInput}\u2588`;const s=e.slice(0,this.cursor),[i,...r]=e.slice(this.cursor);return`${s}${D("inverse",i)}${r.join("")}`}get cursor(){return this._cursor}constructor(e){super({...e,initialUserInput:e.initialUserInput??e.initialValue}),this.on("userInput",s=>{this._setValue(s)}),this.on("finalize",()=>{this.value||(this.value=e.defaultValue),this.value===void 0&&(this.value="")})}}export{Vt as AutocompletePrompt,kt as ConfirmPrompt,yt as GroupMultiSelectPrompt,Lt as MultiSelectPrompt,Mt as PasswordPrompt,B as Prompt,Wt as SelectKeyPrompt,Tt as SelectPrompt,$t as TextPrompt,xt as block,rt as getColumns,nt as getRows,Ct as isCancel,_ as settings,It as updateSettings,Bt as wrapTextWithPrefix};
import{styleText as y}from"node:util";import{stdout as S,stdin as $}from"node:process";import*as _ from"node:readline";import P from"node:readline";import{wrapAnsi as x}from"fast-wrap-ansi";import{cursor as c,erase as M}from"sisteransi";import{ReadStream as D}from"node:tty";function d(r,t,e){if(!e.some(o=>!o.disabled))return r;const s=r+t,i=Math.max(e.length-1,0),n=s<0?i:s>i?0:s;return e[n].disabled?d(n,t<0?-1:1,e):n}const E=["up","down","left","right","space","enter","cancel"],G=["January","February","March","April","May","June","July","August","September","October","November","December"],u={actions:new Set(E),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:[...G],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 K(r){if(r.aliases!==void 0){const t=r.aliases;for(const e in t){if(!Object.hasOwn(t,e))continue;const s=t[e];u.actions.has(s)&&(u.aliases.has(e)||u.aliases.set(e,s))}}if(r.messages!==void 0){const t=r.messages;t.cancel!==void 0&&(u.messages.cancel=t.cancel),t.error!==void 0&&(u.messages.error=t.error)}if(r.withGuide!==void 0&&(u.withGuide=r.withGuide!==!1),r.date!==void 0){const t=r.date;t.monthNames!==void 0&&(u.date.monthNames=[...t.monthNames]),t.messages!==void 0&&(t.messages.required!==void 0&&(u.date.messages.required=t.messages.required),t.messages.invalidMonth!==void 0&&(u.date.messages.invalidMonth=t.messages.invalidMonth),t.messages.invalidDay!==void 0&&(u.date.messages.invalidDay=t.messages.invalidDay),t.messages.afterMin!==void 0&&(u.date.messages.afterMin=t.messages.afterMin),t.messages.beforeMax!==void 0&&(u.date.messages.beforeMax=t.messages.beforeMax))}}function V(r,t){if(typeof r=="string")return u.aliases.get(r)===t;for(const e of r)if(e!==void 0&&V(e,t))return!0;return!1}function j(r,t){if(r===t)return;const e=r.split(`
`),s=t.split(`
`),i=Math.max(e.length,s.length),n=[];for(let o=0;o<i;o++)e[o]!==s[o]&&n.push(o);return{lines:n,numLinesBefore:e.length,numLinesAfter:s.length,numLines:i}}const Y=globalThis.process.platform.startsWith("win"),C=Symbol("clack:cancel");function q(r){return r===C}function w(r,t){const e=r;e.isTTY&&e.setRawMode(t)}function z({input:r=$,output:t=S,overwrite:e=!0,hideCursor:s=!0}={}){const i=_.createInterface({input:r,output:t,prompt:"",tabSize:1});_.emitKeypressEvents(r,i),r instanceof D&&r.isTTY&&r.setRawMode(!0);const n=(o,{name:a,sequence:h})=>{const l=String(o);if(V([l,a,h],"cancel")){s&&t.write(c.show),process.exit(0);return}if(!e)return;const f=a==="return"?0:-1,v=a==="return"?-1:0;_.moveCursor(t,f,v,()=>{_.clearLine(t,1,()=>{r.once("keypress",n)})})};return s&&t.write(c.hide),r.once("keypress",n),()=>{r.off("keypress",n),s&&t.write(c.show),r instanceof D&&r.isTTY&&!Y&&r.setRawMode(!1),i.terminal=!1,i.close()}}const O=r=>"columns"in r&&typeof r.columns=="number"?r.columns:80,A=r=>"rows"in r&&typeof r.rows=="number"?r.rows:20;function R(r,t,e,s=e){const i=O(r??S);return x(t,i-e.length,{hard:!0,trim:!1}).split(`
`).map((n,o)=>`${o===0?s:e}${n}`).join(`
`)}let p=class{input;output;_abortSignal;rl;opts;_render;_track=!1;_prevFrame="";_subscribers=new Map;_cursor=0;state="initial";error="";value;userInput="";constructor(t,e=!0){const{input:s=$,output:i=S,render:n,signal:o,...a}=t;this.opts=a,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=e,this._abortSignal=o,this.input=s,this.output=i}unsubscribe(){this._subscribers.clear()}setSubscriber(t,e){const s=this._subscribers.get(t)??[];s.push(e),this._subscribers.set(t,s)}on(t,e){this.setSubscriber(t,{cb:e})}once(t,e){this.setSubscriber(t,{cb:e,once:!0})}emit(t,...e){const s=this._subscribers.get(t)??[],i=[];for(const n of s)n.cb(...e),n.once&&i.push(()=>s.splice(s.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(C);this._abortSignal.addEventListener("abort",()=>{this.state="cancel",this.close()},{once:!0})}this.rl=P.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(c.show),this.output.off("resize",this.render),w(this.input,!1),t(this.value)}),this.once("cancel",()=>{this.output.write(c.show),this.output.off("resize",this.render),w(this.input,!1),t(C)})})}_isActionKey(t,e){return t===" "}_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:!0,name:"u"}),this._setUserInput("")}onKeypress(t,e){if(this._track&&e.name!=="return"&&(e.name&&this._isActionKey(t,e)&&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"),e?.name&&(!this._track&&u.aliases.has(e.name)&&this.emit("cursor",u.aliases.get(e.name)),u.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?.toLowerCase(),e),e?.name==="return"){if(this.opts.validate){const s=this.opts.validate(this.value);s&&(this.error=s instanceof Error?s.message:s,this.state="error",this.rl?.write(this.userInput))}this.state!=="error"&&(this.state="submit")}V([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(`
`),w(this.input,!1),this.rl?.close(),this.rl=void 0,this.emit(`${this.state}`,this.value),this.unsubscribe()}restoreCursor(){const t=x(this._prevFrame,process.stdout.columns,{hard:!0,trim:!1}).split(`
`).length-1;this.output.write(c.move(-999,t*-1))}render(){const t=x(this._render(this)??"",process.stdout.columns,{hard:!0,trim:!1});if(t!==this._prevFrame){if(this.state==="initial")this.output.write(c.hide);else{const e=j(this._prevFrame,t),s=A(this.output);if(this.restoreCursor(),e){const i=Math.max(0,e.numLinesAfter-s),n=Math.max(0,e.numLinesBefore-s);let o=e.lines.find(a=>a>=i);if(o===void 0){this._prevFrame=t;return}if(e.lines.length===1){this.output.write(c.move(0,o-n)),this.output.write(M.lines(1));const a=t.split(`
`);this.output.write(a[o]),this._prevFrame=t,this.output.write(c.move(0,a.length-o-1));return}else if(e.lines.length>1){if(i<n)o=i;else{const h=o-n;h>0&&this.output.write(c.move(0,h))}this.output.write(M.down());const a=t.split(`
`).slice(o);this.output.write(a.join(`
`)),this._prevFrame=t;return}}this.output.write(M.down())}this.output.write(t),this.state==="initial"&&(this.state="active"),this._prevFrame=t}}};function W(r,t){if(r===void 0||t.length===0)return 0;const e=t.findIndex(s=>s.value===r);return e!==-1?e:0}function B(r,t){return(t.label??String(t.value)).toLowerCase().includes(r.toLowerCase())}function J(r,t){if(t)return r?t:t[0]}let H=class extends p{filteredOptions;multiple;isNavigating=!1;selectedValues=[];focusedValue;#e=0;#o="";#t;#n;#a;get cursor(){return this.#e}get userInputWithCursor(){if(!this.userInput)return y(["inverse","hidden"],"_");if(this._cursor>=this.userInput.length)return`${this.userInput}\u2588`;const t=this.userInput.slice(0,this._cursor),[e,...s]=this.userInput.slice(this._cursor);return`${t}${y("inverse",e)}${s.join("")}`}get options(){return typeof this.#n=="function"?this.#n():this.#n}constructor(t){super(t),this.#n=t.options,this.#a=t.placeholder;const e=this.options;this.filteredOptions=[...e],this.multiple=t.multiple===!0,this.#t=typeof t.options=="function"?t.filter:t.filter??B;let s;if(t.initialValue&&Array.isArray(t.initialValue)?this.multiple?s=t.initialValue:s=t.initialValue.slice(0,1):!this.multiple&&this.options.length>0&&(s=[this.options[0].value]),s)for(const i of s){const n=e.findIndex(o=>o.value===i);n!==-1&&(this.toggleSelected(i),this.#e=n)}this.focusedValue=this.options[this.#e]?.value,this.on("key",(i,n)=>this.#s(i,n)),this.on("userInput",i=>this.#i(i))}_isActionKey(t,e){return t===" "||this.multiple&&this.isNavigating&&e.name==="space"&&t!==void 0&&t!==""}#s(t,e){const s=e.name==="up",i=e.name==="down",n=e.name==="return",o=this.userInput===""||this.userInput===" ",a=this.#a,h=this.options,l=a!==void 0&&a!==""&&h.some(f=>!f.disabled&&(this.#t?this.#t(a,f):!0));if(e.name==="tab"&&o&&l){this.userInput===" "&&this._clearUserInput(),this._setUserInput(a,!0),this.isNavigating=!1;return}s||i?(this.#e=d(this.#e,s?-1:1,this.filteredOptions),this.focusedValue=this.filteredOptions[this.#e]?.value,this.multiple||(this.selectedValues=[this.focusedValue]),this.isNavigating=!0):n?this.value=J(this.multiple,this.selectedValues):this.multiple?this.focusedValue!==void 0&&(e.name==="tab"||this.isNavigating&&e.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(e=>e!==t):this.selectedValues=[...this.selectedValues,t]:this.selectedValues=[t])}#i(t){if(t!==this.#o){this.#o=t;const e=this.options;t&&this.#t?this.filteredOptions=e.filter(n=>this.#t?.(t,n)):this.filteredOptions=[...e];const s=W(this.focusedValue,this.filteredOptions);this.#e=d(s,0,this.filteredOptions);const i=this.filteredOptions[this.#e];i&&!i.disabled?this.focusedValue=i.value:this.focusedValue=void 0,this.multiple||(this.focusedValue!==void 0?this.toggleSelected(this.focusedValue):this.deselectAll())}}};class Q extends p{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",e=>{this.output.write(c.move(0,-1)),this.value=e,this.state="submit",this.close()}),this.on("cursor",()=>{this.value=!this.value})}}const X={Y:{type:"year",len:4},M:{type:"month",len:2},D:{type:"day",len:2}};function L(r){return[...r].map(t=>X[t])}function Z(r){const t=new Intl.DateTimeFormat(r,{year:"numeric",month:"2-digit",day:"2-digit"}).formatToParts(new Date(2e3,0,15)),e=[];let s="/";for(const i of t)i.type==="literal"?s=i.value.trim()||i.value:(i.type==="year"||i.type==="month"||i.type==="day")&&e.push({type:i.type,len:i.type==="year"?4:2});return{segments:e,separator:s}}function k(r){return Number.parseInt((r||"0").replace(/_/g,"0"),10)||0}function I(r){return{year:k(r.year),month:k(r.month),day:k(r.day)}}function T(r,t){return new Date(r||2001,t||1,0).getDate()}function F(r){const{year:t,month:e,day:s}=I(r);if(!t||t<0||t>9999||!e||e<1||e>12||!s||s<1)return;const i=new Date(Date.UTC(t,e-1,s));if(!(i.getUTCFullYear()!==t||i.getUTCMonth()!==e-1||i.getUTCDate()!==s))return{year:t,month:e,day:s}}function N(r){const t=F(r);return t?new Date(Date.UTC(t.year,t.month-1,t.day)):void 0}function tt(r,t,e,s){const i=e?{year:e.getUTCFullYear(),month:e.getUTCMonth()+1,day:e.getUTCDate()}:null,n=s?{year:s.getUTCFullYear(),month:s.getUTCMonth()+1,day:s.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:T(t.year,t.month)}}class et extends p{#e;#o;#t;#n;#a;#s={segmentIndex:0,positionInSegment:0};#i=!0;#r=null;inlineError="";get segmentCursor(){return{...this.#s}}get segmentValues(){return{...this.#t}}get segments(){return this.#e}get separator(){return this.#o}get formattedValue(){return this.#c(this.#t)}#c(t){return this.#e.map(e=>t[e.type]).join(this.#o)}#h(){this._setUserInput(this.#c(this.#t)),this._setValue(N(this.#t)??void 0)}constructor(t){const e=t.format?{segments:L(t.format),separator:t.separator??"/"}:Z(t.locale),s=t.separator??e.separator,i=t.format?L(t.format):e.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:"__"},a=i.map(h=>o[h.type]).join(s);super({...t,initialUserInput:a},!1),this.#e=i,this.#o=s,this.#t=o,this.#n=t.minDate,this.#a=t.maxDate,this.#h(),this.on("cursor",h=>this.#d(h)),this.on("key",(h,l)=>this.#f(h,l)),this.on("finalize",()=>this.#g(t))}#u(){const t=Math.max(0,Math.min(this.#s.segmentIndex,this.#e.length-1)),e=this.#e[t];if(e)return this.#s.positionInSegment=Math.max(0,Math.min(this.#s.positionInSegment,e.len-1)),{segment:e,index:t}}#l(t){this.inlineError="",this.#r=null;const e=this.#u();e&&(this.#s.segmentIndex=Math.max(0,Math.min(this.#e.length-1,e.index+t)),this.#s.positionInSegment=0,this.#i=!0)}#p(t){const e=this.#u();if(!e)return;const{segment:s}=e,i=this.#t[s.type],n=!i||i.replace(/_/g,"")==="",o=Number.parseInt((i||"0").replace(/_/g,"0"),10)||0,a=tt(s.type,I(this.#t),this.#n,this.#a);let h;n?h=t===1?a.min:a.max:h=Math.max(Math.min(a.max,o+t),a.min),this.#t={...this.#t,[s.type]:h.toString().padStart(s.len,"0")},this.#i=!0,this.#r=null,this.#h()}#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,e){if(e?.name==="backspace"||e?.sequence==="\x7F"||e?.sequence==="\b"||t==="\x7F"||t==="\b"){this.inlineError="";const s=this.#u();if(!s)return;if(!this.#t[s.segment.type].replace(/_/g,"")){this.#l(-1);return}this.#t[s.segment.type]="_".repeat(s.segment.len),this.#i=!0,this.#s.positionInSegment=0,this.#h();return}if(e?.name==="tab"){this.inlineError="";const s=this.#u();if(!s)return;const i=e.shift?-1:1,n=s.index+i;n>=0&&n<this.#e.length&&(this.#s.segmentIndex=n,this.#s.positionInSegment=0,this.#i=!0);return}if(t&&/^[0-9]$/.test(t)){const s=this.#u();if(!s)return;const{segment:i}=s,n=!this.#t[i.type].replace(/_/g,"");if(this.#i&&this.#r!==null&&!n){const m=this.#r+t,g={...this.#t,[i.type]:m},b=this.#m(g,i);if(b){this.inlineError=b,this.#r=null,this.#i=!1;return}this.inlineError="",this.#t[i.type]=m,this.#r=null,this.#i=!1,this.#h(),s.index<this.#e.length-1&&(this.#s.segmentIndex=s.index+1,this.#s.positionInSegment=0,this.#i=!0);return}this.#i&&!n&&(this.#t[i.type]="_".repeat(i.len),this.#s.positionInSegment=0),this.#i=!1,this.#r=null;const o=this.#t[i.type],a=o.indexOf("_"),h=a>=0?a:Math.min(this.#s.positionInSegment,i.len-1);if(h<0||h>=i.len)return;let l=o.slice(0,h)+t+o.slice(h+1),f=!1;if(h===0&&o==="__"&&(i.type==="month"||i.type==="day")){const m=Number.parseInt(t,10);l=`0${t}`,f=m<=(i.type==="month"?1:2)}if(i.type==="year"&&(l=(o.replace(/_/g,"")+t).padStart(i.len,"_")),!l.includes("_")){const m={...this.#t,[i.type]:l},g=this.#m(m,i);if(g){this.inlineError=g;return}}this.inlineError="",this.#t[i.type]=l;const v=l.includes("_")?void 0:F(this.#t);if(v){const{year:m,month:g}=v,b=T(m,g);this.#t={year:String(Math.max(0,Math.min(9999,m))).padStart(4,"0"),month:String(Math.max(1,Math.min(12,g))).padStart(2,"0"),day:String(Math.max(1,Math.min(b,v.day))).padStart(2,"0")}}this.#h();const U=l.indexOf("_");f?(this.#i=!0,this.#r=t):U>=0?this.#s.positionInSegment=U:a>=0&&s.index<this.#e.length-1?(this.#s.segmentIndex=s.index+1,this.#s.positionInSegment=0,this.#i=!0):this.#s.positionInSegment=Math.min(h+1,i.len-1)}}#m(t,e){const{month:s,day:i}=I(t);if(e.type==="month"&&(s<0||s>12))return u.date.messages.invalidMonth;if(e.type==="day"&&(i<0||i>31))return u.date.messages.invalidDay(31,"any month")}#g(t){const{year:e,month:s,day:i}=I(this.#t);if(e&&s&&i){const n=T(e,s);this.#t={...this.#t,day:String(Math.min(i,n)).padStart(2,"0")}}this.value=N(this.#t)??t.defaultValue??void 0}}class st extends p{options;cursor=0;#e;getGroupItems(t){return this.options.filter(e=>e.group===t)}isGroupSelected(t){const e=this.getGroupItems(t),s=this.value;return s===void 0?!1:e.every(i=>s.includes(i.value))}toggleValue(){const t=this.options[this.cursor];if(this.value===void 0&&(this.value=[]),t.group===!0){const e=t.value,s=this.getGroupItems(e);this.isGroupSelected(e)?this.value=this.value.filter(i=>s.findIndex(n=>n.value===i)===-1):this.value=[...this.value,...s.map(i=>i.value)],this.value=Array.from(new Set(this.value))}else{const e=this.value.includes(t.value);this.value=e?this.value.filter(s=>s!==t.value):[...this.value,t.value]}}constructor(t){super(t,!1);const{options:e}=t;this.#e=t.selectableGroups!==!1,this.options=Object.entries(e).flatMap(([s,i])=>[{value:s,group:!0,label:s},...i.map(n=>({...n,group:s}))]),this.value=[...t.initialValues??[]],this.cursor=Math.max(this.options.findIndex(({value:s})=>s===t.cursorAt),this.#e?0:1),this.on("cursor",s=>{switch(s){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.#e&&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.#e&&i&&(this.cursor=this.cursor===this.options.length-1?0:this.cursor+1);break}case"space":this.toggleValue();break}})}}let it=class extends p{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,e=this.value!==void 0&&this.value.length===t.length;this.value=e?[]:t.map(s=>s.value)}toggleInvert(){const t=this.value;if(!t)return;const e=this._enabledOptions.filter(s=>!t.includes(s.value));this.value=e.map(s=>s.value)}toggleValue(){this.value===void 0&&(this.value=[]);const t=this.value.includes(this._value);this.value=t?this.value.filter(e=>e!==this._value):[...this.value,this._value]}constructor(t){super(t,!1),this.options=t.options,this.value=[...t.initialValues??[]];const e=Math.max(this.options.findIndex(({value:s})=>s===t.cursorAt),0);this.cursor=this.options[e].disabled?d(e,1,this.options):e,this.on("key",s=>{s==="a"&&this.toggleAll(),s==="i"&&this.toggleInvert()}),this.on("cursor",s=>{switch(s){case"left":case"up":this.cursor=d(this.cursor,-1,this.options);break;case"down":case"right":this.cursor=d(this.cursor,1,this.options);break;case"space":this.toggleValue();break}})}};class rt extends p{_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}${y(["inverse","hidden"],"_")}`;const e=this.masked,s=e.slice(0,this.cursor),i=e.slice(this.cursor);return`${s}${y("inverse",i[0])}${i.slice(1)}`}clear(){this._clearUserInput()}constructor({mask:t,...e}){super(e),this._mask=t??"\u2022",this.on("userInput",s=>{this._setValue(s)})}}class nt extends p{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 e=this.options.findIndex(({value:i})=>i===t.initialValue),s=e===-1?0:e;this.cursor=this.options[s].disabled?d(s,1,this.options):s,this.changeValue(),this.on("cursor",i=>{switch(i){case"left":case"up":this.cursor=d(this.cursor,-1,this.options);break;case"down":case"right":this.cursor=d(this.cursor,1,this.options);break}this.changeValue()})}}class ot extends p{options;cursor=0;constructor(t){super(t,!1),this.options=t.options;const e=t.caseSensitive===!0,s=this.options.map(({value:[i]})=>e?i:i?.toLowerCase());this.cursor=Math.max(s.indexOf(t.initialValue),0),this.on("key",(i,n)=>{if(!i)return;const o=e&&n.shift?i.toUpperCase():i;if(!s.includes(o))return;const a=this.options.find(({value:[h]})=>e?h===o:h?.toLowerCase()===i);a&&(this.value=a.value,this.state="submit",this.emit("submit"))})}}class at extends p{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,...i]=t.slice(this.cursor);return`${e}${y("inverse",s)}${i.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{H as AutocompletePrompt,Q as ConfirmPrompt,et as DatePrompt,st as GroupMultiSelectPrompt,it as MultiSelectPrompt,rt as PasswordPrompt,p as Prompt,ot as SelectKeyPrompt,nt as SelectPrompt,at as TextPrompt,z as block,O as getColumns,A as getRows,q as isCancel,u as settings,K as updateSettings,R as wrapTextWithPrefix};
//# sourceMappingURL=index.mjs.map
{
"name": "@clack/core",
"version": "1.1.0",
"version": "1.2.0",
"type": "module",

@@ -50,7 +50,7 @@ "main": "./dist/index.mjs",

"dependencies": {
"fast-wrap-ansi": "^0.1.3",
"sisteransi": "^1.0.5"
},
"devDependencies": {
"vitest": "^3.2.4",
"fast-wrap-ansi": "^0.1.3"
"vitest": "^3.2.4"
},

@@ -57,0 +57,0 @@ "scripts": {

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