UNPKG

ascii-ui

Version:

Graphic terminal emulator for HTML canvas elements

96 lines 3.21 kB
import { isEmpty } from 'vanilla-type-check/isEmpty'; import { Widget } from '../Widget'; import { deepAssign } from '../util/deepAssign'; export class ProgressBar extends Widget { constructor(terminal, options, parent) { super(terminal, deepAssign({}, ProgressBar.defaultOptions, options), parent); } render() { if (this.options.direction === 'horizontal') { this.renderHorizontal(); } else { this.renderVertical(); } } getProgress() { return this.options.progress; } updateOptions(changes) { if (this.options.direction === 'horizontal') { this.options.height = 1; } else { this.options.width = 1; } if (!isEmpty(changes)) { this.render(); } } renderHorizontal() { const options = this.options; const terminal = this.terminal; const line = options.line; let width = options.width; let col = options.col; if (options.endStyle) { terminal.setTiles(options.endStyle, col + width - 1, line); width--; } if (options.startStyle) { terminal.setTiles(options.startStyle, col, line); width--; col++; } const progress = Math.ceil(width * options.progress); const done = Array(progress); const pending = Array(width - progress); done.fill(options.completedStyle); pending.fill(options.pendingStyle); terminal.setTiles(done, col, line); terminal.setTiles(pending, col + progress, line); if (options.progress > 0 && options.progress < 1 && options.currentStyle) { terminal.setTiles(options.currentStyle, col + progress - 1, line); } } renderVertical() { const options = this.options; const terminal = this.terminal; const col = options.col; let height = options.height; let line = options.line; if (options.endStyle) { terminal.setTiles(options.endStyle, col, line + height - 1); height--; } if (options.startStyle) { terminal.setTiles(options.startStyle, col, line); height--; line++; } const progress = Math.ceil(height * options.progress); line = line + height; for (let i = 0; i < progress; i++) { line--; terminal.setTiles(options.completedStyle, col, line); } if (options.progress > 0 && options.progress < 1 && options.currentStyle) { terminal.setTiles(options.currentStyle, col, line); } for (let i = 0; i < height - progress; i++) { line--; terminal.setTiles(options.pendingStyle, col, line); } } } ProgressBar.defaultOptions = { focusable: false, direction: 'horizontal', progress: 0, completedStyle: { char: '', bg: '#00ff00' }, pendingStyle: { char: '', bg: '#009900' }, currentStyle: { char: '', bg: '#99ff99' }, startStyle: undefined, endStyle: undefined, }; //# sourceMappingURL=ProgressBar.js.map