Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix output of escape characters when using NullOutput interface #28

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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