Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add inputDelay feature #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down Expand Up @@ -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...' }
]
}
)
```
```
16 changes: 10 additions & 6 deletions termynal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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', '');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -167,7 +171,7 @@ class Termynal {

/**
* Helper function for generating attributes string.
*
*
* @param {Object} line - Line data object.
* @returns {string} - String of attributes.
*/
Expand Down