Skip to content

Commit

Permalink
fixing formatting again
Browse files Browse the repository at this point in the history
  • Loading branch information
joetannenbaum committed Sep 26, 2023
1 parent 7f340a4 commit 268cb2f
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions src/TextareaPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(
$this->trackTypedValue(
default: $default,
submit: false,
ignore: fn ($key) => $key === Key::ENTER,
allowNewLine: true,
);

$this->reduceScrollingToFitTerminal();
Expand All @@ -41,11 +41,6 @@ public function __construct(
$this->on(
'key',
function ($key) {
if ($key === Key::ENTER) {
$this->typedValue = mb_substr($this->typedValue, 0, $this->cursorPosition) . $key . mb_substr($this->typedValue, $this->cursorPosition);
$this->cursorPosition++;
}

if ($key[0] === "\e") {
match ($key) {
Key::UP, Key::UP_ARROW, Key::CTRL_P => $this->handleUpKey(),
Expand Down Expand Up @@ -139,6 +134,19 @@ protected function handleDownKey(): void
*/
public function visible(): array
{
$this->adjustVisibleWindow();

$withCursor = $this->valueWithCursor(10_000);

return array_slice(explode(PHP_EOL, $withCursor), $this->firstVisible, $this->scroll, preserve_keys: true);
}

protected function adjustVisibleWindow(): void
{
if (count($this->lines()) < $this->scroll) {
return;
}

$currentLineIndex = $this->currentLineIndex();

if ($this->firstVisible + $this->scroll <= $currentLineIndex) {
Expand All @@ -153,10 +161,6 @@ public function visible(): array
if ($this->firstVisible + $this->scroll > count($this->lines())) {
$this->firstVisible = count($this->lines()) - $this->scroll;
}

$withCursor = $this->valueWithCursor(10_000);

return array_slice(explode(PHP_EOL, $withCursor), $this->firstVisible, $this->scroll, preserve_keys: true);
}

protected function currentLineIndex(): int
Expand All @@ -175,13 +179,7 @@ public function lines(): array
// TODO: Figure out the real number here, this comes from the renderer?
$value = wordwrap($this->value(), 59, PHP_EOL, true);

$lines = explode(PHP_EOL, $value);

while (count($lines) < $this->scroll) {
$lines[] = '';
}

return $lines;
return explode(PHP_EOL, $value);
}

/**
Expand All @@ -191,11 +189,11 @@ public function valueWithCursor(int $maxWidth): string
{
$value = implode(PHP_EOL, $this->lines());

if ($value === '') {
return $this->dim($this->addCursor($this->placeholder, 0, $maxWidth));
if ($this->value() === '') {
return $this->dim($this->addCursor($this->placeholder, 0, 10_000));
}

// TODO: Deal with max width properly
return $this->addCursor($value, $this->cursorPosition, $maxWidth);
return $this->addCursor($value, $this->cursorPosition, 10_000);
}
}

0 comments on commit 268cb2f

Please sign in to comment.