@replit/clui-input
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -37,3 +37,3 @@ import { IToken } from './tokenizer'; | ||
} | ||
interface IRemainder { | ||
export interface IRemainder { | ||
kind: 'REMAINDER'; | ||
@@ -44,3 +44,3 @@ token: IToken; | ||
} | ||
interface IPending { | ||
export interface IPending { | ||
kind: 'PENDING'; | ||
@@ -66,2 +66,1 @@ key: string; | ||
}; | ||
export {}; |
@@ -1,3 +0,2 @@ | ||
import { ICommand, IOption, ArgType, ArgsMap } from './types'; | ||
declare type SearchFn = (args: ISearchArgs) => boolean; | ||
import { ICommand, IOption, ArgType, ArgsMap, SearchFn } from './types'; | ||
export interface IInputUpdates<D = any, R = any> { | ||
@@ -21,6 +20,2 @@ nodeStart?: number; | ||
} | ||
interface ISearchArgs { | ||
source: string; | ||
search: string; | ||
} | ||
export declare const createInput: (config: IConfig<ICommand<any, any>>) => (updates: { | ||
@@ -30,2 +25,1 @@ index?: number | undefined; | ||
}) => void; | ||
export {}; |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -61,78 +72,4 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
var resolver_1 = require("./resolver"); | ||
var commandOptions = function (options) { | ||
return Object.keys(options.commands).reduce(function (acc, key) { | ||
if (!options.search || | ||
options.searchFn({ source: key, search: options.search })) { | ||
var sliceStart = options.sliceStart, sliceEnd = options.sliceEnd, inputValue = options.inputValue; | ||
var newInputValueStart = inputValue && sliceStart !== undefined | ||
? inputValue.slice(0, sliceStart) + key | ||
: key; | ||
var newInputValue = newInputValueStart + | ||
(inputValue && sliceEnd !== undefined | ||
? inputValue.slice(sliceEnd) | ||
: ''); | ||
acc.push({ | ||
value: key, | ||
data: options.commands[key], | ||
inputValue: newInputValue, | ||
cursorTarget: newInputValueStart.length, | ||
searchValue: options.search, | ||
}); | ||
} | ||
return acc; | ||
}, []); | ||
}; | ||
var argsOptions = function (options) { | ||
var _a; | ||
var search = options.search | ||
? (_a = options.search) === null || _a === void 0 ? void 0 : _a.replace(/^-(-?)/, '') : undefined; | ||
return Object.keys(options.args).reduce(function (acc, key) { | ||
if (options.exclude && options.exclude.includes(key)) { | ||
return acc; | ||
} | ||
if (!search || options.searchFn({ source: key, search: search })) { | ||
var value = "--" + key; | ||
var sliceStart = options.sliceStart, sliceEnd = options.sliceEnd, inputValue = options.inputValue; | ||
var newInputValueStart = inputValue && sliceStart !== undefined | ||
? inputValue.slice(0, sliceStart) + value | ||
: value; | ||
var newInputValue = newInputValueStart + | ||
(inputValue && sliceEnd !== undefined | ||
? inputValue.slice(sliceEnd) | ||
: ''); | ||
acc.push({ | ||
value: value, | ||
data: options.args[key], | ||
inputValue: newInputValue, | ||
cursorTarget: newInputValueStart.length, | ||
searchValue: search, | ||
}); | ||
} | ||
return acc; | ||
}, []); | ||
}; | ||
var valueOptions = function (options) { | ||
return options.options.reduce(function (acc, option) { | ||
var key = option.value; | ||
if (!options.search || | ||
options.searchFn({ source: key, search: options.search })) { | ||
var sliceStart = options.sliceStart, sliceEnd = options.sliceEnd, inputValue = options.inputValue; | ||
var newInputValueStart = inputValue && sliceStart !== undefined | ||
? inputValue.slice(0, sliceStart) + key | ||
: key; | ||
var newInputValue = newInputValueStart + | ||
(inputValue && sliceEnd !== undefined | ||
? inputValue.slice(sliceEnd) | ||
: ''); | ||
acc.push({ | ||
value: key, | ||
data: option, | ||
inputValue: newInputValue, | ||
cursorTarget: newInputValueStart.length, | ||
searchValue: options.search, | ||
}); | ||
} | ||
return acc; | ||
}, []); | ||
}; | ||
var optionsList_1 = require("./optionsList"); | ||
var options_1 = require("./options"); | ||
var getRootCommands = function (ast, command, search) { return __awaiter(void 0, void 0, void 0, function () { | ||
@@ -164,7 +101,6 @@ var commands, commands; | ||
var processUpdates = function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var valueStart, current, ast, atWhitespace, previousNode, search, result, currentNode, astCommands, last, args, parsedArgKeys, options, nodeStart, rootCommands, parentCommands, search, ref, argOptions, argsMap, argsMap, argsMap, previousNode, sliceStart, search, commands, ref, token, rootCommands, argsMap, argOptions, ref, argsMap, commands, ref, argOptions, ref, run, commandsList, refRun_1; | ||
var _a, _b; | ||
var _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v; | ||
return __generator(this, function (_w) { | ||
switch (_w.label) { | ||
var valueStart, current, ast, atWhitespace, previousNode, search, result, search, result, currentNode, astCommands, last, args, parsedArgKeys, options, nodeStart, commonParams, getOptions, rootCommands, search, ref, argOptions, previousNode, search, ref, argOptions, run, commandsList, refRun_1; | ||
var _a, _b, _c, _d, _e; | ||
return __generator(this, function (_f) { | ||
switch (_f.label) { | ||
case 0: | ||
@@ -179,7 +115,7 @@ valueStart = value.slice(0, index); | ||
case 1: | ||
ast = _w.sent(); | ||
ast = _f.sent(); | ||
atWhitespace = value[index - 1] === ' '; | ||
if (!(value.length > index || atWhitespace)) return [3, 3]; | ||
if (!(value.length > index || atWhitespace)) return [3, 5]; | ||
previousNode = ast_1.closestPrevious(ast, index); | ||
if (!(((_c = previousNode) === null || _c === void 0 ? void 0 : _c.kind) === 'COMMAND' && | ||
if (!(((_a = previousNode) === null || _a === void 0 ? void 0 : _a.kind) === 'COMMAND' && | ||
typeof previousNode.ref.commands === 'function' && | ||
@@ -190,8 +126,20 @@ !commandsCache[valueStart])) return [3, 3]; | ||
case 2: | ||
result = _w.sent(); | ||
result = _f.sent(); | ||
if (result) { | ||
commandsCache[valueStart] = result; | ||
} | ||
_w.label = 3; | ||
return [3, 5]; | ||
case 3: | ||
if (!(!atWhitespace && | ||
((_b = previousNode) === null || _b === void 0 ? void 0 : _b.kind) === 'REMAINDER' && | ||
typeof ((_c = previousNode.cmdNodeCtx) === null || _c === void 0 ? void 0 : _c.ref.commands) === 'function')) return [3, 5]; | ||
search = valueStart.slice(previousNode.token.start).trim(); | ||
return [4, previousNode.cmdNodeCtx.ref.commands(search || undefined)]; | ||
case 4: | ||
result = _f.sent(); | ||
if (result) { | ||
commandsCache[valueStart] = result; | ||
} | ||
_f.label = 5; | ||
case 5: | ||
if (current !== updatedAt) { | ||
@@ -210,6 +158,24 @@ return [2]; | ||
} | ||
if (!!value) return [3, 5]; | ||
commonParams = { | ||
parsedArgKeys: parsedArgKeys, | ||
value: value, | ||
index: index, | ||
}; | ||
getOptions = options_1.optionsProvider({ | ||
command: config.command, | ||
commandsCache: commandsCache, | ||
optionsCache: optionsCache, | ||
searchFn: searchFn, | ||
}); | ||
if (!!value) return [3, 8]; | ||
rootCommands = commandsCache[''] || null; | ||
if (!!rootCommands) return [3, 7]; | ||
return [4, getRootCommands(ast, config.command)]; | ||
case 4: | ||
rootCommands = _w.sent(); | ||
case 6: | ||
rootCommands = _f.sent(); | ||
if (rootCommands) { | ||
commandsCache[''] = rootCommands; | ||
} | ||
_f.label = 7; | ||
case 7: | ||
if (current !== updatedAt) { | ||
@@ -219,3 +185,3 @@ return [2]; | ||
if (rootCommands) { | ||
options.push.apply(options, __spread(commandOptions({ | ||
options.push.apply(options, __spread(optionsList_1.commandOptions({ | ||
inputValue: value, | ||
@@ -226,192 +192,40 @@ commands: rootCommands, | ||
} | ||
return [3, 31]; | ||
case 5: | ||
if (!(((_d = currentNode) === null || _d === void 0 ? void 0 : _d.kind) === 'COMMAND')) return [3, 6]; | ||
parentCommands = ((_e = currentNode.parent) === null || _e === void 0 ? void 0 : _e.ref.commands) || config.command.commands; | ||
if (typeof parentCommands === 'function' && commandsCache[valueStart]) { | ||
parentCommands = commandsCache[valueStart]; | ||
} | ||
if (typeof parentCommands === 'object') { | ||
options.push.apply(options, __spread(commandOptions({ | ||
commands: parentCommands, | ||
searchFn: searchFn, | ||
sliceStart: nodeStart, | ||
sliceEnd: currentNode.token.end, | ||
search: value.slice(nodeStart, index), | ||
inputValue: value, | ||
}))); | ||
} | ||
return [3, 31]; | ||
case 6: | ||
if (!(((_f = currentNode) === null || _f === void 0 ? void 0 : _f.kind) === 'ARG_VALUE')) return [3, 11]; | ||
return [3, 14]; | ||
case 8: | ||
if (!currentNode) return [3, 11]; | ||
if (!(currentNode.kind === 'ARG_VALUE')) return [3, 10]; | ||
search = value.slice(nodeStart, index); | ||
ref = currentNode.parent.ref; | ||
argOptions = null; | ||
if (!Array.isArray(ref.options)) return [3, 7]; | ||
argOptions = ref.options; | ||
return [3, 10]; | ||
case 7: | ||
if (!(typeof ref.options === 'function')) return [3, 10]; | ||
if (!optionsCache[valueStart]) return [3, 8]; | ||
argOptions = optionsCache[valueStart]; | ||
return [3, 10]; | ||
case 8: return [4, ref.options(search || undefined)]; | ||
if (!(typeof ref.options === 'function' && !optionsCache[valueStart])) return [3, 10]; | ||
return [4, ref.options(search || undefined)]; | ||
case 9: | ||
argOptions = _w.sent(); | ||
argOptions = _f.sent(); | ||
if (argOptions) { | ||
optionsCache[valueStart] = argOptions; | ||
} | ||
if (current !== updatedAt) { | ||
return [2]; | ||
} | ||
if (argOptions) { | ||
optionsCache[valueStart] = argOptions; | ||
} | ||
_w.label = 10; | ||
_f.label = 10; | ||
case 10: | ||
if (argOptions) { | ||
options.push.apply(options, __spread(valueOptions({ | ||
options: argOptions, | ||
search: search, | ||
searchFn: searchFn, | ||
sliceStart: nodeStart, | ||
inputValue: value, | ||
}))); | ||
} | ||
return [3, 31]; | ||
options.push.apply(options, __spread(getOptions(__assign({ currentNode: currentNode }, commonParams)))); | ||
return [3, 14]; | ||
case 11: | ||
if (!(((_g = currentNode) === null || _g === void 0 ? void 0 : _g.kind) === 'ARG_FLAG')) return [3, 12]; | ||
argsMap = ((_h = currentNode.parent) === null || _h === void 0 ? void 0 : _h.ref.args) || (_a = {}, | ||
_a[currentNode.token.value.replace(/^-(-?)/, '')] = currentNode.ref, | ||
_a); | ||
options.push.apply(options, __spread(argsOptions({ | ||
searchFn: searchFn, | ||
sliceStart: nodeStart, | ||
sliceEnd: currentNode.token.end, | ||
search: value.slice(nodeStart, index), | ||
inputValue: value, | ||
args: argsMap, | ||
}))); | ||
return [3, 31]; | ||
case 12: | ||
if (!(((_j = currentNode) === null || _j === void 0 ? void 0 : _j.kind) === 'ARG_KEY')) return [3, 13]; | ||
argsMap = ((_k = currentNode.parent.parent.parent) === null || _k === void 0 ? void 0 : _k.ref.args) || (_b = {}, | ||
_b[currentNode.token.value.replace(/^-(-?)/, '')] = currentNode.parent.ref, | ||
_b); | ||
options.push.apply(options, __spread(argsOptions({ | ||
searchFn: searchFn, | ||
sliceStart: nodeStart, | ||
sliceEnd: currentNode.token.end, | ||
search: value.slice(nodeStart, index), | ||
inputValue: value, | ||
args: argsMap, | ||
}))); | ||
return [3, 31]; | ||
case 13: | ||
if (!(((_l = currentNode) === null || _l === void 0 ? void 0 : _l.kind) === 'REMAINDER')) return [3, 14]; | ||
if (currentNode.cmdNodeCtx) { | ||
argsMap = currentNode.cmdNodeCtx.ref.args; | ||
if (argsMap) { | ||
options.push.apply(options, __spread(argsOptions({ | ||
args: argsMap, | ||
searchFn: searchFn, | ||
sliceStart: nodeStart, | ||
sliceEnd: currentNode.token.end, | ||
search: value.slice(nodeStart, index), | ||
inputValue: value, | ||
}))); | ||
} | ||
} | ||
return [3, 31]; | ||
case 14: | ||
previousNode = ast_1.closestPrevious(ast, index); | ||
sliceStart = atWhitespace ? index : value.lastIndexOf(' ') + 1; | ||
search = !atWhitespace | ||
? value.slice(sliceStart, index).trim() | ||
: undefined; | ||
nodeStart = sliceStart; | ||
if (!(((_m = previousNode) === null || _m === void 0 ? void 0 : _m.kind) === 'COMMAND')) return [3, 15]; | ||
if (previousNode.ref.args) { | ||
options.push.apply(options, __spread(argsOptions({ | ||
searchFn: searchFn, | ||
sliceStart: nodeStart, | ||
search: search, | ||
inputValue: value, | ||
args: previousNode.ref.args, | ||
exclude: parsedArgKeys, | ||
}))); | ||
} | ||
commands = null; | ||
ref = previousNode.ref; | ||
if (typeof ref.commands === 'object') { | ||
commands = ref.commands; | ||
} | ||
else if (typeof ref.commands === 'function') { | ||
commands = commandsCache[valueStart]; | ||
} | ||
if (commands) { | ||
options.push.apply(options, __spread(commandOptions({ | ||
searchFn: searchFn, | ||
sliceStart: nodeStart, | ||
search: search, | ||
inputValue: value, | ||
commands: commands, | ||
}))); | ||
} | ||
return [3, 31]; | ||
case 15: | ||
if (!(((_o = previousNode) === null || _o === void 0 ? void 0 : _o.kind) === 'REMAINDER')) return [3, 19]; | ||
if (!((_p = previousNode.cmdNodeCtx) === null || _p === void 0 ? void 0 : _p.ref.args)) return [3, 16]; | ||
token = previousNode.token; | ||
nodeStart = token.start; | ||
options.push.apply(options, __spread(argsOptions({ | ||
searchFn: searchFn, | ||
sliceStart: nodeStart, | ||
search: token.value.replace(/^-(-?)/, ''), | ||
inputValue: value, | ||
args: previousNode.cmdNodeCtx.ref.args, | ||
exclude: parsedArgKeys, | ||
}))); | ||
return [3, 18]; | ||
case 16: return [4, getRootCommands(ast, config.command, search)]; | ||
case 17: | ||
rootCommands = _w.sent(); | ||
if (rootCommands) { | ||
if (!atWhitespace && previousNode && 'token' in previousNode) { | ||
nodeStart = previousNode.token.start; | ||
options.push.apply(options, __spread(commandOptions({ | ||
inputValue: value, | ||
commands: rootCommands, | ||
searchFn: searchFn, | ||
sliceStart: nodeStart, | ||
search: previousNode.token.value, | ||
}))); | ||
} | ||
_w.label = 18; | ||
case 18: return [3, 31]; | ||
case 19: | ||
if (!(((_q = previousNode) === null || _q === void 0 ? void 0 : _q.kind) === 'ARG_VALUE' && atWhitespace)) return [3, 20]; | ||
argsMap = previousNode.parent.parent.ref.args; | ||
if (argsMap) { | ||
options.push.apply(options, __spread(argsOptions({ | ||
search: search, | ||
searchFn: searchFn, | ||
sliceStart: nodeStart, | ||
inputValue: value, | ||
args: argsMap, | ||
exclude: parsedArgKeys, | ||
}))); | ||
else { | ||
nodeStart = index; | ||
} | ||
return [3, 31]; | ||
case 20: | ||
if (!(((_r = previousNode) === null || _r === void 0 ? void 0 : _r.kind) === 'ARG_VALUE')) return [3, 25]; | ||
argOptions = null; | ||
search = !atWhitespace | ||
? value.slice(nodeStart, index).trim() | ||
: undefined; | ||
if (!previousNode) return [3, 14]; | ||
if (!(previousNode.kind === 'ARG_VALUE')) return [3, 13]; | ||
ref = previousNode.parent.ref; | ||
if (!Array.isArray(ref.options)) return [3, 21]; | ||
argOptions = ref.options; | ||
return [3, 24]; | ||
case 21: | ||
if (!(typeof ref.options === 'function')) return [3, 24]; | ||
if (!optionsCache[valueStart]) return [3, 22]; | ||
argOptions = optionsCache[valueStart]; | ||
return [3, 24]; | ||
case 22: return [4, ref.options(search || undefined)]; | ||
case 23: | ||
argOptions = _w.sent(); | ||
if (!(typeof ref.options === 'function' && !optionsCache[valueStart])) return [3, 13]; | ||
return [4, ref.options(search || undefined)]; | ||
case 12: | ||
argOptions = _f.sent(); | ||
if (current !== updatedAt) { | ||
@@ -423,79 +237,7 @@ return [2]; | ||
} | ||
_w.label = 24; | ||
case 24: | ||
if (argOptions) { | ||
options.push.apply(options, __spread(valueOptions({ | ||
options: argOptions, | ||
search: search, | ||
searchFn: searchFn, | ||
sliceStart: nodeStart, | ||
inputValue: value, | ||
}))); | ||
} | ||
return [3, 31]; | ||
case 25: | ||
if (!(((_s = previousNode) === null || _s === void 0 ? void 0 : _s.kind) === 'ARG_FLAG')) return [3, 26]; | ||
argsMap = previousNode.parent.ref.args; | ||
if (argsMap) { | ||
options.push.apply(options, __spread(argsOptions({ | ||
search: search, | ||
searchFn: searchFn, | ||
sliceStart: nodeStart, | ||
inputValue: value, | ||
args: argsMap, | ||
exclude: parsedArgKeys, | ||
}))); | ||
} | ||
commands = null; | ||
ref = previousNode.parent.ref; | ||
if (typeof ref.commands === 'object') { | ||
commands = ref.commands; | ||
} | ||
else if (typeof ref.commands === 'function') { | ||
commands = commandsCache[valueStart]; | ||
} | ||
if (commands) { | ||
options.push.apply(options, __spread(commandOptions({ | ||
searchFn: searchFn, | ||
sliceStart: nodeStart, | ||
search: search, | ||
inputValue: value, | ||
commands: commands, | ||
}))); | ||
} | ||
return [3, 31]; | ||
case 26: | ||
if (!(((_t = previousNode) === null || _t === void 0 ? void 0 : _t.kind) === 'ARG_KEY')) return [3, 31]; | ||
argOptions = null; | ||
ref = previousNode.parent.ref; | ||
if (!Array.isArray(ref.options)) return [3, 27]; | ||
argOptions = ref.options; | ||
return [3, 30]; | ||
case 27: | ||
if (!(typeof ref.options === 'function')) return [3, 30]; | ||
if (!optionsCache[valueStart]) return [3, 28]; | ||
argOptions = optionsCache[valueStart]; | ||
return [3, 30]; | ||
case 28: return [4, ref.options(search || undefined)]; | ||
case 29: | ||
argOptions = _w.sent(); | ||
if (current !== updatedAt) { | ||
return [2]; | ||
} | ||
if (argOptions) { | ||
optionsCache[valueStart] = argOptions; | ||
} | ||
_w.label = 30; | ||
case 30: | ||
if (argOptions) { | ||
options.push.apply(options, __spread(valueOptions({ | ||
options: argOptions, | ||
search: search, | ||
searchFn: searchFn, | ||
sliceStart: nodeStart, | ||
inputValue: value, | ||
}))); | ||
} | ||
_w.label = 31; | ||
case 31: | ||
_f.label = 13; | ||
case 13: | ||
options.push.apply(options, __spread(getOptions(__assign({ previousNode: previousNode }, commonParams)))); | ||
_f.label = 14; | ||
case 14: | ||
commandsList = astCommands.map(function (c) { | ||
@@ -518,6 +260,9 @@ var cargs = ast_1.toArgs(c); | ||
} | ||
if (current !== updatedAt) { | ||
return [2]; | ||
} | ||
config.onUpdate({ | ||
args: (_u = args) === null || _u === void 0 ? void 0 : _u.parsed, | ||
args: (_d = args) === null || _d === void 0 ? void 0 : _d.parsed, | ||
nodeStart: nodeStart, | ||
exhausted: !!((_v = args) === null || _v === void 0 ? void 0 : _v.exhausted) && !last.ref.commands, | ||
exhausted: !!((_e = args) === null || _e === void 0 ? void 0 : _e.exhausted) && !last.ref.commands, | ||
commands: commandsList, | ||
@@ -524,0 +269,0 @@ options: options, |
@@ -153,2 +153,10 @@ "use strict"; | ||
} | ||
else { | ||
ast.remainder = { | ||
kind: 'REMAINDER', | ||
token: token, | ||
cmdNodeCtx: cmdNodeCtx || undefined, | ||
argNodeCtx: argNodeCtx || undefined, | ||
}; | ||
} | ||
} | ||
@@ -155,0 +163,0 @@ else { |
@@ -8,2 +8,7 @@ export interface IOption<D = any> { | ||
} | ||
interface ISearchArgs { | ||
source: string; | ||
search: string; | ||
} | ||
export declare type SearchFn = (args: ISearchArgs) => boolean; | ||
export declare type ArgTypeDef = 'boolean' | 'string' | 'int' | 'float'; | ||
@@ -10,0 +15,0 @@ export interface IArgsOption { |
{ | ||
"name": "@replit/clui-input", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "A utility library for building CLI style interfaces with autocomplete", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
87890
29
1405