Skip to content

Commit

Permalink
Use output interface for cursor movement and erasing (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
jessarcher authored Aug 2, 2023
1 parent 9f955bc commit a5295e1
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/Concerns/Cursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ trait Cursor
*/
public function hideCursor(): void
{
$this->terminal()->write("\e[?25l");
static::writeDirectly("\e[?25l");

static::$cursorHidden = true;
}
Expand All @@ -24,7 +24,7 @@ public function hideCursor(): void
*/
public function showCursor(): void
{
$this->terminal()->write("\e[?25h");
static::writeDirectly("\e[?25h");

static::$cursorHidden = false;
}
Expand Down Expand Up @@ -58,6 +58,6 @@ public function moveCursor(int $x, int $y = 0): void
$sequence .= "\e[{$y}B"; // Down
}

$this->terminal()->write($sequence);
static::writeDirectly($sequence);
}
}
4 changes: 2 additions & 2 deletions src/Concerns/Erase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ public function eraseLines(int $count): void
$clear .= "\e[G";
}

$this->terminal()->write($clear);
static::writeDirectly($clear);
}

/**
* Erase from cursor until end of screen.
*/
public function eraseDown(): void
{
$this->terminal()->write("\e[J");
static::writeDirectly("\e[J");
}
}
8 changes: 8 additions & 0 deletions src/Output/ConsoleOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@ protected function doWrite(string $message, bool $newline): void
$this->newLinesWritten = $trailingNewLines;
}
}

/**
* Write output directly, bypassing newline capture.
*/
public function writeDirectly(string $message): void
{
parent::doWrite($message, false);
}
}
12 changes: 12 additions & 0 deletions src/Prompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ protected static function output(): OutputInterface
return self::$output ??= new ConsoleOutput();
}

/**
* Write output directly, bypassing newline capture.
*/
protected static function writeDirectly(string $message): void
{
match (true) {
method_exists(static::output(), 'writeDirectly') => static::output()->writeDirectly($message),
method_exists(static::output(), 'getOutput') => static::output()->getOutput()->write($message),
default => static::output()->write($message),
};
}

/**
* Get the terminal instance.
*/
Expand Down
8 changes: 0 additions & 8 deletions src/Terminal.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ public function read(): string
return $input !== false ? $input : '';
}

/**
* Write data to the terminal.
*/
public function write(string $data): void
{
fwrite(STDOUT, $data);
}

/**
* Set the TTY mode.
*/
Expand Down

0 comments on commit a5295e1

Please sign in to comment.