Skip to content

NodeJS Error/Fix process.stdout.clearLine not found #67

@Cupidazul

Description

@Cupidazul

Error occurs in node 14.x. process.stdout.clearLine and cursorTo not found ...

Fix: ProgressBar.js

class LoadingBar {
    constructor() {
        this.interval = 100;
        this.currentIndex = 0;
        this.fill = '█';
        this.empty = '⣿';
        this.bar_length = 50;
        this.loop = null;
        this.flip = 1;
        this.chunksDownloaded = "0";
        this.isRunning = false;
        this.success = ' √';
        this.failed = ' X';
        this.message = '';
        this.messageSize = 0;
    }
    reset() {
        this.constructor();
    }
    next() {
        this.clear();
        this.currentIndex += this.flip;
        if (this.currentIndex === this.bar_length || this.currentIndex === 0) {
            this.flip *= -1;
        }
        let _left = this.currentIndex - 1;
        _left = _left < 0 ? 0 : _left;
        let _right = this.bar_length - _left - 1;
        const _str = `${this.message}${this.chunksDownloaded} :` + chalk.green(` ${this.empty.repeat(_left)}${this.fill}${this.empty.repeat(_right)}`);
        this.messageSize = _str.length;
        process.stdout.write(_str);
    }
    clear() {
        if ('undefined'!=typeof(process.stdout.clearLine)) {
            //@ts-ignore
            process.stdout.clearLine();
            //@ts-ignore
            process.stdout.cursorTo(0);
        } else {
            if (this.currentIndex>0) process.stdout.write('\b'.repeat(this.messageSize));
        }
    }

.
.
.
.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions