File: prosediff.php

package info (click to toggle)
phabricator 0~git20190207-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 71,728 kB
  • sloc: php: 552,244; sql: 16,997; ansic: 3,619; yacc: 2,503; sh: 754; xml: 519; lex: 488; cpp: 221; python: 186; makefile: 177; sed: 66
file content (50 lines) | stat: -rwxr-xr-x 1,078 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env php
<?php

require_once dirname(__FILE__).'/../__init_script__.php';

$args = new PhutilArgumentParser($argv);
$args->setTagline(pht('show prose differences'));
$args->setSynopsis(<<<EOHELP
**prosediff.php** __old__ __new__ [__options__]
    Diff two prose files.

EOHELP
);

$args->parseStandardArguments();
$args->parse(
  array(
    array(
      'name'      => 'files',
      'wildcard'  => true,
    ),
  ));

$files = $args->getArg('files');
if (count($files) !== 2) {
  $args->printHelpAndExit();
}
$old_file = head($files);
$new_file = last($files);

$old_data = Filesystem::readFile($old_file);
$new_data = Filesystem::readFile($new_file);

$engine = new PhutilProseDifferenceEngine();

$prose_diff = $engine->getDiff($old_data, $new_data);

foreach ($prose_diff->getParts() as $part) {
  switch ($part['type']) {
    case '-':
      echo tsprintf('<bg:red>%B</bg>', $part['text']);
      break;
    case '+':
      echo tsprintf('<bg:green>%B</bg>', $part['text']);
      break;
    case '=':
      echo tsprintf('%B', $part['text']);
      break;
  }
}