All files / roguelike/src/game console.ts

100% Statements 25/25
91.67% Branches 11/12
100% Functions 4/4
100% Lines 25/25

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          23x   24x 6x     24x                 22x   22x   22x     22x     22x   22x 22x   22x 22x   22x 22x     22x 22x 22x   22x 2x 1x   1x             1x 1x   1x      
export interface ConsoleInterface {
    append(message: string): void;
    getContainer(): HTMLElement | null;
}
 
export class Console implements ConsoleInterface {
    public static getInstance(): Console {
        if (!Console.instance) {
            Console.instance = new Console();
        }
 
        return Console.instance;
    }
 
    private static instance: Console;
 
    private constructor() {}
 
    public append(message: string): void {
        // eslint-disable-next-line angular/document-service
        const element = document.getElementsByClassName("console")[0];
 
        const now = new Date(Date.now());
        const month =
            now.getMonth() + 1 < 10
                ? `0${now.getMonth() + 1}`
                : `${now.getMonth() + 1}`;
        const today = `${now.getFullYear()}-${month}-${now.getDate()}`;
 
        const hours =
            now.getHours() < 10 ? `0${now.getHours()}` : now.getHours();
        const seconds =
            now.getSeconds() < 10 ? `0${now.getSeconds()}` : now.getSeconds();
        const time = `${hours}:${now.getMinutes()}:${seconds}`;
 
        const when = document.createElement("span");
        when.textContent = `${today} ${time}`;
 
        const what = document.createElement("span");
        what.textContent = ` - ${message}`;
 
        // eslint-disable-next-line angular/document-service
        const log = document.createElement("p");
        log.appendChild(when);
        log.appendChild(what);
 
        if (null != element) {
            if (element.firstChild) {
                element.insertBefore(log, element.firstChild);
            } else {
                element.appendChild(log);
            }
        }
    }
 
    public getContainer(): HTMLElement | null {
        // eslint-disable-next-line angular/document-service
        const element = document.createElement("div");
        element.classList.add("console");
 
        return element;
    }
}