code
stringlengths 24
2.07M
| docstring
stringlengths 25
85.3k
| func_name
stringlengths 1
92
| language
stringclasses 1
value | repo
stringlengths 5
64
| path
stringlengths 4
172
| url
stringlengths 44
218
| license
stringclasses 7
values |
---|---|---|---|---|---|---|---|
autofill = (answers = {}) => {
return enquirer => {
let prompt = enquirer.prompt.bind(enquirer);
let context = { ...enquirer.answers, ...answers };
enquirer.prompt = async questions => {
let list = [].concat(questions || []);
let choices = [];
for (let item of list) {
let value = context[item.name];
if (value !== void 0) {
choices.push({ name: item.name, value, hint: `(${value})` });
}
}
if (choices.length) {
let values = await enquirer.prompt({
type: 'multiselect',
name: 'autofill',
message: 'Would you like to autofill prompts with the following values?',
choices
});
for (let item of list) {
if (values.autofill.includes(item.name)) {
item.initial = context[item.name];
}
}
if (enquirer.cancelled) {
return values;
}
}
return prompt(list);
};
};
}
|
Example "autofill" plugin - to achieve similar goal to autofill for web forms.
_This isn't really needed in Enquirer, since the `autofill` option does
effectively the same thing natively_. This is just an example.
|
autofill
|
javascript
|
enquirer/enquirer
|
examples/autofill-plugin.js
|
https://github.com/enquirer/enquirer/blob/master/examples/autofill-plugin.js
|
MIT
|
format() {
return prompt.input + ' ' + prompt.styles.muted(prompt.state.hint);
}
|
Example that shows all of the prompt elements displayed at once.
|
format
|
javascript
|
enquirer/enquirer
|
examples/everything.js
|
https://github.com/enquirer/enquirer/blob/master/examples/everything.js
|
MIT
|
pointer(state, choice, i) {
return (state.index === i ? state.symbols.pointer : ' ') + ' ';
}
|
Example that shows all of the prompt elements displayed at once.
|
pointer
|
javascript
|
enquirer/enquirer
|
examples/everything.js
|
https://github.com/enquirer/enquirer/blob/master/examples/everything.js
|
MIT
|
footer(state) {
if (state.limit < state.choices.length) {
return colors.dim('(Scroll up and down to reveal more choices)');
}
}
|
Example that shows all of the prompt elements displayed at once.
|
footer
|
javascript
|
enquirer/enquirer
|
examples/everything.js
|
https://github.com/enquirer/enquirer/blob/master/examples/everything.js
|
MIT
|
result(names) {
return this.map(names);
}
|
Example that shows all of the prompt elements displayed at once.
|
result
|
javascript
|
enquirer/enquirer
|
examples/everything.js
|
https://github.com/enquirer/enquirer/blob/master/examples/everything.js
|
MIT
|
constructor(options = {}) {
this.options = options;
}
|
Imagine this class does a search against an external api that returns a stream
of results. The results are used in the autocomplete suggest function below.
|
constructor
|
javascript
|
enquirer/enquirer
|
examples/autocomplete/option-suggest-streaming.js
|
https://github.com/enquirer/enquirer/blob/master/examples/autocomplete/option-suggest-streaming.js
|
MIT
|
search(filter) {
let events = new Events();
let i = 0;
if (this.interval) this.stop();
this.interval = setInterval(() => {
let choice = this.options.choices[i++];
if (choice && choice.includes(filter)) {
events.emit('data', choice);
}
}, this.options.interval);
this.timeout = setTimeout(() => {
clearInterval(this.interval);
this.interval = null;
events.emit('end');
}, this.options.timeout);
events.cancel = this.stop.bind(this);
return events;
}
|
Imagine this class does a search against an external api that returns a stream
of results. The results are used in the autocomplete suggest function below.
|
search
|
javascript
|
enquirer/enquirer
|
examples/autocomplete/option-suggest-streaming.js
|
https://github.com/enquirer/enquirer/blob/master/examples/autocomplete/option-suggest-streaming.js
|
MIT
|
stop() {
clearInterval(this.interval);
this.interval = null;
clearTimeout(this.timeout);
this.timeout = null;
}
|
Imagine this class does a search against an external api that returns a stream
of results. The results are used in the autocomplete suggest function below.
|
stop
|
javascript
|
enquirer/enquirer
|
examples/autocomplete/option-suggest-streaming.js
|
https://github.com/enquirer/enquirer/blob/master/examples/autocomplete/option-suggest-streaming.js
|
MIT
|
dispatch(ch) {
if (!ch) return this.alert();
this.input += ch;
this.cursor += 1;
this.render();
}
|
> Using a custom Prompt class as an Enquirer plugin
Custom prompt class - in this example, we use a custom prompt class
to show how to use a custom prompt as an Enquirer plugin.
This is necessary if you want Enquirer to be able to automatically run
your custom prompt when specified on the question "type".
|
dispatch
|
javascript
|
enquirer/enquirer
|
examples/enquirer/custom-prompt-plugin-class.js
|
https://github.com/enquirer/enquirer/blob/master/examples/enquirer/custom-prompt-plugin-class.js
|
MIT
|
delete() {
this.input = this.input.slice(0, -1);
this.cursor = this.input.length;
this.render();
}
|
> Using a custom Prompt class as an Enquirer plugin
Custom prompt class - in this example, we use a custom prompt class
to show how to use a custom prompt as an Enquirer plugin.
This is necessary if you want Enquirer to be able to automatically run
your custom prompt when specified on the question "type".
|
delete
|
javascript
|
enquirer/enquirer
|
examples/enquirer/custom-prompt-plugin-class.js
|
https://github.com/enquirer/enquirer/blob/master/examples/enquirer/custom-prompt-plugin-class.js
|
MIT
|
render() {
this.clear();
let prefix = this.style(this.symbols.prefix[this.state.status]);
let msg = this.styles.strong(this.state.message);
let sep = this.styles.muted(this.symbols.separator[this.state.status]);
let prompt = [prefix, msg, sep].filter(Boolean).join(' ');
this.write(prompt + ' ' + this.input);
}
|
> Using a custom Prompt class as an Enquirer plugin
Custom prompt class - in this example, we use a custom prompt class
to show how to use a custom prompt as an Enquirer plugin.
This is necessary if you want Enquirer to be able to automatically run
your custom prompt when specified on the question "type".
|
render
|
javascript
|
enquirer/enquirer
|
examples/enquirer/custom-prompt-plugin-class.js
|
https://github.com/enquirer/enquirer/blob/master/examples/enquirer/custom-prompt-plugin-class.js
|
MIT
|
customPrompt = (Prompt, enquirer) => {
class CustomInput extends Prompt {
dispatch(ch) {
if (!ch) return this.alert();
this.value = (this.value || '') + ch;
this.cursor += 1;
this.render();
}
delete() {
let value = this.value || '';
this.value = value ? value.slice(0, -1) : '';
this.cursor = value.length;
this.render();
}
async render() {
this.clear();
let value = this.value || '';
let message = await this.message();
this.write(`${message} ${value}`);
}
}
return CustomInput;
}
|
This example builds off of the "custom-prompt-plugin-class.js" example.
When you register a custom Prompt class as a plugin, you can optionally
wrap your class in a function with `Prompt` and `enquirer` params, where
`Prompt` is a the base Prompt class, and `enquirer` is an instance
of Enquirer.
|
customPrompt
|
javascript
|
enquirer/enquirer
|
examples/enquirer/custom-prompt-plugin-function.js
|
https://github.com/enquirer/enquirer/blob/master/examples/enquirer/custom-prompt-plugin-function.js
|
MIT
|
customPrompt = (Prompt, enquirer) => {
class CustomInput extends Prompt {
dispatch(ch) {
if (!ch) return this.alert();
this.value = (this.value || '') + ch;
this.cursor += 1;
this.render();
}
delete() {
let value = this.value || '';
this.value = value ? value.slice(0, -1) : '';
this.cursor = value.length;
this.render();
}
async render() {
this.clear();
let value = this.value || '';
let message = await this.message();
this.write(`${message} ${value}`);
}
}
return CustomInput;
}
|
This example builds off of the "custom-prompt-plugin-class.js" example.
When you register a custom Prompt class as a plugin, you can optionally
wrap your class in a function with `Prompt` and `enquirer` params, where
`Prompt` is a the base Prompt class, and `enquirer` is an instance
of Enquirer.
|
customPrompt
|
javascript
|
enquirer/enquirer
|
examples/enquirer/custom-prompt-plugin-function.js
|
https://github.com/enquirer/enquirer/blob/master/examples/enquirer/custom-prompt-plugin-function.js
|
MIT
|
dispatch(ch) {
if (!ch) return this.alert();
this.value = (this.value || '') + ch;
this.cursor += 1;
this.render();
}
|
This example builds off of the "custom-prompt-plugin-class.js" example.
When you register a custom Prompt class as a plugin, you can optionally
wrap your class in a function with `Prompt` and `enquirer` params, where
`Prompt` is a the base Prompt class, and `enquirer` is an instance
of Enquirer.
|
dispatch
|
javascript
|
enquirer/enquirer
|
examples/enquirer/custom-prompt-plugin-function.js
|
https://github.com/enquirer/enquirer/blob/master/examples/enquirer/custom-prompt-plugin-function.js
|
MIT
|
delete() {
let value = this.value || '';
this.value = value ? value.slice(0, -1) : '';
this.cursor = value.length;
this.render();
}
|
This example builds off of the "custom-prompt-plugin-class.js" example.
When you register a custom Prompt class as a plugin, you can optionally
wrap your class in a function with `Prompt` and `enquirer` params, where
`Prompt` is a the base Prompt class, and `enquirer` is an instance
of Enquirer.
|
delete
|
javascript
|
enquirer/enquirer
|
examples/enquirer/custom-prompt-plugin-function.js
|
https://github.com/enquirer/enquirer/blob/master/examples/enquirer/custom-prompt-plugin-function.js
|
MIT
|
async render() {
this.clear();
let value = this.value || '';
let message = await this.message();
this.write(`${message} ${value}`);
}
|
This example builds off of the "custom-prompt-plugin-class.js" example.
When you register a custom Prompt class as a plugin, you can optionally
wrap your class in a function with `Prompt` and `enquirer` params, where
`Prompt` is a the base Prompt class, and `enquirer` is an instance
of Enquirer.
|
render
|
javascript
|
enquirer/enquirer
|
examples/enquirer/custom-prompt-plugin-function.js
|
https://github.com/enquirer/enquirer/blob/master/examples/enquirer/custom-prompt-plugin-function.js
|
MIT
|
dispatch(ch) {
if (!ch) return this.alert();
this.value = (this.value || '') + ch;
this.cursor += 1;
this.render();
}
|
_Using a custom Prompt class_
Custom prompt class - in this example, we create custom prompt by
extending the built-in Prompt class (the base class used by all
Enquirer prompts).
Here we use this custom prompt as a standalone prompt, but you can
also register prompts as plugins on enquirer
(see "custom-prompt-plugin-class.js" example for more details).
|
dispatch
|
javascript
|
enquirer/enquirer
|
examples/enquirer/custom-prompt-standalone.js
|
https://github.com/enquirer/enquirer/blob/master/examples/enquirer/custom-prompt-standalone.js
|
MIT
|
delete() {
let value = this.value || '';
this.value = value ? value.slice(0, -1) : '';
this.cursor = value.length;
this.render();
}
|
_Using a custom Prompt class_
Custom prompt class - in this example, we create custom prompt by
extending the built-in Prompt class (the base class used by all
Enquirer prompts).
Here we use this custom prompt as a standalone prompt, but you can
also register prompts as plugins on enquirer
(see "custom-prompt-plugin-class.js" example for more details).
|
delete
|
javascript
|
enquirer/enquirer
|
examples/enquirer/custom-prompt-standalone.js
|
https://github.com/enquirer/enquirer/blob/master/examples/enquirer/custom-prompt-standalone.js
|
MIT
|
async render() {
this.clear();
let value = this.value || '';
let message = await this.message();
this.write(`${message} ${value}`);
}
|
_Using a custom Prompt class_
Custom prompt class - in this example, we create custom prompt by
extending the built-in Prompt class (the base class used by all
Enquirer prompts).
Here we use this custom prompt as a standalone prompt, but you can
also register prompts as plugins on enquirer
(see "custom-prompt-plugin-class.js" example for more details).
|
render
|
javascript
|
enquirer/enquirer
|
examples/enquirer/custom-prompt-standalone.js
|
https://github.com/enquirer/enquirer/blob/master/examples/enquirer/custom-prompt-standalone.js
|
MIT
|
input = (name, message, initial) => prompt => {
const p = new Input({ name, message, initial });
return p.run().then(value => ({ name, message, initial: value, value }));
}
|
First, let's wrap the Input prompt to cut down on boilerplate,
since we know in advance that we only need to define a few options.
|
input
|
javascript
|
enquirer/enquirer
|
examples/form/async-choices.js
|
https://github.com/enquirer/enquirer/blob/master/examples/form/async-choices.js
|
MIT
|
input = (name, message, initial) => prompt => {
const p = new Input({ name, message, initial });
return p.run().then(value => ({ name, message, initial: value, value }));
}
|
First, let's wrap the Input prompt to cut down on boilerplate,
since we know in advance that we only need to define a few options.
|
input
|
javascript
|
enquirer/enquirer
|
examples/form/async-choices.js
|
https://github.com/enquirer/enquirer/blob/master/examples/form/async-choices.js
|
MIT
|
header() {
return `${dim('You have')} ${color(time)} ${dim('seconds left to answer!')}`;
}
|
This example shows how to create a "countdown" effect. You can
put the countdown timer in the prefix, footer, header, separator,
or whatever prompt position makes sense for your goal.
|
header
|
javascript
|
enquirer/enquirer
|
examples/fun/countdown.js
|
https://github.com/enquirer/enquirer/blob/master/examples/fun/countdown.js
|
MIT
|
separator() {
return ''; // hide separator
}
|
This example shows how to create a "countdown" effect. You can
put the countdown timer in the prefix, footer, header, separator,
or whatever prompt position makes sense for your goal.
|
separator
|
javascript
|
enquirer/enquirer
|
examples/fun/countdown.js
|
https://github.com/enquirer/enquirer/blob/master/examples/fun/countdown.js
|
MIT
|
message(state) {
if (state.submitted && !state.input) return 'Really? Your own name?';
return state.submitted ? 'Well done,' : 'What is your full name!!!';
}
|
This example shows how to create a "countdown" effect. You can
put the countdown timer in the prefix, footer, header, separator,
or whatever prompt position makes sense for your goal.
|
message
|
javascript
|
enquirer/enquirer
|
examples/fun/countdown.js
|
https://github.com/enquirer/enquirer/blob/master/examples/fun/countdown.js
|
MIT
|
pointer(state, choice, i) {
return state.index === i ? frame(colors, state.timer.tick) + ' ' : ' ';
}
|
This examples shows how to use the `timers` option to
create multiple heartbeat effects in different positions.
|
pointer
|
javascript
|
enquirer/enquirer
|
examples/fun/heartbeats.js
|
https://github.com/enquirer/enquirer/blob/master/examples/fun/heartbeats.js
|
MIT
|
timeout = (fn, ms = 0) => {
return new Promise((resolve, reject) => {
setTimeout(() => fn().then(resolve).catch(reject), ms);
});
}
|
This examples shows how to "play" an array of keypresses.
|
timeout
|
javascript
|
enquirer/enquirer
|
examples/fun/play-steps.js
|
https://github.com/enquirer/enquirer/blob/master/examples/fun/play-steps.js
|
MIT
|
timeout = (fn, ms = 0) => {
return new Promise((resolve, reject) => {
setTimeout(() => fn().then(resolve).catch(reject), ms);
});
}
|
This examples shows how to "play" an array of keypresses.
|
timeout
|
javascript
|
enquirer/enquirer
|
examples/fun/play-steps.js
|
https://github.com/enquirer/enquirer/blob/master/examples/fun/play-steps.js
|
MIT
|
footer() {
let fn = colors[keys[++idx % keys.length]];
return '\n' + fn('(Scroll up and down to reveal more choices)');
}
|
This prompt shows how you can easily customize the footer
to render a different value based on conditions.
|
footer
|
javascript
|
enquirer/enquirer
|
examples/select/select-long.js
|
https://github.com/enquirer/enquirer/blob/master/examples/select/select-long.js
|
MIT
|
constructor(token) {
this.name = token.key;
this.field = token.field || {};
this.value = clean(token.initial || this.field.initial || '');
this.message = token.message || this.name;
this.cursor = 0;
this.input = '';
this.lines = [];
}
|
This file contains the interpolation and rendering logic for
the Snippet prompt.
|
constructor
|
javascript
|
enquirer/enquirer
|
lib/interpolate.js
|
https://github.com/enquirer/enquirer/blob/master/lib/interpolate.js
|
MIT
|
tokenize = async(options = {}, defaults = {}, fn = token => token) => {
let unique = new Set();
let fields = options.fields || [];
let input = options.template;
let tabstops = [];
let items = [];
let keys = [];
let line = 1;
if (typeof input === 'function') {
input = await input();
}
let i = -1;
let next = () => input[++i];
let peek = () => input[i + 1];
let push = token => {
token.line = line;
tabstops.push(token);
};
push({ type: 'bos', value: '' });
while (i < input.length - 1) {
let value = next();
if (/^[^\S\n ]$/.test(value)) {
push({ type: 'text', value });
continue;
}
if (value === '\n') {
push({ type: 'newline', value });
line++;
continue;
}
if (value === '\\') {
value += next();
push({ type: 'text', value });
continue;
}
if ((value === '$' || value === '#' || value === '{') && peek() === '{') {
let n = next();
value += n;
let token = { type: 'template', open: value, inner: '', close: '', value };
let ch;
while ((ch = next())) {
if (ch === '}') {
if (peek() === '}') ch += next();
token.value += ch;
token.close = ch;
break;
}
if (ch === ':') {
token.initial = '';
token.key = token.inner;
} else if (token.initial !== void 0) {
token.initial += ch;
}
token.value += ch;
token.inner += ch;
}
token.template = token.open + (token.initial || token.inner) + token.close;
token.key = token.key || token.inner;
if (hasOwnProperty.call(defaults, token.key)) {
token.initial = defaults[token.key];
}
token = fn(token);
push(token);
keys.push(token.key);
unique.add(token.key);
let item = items.find(item => item.name === token.key);
token.field = fields.find(ch => ch.name === token.key);
if (!item) {
item = new Item(token);
items.push(item);
}
item.lines.push(token.line - 1);
continue;
}
let last = tabstops[tabstops.length - 1];
if (last.type === 'text' && last.line === line) {
last.value += value;
} else {
push({ type: 'text', value });
}
}
push({ type: 'eos', value: '' });
return { input, tabstops, unique, keys, items };
}
|
This file contains the interpolation and rendering logic for
the Snippet prompt.
|
tokenize
|
javascript
|
enquirer/enquirer
|
lib/interpolate.js
|
https://github.com/enquirer/enquirer/blob/master/lib/interpolate.js
|
MIT
|
tokenize = async(options = {}, defaults = {}, fn = token => token) => {
let unique = new Set();
let fields = options.fields || [];
let input = options.template;
let tabstops = [];
let items = [];
let keys = [];
let line = 1;
if (typeof input === 'function') {
input = await input();
}
let i = -1;
let next = () => input[++i];
let peek = () => input[i + 1];
let push = token => {
token.line = line;
tabstops.push(token);
};
push({ type: 'bos', value: '' });
while (i < input.length - 1) {
let value = next();
if (/^[^\S\n ]$/.test(value)) {
push({ type: 'text', value });
continue;
}
if (value === '\n') {
push({ type: 'newline', value });
line++;
continue;
}
if (value === '\\') {
value += next();
push({ type: 'text', value });
continue;
}
if ((value === '$' || value === '#' || value === '{') && peek() === '{') {
let n = next();
value += n;
let token = { type: 'template', open: value, inner: '', close: '', value };
let ch;
while ((ch = next())) {
if (ch === '}') {
if (peek() === '}') ch += next();
token.value += ch;
token.close = ch;
break;
}
if (ch === ':') {
token.initial = '';
token.key = token.inner;
} else if (token.initial !== void 0) {
token.initial += ch;
}
token.value += ch;
token.inner += ch;
}
token.template = token.open + (token.initial || token.inner) + token.close;
token.key = token.key || token.inner;
if (hasOwnProperty.call(defaults, token.key)) {
token.initial = defaults[token.key];
}
token = fn(token);
push(token);
keys.push(token.key);
unique.add(token.key);
let item = items.find(item => item.name === token.key);
token.field = fields.find(ch => ch.name === token.key);
if (!item) {
item = new Item(token);
items.push(item);
}
item.lines.push(token.line - 1);
continue;
}
let last = tabstops[tabstops.length - 1];
if (last.type === 'text' && last.line === line) {
last.value += value;
} else {
push({ type: 'text', value });
}
}
push({ type: 'eos', value: '' });
return { input, tabstops, unique, keys, items };
}
|
This file contains the interpolation and rendering logic for
the Snippet prompt.
|
tokenize
|
javascript
|
enquirer/enquirer
|
lib/interpolate.js
|
https://github.com/enquirer/enquirer/blob/master/lib/interpolate.js
|
MIT
|
push = token => {
token.line = line;
tabstops.push(token);
}
|
This file contains the interpolation and rendering logic for
the Snippet prompt.
|
push
|
javascript
|
enquirer/enquirer
|
lib/interpolate.js
|
https://github.com/enquirer/enquirer/blob/master/lib/interpolate.js
|
MIT
|
push = token => {
token.line = line;
tabstops.push(token);
}
|
This file contains the interpolation and rendering logic for
the Snippet prompt.
|
push
|
javascript
|
enquirer/enquirer
|
lib/interpolate.js
|
https://github.com/enquirer/enquirer/blob/master/lib/interpolate.js
|
MIT
|
validate = async(value, state, item, index) => {
let error = await isValid(value, state, item, index);
if (error === false) {
return 'Invalid field ' + item.name;
}
return error;
}
|
This file contains the interpolation and rendering logic for
the Snippet prompt.
|
validate
|
javascript
|
enquirer/enquirer
|
lib/interpolate.js
|
https://github.com/enquirer/enquirer/blob/master/lib/interpolate.js
|
MIT
|
validate = async(value, state, item, index) => {
let error = await isValid(value, state, item, index);
if (error === false) {
return 'Invalid field ' + item.name;
}
return error;
}
|
This file contains the interpolation and rendering logic for
the Snippet prompt.
|
validate
|
javascript
|
enquirer/enquirer
|
lib/interpolate.js
|
https://github.com/enquirer/enquirer/blob/master/lib/interpolate.js
|
MIT
|
function createFn(prop, prompt, options, fallback) {
return (value, state, item, index) => {
if (typeof item.field[prop] === 'function') {
return item.field[prop].call(prompt, value, state, item, index);
}
return [fallback, value].find(v => prompt.isValue(v));
};
}
|
This file contains the interpolation and rendering logic for
the Snippet prompt.
|
createFn
|
javascript
|
enquirer/enquirer
|
lib/interpolate.js
|
https://github.com/enquirer/enquirer/blob/master/lib/interpolate.js
|
MIT
|
constructor(options = {}) {
super();
this.name = options.name;
this.type = options.type;
this.options = options;
theme(this);
timer(this);
this.state = new State(this);
this.initial = [options.initial, options.default].find(v => v != null);
this.stdout = options.stdout || process.stdout;
this.stdin = options.stdin || process.stdin;
this.scale = options.scale || 1;
this.term = this.options.term || process.env.TERM_PROGRAM;
this.margin = margin(this.options.margin);
this.setMaxListeners(0);
setOptions(this);
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
constructor
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
async keypress(input, event = {}) {
this.keypressed = true;
let key = keypress.action(input, keypress(input, event), this.options.actions);
this.state.keypress = key;
this.emit('keypress', input, key);
this.emit('state', this.state.clone());
const fn = this.options[key.action] || this[key.action] || this.dispatch;
if (typeof fn === 'function') {
return await fn.call(this, input, key);
}
this.alert();
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
keypress
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
alert() {
delete this.state.alert;
if (this.options.show === false) {
this.emit('alert');
} else {
this.stdout.write(ansi.code.beep);
}
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
alert
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
cursorHide() {
this.stdout.write(ansi.cursor.hide());
const releaseOnExit = utils.onExit(() => this.cursorShow());
this.on('close', () => {
this.cursorShow();
releaseOnExit();
});
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
cursorHide
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
write(str) {
if (!str) return;
if (this.stdout && this.state.show !== false) {
this.stdout.write(str);
}
this.state.buffer += str;
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
write
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
clear(lines = 0) {
let buffer = this.state.buffer;
this.state.buffer = '';
if ((!buffer && !lines) || this.options.show === false) return;
this.stdout.write(ansi.cursor.down(lines) + ansi.clear(buffer, this.width));
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
clear
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
restore() {
if (this.state.closed || this.options.show === false) return;
let { prompt, after, rest } = this.sections();
let { cursor, initial = '', input = '', value = '' } = this;
let size = this.state.size = rest.length;
let state = { after, cursor, initial, input, prompt, size, value };
let codes = ansi.cursor.restore(state);
if (codes) {
this.stdout.write(codes);
}
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
restore
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
sections() {
let { buffer, input, prompt } = this.state;
prompt = stripAnsi(prompt);
let buf = stripAnsi(buffer);
let idx = buf.indexOf(prompt);
let header = buf.slice(0, idx);
let rest = buf.slice(idx);
let lines = rest.split('\n');
let first = lines[0];
let last = lines[lines.length - 1];
let promptLine = prompt + (input ? ' ' + input : '');
let len = promptLine.length;
let after = len < first.length ? first.slice(len + 1) : '';
return { header, prompt: first, after, rest: lines.slice(1), last };
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
sections
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
async submit() {
this.state.submitted = true;
this.state.validating = true;
// this will only be called when the prompt is directly submitted
// without initializing, i.e. when the prompt is skipped, etc. Otherwize,
// "options.onSubmit" is will be handled by the "initialize()" method.
if (this.options.onSubmit) {
await this.options.onSubmit.call(this, this.name, this.value, this);
}
let result = this.state.error || await this.validate(this.value, this.state);
if (result !== true) {
let error = '\n' + this.symbols.pointer + ' ';
if (typeof result === 'string') {
error += result.trim();
} else {
error += 'Invalid input';
}
this.state.error = '\n' + this.styles.danger(error);
this.state.submitted = false;
await this.render();
await this.alert();
this.state.validating = false;
this.state.error = void 0;
return;
}
this.state.validating = false;
await this.render();
await this.close();
this.value = await this.result(this.value);
this.emit('submit', this.value);
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
submit
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
async cancel(err) {
this.state.cancelled = this.state.submitted = true;
await this.render();
await this.close();
if (typeof this.options.onCancel === 'function') {
await this.options.onCancel.call(this, this.name, this.value, this);
}
this.emit('cancel', await this.error(err));
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
cancel
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
async close() {
this.state.closed = true;
try {
let sections = this.sections();
let lines = Math.ceil(sections.prompt.length / this.width);
if (sections.rest) {
this.write(ansi.cursor.down(sections.rest.length));
}
this.write('\n'.repeat(lines));
} catch (err) { /* do nothing */ }
this.emit('close');
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
close
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
start() {
if (!this.stop && this.options.show !== false) {
this.stop = keypress.listen(this, this.keypress.bind(this));
this.once('close', this.stop);
this.emit('start', this);
}
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
start
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
async skip() {
this.skipped = this.options.skip === true;
if (typeof this.options.skip === 'function') {
this.skipped = await this.options.skip.call(this, this.name, this.value);
}
return this.skipped;
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
skip
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
async initialize() {
let { format, options, result } = this;
this.format = () => format.call(this, this.value);
this.result = () => result.call(this, this.value);
if (typeof options.initial === 'function') {
this.initial = await options.initial.call(this, this);
}
if (typeof options.onRun === 'function') {
await options.onRun.call(this, this);
}
// if "options.onSubmit" is defined, we wrap the "submit" method to guarantee
// that "onSubmit" will always called first thing inside the submit
// method, regardless of how it's handled in inheriting prompts.
if (typeof options.onSubmit === 'function') {
let onSubmit = options.onSubmit.bind(this);
let submit = this.submit.bind(this);
delete this.options.onSubmit;
this.submit = async() => {
await onSubmit(this.name, this.value, this);
return submit();
};
}
await this.start();
await this.render();
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
initialize
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
render() {
throw new Error('expected prompt to have a custom render method');
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
render
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
run() {
return new Promise(async(resolve, reject) => {
this.once('submit', resolve);
this.once('cancel', reject);
if (await this.skip()) {
this.render = () => {};
return this.submit();
}
await this.initialize();
this.emit('run');
});
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
run
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
async element(name, choice, i) {
let { options, state, symbols, timers } = this;
let timer = timers && timers[name];
state.timer = timer;
let value = options[name] || state[name] || symbols[name];
let val = choice && choice[name] != null ? choice[name] : await value;
if (val === '') return val;
let res = await this.resolve(val, state, choice, i);
if (!res && choice && choice[name]) {
return this.resolve(value, state, choice, i);
}
return res;
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
element
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
async prefix() {
let element = await this.element('prefix') || this.symbols;
let timer = this.timers && this.timers.prefix;
let state = this.state;
state.timer = timer;
if (utils.isObject(element)) element = element[state.status] || element.pending;
if (!utils.hasColor(element)) {
let style = this.styles[state.status] || this.styles.pending;
return style(element);
}
return element;
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
prefix
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
async message() {
let message = await this.element('message');
if (!utils.hasColor(message)) {
return this.styles.strong(message);
}
return message;
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
message
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
async separator() {
let element = await this.element('separator') || this.symbols;
let timer = this.timers && this.timers.separator;
let state = this.state;
state.timer = timer;
let value = element[state.status] || element.pending || state.separator;
let ele = await this.resolve(value, state);
if (utils.isObject(ele)) ele = ele[state.status] || ele.pending;
if (!utils.hasColor(ele)) {
return this.styles.muted(ele);
}
return ele;
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
separator
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
async pointer(choice, i) {
let val = await this.element('pointer', choice, i);
if (typeof val === 'string' && utils.hasColor(val)) {
return val;
}
if (val) {
let styles = this.styles;
let focused = this.index === i;
let style = focused ? styles.primary : val => val;
let ele = await this.resolve(val[focused ? 'on' : 'off'] || val, this.state);
let styled = !utils.hasColor(ele) ? style(ele) : ele;
return focused ? styled : ' '.repeat(ele.length);
}
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
pointer
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
async indicator(choice, i) {
let val = await this.element('indicator', choice, i);
if (typeof val === 'string' && utils.hasColor(val)) {
return val;
}
if (val) {
let styles = this.styles;
let enabled = choice.enabled === true;
let style = enabled ? styles.success : styles.dark;
let ele = val[enabled ? 'on' : 'off'] || val;
return !utils.hasColor(ele) ? style(ele) : ele;
}
return '';
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
indicator
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
body() {
return null;
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
body
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
footer() {
if (this.state.status === 'pending') {
return this.element('footer');
}
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
footer
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
header() {
if (this.state.status === 'pending') {
return this.element('header');
}
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
header
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
async hint() {
if (this.state.status === 'pending' && !this.isValue(this.state.input)) {
let hint = await this.element('hint');
if (!utils.hasColor(hint)) {
return this.styles.muted(hint);
}
return hint;
}
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
hint
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
error(err) {
return !this.state.submitted ? (err || this.state.error) : '';
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
error
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
format(value) {
return value;
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
format
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
result(value) {
return value;
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
result
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
validate(value) {
if (this.options.required === true) {
return this.isValue(value);
}
return true;
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
validate
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
isValue(value) {
return value != null && value !== '';
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
isValue
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
resolve(value, ...args) {
return utils.resolve(this, value, ...args);
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
resolve
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
get base() {
return Prompt.prototype;
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
base
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
get style() {
return this.styles[this.state.status];
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
style
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
get height() {
return this.options.rows || utils.height(this.stdout, 25);
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
height
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
get width() {
return this.options.columns || utils.width(this.stdout, 80);
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
width
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
get size() {
return { width: this.width, height: this.height };
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
size
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
set cursor(value) {
this.state.cursor = value;
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
cursor
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
get cursor() {
return this.state.cursor;
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
cursor
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
set input(value) {
this.state.input = value;
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
input
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
get input() {
return this.state.input;
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
input
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
set value(value) {
this.state.value = value;
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
value
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
get value() {
let { input, value } = this.state;
let result = [value, input].find(this.isValue.bind(this));
return this.isValue(result) ? result : this.initial;
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
value
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
static get prompt() {
return options => new this(options).run();
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
prompt
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
function setOptions(prompt) {
let isValidKey = key => {
return prompt[key] === void 0 || typeof prompt[key] === 'function';
};
let ignore = [
'actions',
'choices',
'initial',
'margin',
'roles',
'styles',
'symbols',
'theme',
'timers',
'value'
];
let ignoreFn = [
'body',
'footer',
'error',
'header',
'hint',
'indicator',
'message',
'prefix',
'separator',
'skip'
];
for (let key of Object.keys(prompt.options)) {
if (ignore.includes(key)) continue;
if (/^on[A-Z]/.test(key)) continue;
let option = prompt.options[key];
if (typeof option === 'function' && isValidKey(key)) {
if (!ignoreFn.includes(key)) {
prompt[key] = option.bind(prompt);
}
} else if (typeof prompt[key] !== 'function') {
prompt[key] = option;
}
}
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
setOptions
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
isValidKey = key => {
return prompt[key] === void 0 || typeof prompt[key] === 'function';
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
isValidKey
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
isValidKey = key => {
return prompt[key] === void 0 || typeof prompt[key] === 'function';
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
isValidKey
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
function margin(value) {
if (typeof value === 'number') {
value = [value, value, value, value];
}
let arr = [].concat(value || []);
let pad = i => i % 2 === 0 ? '\n' : ' ';
let res = [];
for (let i = 0; i < 4; i++) {
let char = pad(i);
if (arr[i]) {
res.push(char.repeat(arr[i]));
} else {
res.push('');
}
}
return res;
}
|
Base class for creating a new Prompt.
@param {Object} `options` Question object.
|
margin
|
javascript
|
enquirer/enquirer
|
lib/prompt.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompt.js
|
MIT
|
onExit = (quit, code) => {
if (onExitCalled) return;
onExitCalled = true;
onExitCallbacks.forEach(fn => fn());
if (quit === true) {
process.exit(128 + code);
}
}
|
Get a value from the given object.
@param {Object} obj
@param {String} prop
|
onExit
|
javascript
|
enquirer/enquirer
|
lib/utils.js
|
https://github.com/enquirer/enquirer/blob/master/lib/utils.js
|
MIT
|
onExit = (quit, code) => {
if (onExitCalled) return;
onExitCalled = true;
onExitCallbacks.forEach(fn => fn());
if (quit === true) {
process.exit(128 + code);
}
}
|
Get a value from the given object.
@param {Object} obj
@param {String} prop
|
onExit
|
javascript
|
enquirer/enquirer
|
lib/utils.js
|
https://github.com/enquirer/enquirer/blob/master/lib/utils.js
|
MIT
|
set(val) {
custom = val;
}
|
Get a value from the given object.
@param {Object} obj
@param {String} prop
|
set
|
javascript
|
enquirer/enquirer
|
lib/utils.js
|
https://github.com/enquirer/enquirer/blob/master/lib/utils.js
|
MIT
|
get() {
return custom ? custom() : fn();
}
|
Get a value from the given object.
@param {Object} obj
@param {String} prop
|
get
|
javascript
|
enquirer/enquirer
|
lib/utils.js
|
https://github.com/enquirer/enquirer/blob/master/lib/utils.js
|
MIT
|
renderScaleKey() {
if (this.scaleKey === false) return '';
if (this.state.submitted) return '';
let scale = this.scale.map(item => ` ${item.name} - ${item.message}`);
let key = ['', ...scale].map(item => this.styles.muted(item));
return key.join('\n');
}
|
Render the scale "Key". Something like:
@return {String}
|
renderScaleKey
|
javascript
|
enquirer/enquirer
|
lib/prompts/scale.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompts/scale.js
|
MIT
|
renderScaleHeading(max) {
let keys = this.scale.map(ele => ele.name);
if (typeof this.options.renderScaleHeading === 'function') {
keys = this.options.renderScaleHeading.call(this, max);
}
let diff = this.scaleLength - keys.join('').length;
let spacing = Math.round(diff / (keys.length - 1));
let names = keys.map(key => this.styles.strong(key));
let headings = names.join(' '.repeat(spacing));
let padding = ' '.repeat(this.widths[0]);
return this.margin[3] + padding + this.margin[1] + headings;
}
|
Render the heading row for the scale.
@return {String}
|
renderScaleHeading
|
javascript
|
enquirer/enquirer
|
lib/prompts/scale.js
|
https://github.com/enquirer/enquirer/blob/master/lib/prompts/scale.js
|
MIT
|
highlight(code, lang) {
let hljs = require('highlight.js');
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(lang, code).value;
} catch (err) {}
}
try {
return hljs.highlightAuto(code).value;
} catch (err) {}
return code;
}
|
Default options for highlight.js markdown plugin
|
highlight
|
javascript
|
enquirer/enquirer
|
support/build/defaults.js
|
https://github.com/enquirer/enquirer/blob/master/support/build/defaults.js
|
MIT
|
function getDerivative(derivative, t, vs) {
// the derivative of any 't'-less function is zero.
var n = vs.length - 1,
_vs,
value,
k;
if (n === 0) {
return 0;
}
// direct values? compute!
if (derivative === 0) {
value = 0;
for (k = 0; k <= n; k++) {
value += binomials(n, k) * Math.pow(1 - t, n - k) * Math.pow(t, k) * vs[k];
}
return value;
} else {
// Still some derivative? go down one order, then try
// for the lower order curve's.
_vs = new Array(n);
for (k = 0; k < n; k++) {
_vs[k] = n * (vs[k + 1] - vs[k]);
}
return getDerivative(derivative - 1, t, _vs);
}
}
|
Compute the curve derivative (hodograph) at t.
|
getDerivative
|
javascript
|
veltman/flubber
|
build/flubber.js
|
https://github.com/veltman/flubber/blob/master/build/flubber.js
|
MIT
|
function B(xs, ys, t) {
var xbase = getDerivative(1, t, xs);
var ybase = getDerivative(1, t, ys);
var combined = xbase * xbase + ybase * ybase;
return Math.sqrt(combined);
}
|
Compute the curve derivative (hodograph) at t.
|
B
|
javascript
|
veltman/flubber
|
build/flubber.js
|
https://github.com/veltman/flubber/blob/master/build/flubber.js
|
MIT
|
function getCubicArcLength(xs, ys, t) {
var z, sum, i, correctedT;
/*if (xs.length >= tValues.length) {
throw new Error('too high n bezier');
}*/
if (t === undefined) {
t = 1;
}
var n = 20;
z = t / 2;
sum = 0;
for (i = 0; i < n; i++) {
correctedT = z * tValues[n][i] + z;
sum += cValues[n][i] * B(xs, ys, correctedT);
}
return z * sum;
}
|
Compute the curve derivative (hodograph) at t.
|
getCubicArcLength
|
javascript
|
veltman/flubber
|
build/flubber.js
|
https://github.com/veltman/flubber/blob/master/build/flubber.js
|
MIT
|
function unit_vector_angle$1(ux, uy, vx, vy) {
var sign = (ux * vy - uy * vx < 0) ? -1 : 1;
var dot = ux * vx + uy * vy;
// Add this to work with arbitrary vectors:
// dot /= Math.sqrt(ux * ux + uy * uy) * Math.sqrt(vx * vx + vy * vy);
// rounding errors, e.g. -1.0000000000000002 can screw up this
if (dot > 1.0) { dot = 1.0; }
if (dot < -1.0) { dot = -1.0; }
return sign * Math.acos(dot);
}
|
Compute the curve derivative (hodograph) at t.
|
unit_vector_angle$1
|
javascript
|
veltman/flubber
|
build/flubber.js
|
https://github.com/veltman/flubber/blob/master/build/flubber.js
|
MIT
|
function get_arc_center$1(x1, y1, x2, y2, fa, fs, rx, ry, sin_phi, cos_phi) {
// Step 1.
//
// Moving an ellipse so origin will be the middlepoint between our two
// points. After that, rotate it to line up ellipse axes with coordinate
// axes.
//
var x1p = cos_phi*(x1-x2)/2 + sin_phi*(y1-y2)/2;
var y1p = -sin_phi*(x1-x2)/2 + cos_phi*(y1-y2)/2;
var rx_sq = rx * rx;
var ry_sq = ry * ry;
var x1p_sq = x1p * x1p;
var y1p_sq = y1p * y1p;
// Step 2.
//
// Compute coordinates of the centre of this ellipse (cx', cy')
// in the new coordinate system.
//
var radicant = (rx_sq * ry_sq) - (rx_sq * y1p_sq) - (ry_sq * x1p_sq);
if (radicant < 0) {
// due to rounding errors it might be e.g. -1.3877787807814457e-17
radicant = 0;
}
radicant /= (rx_sq * y1p_sq) + (ry_sq * x1p_sq);
radicant = Math.sqrt(radicant) * (fa === fs ? -1 : 1);
var cxp = radicant * rx/ry * y1p;
var cyp = radicant * -ry/rx * x1p;
// Step 3.
//
// Transform back to get centre coordinates (cx, cy) in the original
// coordinate system.
//
var cx = cos_phi*cxp - sin_phi*cyp + (x1+x2)/2;
var cy = sin_phi*cxp + cos_phi*cyp + (y1+y2)/2;
// Step 4.
//
// Compute angles (theta1, delta_theta).
//
var v1x = (x1p - cxp) / rx;
var v1y = (y1p - cyp) / ry;
var v2x = (-x1p - cxp) / rx;
var v2y = (-y1p - cyp) / ry;
var theta1 = unit_vector_angle$1(1, 0, v1x, v1y);
var delta_theta = unit_vector_angle$1(v1x, v1y, v2x, v2y);
if (fs === 0 && delta_theta > 0) {
delta_theta -= TAU$1;
}
if (fs === 1 && delta_theta < 0) {
delta_theta += TAU$1;
}
return [ cx, cy, theta1, delta_theta ];
}
|
Compute the curve derivative (hodograph) at t.
|
get_arc_center$1
|
javascript
|
veltman/flubber
|
build/flubber.js
|
https://github.com/veltman/flubber/blob/master/build/flubber.js
|
MIT
|
function approximate_unit_arc$1(theta1, delta_theta) {
var alpha = 4/3 * Math.tan(delta_theta/4);
var x1 = Math.cos(theta1);
var y1 = Math.sin(theta1);
var x2 = Math.cos(theta1 + delta_theta);
var y2 = Math.sin(theta1 + delta_theta);
return [ x1, y1, x1 - y1*alpha, y1 + x1*alpha, x2 + y2*alpha, y2 - x2*alpha, x2, y2 ];
}
|
Compute the curve derivative (hodograph) at t.
|
approximate_unit_arc$1
|
javascript
|
veltman/flubber
|
build/flubber.js
|
https://github.com/veltman/flubber/blob/master/build/flubber.js
|
MIT
|
Arc = function(x0, y0, rx,ry, xAxisRotate, LargeArcFlag,SweepFlag, x,y) {
return new Arc$1(x0, y0, rx,ry, xAxisRotate, LargeArcFlag,SweepFlag, x,y);
}
|
Compute the curve derivative (hodograph) at t.
|
Arc
|
javascript
|
veltman/flubber
|
build/flubber.js
|
https://github.com/veltman/flubber/blob/master/build/flubber.js
|
MIT
|
function Arc$1(x0, y0,rx,ry, xAxisRotate, LargeArcFlag,SweepFlag,x,y) {
var length = 0;
var partialLengths = [];
var curves = [];
var res = a2c$3(x0, y0,rx,ry, xAxisRotate, LargeArcFlag,SweepFlag,x,y);
res.forEach(function(d){
var curve = new Bezier(d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7]);
var curveLength = curve.getTotalLength();
length += curveLength;
partialLengths.push(curveLength);
curves.push(curve);
});
this.length = length;
this.partialLengths = partialLengths;
this.curves = curves;
}
|
Compute the curve derivative (hodograph) at t.
|
Arc$1
|
javascript
|
veltman/flubber
|
build/flubber.js
|
https://github.com/veltman/flubber/blob/master/build/flubber.js
|
MIT
|
LinearPosition = function(x0, x1, y0, y1) {
return new LinearPosition$1(x0, x1, y0, y1);
}
|
Compute the curve derivative (hodograph) at t.
|
LinearPosition
|
javascript
|
veltman/flubber
|
build/flubber.js
|
https://github.com/veltman/flubber/blob/master/build/flubber.js
|
MIT
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.