All files / rusted-chromium/log printer.ts

75% Statements 24/32
50% Branches 1/2
60% Functions 9/15
74.07% Lines 20/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71  4x       4x             4x   37x 33x 12x 12x 12x   12x                         107x 107x   107x       63x 63x         63x       53x 53x         2x     2x 2x                    
/* eslint-disable-next-line import/no-namespace */
import * as chalk from 'chalk'
 
import type { PrinterWriteStream } from '../interfaces/printer.interfaces'
 
const silentWriteStream: PrinterWriteStream = {
    clearLine: () => true,
    write: () => true,
    cursorTo: () => true,
    moveCursor: () => true,
}
 
export abstract class Printer<T extends Printer<T>> {
 
    protected readonly SUCCESS_FN = (msg: string): string => chalk.green(`✔ ${msg}`)
    protected readonly ERROR_FN = (msg: string): string => chalk.red(`✘ ${msg}`)
    protected readonly WARN_FN = (msg: string): string => chalk.yellow(`! ${msg}`)
    protected readonly INFO_FN = (msg: string): string => chalk.blue(`➔ ${msg}`)
    protected readonly DEBUG_FN = (msg: string): string => chalk.magenta(`? ${msg}`)
 
    protected constructor(private stdio: PrinterWriteStream) {
    }
 
    protected abstract self(): T
 
    protected abstract stop(): T
 
    /**
     * Writes the given text to stdio. Does nothing, if no text is provided
     * @param text 
     * @returns 
     */
    protected write(text?: string): T {
        if (text) {
            this.stdio.write(text)
        }
        return this.self()
    }
 
    protected clearLine(): T {
        try {
            this.stdio.clearLine(0)
            this.stdio.cursorTo(0)
        } catch {
            // this might fail when piping stdout to /dev/null. Just ignore it in this case
        }
        return this.self()
    }
 
    protected newline(): T {
        this.stdio.write('\n')
        return this.self()
    }
 
    protected deleteLastLine(): T {
        // stdio.moveCursor might not be available in some environments
        Iif (typeof this.stdio.moveCursor === 'function') {
            this.stdio.moveCursor(0, -1)
        }
        this.clearLine()
        return this.self()
    }
 
    /**
     * Suppresses all log output. Can't be undone on a running instance
     */
    public silent(): void {
        this.stdio = silentWriteStream
    }
}