index.d.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Type definitions for Anser
  2. // Project: https://github.com/IonicaBizau/anser
  3. export interface AnserJsonEntry {
  4. /** The text. */
  5. content: string;
  6. /** The foreground color. */
  7. fg: string;
  8. /** The background color. */
  9. bg: string;
  10. /** The foreground true color (if 16m color is enabled). */
  11. fg_truecolor: string;
  12. /** The background true color (if 16m color is enabled). */
  13. bg_truecolor: string;
  14. /** `true` if a carriageReturn \r was fount at end of line. */
  15. clearLine: boolean;
  16. decoration: null | 'bold' | 'dim' | 'italic' | 'underline' | 'blink' | 'reverse' | 'hidden' | 'strikethrough';
  17. /** `true` if the colors were processed, `false` otherwise. */
  18. was_processed: boolean;
  19. /** A function returning `true` if the content is empty, or `false` otherwise. */
  20. isEmpty(): boolean;
  21. }
  22. export interface AnserOptions {
  23. /** If `true`, the result will be an object. */
  24. json?: boolean;
  25. /** If `true`, HTML classes will be appended to the HTML output. */
  26. use_classes?: boolean;
  27. remove_empty?: boolean;
  28. }
  29. type OptionsWithJson = AnserOptions & { json: true };
  30. export default class Anser {
  31. /**
  32. * Escape the input HTML.
  33. *
  34. * This does the minimum escaping of text to make it compliant with HTML.
  35. * In particular, the '&','<', and '>' characters are escaped. This should
  36. * be run prior to `ansiToHtml`.
  37. *
  38. * @param txt The input text (containing the ANSI snippets).
  39. * @returns The escaped html.
  40. */
  41. static escapeForHtml (txt: string): string;
  42. /**
  43. * Adds the links in the HTML.
  44. *
  45. * This replaces any links in the text with anchor tags that display the
  46. * link. The links should have at least one whitespace character
  47. * surrounding it. Also, you should apply this after you have run
  48. * `ansiToHtml` on the text.
  49. *
  50. * @param txt The input text.
  51. * @returns The HTML containing the <a> tags (unescaped).
  52. */
  53. static linkify (txt: string): string;
  54. /**
  55. * This replaces ANSI terminal escape codes with SPAN tags that wrap the
  56. * content.
  57. *
  58. * This function only interprets ANSI SGR (Select Graphic Rendition) codes
  59. * that can be represented in HTML.
  60. * For example, cursor movement codes are ignored and hidden from output.
  61. * The default style uses colors that are very close to the prescribed
  62. * standard. The standard assumes that the text will have a black
  63. * background. These colors are set as inline styles on the SPAN tags.
  64. *
  65. * Another option is to set `use_classes: true` in the options argument.
  66. * This will instead set classes on the spans so the colors can be set via
  67. * CSS. The class names used are of the format `ansi-*-fg/bg` and
  68. * `ansi-bright-*-fg/bg` where `*` is the color name,
  69. * i.e black/red/green/yellow/blue/magenta/cyan/white.
  70. *
  71. * @param txt The input text.
  72. * @param options The options.
  73. * @returns The HTML output.
  74. */
  75. static ansiToHtml (txt: string, options?: AnserOptions): string;
  76. /**
  77. * Converts ANSI input into JSON output.
  78. *
  79. * @param txt The input text.
  80. * @param options The options.
  81. * @returns The HTML output.
  82. */
  83. static ansiToJson (txt: string, options?: AnserOptions): AnserJsonEntry[];
  84. /**
  85. * Converts ANSI input into text output.
  86. *
  87. * @param txt The input text.
  88. * @returns The text output.
  89. */
  90. static ansiToText (txt: string, options?: AnserOptions): string;
  91. /**
  92. * Sets up the palette.
  93. */
  94. setupPalette (): void;
  95. /**
  96. * Escapes the input text.
  97. *
  98. * @param txt The input text.
  99. * @returns The escpaed HTML output.
  100. */
  101. escapeForHtml (txt: string): string;
  102. /**
  103. * Adds HTML link elements.
  104. *
  105. * @param txt The input text.
  106. * @returns The HTML output containing link elements.
  107. */
  108. linkify (txt: string): string;
  109. /**
  110. * Converts ANSI input into HTML output.
  111. *
  112. * @param txt The input text.
  113. * @param options The options.
  114. * @returns The HTML output.
  115. */
  116. ansiToHtml (txt: string, options?: AnserOptions): string;
  117. /**
  118. * Converts ANSI input into HTML output.
  119. *
  120. * @param txt The input text.
  121. * @param options The options.
  122. * @returns The JSON output.
  123. */
  124. ansiToJson (txt: string, options?: AnserOptions): AnserJsonEntry[];
  125. /**
  126. * Converts ANSI input into HTML output.
  127. *
  128. * @param txt The input text.
  129. * @returns The text output.
  130. */
  131. ansiToText (txt: string, options?: AnserOptions): string;
  132. /**
  133. * Processes the input.
  134. *
  135. * @param txt The input text.
  136. * @param options The options.
  137. * @param markup If false, the colors will not be parsed.
  138. */
  139. process (txt: string, options: OptionsWithJson, markup?: boolean): AnserJsonEntry[];
  140. process (txt: string, options?: AnserOptions, markup?: boolean): string;
  141. /**
  142. * Processes the current chunk into json output.
  143. *
  144. * @param text The input text.
  145. * @param options The options.
  146. * @param markup If false, the colors will not be parsed.
  147. * @return The JSON output.
  148. */
  149. processChunkJson (text: string, options?: AnserOptions, markup?: boolean): AnserJsonEntry;
  150. /**
  151. * Processes the current chunk of text.
  152. *
  153. * @param text The input text.
  154. * @param options The options.
  155. * @param markup If false, the colors will not be parsed.
  156. * @return The result (object if `json` is wanted back or string otherwise).
  157. */
  158. processChunk (text: string, options: OptionsWithJson, markup?: boolean): AnserJsonEntry;
  159. processChunk (text: string, options?: AnserOptions, markup?: boolean): string;
  160. }