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.

printDiffs.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.diffStringsRaw =
  6. exports.diffStringsUnified =
  7. exports.createPatchMark =
  8. exports.printDiffLines =
  9. exports.printAnnotation =
  10. exports.countChanges =
  11. exports.hasCommonDiff =
  12. exports.printCommonLine =
  13. exports.printInsertLine =
  14. exports.printDeleteLine =
  15. void 0;
  16. var _cleanupSemantic = require('./cleanupSemantic');
  17. var _diffLines = require('./diffLines');
  18. var _diffStrings = _interopRequireDefault(require('./diffStrings'));
  19. var _getAlignedDiffs = _interopRequireDefault(require('./getAlignedDiffs'));
  20. var _joinAlignedDiffs = require('./joinAlignedDiffs');
  21. var _normalizeDiffOptions = require('./normalizeDiffOptions');
  22. function _interopRequireDefault(obj) {
  23. return obj && obj.__esModule ? obj : {default: obj};
  24. }
  25. /**
  26. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  27. *
  28. * This source code is licensed under the MIT license found in the
  29. * LICENSE file in the root directory of this source tree.
  30. */
  31. const formatTrailingSpaces = (line, trailingSpaceFormatter) =>
  32. line.replace(/\s+$/, match => trailingSpaceFormatter(match));
  33. const printDiffLine = (
  34. line,
  35. isFirstOrLast,
  36. color,
  37. indicator,
  38. trailingSpaceFormatter,
  39. emptyFirstOrLastLinePlaceholder
  40. ) =>
  41. line.length !== 0
  42. ? color(
  43. indicator + ' ' + formatTrailingSpaces(line, trailingSpaceFormatter)
  44. )
  45. : indicator !== ' '
  46. ? color(indicator)
  47. : isFirstOrLast && emptyFirstOrLastLinePlaceholder.length !== 0
  48. ? color(indicator + ' ' + emptyFirstOrLastLinePlaceholder)
  49. : '';
  50. const printDeleteLine = (
  51. line,
  52. isFirstOrLast,
  53. {
  54. aColor,
  55. aIndicator,
  56. changeLineTrailingSpaceColor,
  57. emptyFirstOrLastLinePlaceholder
  58. }
  59. ) =>
  60. printDiffLine(
  61. line,
  62. isFirstOrLast,
  63. aColor,
  64. aIndicator,
  65. changeLineTrailingSpaceColor,
  66. emptyFirstOrLastLinePlaceholder
  67. );
  68. exports.printDeleteLine = printDeleteLine;
  69. const printInsertLine = (
  70. line,
  71. isFirstOrLast,
  72. {
  73. bColor,
  74. bIndicator,
  75. changeLineTrailingSpaceColor,
  76. emptyFirstOrLastLinePlaceholder
  77. }
  78. ) =>
  79. printDiffLine(
  80. line,
  81. isFirstOrLast,
  82. bColor,
  83. bIndicator,
  84. changeLineTrailingSpaceColor,
  85. emptyFirstOrLastLinePlaceholder
  86. );
  87. exports.printInsertLine = printInsertLine;
  88. const printCommonLine = (
  89. line,
  90. isFirstOrLast,
  91. {
  92. commonColor,
  93. commonIndicator,
  94. commonLineTrailingSpaceColor,
  95. emptyFirstOrLastLinePlaceholder
  96. }
  97. ) =>
  98. printDiffLine(
  99. line,
  100. isFirstOrLast,
  101. commonColor,
  102. commonIndicator,
  103. commonLineTrailingSpaceColor,
  104. emptyFirstOrLastLinePlaceholder
  105. );
  106. exports.printCommonLine = printCommonLine;
  107. const hasCommonDiff = (diffs, isMultiline) => {
  108. if (isMultiline) {
  109. // Important: Ignore common newline that was appended to multiline strings!
  110. const iLast = diffs.length - 1;
  111. return diffs.some(
  112. (diff, i) =>
  113. diff[0] === _cleanupSemantic.DIFF_EQUAL &&
  114. (i !== iLast || diff[1] !== '\n')
  115. );
  116. }
  117. return diffs.some(diff => diff[0] === _cleanupSemantic.DIFF_EQUAL);
  118. };
  119. exports.hasCommonDiff = hasCommonDiff;
  120. const countChanges = diffs => {
  121. let a = 0;
  122. let b = 0;
  123. diffs.forEach(diff => {
  124. switch (diff[0]) {
  125. case _cleanupSemantic.DIFF_DELETE:
  126. a += 1;
  127. break;
  128. case _cleanupSemantic.DIFF_INSERT:
  129. b += 1;
  130. break;
  131. }
  132. });
  133. return {
  134. a,
  135. b
  136. };
  137. };
  138. exports.countChanges = countChanges;
  139. const printAnnotation = (
  140. {
  141. aAnnotation,
  142. aColor,
  143. aIndicator,
  144. bAnnotation,
  145. bColor,
  146. bIndicator,
  147. includeChangeCounts,
  148. omitAnnotationLines
  149. },
  150. changeCounts
  151. ) => {
  152. if (omitAnnotationLines) {
  153. return '';
  154. }
  155. let aRest = '';
  156. let bRest = '';
  157. if (includeChangeCounts) {
  158. const aCount = String(changeCounts.a);
  159. const bCount = String(changeCounts.b); // Padding right aligns the ends of the annotations.
  160. const baAnnotationLengthDiff = bAnnotation.length - aAnnotation.length;
  161. const aAnnotationPadding = ' '.repeat(Math.max(0, baAnnotationLengthDiff));
  162. const bAnnotationPadding = ' '.repeat(Math.max(0, -baAnnotationLengthDiff)); // Padding left aligns the ends of the counts.
  163. const baCountLengthDiff = bCount.length - aCount.length;
  164. const aCountPadding = ' '.repeat(Math.max(0, baCountLengthDiff));
  165. const bCountPadding = ' '.repeat(Math.max(0, -baCountLengthDiff));
  166. aRest =
  167. aAnnotationPadding + ' ' + aIndicator + ' ' + aCountPadding + aCount;
  168. bRest =
  169. bAnnotationPadding + ' ' + bIndicator + ' ' + bCountPadding + bCount;
  170. }
  171. return (
  172. aColor(aIndicator + ' ' + aAnnotation + aRest) +
  173. '\n' +
  174. bColor(bIndicator + ' ' + bAnnotation + bRest) +
  175. '\n\n'
  176. );
  177. };
  178. exports.printAnnotation = printAnnotation;
  179. const printDiffLines = (diffs, options) =>
  180. printAnnotation(options, countChanges(diffs)) +
  181. (options.expand
  182. ? (0, _joinAlignedDiffs.joinAlignedDiffsExpand)(diffs, options)
  183. : (0, _joinAlignedDiffs.joinAlignedDiffsNoExpand)(diffs, options)); // In GNU diff format, indexes are one-based instead of zero-based.
  184. exports.printDiffLines = printDiffLines;
  185. const createPatchMark = (aStart, aEnd, bStart, bEnd, {patchColor}) =>
  186. patchColor(
  187. `@@ -${aStart + 1},${aEnd - aStart} +${bStart + 1},${bEnd - bStart} @@`
  188. ); // Compare two strings character-by-character.
  189. // Format as comparison lines in which changed substrings have inverse colors.
  190. exports.createPatchMark = createPatchMark;
  191. const diffStringsUnified = (a, b, options) => {
  192. if (a !== b && a.length !== 0 && b.length !== 0) {
  193. const isMultiline = a.includes('\n') || b.includes('\n'); // getAlignedDiffs assumes that a newline was appended to the strings.
  194. const diffs = diffStringsRaw(
  195. isMultiline ? a + '\n' : a,
  196. isMultiline ? b + '\n' : b,
  197. true // cleanupSemantic
  198. );
  199. if (hasCommonDiff(diffs, isMultiline)) {
  200. const optionsNormalized = (0, _normalizeDiffOptions.normalizeDiffOptions)(
  201. options
  202. );
  203. const lines = (0, _getAlignedDiffs.default)(
  204. diffs,
  205. optionsNormalized.changeColor
  206. );
  207. return printDiffLines(lines, optionsNormalized);
  208. }
  209. } // Fall back to line-by-line diff.
  210. return (0, _diffLines.diffLinesUnified)(
  211. a.split('\n'),
  212. b.split('\n'),
  213. options
  214. );
  215. }; // Compare two strings character-by-character.
  216. // Optionally clean up small common substrings, also known as chaff.
  217. exports.diffStringsUnified = diffStringsUnified;
  218. const diffStringsRaw = (a, b, cleanup) => {
  219. const diffs = (0, _diffStrings.default)(a, b);
  220. if (cleanup) {
  221. (0, _cleanupSemantic.cleanupSemantic)(diffs); // impure function
  222. }
  223. return diffs;
  224. };
  225. exports.diffStringsRaw = diffStringsRaw;