-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use and require readline instead of reinventing readline
readline is enabled by default since php7.1 (also in windows)
- Loading branch information
Showing
5 changed files
with
56 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
}, | ||
"require": { | ||
"php": "^7.1", | ||
"ext-readline": "*", | ||
"ext-mbstring": "*", | ||
"psr/log": "^1.0" | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters