Skip to content

Commit

Permalink
ReadLine long input support.
Browse files Browse the repository at this point in the history
  • Loading branch information
onovy committed Oct 20, 2014
1 parent bc00300 commit 0e4799b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions Gui.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ public function start() {

// Screen width
$maxWidth = $this->getTerminalWidth();
$this->readLine->maxWidth = $maxWidth;
$width = -1;
foreach ($lengths as $length) {
$width += $length + 3;
Expand Down
44 changes: 39 additions & 5 deletions ReadLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class ReadLine {
public $timeout = false;
private $completitionCallback = null;
private $completition = '';
public $maxWidth = 80;
private $leftOffset = 0;
private $leftOffsetChange = 10;

public function setCompletitionCallback(callable $callback) {
$this->completitionCallback = $callback;
Expand Down Expand Up @@ -81,6 +84,7 @@ public function read($prefix = '', $prefill = '', $timeout = null) {
$this->line = $prefill;
$this->pos = strlen($this->line);
$this->completition = '';
$this->leftOffset = 0;
}

$this->state = self::READ;
Expand Down Expand Up @@ -119,21 +123,52 @@ public function read($prefix = '', $prefill = '', $timeout = null) {
}

private function repaint() {
$this->checkLeftOffset();

echo PHP_EOL;
echo "\033[K";
echo $this->completition;
if (mb_strlen($this->completition) > $this->maxWidth) {
echo mb_substr($this->completition, 0, $this->maxWidth - 1) . '';
} else {
echo $this->completition;
}
echo "\033[1A";
echo "\033[9999D";
echo "\033[K";

echo $this->prefix;
echo $this->line;
$l = mb_strlen($this->line) - $this->pos;
if ($l) {

$maxLength = $this->maxWidth - mb_strlen($this->prefix);
$length = mb_strlen($this->line) - $this->leftOffset;
if ($this->leftOffset) {
echo '';
$maxLength--;
}
echo mb_substr($this->line, $this->leftOffset, $maxLength - 1);
if ($length >= $maxLength) {
echo "\033[1D";
$length = $maxLength - 1;
}

$l = $length - $this->pos + $this->leftOffset;
if ($l > 0) {
echo "\033[" . $l . "D";
}
}

private function checkLeftOffset() {
$maxLength = $this->maxWidth - mb_strlen($this->prefix) - 2;
if ($this->leftOffset) {
$maxLength--;
}
if ($this->pos <= $this->leftOffset) {
$this->leftOffset = floor($this->pos / $this->leftOffsetChange) * $this->leftOffsetChange;
}
if ($this->pos - $this->leftOffset >= $maxLength) {
$this->leftOffset = ceil(($this->pos - $maxLength) / $this->leftOffsetChange) * $this->leftOffsetChange;
}
}

private function parseInput() {
for ($i = 0 ; $i < mb_strlen($this->input) ; $i++) {
$ch = mb_substr($this->input, $i, 1);
Expand Down Expand Up @@ -181,7 +216,6 @@ private function parseInput() {
} elseif (count($ar) > 1) {
$this->completition = implode(' ', $ar);
}

}
break;
case "\033":
Expand Down

0 comments on commit 0e4799b

Please sign in to comment.