All files output.ts

100% Statements 20/20
100% Branches 3/3
100% Functions 8/8
100% Lines 19/19

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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142                                            11x           14x               128x                 35x                           25x 25x                         8x                                                 16x 16x 16x                           9x 9x 9x                           35x 58x 49x   35x 35x 35x          
// Internals
import { LogMessage } from "./index";
import { COLORS } from "./colors";
 
/**
 * The message or nested output
 */
export type OutputMessage = LogMessage | Output;
 
/**
 * Store and flush a series of outputs synchronously
 */
export class Output {
  /**
   *
   * ANSI Escape Sequences
   *
   * @example
   * ```javascript
   * const bold = Output.Colors.Bright;
   * ```
   */
  public static readonly COLORS = COLORS;
  /**
   *
   * All messages or nested outputs of this Output instance
   *
   */
  private _output: OutputMessage[] = [];
 
  /**
   *
   * All messages or nested outputs of this Output instance
   *
   */
  public get output(): OutputMessage[] {
    return this._output;
  }
 
  /**
   *
   * All messages or nested outputs of this Output instance
   *
   */
  public set output(newOutput: OutputMessage[]) {
    this._output = newOutput;
  }
 
  /**
   *
   * Adds a message to the output array
   *
   * @param value - The message to add
   * @example
   * ```javascript
   * new Output().log("hi");
   * ```
   */
  log<T extends LogMessage>(value: T): T {
    this.output.push(value);
    return value;
  }
 
  /**
   *
   * Adds a line break to the output array
   *
   * @example
   * ```javascript
   * new Output().line();
   * ```
   */
  public line(): void {
    this.output.push("");
  }
 
  /**
   *
   * Adds a colored message to the output array
   *
   * @param color - The color to log in
   * @param message - The message to add
   * @param afterColored - The optional message after the colored message (on the same line)
   * @returns The colored log message
   * @example
   * ```javascript
   * new Output().coloredLog("BgBlue", "hi");
   * ```
   * @example
   * ```javascript
   * new Output().coloredLog("FgYellow", "hi", "this string will not be colored");
   * ```
   */
  coloredLog(
    color: keyof typeof Output.COLORS,
    message: LogMessage,
    afterColored = ""
  ): string {
    const colored = `${Output.COLORS[color]}${message}${Output.COLORS.Reset}${afterColored}`;
    this.output.push(colored);
    return colored;
  }
 
  /**
   *
   * Adds a nested output instance to the output array
   *
   * @returns The nested output
   * @example
   * ```javascript
   * const nested = new Output().nested();
   * ```
   */
  nested(): Output {
    const nestedOutput = new Output();
    this.output.push(nestedOutput);
    return nestedOutput;
  }
 
  /**
   *
   * Logs all messages in the output array
   *
   * @returns The output array
   * @example
   * ```javascript
   * new Output().flush();
   * ```
   */
  flush(): OutputMessage[] {
    this.output.forEach((value) => {
      if (value instanceof Output) value.flush();
      else console.log(value);
    });
    const flushed = [...this.output];
    this.output = [];
    return flushed;
  }
}
 
export default Output;