Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

align.ts 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { Transform } from './index';
  2. import { Markers, Block, Line } from '../primitives';
  3. import { rewireSource } from '../util';
  4. interface Width {
  5. start: number;
  6. tag: number;
  7. type: number;
  8. name: number;
  9. }
  10. const zeroWidth = {
  11. start: 0,
  12. tag: 0,
  13. type: 0,
  14. name: 0,
  15. };
  16. const getWidth = (w: Width, { tokens: t }: Line) => ({
  17. start: t.delimiter === Markers.start ? t.start.length : w.start,
  18. tag: Math.max(w.tag, t.tag.length),
  19. type: Math.max(w.type, t.type.length),
  20. name: Math.max(w.name, t.name.length),
  21. });
  22. const space = (len: number) => ''.padStart(len, ' ');
  23. export default function align(): Transform {
  24. let intoTags = false;
  25. let w: Width;
  26. function update(line: Line): Line {
  27. const tokens = { ...line.tokens };
  28. if (tokens.tag !== '') intoTags = true;
  29. const isEmpty =
  30. tokens.tag === '' &&
  31. tokens.name === '' &&
  32. tokens.type === '' &&
  33. tokens.description === '';
  34. // dangling '*/'
  35. if (tokens.end === Markers.end && isEmpty) {
  36. tokens.start = space(w.start + 1);
  37. return { ...line, tokens };
  38. }
  39. switch (tokens.delimiter) {
  40. case Markers.start:
  41. tokens.start = space(w.start);
  42. break;
  43. case Markers.delim:
  44. tokens.start = space(w.start + 1);
  45. break;
  46. default:
  47. tokens.delimiter = '';
  48. tokens.start = space(w.start + 2); // compensate delimiter
  49. }
  50. if (!intoTags) {
  51. tokens.postDelimiter = tokens.description === '' ? '' : ' ';
  52. return { ...line, tokens };
  53. }
  54. const nothingAfter = {
  55. delim: false,
  56. tag: false,
  57. type: false,
  58. name: false,
  59. };
  60. if (tokens.description === '') {
  61. nothingAfter.name = true;
  62. tokens.postName = '';
  63. if (tokens.name === '') {
  64. nothingAfter.type = true;
  65. tokens.postType = '';
  66. if (tokens.type === '') {
  67. nothingAfter.tag = true;
  68. tokens.postTag = '';
  69. if (tokens.tag === '') {
  70. nothingAfter.delim = true;
  71. }
  72. }
  73. }
  74. }
  75. tokens.postDelimiter = nothingAfter.delim ? '' : ' ';
  76. if (!nothingAfter.tag)
  77. tokens.postTag = space(w.tag - tokens.tag.length + 1);
  78. if (!nothingAfter.type)
  79. tokens.postType = space(w.type - tokens.type.length + 1);
  80. if (!nothingAfter.name)
  81. tokens.postName = space(w.name - tokens.name.length + 1);
  82. return { ...line, tokens };
  83. }
  84. return ({ source, ...fields }: Block): Block => {
  85. w = source.reduce(getWidth, { ...zeroWidth });
  86. return rewireSource({ ...fields, source: source.map(update) });
  87. };
  88. }