Skip to content

Commit

Permalink
Support reading from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Apr 5, 2019
1 parent d500a21 commit a215a30
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Utils/CLI.php
Expand Up @@ -77,6 +77,11 @@ public function runHighlight()

return 0;
}
if (!isset($params['q'])) {
if ($stdIn = $this->readStdin()) {
$params['q'] = $stdIn;
}
}
if (isset($params['q'])) {
echo Formatter::format(
$params['q'],
Expand Down Expand Up @@ -127,6 +132,11 @@ public function runLint()
if (isset($params['c'])) {
Context::load($params['c']);
}
if (!isset($params['q'])) {
if ($stdIn = $this->readStdin()) {
$params['q'] = $stdIn;
}
}
if (isset($params['q'])) {
$lexer = new Lexer($params['q'], false);
$parser = new Parser($lexer->list);
Expand Down Expand Up @@ -177,6 +187,11 @@ public function runTokenize()

return 0;
}
if (!isset($params['q'])) {
if ($stdIn = $this->readStdin()) {
$params['q'] = $stdIn;
}
}
if (isset($params['q'])) {
$lexer = new Lexer($params['q'], false);
foreach ($lexer->list->tokens as $idx => $token) {
Expand All @@ -199,4 +214,12 @@ public function runTokenize()

return 1;
}

private function readStdin() {
stream_set_blocking(STDIN, false);
$stdin = stream_get_contents(STDIN);
// restore-default block-mode setting
stream_set_blocking(STDIN, true);
return $stdin;
}
}

0 comments on commit a215a30

Please sign in to comment.