index.d.ts 5.65 KB
Newer Older
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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
// Type definitions for Anser
// Project: https://github.com/IonicaBizau/anser

export interface AnserJsonEntry {
    /** The text. */
    content: string;
    /** The foreground color. */
    fg: string;
    /** The background color. */
    bg: string;
    /** The foreground true color (if 16m color is enabled). */
    fg_truecolor: string;
    /** The background true color (if 16m color is enabled). */
    bg_truecolor: string;
    /** `true` if a carriageReturn \r was fount at end of line. */
    clearLine: boolean;
    decoration: null | 'bold' | 'dim' | 'italic' | 'underline' | 'blink' | 'reverse' | 'hidden' | 'strikethrough';
    /** `true` if the colors were processed, `false` otherwise. */
    was_processed: boolean;
    /** A function returning `true` if the content is empty, or `false` otherwise. */
    isEmpty(): boolean;
}

export interface AnserOptions {
    /** If `true`, the result will be an object. */
    json?: boolean;
    /** If `true`, HTML classes will be appended to the HTML output. */
    use_classes?: boolean;
    remove_empty?: boolean;
}

type OptionsWithJson = AnserOptions & { json: true };

export default class Anser {
    /**
     * Escape the input HTML.
     *
     * This does the minimum escaping of text to make it compliant with HTML.
     * In particular, the '&','<', and '>' characters are escaped. This should
     * be run prior to `ansiToHtml`.
     *
     * @param txt The input text (containing the ANSI snippets).
     * @returns The escaped html.
     */
    static escapeForHtml (txt: string): string;

    /**
     * Adds the links in the HTML.
     *
     * This replaces any links in the text with anchor tags that display the
     * link. The links should have at least one whitespace character
     * surrounding it. Also, you should apply this after you have run
     * `ansiToHtml` on the text.
     *
     * @param txt The input text.
     * @returns The HTML containing the <a> tags (unescaped).
     */
    static linkify (txt: string): string;

    /**
     * This replaces ANSI terminal escape codes with SPAN tags that wrap the
     * content.
     *
     * This function only interprets ANSI SGR (Select Graphic Rendition) codes
     * that can be represented in HTML.
     * For example, cursor movement codes are ignored and hidden from output.
     * The default style uses colors that are very close to the prescribed
     * standard. The standard assumes that the text will have a black
     * background. These colors are set as inline styles on the SPAN tags.
     *
     * Another option is to set `use_classes: true` in the options argument.
     * This will instead set classes on the spans so the colors can be set via
     * CSS. The class names used are of the format `ansi-*-fg/bg` and
     * `ansi-bright-*-fg/bg` where `*` is the color name,
     * i.e black/red/green/yellow/blue/magenta/cyan/white.
     *
     * @param txt The input text.
     * @param options The options.
     * @returns The HTML output.
     */
    static ansiToHtml (txt: string, options?: AnserOptions): string;

    /**
     * Converts ANSI input into JSON output.
     *
     * @param txt The input text.
     * @param options The options.
     * @returns The HTML output.
     */
    static ansiToJson (txt: string, options?: AnserOptions): AnserJsonEntry[];

    /**
     * Converts ANSI input into text output.
     *
     * @param txt The input text.
     * @returns The text output.
     */
    static ansiToText (txt: string, options?: AnserOptions): string;

    /**
     * Sets up the palette.
     */
    setupPalette (): void;

    /**
     * Escapes the input text.
     *
     * @param txt The input text.
     * @returns The escpaed HTML output.
     */
    escapeForHtml (txt: string): string;

    /**
     * Adds HTML link elements.
     *
     * @param txt The input text.
     * @returns The HTML output containing link elements.
     */
    linkify (txt: string): string;

    /**
     * Converts ANSI input into HTML output.
     *
     * @param txt The input text.
     * @param options The options.
     * @returns The HTML output.
     */
    ansiToHtml (txt: string, options?: AnserOptions): string;

    /**
     * Converts ANSI input into HTML output.
     *
     * @param txt The input text.
     * @param options The options.
     * @returns The JSON output.
     */
    ansiToJson (txt: string, options?: AnserOptions): AnserJsonEntry[];

    /**
     * Converts ANSI input into HTML output.
     *
     * @param txt The input text.
     * @returns The text output.
     */
    ansiToText (txt: string, options?: AnserOptions): string;

    /**
     * Processes the input.
     *
     * @param txt The input text.
     * @param options The options.
     * @param markup If false, the colors will not be parsed.
     */
    process (txt: string, options: OptionsWithJson, markup?: boolean): AnserJsonEntry[];
    process (txt: string, options?: AnserOptions, markup?: boolean): string;

    /**
     * Processes the current chunk into json output.
     *
     * @param text The input text.
     * @param options The options.
     * @param markup If false, the colors will not be parsed.
     * @return The JSON output.
     */
    processChunkJson (text: string, options?: AnserOptions, markup?: boolean): AnserJsonEntry;

    /**
     * Processes the current chunk of text.
     *
     * @param text The input text.
     * @param options The options.
     * @param markup If false, the colors will not be parsed.
     * @return The result (object if `json` is wanted back or string otherwise).
     */
    processChunk (text: string, options: OptionsWithJson, markup?: boolean): AnserJsonEntry;
    processChunk (text: string, options?: AnserOptions, markup?: boolean): string;
}