@clack/core
Advanced tools
+24
-0
| # @clack/core | ||
| ## 1.4.0 | ||
| ### Minor Changes | ||
| - [#543](https://github.com/bombshell-dev/clack/pull/543) [`83428ac`](https://github.com/bombshell-dev/clack/commit/83428ac6d8bc5eda87615cc7b1f14e0c8b16e1b6) Thanks [@florian-lefebvre](https://github.com/florian-lefebvre)! - Adds support for Standard Schema validation | ||
| Prompts accept an optional `validate()` function to validate user input. While a function provides more flexibility and customization over your validation, it can be a bit verbose. To help solve this, there are libraries that provide schema-based validation to make shorthand and type-strict validation substantially easier. | ||
| Libraries following the [Standard Schema specification](https://github.com/standard-schema/standard-schema) are now natively supported. For example, using [Arktype](https://arktype.io/): | ||
| ```diff | ||
| import { text } from '@clack/prompts'; | ||
| import { type } from 'arktype'; | ||
| const name = await text({ | ||
| message: 'Enter your email', | ||
| + validate: type('string.email').describe('Invalid email'), | ||
| }); | ||
| ``` | ||
| ### Patch Changes | ||
| - [#534](https://github.com/bombshell-dev/clack/pull/534) [`3dcb31a`](https://github.com/bombshell-dev/clack/commit/3dcb31a7d63827d95a5a52ac630cbd48e3a68364) Thanks [@MattStypa](https://github.com/MattStypa)! - Fixed spaces and uppercase characters in multiline prompt | ||
| ## 1.3.1 | ||
@@ -4,0 +28,0 @@ |
+109
-3
@@ -96,2 +96,103 @@ import { Key } from 'node:readline'; | ||
| /** The Standard Schema interface. */ | ||
| interface StandardSchemaV1<Input = unknown, Output = Input> { | ||
| /** The Standard Schema properties. */ | ||
| readonly '~standard': StandardSchemaV1.Props<Input, Output>; | ||
| } | ||
| declare namespace StandardSchemaV1 { | ||
| /** The Standard Schema properties interface. */ | ||
| interface Props<Input = unknown, Output = Input> { | ||
| /** The version number of the standard. */ | ||
| readonly version: 1; | ||
| /** The vendor name of the schema library. */ | ||
| readonly vendor: string; | ||
| /** Validates unknown input values. */ | ||
| readonly validate: (value: unknown, options?: StandardSchemaV1.Options | undefined) => Result<Output> | Promise<Result<Output>>; | ||
| /** Inferred types associated with the schema. */ | ||
| readonly types?: Types<Input, Output> | undefined; | ||
| } | ||
| /** The result interface of the validate function. */ | ||
| type Result<Output> = SuccessResult<Output> | FailureResult; | ||
| /** The result interface if validation succeeds. */ | ||
| interface SuccessResult<Output> { | ||
| /** The typed output value. */ | ||
| readonly value: Output; | ||
| /** A falsy value for `issues` indicates success. */ | ||
| readonly issues?: undefined; | ||
| } | ||
| interface Options { | ||
| /** Explicit support for additional vendor-specific parameters, if needed. */ | ||
| readonly libraryOptions?: Record<string, unknown> | undefined; | ||
| } | ||
| /** The result interface if validation fails. */ | ||
| interface FailureResult { | ||
| /** The issues of failed validation. */ | ||
| readonly issues: ReadonlyArray<Issue>; | ||
| } | ||
| /** The issue interface of the failure output. */ | ||
| interface Issue { | ||
| /** The error message of the issue. */ | ||
| readonly message: string; | ||
| /** The path of the issue, if any. */ | ||
| readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined; | ||
| } | ||
| /** The path segment interface of the issue. */ | ||
| interface PathSegment { | ||
| /** The key representing a path segment. */ | ||
| readonly key: PropertyKey; | ||
| } | ||
| /** The Standard Schema types interface. */ | ||
| interface Types<Input = unknown, Output = Input> { | ||
| /** The input type of the schema. */ | ||
| readonly input: Input; | ||
| /** The output type of the schema. */ | ||
| readonly output: Output; | ||
| } | ||
| /** Infers the input type of a Standard Schema. */ | ||
| type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema['~standard']['types']>['input']; | ||
| /** Infers the output type of a Standard Schema. */ | ||
| type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema['~standard']['types']>['output']; | ||
| } | ||
| /** | ||
| * A function or [Standard Schema](https://github.com/standard-schema/standard-schema) | ||
| * that validates user input. If a custom function is given, you should return a | ||
| * `string` or `Error` to show as a validation error, or `undefined` to accept the result. | ||
| * | ||
| * @example Using arktype | ||
| * ```ts | ||
| * import { text } from '@clack/prompts'; | ||
| * import { type } from 'arktype'; | ||
| * | ||
| * const name = await text({ | ||
| * message: 'Enter your name (letters only)', | ||
| * validate: type('string.alpha').describe('Name can only contain letters'), | ||
| * }); | ||
| * ``` | ||
| * | ||
| * @example Custom validator | ||
| * ```ts | ||
| * import { text } from '@clack/prompts'; | ||
| * | ||
| * const age = await text({ | ||
| * message: 'Enter your age:', | ||
| * validate(value) { | ||
| * if (!value) return 'Please enter a value'; | ||
| * const num = parseInt(value); | ||
| * if (isNaN(num)) return 'Please enter a valid number'; | ||
| * if (num < 0 || num > 120) return 'Age must be between 0 and 120'; | ||
| * return undefined; | ||
| * }, | ||
| * }); | ||
| * ``` | ||
| */ | ||
| type Validate<TValue> = ((value: TValue | undefined) => string | Error | undefined) | StandardSchemaV1<TValue | undefined, unknown>; | ||
| /** | ||
| * Runs the `validate()` option and normalizes the result | ||
| * @param validate - The validate option | ||
| * @param value - The user input | ||
| * @returns the validation result | ||
| */ | ||
| declare function runValidation<TValue>(validate: Validate<TValue>, value: TValue | undefined): string | Error | undefined; | ||
| interface PromptOptions<TValue, Self extends Prompt<TValue>> { | ||
@@ -101,3 +202,8 @@ render(this: Omit<Self, 'prompt'>): string | undefined; | ||
| initialUserInput?: string; | ||
| validate?: ((value: TValue | undefined) => string | Error | undefined) | undefined; | ||
| /** | ||
| * A function or a [Standard Schema](https://github.com/standard-schema/standard-schema) | ||
| * that validates user input. If a custom function is given, you should return a `string` or `Error` | ||
| * to show as a validation error, or `undefined` to accept the result. | ||
| */ | ||
| validate?: Validate<TValue> | undefined; | ||
| input?: Readable; | ||
@@ -364,3 +470,3 @@ output?: Writable; | ||
| export { AutocompletePrompt, ConfirmPrompt, DatePrompt, GroupMultiSelectPrompt, MultiLinePrompt, MultiSelectPrompt, PasswordPrompt, Prompt, SelectKeyPrompt, SelectPrompt, TextPrompt, block, getColumns, getRows, isCancel, settings, updateSettings, wrapTextWithPrefix }; | ||
| export type { AutocompleteOptions, ClackSettings, ConfirmOptions, DateFormat, DateOptions, DateParts, GroupMultiSelectOptions, MultiLineOptions, MultiSelectOptions, PasswordOptions, PromptOptions, SelectKeyOptions, SelectOptions, ClackState as State, TextOptions }; | ||
| export { AutocompletePrompt, ConfirmPrompt, DatePrompt, GroupMultiSelectPrompt, MultiLinePrompt, MultiSelectPrompt, PasswordPrompt, Prompt, SelectKeyPrompt, SelectPrompt, TextPrompt, block, getColumns, getRows, isCancel, runValidation, settings, updateSettings, wrapTextWithPrefix }; | ||
| export type { AutocompleteOptions, ClackSettings, ConfirmOptions, DateFormat, DateOptions, DateParts, GroupMultiSelectOptions, MultiLineOptions, MultiSelectOptions, PasswordOptions, PromptOptions, SelectKeyOptions, SelectOptions, ClackState as State, TextOptions, Validate }; |
+7
-7
@@ -1,12 +0,12 @@ | ||
| import{styleText as v}from"node:util";import{stdout as x,stdin as D}from"node:process";import*as b from"node:readline";import E 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 G=["up","down","left","right","space","enter","cancel"],K=["January","February","March","April","May","June","July","August","September","October","November","December"],h={actions:new Set(G),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:[...K],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 j(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 z(r,t){if(r===t)return;const s=r.split(` | ||
| 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 Y=globalThis.process.platform.startsWith("win"),k=Symbol("clack:cancel");function q(r){return r===k}function w(r,t){const s=r;s.isTTY&&s.setRawMode(t)}function R({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&&!Y&&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 W(r,t,s,e=s,i=s,n){const o=A(r??x);return M(t,o-s.length,{hard:!0,trim:!1}).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(` | ||
| `)}let m=class{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=E.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?.toLowerCase(),s),s?.name==="return"&&this._shouldSubmit(t,s)){if(this.opts.validate){const e=this.opts.validate(this.value);e&&(this.error=e instanceof Error?e.message:e,this.state="error",this.rl?.write(this.userInput))}this.state!=="error"&&(this.state="submit")}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(` | ||
| `)}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=z(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(` | ||
| `).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 B(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 J(r,t){return(t.label??String(t.value)).toLowerCase().includes(r.toLowerCase())}function H(r,t){if(t)return r?t:t[0]}let Q=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??J;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=H(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=B(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 X 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 Z={Y:{type:"year",len:4},M:{type:"month",len:2},D:{type:"day",len:2}};function P(r){return[...r].map(t=>Z[t])}function tt(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 F(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 N(r){const t=F(r);return t?new Date(Date.UTC(t.year,t.month-1,t.day)):void 0}function st(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 et 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(N(this.#t)??void 0)}constructor(t){const s=t.format?{segments:P(t.format),separator:t.separator??"/"}:tt(t.locale),e=t.separator??s.separator,i=t.format?P(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=st(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:F(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=N(this.#t)??t.defaultValue??void 0}}class it 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}})}}class rt 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===` | ||
| `)),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 | ||
@@ -16,3 +16,3 @@ ${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._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&&h.actions.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 nt=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=>{e==="a"&&this.toggleAll(),e==="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 ot 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 ut 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 at 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,n)=>{if(!i)return;const o=s&&n.shift?i.toUpperCase():i;if(!e.includes(o))return;const u=this.options.find(({value:[a]})=>s?a===o:a?.toLowerCase()===i);u&&(this.value=u.value,this.state="submit",this.emit("submit"))})}}class ht 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{Q as AutocompletePrompt,X as ConfirmPrompt,et as DatePrompt,it as GroupMultiSelectPrompt,rt as MultiLinePrompt,nt as MultiSelectPrompt,ot as PasswordPrompt,m as Prompt,at as SelectKeyPrompt,ut as SelectPrompt,ht as TextPrompt,R as block,A as getColumns,L as getRows,q as isCancel,h as settings,j as updateSettings,W as wrapTextWithPrefix}; | ||
| `),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 |
+2
-1
| { | ||
| "name": "@clack/core", | ||
| "version": "1.3.1", | ||
| "version": "1.4.0", | ||
| "type": "module", | ||
@@ -57,2 +57,3 @@ "main": "./dist/index.mjs", | ||
| "devDependencies": { | ||
| "arktype": "^2.2.0", | ||
| "vitest": "^3.2.4" | ||
@@ -59,0 +60,0 @@ }, |
Sorry, the diff of this file is too big to display
148803
6.28%103
1.98%2
100%