utf8.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "toys.h"
  2. // Show width many columns, negative means from right edge, out=0 just measure
  3. // if escout, send it unprintable chars, otherwise pass through raw data.
  4. // Returns width in columns, moves *str to end of data consumed.
  5. int crunch_str(char **str, int width, FILE *out, char *escmore,
  6. int (*escout)(FILE *out, int cols, int wc))
  7. {
  8. int columns = 0, col, bytes;
  9. char *start, *end;
  10. unsigned wc;
  11. for (end = start = *str; *end; columns += col, end += bytes) {
  12. if ((bytes = utf8towc(&wc, end, 4))>0 && (col = wcwidth(wc))>=0) {
  13. if (!escmore || wc>255 || !strchr(escmore, wc)) {
  14. if (width-columns<col) break;
  15. if (out) fwrite(end, bytes, 1, out);
  16. continue;
  17. }
  18. }
  19. if (bytes<1) {
  20. bytes = 1;
  21. wc = *end;
  22. }
  23. col = width-columns;
  24. if (col<1) break;
  25. if (escout) {
  26. if ((col = escout(out, col, wc))<0) break;
  27. } else if (out) fwrite(end, 1, bytes, out);
  28. }
  29. *str = end;
  30. return columns;
  31. }
  32. // standard escapes: ^X if <32, <XX> if invalid UTF8, U+XXXX if UTF8 !iswprint()
  33. int crunch_escape(FILE *out, int cols, int wc)
  34. {
  35. char buf[11];
  36. int rc;
  37. if (wc<' ') rc = sprintf(buf, "^%c", '@'+wc);
  38. else if (wc<256) rc = sprintf(buf, "<%02X>", wc);
  39. else rc = sprintf(buf, "U+%04X", wc);
  40. if (rc > cols) buf[rc = cols] = 0;
  41. if (out) fputs(buf, out);
  42. return rc;
  43. }
  44. // Display "standard" escapes in reverse video.
  45. int crunch_rev_escape(FILE *out, int cols, int wc)
  46. {
  47. int rc;
  48. xputsn("\e[7m");
  49. rc = crunch_escape(out, cols, wc);
  50. xputsn("\e[27m");
  51. return rc;
  52. }
  53. // Write width chars at start of string to strdout with standard escapes
  54. // Returns length in columns so caller can pad it out with spaces.
  55. int draw_str(char *start, int width)
  56. {
  57. return crunch_str(&start, width, stdout, 0, crunch_rev_escape);
  58. }
  59. // Return utf8 columns
  60. int utf8len(char *str)
  61. {
  62. return crunch_str(&str, INT_MAX, 0, 0, crunch_rev_escape);
  63. }
  64. // Return bytes used by (up to) this many columns
  65. int utf8skip(char *str, int width)
  66. {
  67. char *s = str;
  68. crunch_str(&s, width, 0, 0, crunch_rev_escape);
  69. return s-str;
  70. }
  71. // Print utf8 to stdout with standard escapes, trimmed to width and padded
  72. // out to padto. If padto<0 left justify. Returns columns printed
  73. int draw_trim_esc(char *str, int padto, int width, char *escmore,
  74. int (*escout)(FILE *out, int cols, int wc))
  75. {
  76. int apad = abs(padto), len = utf8len(str);
  77. if (padto>=0 && len>width) str += utf8skip(str, len-width);
  78. if (len>width) len = width;
  79. // Left pad if right justified
  80. if (padto>0 && apad>len) printf("%*s", apad-len, "");
  81. crunch_str(&str, len, stdout, 0, crunch_rev_escape);
  82. if (padto<0 && apad>len) printf("%*s", apad-len, "");
  83. return (apad > len) ? apad : len;
  84. }
  85. // draw_trim_esc() with default escape
  86. int draw_trim(char *str, int padto, int width)
  87. {
  88. return draw_trim_esc(str, padto, width, 0, 0);
  89. }