Skip to content

Commit

Permalink
use and require readline instead of reinventing readline
Browse files Browse the repository at this point in the history
readline is enabled by default since php7.1 (also in windows)
  • Loading branch information
tflori committed Aug 23, 2018
1 parent fda6f54 commit b8b773b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 69 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"require": {
"php": "^7.1",
"ext-readline": "*",
"ext-mbstring": "*",
"psr/log": "^1.0"
},
Expand Down
47 changes: 47 additions & 0 deletions src/Input/ReadlineHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Hugga\Input;

class ReadlineHandler extends AbstractInputHandler
{
public function readLine(string $prompt = null): string
{
return readLine($prompt ?? " \e[D");
}

public function read(int $count, string $prompt = null): string
{
$str = $this->readConditional(function ($str) use ($count) {
return strlen($str) >= $count;
}, $prompt);
return $str;
}

public function readUntil(string $sequence, string $prompt = null): string
{
$seqLen = strlen($sequence);
$str = $this->readConditional(function ($str) use ($sequence, $seqLen) {
return substr($str, -$seqLen) === $sequence;
}, $prompt);
return substr($str, 0, -$seqLen);
}

protected function readConditional(callable $conditionMet, string $prompt = null): string
{
$previous = '';
readline_callback_handler_install($prompt ?? " \e[D", function ($str) use (&$previous) {
$previous .= $str . PHP_EOL;
});
do {
$r = array(STDIN);
$n = stream_select($r, $w, $e, null);
if ($n && in_array(STDIN, $r)) {
readline_callback_read_char();
$str = $previous . readline_info('line_buffer');
}
} while (!$conditionMet($str));
readline_callback_handler_remove();

return $str;
}
}
10 changes: 5 additions & 5 deletions src/Input/ResourceHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@ class ResourceHandler extends AbstractInputHandler
{
const BUFFER_SIZE = 4096;

public function readLine()
public function readLine(string $prompt = null): string
{
// reset the position to read new lines
fseek($this->resource, ftell($this->resource));
return (string)fgets($this->resource);
return rtrim(fgets($this->resource));
}

public function read(int $count)
public function read(int $count, string $prompt = null): string
{
// reset the position to read new lines
fseek($this->resource, ftell($this->resource));
return fread($this->resource, $count);
}

public function readUntil(string $sequence)
public function readUntil(string $sequence, string $prompt = null): string
{
$currentPos = ftell($this->resource);
fseek($this->resource, $currentPos);
$seqLen = strlen($sequence);
$buffer = '';
do {
Expand All @@ -37,7 +38,6 @@ public function readUntil(string $sequence)
$str = substr($buffer, 0, $pos);
$strLen = strlen($str);
if ((strlen($buffer) - $seqLen) > $strLen) {
echo 'seeking to ' . ($currentPos + $strLen + $seqLen) . PHP_EOL;
fseek($this->resource, $currentPos + $strLen + $seqLen);
}

Expand Down
61 changes: 0 additions & 61 deletions src/Input/TtyHandler.php

This file was deleted.

6 changes: 3 additions & 3 deletions src/InputInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

interface InputInterface
{
public function readLine();
public function readLine(string $prompt = null): string;

public function read(int $count);
public function read(int $count, string $prompt = null): string;

public function readUntil(string $sequence);
public function readUntil(string $sequence, string $prompt = null): string;
}

0 comments on commit b8b773b

Please sign in to comment.