@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3/**
4 * Simple syntax highlighter for console output. We just try to highlight the
5 * commands so it's easier to follow transcripts.
6 */
7final class PhutilConsoleSyntaxHighlighter extends Phobject {
8
9 private $config = array();
10
11 public function setConfig($key, $value) {
12 $this->config[$key] = $value;
13 return $this;
14 }
15
16 public function getHighlightFuture($source) {
17 $in_command = false;
18 $lines = explode("\n", $source);
19 foreach ($lines as $key => $line) {
20 $matches = null;
21
22 // Parse commands like this:
23 //
24 // some/path/ $ ./bin/example # Do things
25 //
26 // ...into path, command, and comment components.
27
28 $pattern =
29 '@'.
30 ($in_command ? '()(.*?)' : '^(\S+[\\\\/] )?([$] .*?)').
31 '(#.*|\\\\)?$@';
32
33 if (preg_match($pattern, $line, $matches)) {
34 $lines[$key] = hsprintf(
35 '%s<span class="gp">%s</span>%s',
36 $matches[1],
37 $matches[2],
38 (!empty($matches[3])
39 ? hsprintf('<span class="k">%s</span>', $matches[3])
40 : ''));
41 $in_command = (idx($matches, 3) == '\\');
42 } else {
43 $lines[$key] = hsprintf('<span class="go">%s</span>', $line);
44 }
45 }
46 $lines = phutil_implode_html("\n", $lines);
47
48 return new ImmediateFuture($lines);
49 }
50
51}