string.tin 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #nop vim: set filetype=tt:;
  2. /*
  3. 本文件属于 PaoTin++ 的一部分。
  4. PaoTin++ © 2020~2023 的所有版权均由担子炮(dzp <danzipao@gmail.com>) 享有并保留一切法律权利
  5. 你可以在遵照 GPLv3 协议的基础之上使用、修改及重新分发本程序。
  6. */
  7. #func {len} {#format result {%L} {%0}};
  8. #func {width} {#format result {%W} {%0}};
  9. #func {space} {#var result {@repeat{%0;{ }}}};
  10. #func {repeat} {#var result {}; #loop 1 %1 tmp {#cat result {%2}}};
  11. #func {reverseStr} {#var result {}; #parse {%1} {tmp} {#var result {$tmp$result}}};
  12. #func {trim} {#format result {%p} {%0}};
  13. #func {trimAll} {#var result {%0}; #replace {result} {%+1..s} {}};
  14. #func {plain} {#format result {%P} {%0}};
  15. #func {replace} {#var result %1; #replace result {%2} {%3}};
  16. #func {toLower} {#format result {%l} {%0}};
  17. #func {toUpper} {#format result {%u} {%0}};
  18. #func {capital} {#format result {%n} {%0}};
  19. #func {char} {#format result {%X} {%0}; #format result {%x} {$result}};
  20. #func {codepoint} {#format result {%A} {%0}};
  21. #func {hex2char} {#format result {%x} {%0}};
  22. #func {char2hex} {#format result {%A} {%0}; #format result {%X} {$result}};
  23. #func {format} {#format result {%1} {%2}};
  24. #func {left} {#local width {%2}; #if {$width<=0} {#return {}} {#format result {%.${width}s} {%1}}};
  25. #func {right} {#local width {%2}; #if {$width<=0} {#return {}} {#format result {%.-${width}s} {%1}}};
  26. #func {substr} {#return {@left{{@right{{%1}; @eval{@width{%1} - %2}}}; %3}}};
  27. #func {alignLeft} {#format result {%-%2s} {%1}};
  28. #func {alignRight} {#format result {%+%2s} {%1}};
  29. #func {alignCenter} {
  30. #local str {%1};
  31. #local max {@default{%2;80}};
  32. #local width {@width{$str}};
  33. #local left {};
  34. #local right {};
  35. #math left {($max - $width) / 2 + $width};
  36. #math right {$max - $left};
  37. #format result {%+${left}s%${right}s} {%1} {};
  38. #return {$result};
  39. };
  40. #func {colorBar} {
  41. #local str {%1};
  42. #local args {};
  43. #list args create {%0};
  44. #list args delete {1} {1};
  45. #local parts {};
  46. #list parts create {};
  47. #local count {0};
  48. #local sum {0};
  49. #while { $count < &args[] } {
  50. #local color {$args[@eval{$count + 1}]};
  51. #local weight {@defaultNum{$args[@eval{$count + 2}];0}};
  52. #list parts {add} {{
  53. {color}{$color}
  54. {weight}{$weight}
  55. }};
  56. #math count {$count + 2};
  57. #math sum {$sum + $weight};
  58. };
  59. #local elem {};
  60. #local len {@len{$str}};
  61. #local leftLen {0};
  62. #local leftWeight {0};
  63. #local colorStr {};
  64. #foreach {$parts[%*]} {elem} {
  65. #local elemLen {@eval{($elem[weight] + $leftWeight) * $len / $sum - $leftLen}};
  66. #local text {@substr{{$str};$leftLen;$elemLen}};
  67. #cat colorStr {$elem[color]$text<099>};
  68. #math leftLen {$leftLen + $elemLen};
  69. #math leftWeight {$leftWeight + $elem[weight]};
  70. };
  71. #return {$colorStr};
  72. };