app-terminal-width.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. const TRUNCATION_SUFFIX = "...";
  2. const INTER_WORD_SPACE_WIDTH = 1;
  3. const LINE_BREAK_PATTERN = /\r?\n/u;
  4. const WHITESPACE_PATTERN = /\s+/u;
  5. export function truncateDisplay(value: string, maxCells: number): string {
  6. if (displayWidth(value) <= maxCells) return value;
  7. if (maxCells <= TRUNCATION_SUFFIX.length) return value.slice(0, Math.max(0, maxCells));
  8. let cells = 0;
  9. let output = "";
  10. for (const char of value) {
  11. const width = terminalCharWidth(char);
  12. if (cells + width > maxCells - TRUNCATION_SUFFIX.length) break;
  13. output += char;
  14. cells += width;
  15. }
  16. return `${output}${TRUNCATION_SUFFIX}`;
  17. }
  18. export function wordWrappedLineCount(value: string, maxCells: number): number {
  19. const widthLimit = Math.max(1, Math.floor(maxCells));
  20. let lineCount = 0;
  21. for (const paragraph of value.split(LINE_BREAK_PATTERN)) {
  22. const words = paragraph.trim().split(WHITESPACE_PATTERN).filter(Boolean);
  23. if (words.length === 0) {
  24. lineCount += 1;
  25. continue;
  26. }
  27. let currentLineWidth = 0;
  28. for (const word of words) {
  29. const wordWidth = displayWidth(word);
  30. if (currentLineWidth === 0) {
  31. const wrapped = wrappedTokenLineCount(wordWidth, widthLimit);
  32. lineCount += wrapped - 1;
  33. currentLineWidth = wrappedTokenRemainderWidth(wordWidth, widthLimit);
  34. continue;
  35. }
  36. const nextLineWidth = currentLineWidth + INTER_WORD_SPACE_WIDTH + wordWidth;
  37. if (nextLineWidth <= widthLimit) {
  38. currentLineWidth = nextLineWidth;
  39. continue;
  40. }
  41. lineCount += 1;
  42. const wrapped = wrappedTokenLineCount(wordWidth, widthLimit);
  43. lineCount += wrapped - 1;
  44. currentLineWidth = wrappedTokenRemainderWidth(wordWidth, widthLimit);
  45. }
  46. lineCount += 1;
  47. }
  48. return lineCount;
  49. }
  50. export function displayWidth(value: string): number {
  51. let width = 0;
  52. for (const char of value) width += terminalCharWidth(char);
  53. return width;
  54. }
  55. function terminalCharWidth(char: string): number {
  56. const code = char.codePointAt(0) ?? 0;
  57. if (code === 0 || code < 32 || (code >= 0x7f && code < 0xa0)) return 0;
  58. if (
  59. code >= 0x1100 &&
  60. (code <= 0x115f ||
  61. code === 0x2329 ||
  62. code === 0x232a ||
  63. (code >= 0x2e80 && code <= 0xa4cf) ||
  64. (code >= 0xac00 && code <= 0xd7a3) ||
  65. (code >= 0xf900 && code <= 0xfaff) ||
  66. (code >= 0xfe10 && code <= 0xfe19) ||
  67. (code >= 0xfe30 && code <= 0xfe6f) ||
  68. (code >= 0xff00 && code <= 0xff60) ||
  69. (code >= 0xffe0 && code <= 0xffe6))
  70. ) {
  71. return 2;
  72. }
  73. return 1;
  74. }
  75. function wrappedTokenLineCount(width: number, widthLimit: number): number {
  76. return Math.max(1, Math.ceil(width / widthLimit));
  77. }
  78. function wrappedTokenRemainderWidth(width: number, widthLimit: number): number {
  79. if (width <= 0) return 0;
  80. const remainder = width % widthLimit;
  81. return remainder === 0 ? widthLimit : remainder;
  82. }