index.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // process.env dot-notation access prints:
  2. // Property 'TERM' comes from an index signature, so it must be accessed with ['TERM'].ts(4111)
  3. /* eslint dot-notation: ["off"] */
  4. import process from 'node:process';
  5. // Ported from is-unicode-supported
  6. function isUnicodeSupported() {
  7. if (process.platform !== 'win32') {
  8. return process.env['TERM'] !== 'linux'; // Linux console (kernel)
  9. }
  10. return (Boolean(process.env['WT_SESSION']) || // Windows Terminal
  11. Boolean(process.env['TERMINUS_SUBLIME']) || // Terminus (<0.2.27)
  12. process.env['ConEmuTask'] === '{cmd::Cmder}' || // ConEmu and cmder
  13. process.env['TERM_PROGRAM'] === 'Terminus-Sublime' ||
  14. process.env['TERM_PROGRAM'] === 'vscode' ||
  15. process.env['TERM'] === 'xterm-256color' ||
  16. process.env['TERM'] === 'alacritty' ||
  17. process.env['TERMINAL_EMULATOR'] === 'JetBrains-JediTerm');
  18. }
  19. // Ported from figures
  20. const common = {
  21. circleQuestionMark: '(?)',
  22. questionMarkPrefix: '(?)',
  23. square: '█',
  24. squareDarkShade: '▓',
  25. squareMediumShade: '▒',
  26. squareLightShade: '░',
  27. squareTop: '▀',
  28. squareBottom: '▄',
  29. squareLeft: '▌',
  30. squareRight: '▐',
  31. squareCenter: '■',
  32. bullet: '●',
  33. dot: '․',
  34. ellipsis: '…',
  35. pointerSmall: '›',
  36. triangleUp: '▲',
  37. triangleUpSmall: '▴',
  38. triangleDown: '▼',
  39. triangleDownSmall: '▾',
  40. triangleLeftSmall: '◂',
  41. triangleRightSmall: '▸',
  42. home: '⌂',
  43. heart: '♥',
  44. musicNote: '♪',
  45. musicNoteBeamed: '♫',
  46. arrowUp: '↑',
  47. arrowDown: '↓',
  48. arrowLeft: '←',
  49. arrowRight: '→',
  50. arrowLeftRight: '↔',
  51. arrowUpDown: '↕',
  52. almostEqual: '≈',
  53. notEqual: '≠',
  54. lessOrEqual: '≤',
  55. greaterOrEqual: '≥',
  56. identical: '≡',
  57. infinity: '∞',
  58. subscriptZero: '₀',
  59. subscriptOne: '₁',
  60. subscriptTwo: '₂',
  61. subscriptThree: '₃',
  62. subscriptFour: '₄',
  63. subscriptFive: '₅',
  64. subscriptSix: '₆',
  65. subscriptSeven: '₇',
  66. subscriptEight: '₈',
  67. subscriptNine: '₉',
  68. oneHalf: '½',
  69. oneThird: '⅓',
  70. oneQuarter: '¼',
  71. oneFifth: '⅕',
  72. oneSixth: '⅙',
  73. oneEighth: '⅛',
  74. twoThirds: '⅔',
  75. twoFifths: '⅖',
  76. threeQuarters: '¾',
  77. threeFifths: '⅗',
  78. threeEighths: '⅜',
  79. fourFifths: '⅘',
  80. fiveSixths: '⅚',
  81. fiveEighths: '⅝',
  82. sevenEighths: '⅞',
  83. line: '─',
  84. lineBold: '━',
  85. lineDouble: '═',
  86. lineDashed0: '┄',
  87. lineDashed1: '┅',
  88. lineDashed2: '┈',
  89. lineDashed3: '┉',
  90. lineDashed4: '╌',
  91. lineDashed5: '╍',
  92. lineDashed6: '╴',
  93. lineDashed7: '╶',
  94. lineDashed8: '╸',
  95. lineDashed9: '╺',
  96. lineDashed10: '╼',
  97. lineDashed11: '╾',
  98. lineDashed12: '−',
  99. lineDashed13: '–',
  100. lineDashed14: '‐',
  101. lineDashed15: '⁃',
  102. lineVertical: '│',
  103. lineVerticalBold: '┃',
  104. lineVerticalDouble: '║',
  105. lineVerticalDashed0: '┆',
  106. lineVerticalDashed1: '┇',
  107. lineVerticalDashed2: '┊',
  108. lineVerticalDashed3: '┋',
  109. lineVerticalDashed4: '╎',
  110. lineVerticalDashed5: '╏',
  111. lineVerticalDashed6: '╵',
  112. lineVerticalDashed7: '╷',
  113. lineVerticalDashed8: '╹',
  114. lineVerticalDashed9: '╻',
  115. lineVerticalDashed10: '╽',
  116. lineVerticalDashed11: '╿',
  117. lineDownLeft: '┐',
  118. lineDownLeftArc: '╮',
  119. lineDownBoldLeftBold: '┓',
  120. lineDownBoldLeft: '┒',
  121. lineDownLeftBold: '┑',
  122. lineDownDoubleLeftDouble: '╗',
  123. lineDownDoubleLeft: '╖',
  124. lineDownLeftDouble: '╕',
  125. lineDownRight: '┌',
  126. lineDownRightArc: '╭',
  127. lineDownBoldRightBold: '┏',
  128. lineDownBoldRight: '┎',
  129. lineDownRightBold: '┍',
  130. lineDownDoubleRightDouble: '╔',
  131. lineDownDoubleRight: '╓',
  132. lineDownRightDouble: '╒',
  133. lineUpLeft: '┘',
  134. lineUpLeftArc: '╯',
  135. lineUpBoldLeftBold: '┛',
  136. lineUpBoldLeft: '┚',
  137. lineUpLeftBold: '┙',
  138. lineUpDoubleLeftDouble: '╝',
  139. lineUpDoubleLeft: '╜',
  140. lineUpLeftDouble: '╛',
  141. lineUpRight: '└',
  142. lineUpRightArc: '╰',
  143. lineUpBoldRightBold: '┗',
  144. lineUpBoldRight: '┖',
  145. lineUpRightBold: '┕',
  146. lineUpDoubleRightDouble: '╚',
  147. lineUpDoubleRight: '╙',
  148. lineUpRightDouble: '╘',
  149. lineUpDownLeft: '┤',
  150. lineUpBoldDownBoldLeftBold: '┫',
  151. lineUpBoldDownBoldLeft: '┨',
  152. lineUpDownLeftBold: '┥',
  153. lineUpBoldDownLeftBold: '┩',
  154. lineUpDownBoldLeftBold: '┪',
  155. lineUpDownBoldLeft: '┧',
  156. lineUpBoldDownLeft: '┦',
  157. lineUpDoubleDownDoubleLeftDouble: '╣',
  158. lineUpDoubleDownDoubleLeft: '╢',
  159. lineUpDownLeftDouble: '╡',
  160. lineUpDownRight: '├',
  161. lineUpBoldDownBoldRightBold: '┣',
  162. lineUpBoldDownBoldRight: '┠',
  163. lineUpDownRightBold: '┝',
  164. lineUpBoldDownRightBold: '┡',
  165. lineUpDownBoldRightBold: '┢',
  166. lineUpDownBoldRight: '┟',
  167. lineUpBoldDownRight: '┞',
  168. lineUpDoubleDownDoubleRightDouble: '╠',
  169. lineUpDoubleDownDoubleRight: '╟',
  170. lineUpDownRightDouble: '╞',
  171. lineDownLeftRight: '┬',
  172. lineDownBoldLeftBoldRightBold: '┳',
  173. lineDownLeftBoldRightBold: '┯',
  174. lineDownBoldLeftRight: '┰',
  175. lineDownBoldLeftBoldRight: '┱',
  176. lineDownBoldLeftRightBold: '┲',
  177. lineDownLeftRightBold: '┮',
  178. lineDownLeftBoldRight: '┭',
  179. lineDownDoubleLeftDoubleRightDouble: '╦',
  180. lineDownDoubleLeftRight: '╥',
  181. lineDownLeftDoubleRightDouble: '╤',
  182. lineUpLeftRight: '┴',
  183. lineUpBoldLeftBoldRightBold: '┻',
  184. lineUpLeftBoldRightBold: '┷',
  185. lineUpBoldLeftRight: '┸',
  186. lineUpBoldLeftBoldRight: '┹',
  187. lineUpBoldLeftRightBold: '┺',
  188. lineUpLeftRightBold: '┶',
  189. lineUpLeftBoldRight: '┵',
  190. lineUpDoubleLeftDoubleRightDouble: '╩',
  191. lineUpDoubleLeftRight: '╨',
  192. lineUpLeftDoubleRightDouble: '╧',
  193. lineUpDownLeftRight: '┼',
  194. lineUpBoldDownBoldLeftBoldRightBold: '╋',
  195. lineUpDownBoldLeftBoldRightBold: '╈',
  196. lineUpBoldDownLeftBoldRightBold: '╇',
  197. lineUpBoldDownBoldLeftRightBold: '╊',
  198. lineUpBoldDownBoldLeftBoldRight: '╉',
  199. lineUpBoldDownLeftRight: '╀',
  200. lineUpDownBoldLeftRight: '╁',
  201. lineUpDownLeftBoldRight: '┽',
  202. lineUpDownLeftRightBold: '┾',
  203. lineUpBoldDownBoldLeftRight: '╂',
  204. lineUpDownLeftBoldRightBold: '┿',
  205. lineUpBoldDownLeftBoldRight: '╃',
  206. lineUpBoldDownLeftRightBold: '╄',
  207. lineUpDownBoldLeftBoldRight: '╅',
  208. lineUpDownBoldLeftRightBold: '╆',
  209. lineUpDoubleDownDoubleLeftDoubleRightDouble: '╬',
  210. lineUpDoubleDownDoubleLeftRight: '╫',
  211. lineUpDownLeftDoubleRightDouble: '╪',
  212. lineCross: '╳',
  213. lineBackslash: '╲',
  214. lineSlash: '╱',
  215. };
  216. const specialMainSymbols = {
  217. tick: '✔',
  218. info: 'ℹ',
  219. warning: '⚠',
  220. cross: '✘',
  221. squareSmall: '◻',
  222. squareSmallFilled: '◼',
  223. circle: '◯',
  224. circleFilled: '◉',
  225. circleDotted: '◌',
  226. circleDouble: '◎',
  227. circleCircle: 'ⓞ',
  228. circleCross: 'ⓧ',
  229. circlePipe: 'Ⓘ',
  230. radioOn: '◉',
  231. radioOff: '◯',
  232. checkboxOn: '☒',
  233. checkboxOff: '☐',
  234. checkboxCircleOn: 'ⓧ',
  235. checkboxCircleOff: 'Ⓘ',
  236. pointer: '❯',
  237. triangleUpOutline: '△',
  238. triangleLeft: '◀',
  239. triangleRight: '▶',
  240. lozenge: '◆',
  241. lozengeOutline: '◇',
  242. hamburger: '☰',
  243. smiley: '㋡',
  244. mustache: '෴',
  245. star: '★',
  246. play: '▶',
  247. nodejs: '⬢',
  248. oneSeventh: '⅐',
  249. oneNinth: '⅑',
  250. oneTenth: '⅒',
  251. };
  252. const specialFallbackSymbols = {
  253. tick: '√',
  254. info: 'i',
  255. warning: '‼',
  256. cross: '×',
  257. squareSmall: '□',
  258. squareSmallFilled: '■',
  259. circle: '( )',
  260. circleFilled: '(*)',
  261. circleDotted: '( )',
  262. circleDouble: '( )',
  263. circleCircle: '(○)',
  264. circleCross: '(×)',
  265. circlePipe: '(│)',
  266. radioOn: '(*)',
  267. radioOff: '( )',
  268. checkboxOn: '[×]',
  269. checkboxOff: '[ ]',
  270. checkboxCircleOn: '(×)',
  271. checkboxCircleOff: '( )',
  272. pointer: '>',
  273. triangleUpOutline: '∆',
  274. triangleLeft: '◄',
  275. triangleRight: '►',
  276. lozenge: '♦',
  277. lozengeOutline: '◊',
  278. hamburger: '≡',
  279. smiley: '☺',
  280. mustache: '┌─┐',
  281. star: '✶',
  282. play: '►',
  283. nodejs: '♦',
  284. oneSeventh: '1/7',
  285. oneNinth: '1/9',
  286. oneTenth: '1/10',
  287. };
  288. export const mainSymbols = { ...common, ...specialMainSymbols };
  289. export const fallbackSymbols = {
  290. ...common,
  291. ...specialFallbackSymbols,
  292. };
  293. const shouldUseMain = isUnicodeSupported();
  294. const figures = shouldUseMain ? mainSymbols : fallbackSymbols;
  295. export default figures;
  296. const replacements = Object.entries(specialMainSymbols);
  297. // On terminals which do not support Unicode symbols, substitute them to other symbols
  298. export const replaceSymbols = (string, { useFallback = !shouldUseMain } = {}) => {
  299. if (useFallback) {
  300. for (const [key, mainSymbol] of replacements) {
  301. const fallbackSymbol = fallbackSymbols[key];
  302. if (!fallbackSymbol) {
  303. throw new Error(`Unable to find fallback for ${key}`);
  304. }
  305. string = string.replaceAll(mainSymbol, fallbackSymbol);
  306. }
  307. }
  308. return string;
  309. };