utils.d.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. type BoxBorderStyle = {
  2. /**
  3. * Top left corner
  4. * @example `┌`
  5. * @example `╔`
  6. * @example `╓`
  7. */
  8. tl: string;
  9. /**
  10. * Top right corner
  11. * @example `┐`
  12. * @example `╗`
  13. * @example `╖`
  14. */
  15. tr: string;
  16. /**
  17. * Bottom left corner
  18. * @example `└`
  19. * @example `╚`
  20. * @example `╙`
  21. */
  22. bl: string;
  23. /**
  24. * Bottom right corner
  25. * @example `┘`
  26. * @example `╝`
  27. * @example `╜`
  28. */
  29. br: string;
  30. /**
  31. * Horizontal line
  32. * @example `─`
  33. * @example `═`
  34. * @example `─`
  35. */
  36. h: string;
  37. /**
  38. * Vertical line
  39. * @example `│`
  40. * @example `║`
  41. * @example `║`
  42. */
  43. v: string;
  44. };
  45. declare const boxStylePresets: Record<string, BoxBorderStyle>;
  46. type BoxStyle = {
  47. /**
  48. * The border color
  49. * @default 'white'
  50. */
  51. borderColor: "black" | "red" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "white" | "gray" | "blackBright" | "redBright" | "greenBright" | "yellowBright" | "blueBright" | "magentaBright" | "cyanBright" | "whiteBright";
  52. /**
  53. * The border style
  54. * @default 'solid'
  55. * @example 'single-double-rounded'
  56. * @example
  57. * ```ts
  58. * {
  59. * tl: '┌',
  60. * tr: '┐',
  61. * bl: '└',
  62. * br: '┘',
  63. * h: '─',
  64. * v: '│',
  65. * }
  66. * ```
  67. */
  68. borderStyle: BoxBorderStyle | keyof typeof boxStylePresets;
  69. /**
  70. * The vertical alignment of the text
  71. * @default 'center'
  72. */
  73. valign: "top" | "center" | "bottom";
  74. /**
  75. * The padding of the box
  76. * @default 2
  77. */
  78. padding: number;
  79. /**
  80. * The left margin of the box
  81. * @default 1
  82. */
  83. marginLeft: number;
  84. /**
  85. * The top margin of the box
  86. * @default 1
  87. */
  88. marginTop: number;
  89. /**
  90. * The top margin of the box
  91. * @default 1
  92. */
  93. marginBottom: number;
  94. };
  95. /**
  96. * The border options of the box
  97. */
  98. type BoxOpts = {
  99. /**
  100. * Title that will be displayed on top of the box
  101. * @example 'Hello World'
  102. * @example 'Hello {name}'
  103. */
  104. title?: string;
  105. style?: Partial<BoxStyle>;
  106. };
  107. /**
  108. * Creates a styled box with text content, customisable via options.
  109. * @param {string} text - The text to display in the box.
  110. * @param {BoxOpts} [_opts={}] - Optional settings for the appearance and behaviour of the box. See {@link BoxOpts}.
  111. * @returns {string} The formatted box as a string, ready for printing or logging.
  112. */
  113. declare function box(text: string, _opts?: BoxOpts): string;
  114. /**
  115. * Based on https://github.com/jorgebucaran/colorette
  116. * Read LICENSE file for more information
  117. * https://github.com/jorgebucaran/colorette/blob/20fc196d07d0f87c61e0256eadd7831c79b24108/index.js
  118. */
  119. declare const colorDefs: {
  120. reset: (string: string) => string;
  121. bold: (string: string) => string;
  122. dim: (string: string) => string;
  123. italic: (string: string) => string;
  124. underline: (string: string) => string;
  125. inverse: (string: string) => string;
  126. hidden: (string: string) => string;
  127. strikethrough: (string: string) => string;
  128. black: (string: string) => string;
  129. red: (string: string) => string;
  130. green: (string: string) => string;
  131. yellow: (string: string) => string;
  132. blue: (string: string) => string;
  133. magenta: (string: string) => string;
  134. cyan: (string: string) => string;
  135. white: (string: string) => string;
  136. gray: (string: string) => string;
  137. bgBlack: (string: string) => string;
  138. bgRed: (string: string) => string;
  139. bgGreen: (string: string) => string;
  140. bgYellow: (string: string) => string;
  141. bgBlue: (string: string) => string;
  142. bgMagenta: (string: string) => string;
  143. bgCyan: (string: string) => string;
  144. bgWhite: (string: string) => string;
  145. blackBright: (string: string) => string;
  146. redBright: (string: string) => string;
  147. greenBright: (string: string) => string;
  148. yellowBright: (string: string) => string;
  149. blueBright: (string: string) => string;
  150. magentaBright: (string: string) => string;
  151. cyanBright: (string: string) => string;
  152. whiteBright: (string: string) => string;
  153. bgBlackBright: (string: string) => string;
  154. bgRedBright: (string: string) => string;
  155. bgGreenBright: (string: string) => string;
  156. bgYellowBright: (string: string) => string;
  157. bgBlueBright: (string: string) => string;
  158. bgMagentaBright: (string: string) => string;
  159. bgCyanBright: (string: string) => string;
  160. bgWhiteBright: (string: string) => string;
  161. };
  162. type ColorName = keyof typeof colorDefs;
  163. type ColorFunction = (text: string | number) => string;
  164. /**
  165. * An object containing functions for colouring text. Each function corresponds to a terminal colour. See {@link ColorName} for available colours.
  166. */
  167. declare const colors: Record<ColorName, ColorFunction>;
  168. /**
  169. * Gets a colour function by name, with an option for a fallback colour if the requested colour is not found.
  170. * @param {ColorName} color - The name of the colour function to get. See {@link ColorName}.
  171. * @param {ColorName} [fallback="reset"] - The name of the fallback colour function if the requested colour is not found. See {@link ColorName}.
  172. * @returns {ColorFunction} The colour function that corresponds to the requested colour, or the fallback colour function. See {@link ColorFunction}.
  173. */
  174. declare function getColor(color: ColorName, fallback?: ColorName): ColorFunction;
  175. /**
  176. * Applies a specified colour to a given text string or number.
  177. * @param {ColorName} color - The colour to apply. See {@link ColorName}.
  178. * @param {string | number} text - The text to colour.
  179. * @returns {string} The coloured text.
  180. */
  181. declare function colorize(color: ColorName, text: string | number): string;
  182. /**
  183. * Removes ANSI escape codes from a given string. This is particularly useful for
  184. * processing text that contains formatting codes, such as colours or styles, so that the
  185. * the raw text without any visual formatting.
  186. *
  187. * @param {string} text - The text string from which to strip the ANSI escape codes.
  188. * @returns {string} The text without ANSI escape codes.
  189. */
  190. declare function stripAnsi(text: string): string;
  191. /**
  192. * Centers a string within a specified total width, padding it with spaces or another specified character.
  193. * If the string is longer than the total width, it is returned as is.
  194. *
  195. * @param {string} str - The string to centre.
  196. * @param {number} len - The total width in which to centre the string.
  197. * @param {string} [space=" "] - The character to use for padding. Defaults to a space.
  198. * @returns {string} The centred string.
  199. */
  200. declare function centerAlign(str: string, len: number, space?: string): string;
  201. /**
  202. * Right-justifies a string within a given total width, padding it with whitespace or another specified character.
  203. * If the string is longer than the total width, it is returned as is.
  204. *
  205. * @param {string} str - The string to right-justify.
  206. * @param {number} len - The total width to align the string.
  207. * @param {string} [space=" "] - The character to use for padding. Defaults to a space.
  208. * @returns {string} The right-justified string.
  209. */
  210. declare function rightAlign(str: string, len: number, space?: string): string;
  211. /**
  212. * Left-aligns a string within a given total width, padding it with whitespace or another specified character on the right.
  213. * If the string is longer than the total width, it is returned as is.
  214. *
  215. * @param {string} str - The string to align left.
  216. * @param {number} len - The total width to align the string.
  217. * @param {string} [space=" "] - The character to use for padding. Defaults to a space.
  218. * @returns {string} The left-justified string.
  219. */
  220. declare function leftAlign(str: string, len: number, space?: string): string;
  221. /**
  222. * Aligns a string (left, right, or centre) within a given total width, padding it with spaces or another specified character.
  223. * If the string is longer than the total width, it is returned as is. This function acts as a wrapper for individual alignment functions.
  224. *
  225. * @param {"left" | "right" | "centre"} alignment - The desired alignment of the string.
  226. * @param {string} str - The string to align.
  227. * @param {number} len - The total width in which to align the string.
  228. * @param {string} [space=" "] - The character to use for padding. Defaults to a space.
  229. * @returns {string} The aligned string, according to the given alignment.
  230. */
  231. declare function align(alignment: "left" | "right" | "center", str: string, len: number, space?: string): string;
  232. type TreeItemObject = {
  233. /**
  234. * Text of the item
  235. */
  236. text: string;
  237. /**
  238. * Children of the item
  239. */
  240. children?: TreeItem[];
  241. /**
  242. * Color of the item
  243. */
  244. color?: ColorName;
  245. };
  246. type TreeItem = string | TreeItemObject;
  247. type TreeOptions = {
  248. /**
  249. * Color of the tree
  250. */
  251. color?: ColorName;
  252. /**
  253. * Prefix of the tree
  254. *
  255. * @default " "
  256. */
  257. prefix?: string;
  258. /**
  259. * The max depth of tree
  260. */
  261. maxDepth?: number;
  262. /**
  263. * Ellipsis of the tree
  264. *
  265. * @default "..."
  266. */
  267. ellipsis?: string;
  268. };
  269. /**
  270. * Formats a hierarchical list of items into a string representing a tree structure.
  271. * Each item in the tree can be a simple string or an object defining the text of the item,
  272. * optional children, and colour. The tree structure can be customised with options
  273. * Specify the overall colour and the prefix used for indentation and tree lines.
  274. *
  275. * @param {TreeItem[]} items - An array of items to include in the tree. Each item can be
  276. * either a string or an object with `text', `children' and `colour' properties.
  277. * @param {TreeOptions} [options] - Optional settings to customise the appearance of the tree, including
  278. * the colour of the tree text and the prefix for branches. See {@link TreeOptions}.
  279. * @returns {string} The formatted tree as a string, ready for printing to the console or elsewhere.
  280. */
  281. declare function formatTree(items: TreeItem[], options?: TreeOptions): string;
  282. export { type BoxBorderStyle, type BoxOpts, type BoxStyle, type ColorFunction, type ColorName, type TreeItem, type TreeItemObject, type TreeOptions, align, box, centerAlign, colorize, colors, formatTree, getColor, leftAlign, rightAlign, stripAnsi };