From 5ad8348c8ceec9d8ec374d6261e41abeef4da83e Mon Sep 17 00:00:00 2001 From: max furman Date: Fri, 2 Nov 2018 21:00:14 -0700 Subject: [PATCH] add inputDelay feature Delay between prompt and input in ms. --- README.md | 5 +++-- termynal.js | 16 ++++++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 9397a6a..9cc1709 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ The following settings are available: | `startDelay` | number | `600` | Delay before animation, in ms. | | `typeDelay` | number | `90` | Delay between each typed character, in ms. | | `lineDelay` | number | `1500` | Delay between each line, in ms. | +| `inputDelay` | number | `0` | Delay between prompt and input, in ms. | | `progressLength` | number | `40` | Number of characters displayed as progress bar. | | `progressChar` | string | `'█'` | Character to use for progress bar. | | `cursor` | string | `'▋'` | Character to use for cursor. | @@ -157,9 +158,9 @@ var termynal = new Termynal('#termynal', lineData: [ { type: 'input', value: 'pip install spacy' }, { value: 'Are you sure you want to install \'spaCy\'?' }, - { type: 'input', typeDelay: 1000, prompt: '(y/n)', value: 'y' }, + { type: 'input', inputDelay: 1000, prompt: '(y/n)', value: 'y' }, { delay: 1000, value: 'Installing spaCy...' } ] } ) -``` \ No newline at end of file +``` diff --git a/termynal.js b/termynal.js index 77ec6cb..0d5a91c 100644 --- a/termynal.js +++ b/termynal.js @@ -36,6 +36,8 @@ class Termynal { || parseFloat(this.container.getAttribute(`${this.pfx}-typeDelay`)) || 90; this.lineDelay = options.lineDelay || parseFloat(this.container.getAttribute(`${this.pfx}-lineDelay`)) || 1500; + this.inputDelay = options.inputDelay + || parseFloat(this.container.getAttribute(`${this.pfx}-inputDelay`)) || 0; this.progressLength = options.progressLength || parseFloat(this.container.getAttribute(`${this.pfx}-progressLength`)) || 40; this.progressChar = options.progressChar @@ -55,14 +57,14 @@ class Termynal { // Appends dynamically loaded lines to existing line elements. this.lines = [...this.container.querySelectorAll(`[${this.pfx}]`)].concat(this.lineData); - /** + /** * Calculates width and height of Termynal container. * If container is empty and lines are dynamically loaded, defaults to browser `auto` or CSS. - */ + */ const containerStyle = getComputedStyle(this.container); - this.container.style.width = containerStyle.width !== '0px' ? + this.container.style.width = containerStyle.width !== '0px' ? containerStyle.width : undefined; - this.container.style.minHeight = containerStyle.height !== '0px' ? + this.container.style.minHeight = containerStyle.height !== '0px' ? containerStyle.height : undefined; this.container.setAttribute('data-termynal', ''); @@ -107,9 +109,11 @@ class Termynal { async type(line) { const chars = [...line.textContent]; const delay = line.getAttribute(`${this.pfx}-typeDelay`) || this.typeDelay; + const inputDelay = line.getAttribute(`${this.pfx}-inputDelay`) || this.inputDelay; line.textContent = ''; this.container.appendChild(line); + await this._wait(inputDelay) for (let char of chars) { await this._wait(delay); line.textContent += char; @@ -151,7 +155,7 @@ class Termynal { /** * Converts line data objects into line elements. - * + * * @param {Object[]} lineData - Dynamically loaded lines. * @param {Object} line - Line data object. * @returns {Element[]} - Array of line elements. @@ -167,7 +171,7 @@ class Termynal { /** * Helper function for generating attributes string. - * + * * @param {Object} line - Line data object. * @returns {string} - String of attributes. */