Skip to content
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
22 changes: 22 additions & 0 deletions src/Agentic/TerminalDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Rector\Agentic;

/**
* Detects whether Rector runs in an interactive terminal, so non-interactive callers - pipes, CI,
* agents - get clean machine output instead of human chrome (progress bar, ANSI, prompts).
*/
final class TerminalDetector
{
public static function isOutputTty(): bool
{
return defined('STDOUT') && stream_isatty(STDOUT);
}

public static function isInputTty(): bool
{
return defined('STDIN') && stream_isatty(STDIN);
}
}
3 changes: 2 additions & 1 deletion src/Configuration/ConfigInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Configuration;

use Nette\Utils\FileSystem;
use Rector\Agentic\TerminalDetector;
use Rector\Bootstrap\RectorConfigsResolver;
use Rector\Contract\Rector\RectorInterface;
use Rector\FileSystem\InitFilePathsResolver;
Expand Down Expand Up @@ -54,7 +55,7 @@ public function createConfig(string $projectDirectory): void

// non-interactive terminal, e.g. piped output, CI or an agent: never prompt or silently write a
// config, just say what to do - Symfony still treats a closed STDIN as interactive here
if (! defined('STDIN') || ! stream_isatty(STDIN)) {
if (! TerminalDetector::isInputTty()) {
$this->symfonyStyle->warning(sprintf(
'No "%s" config found. Create one, or pass "--config <path>".',
RectorConfigsResolver::DEFAULT_CONFIG_FILE
Expand Down
8 changes: 2 additions & 6 deletions src/Configuration/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\Configuration;

use Rector\Agentic\TerminalDetector;
use Rector\ChangesReporting\Output\ConsoleOutputFormatter;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\FileSystem\FilePathFilter;
Expand Down Expand Up @@ -175,7 +176,7 @@ private function shouldShowProgressBar(InputInterface $input, string $outputForm
}

// no interactive terminal, e.g. piped output, CI or an agent - the redraws are just noise
if (! $this->isTtyOutput()) {
if (! TerminalDetector::isOutputTty()) {
return false;
}

Expand All @@ -186,11 +187,6 @@ private function shouldShowProgressBar(InputInterface $input, string $outputForm
return $outputFormat === ConsoleOutputFormatter::NAME;
}

private function isTtyOutput(): bool
{
return defined('STDOUT') && stream_isatty(STDOUT);
}

private function shouldShowDiffs(InputInterface $input): bool
{
$noDiffs = (bool) $input->getOption(Option::NO_DIFFS);
Expand Down
3 changes: 2 additions & 1 deletion src/Console/Style/SymfonyStyleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\Console\Style;

use Rector\Agentic\TerminalDetector;
use Rector\Util\Reflection\PrivatesAccessor;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
Expand Down Expand Up @@ -41,7 +42,7 @@ public function create(): RectorStyle
}

// no interactive terminal, e.g. piped output, CI or an agent - never emit ANSI, even if forced via --ansi
if (! defined('STDOUT') || ! stream_isatty(STDOUT)) {
if (! TerminalDetector::isOutputTty()) {
$consoleOutput->setDecorated(false);
}

Expand Down
Loading