Skip to content

Commit

Permalink
Fix CLI output if STDOUT is not a tty
Browse files Browse the repository at this point in the history
  • Loading branch information
FO-nTTaX committed Apr 20, 2022
1 parent 8d4d4ca commit 8611bd2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "liquipedia/sqllint",
"description": "A thin wrapper around the SqlParser from the phpMyAdmin project which can be used to lint any amount of sql files from the command line.",
"version": "1.1.0",
"version": "1.1.1",
"type": "library",
"license": "GPL-2.0-or-later",
"keywords": [ "sql", "lint", "junit" ],
Expand Down
54 changes: 46 additions & 8 deletions src/Report/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,32 @@ class CLI extends Report {
*/
private $totalCounter = 0;

/**
* Mark if this is a tty which supports control codes
* @var bool
*/
private $isTty = false;

public function __construct() {
$this->output = PHP_EOL . PHP_EOL;
if ( defined( 'STDOUT' ) ) {
$this->isTty = stream_isatty( STDOUT );
}
}

/**
* @param string $fileName
*/
public function addSuccess( string $fileName ): void {
echo '.';
$this->updateCounter();
$this->addNewLineMaybe();
$this->markProgress( '.' );
}

/**
* @param string $fileName
* @param array<int, array<int, mixed>> $errors
*/
public function addError( string $fileName, array $errors ): void {
echo 'E';
$this->updateCounter();
$this->addNewLineMaybe();
$this->markProgress( 'E' );
$this->exitCode = 1;
$countErrors = count( $errors );
$this->output .= 'FILE: ' . realpath( $fileName ) . PHP_EOL;
Expand All @@ -64,6 +69,19 @@ public function printBody(): void {
}
}

/**
* @param string $letter
*/
private function markProgress( string $letter ): void {
echo $letter;
if ( $this->isTty ) {
$this->updateCounter();
$this->addNewLineMaybe();
} else {
$this->addNonTtyCounterMaybe();
}
}

/**
* Update counters so we can show a nice progress bar
*/
Expand All @@ -73,11 +91,31 @@ private function updateCounter(): void {
echo self::SAVE_CURSOR_POSITION;
echo self::GOTO_COLUMN_62;
echo self::CLEAR_LINE_AFTER_CURRENT_POSITION;
echo $this->totalCounter . '/' . $this->amount
. ' (' . round( ( $this->totalCounter / $this->amount ) * 100, 1 ) . '%)';
echo $this->makeCounterText();
echo self::RESTORE_CURSOR_POSITION;
}

/**
* Update counters so we can show a nice progress bar
*/
private function addNonTtyCounterMaybe(): void {
if ( $this->lineCounter >= 60 ) {
echo ' ';
echo $this->makeCounterText();
echo PHP_EOL;
$this->lineCounter = 0;
}
}

/**
* Make text for percentage
* @return string
*/
private function makeCounterText(): string {
return $this->totalCounter . '/' . $this->amount
. ' (' . round( ( $this->totalCounter / $this->amount ) * 100, 1 ) . '%)';
}

/**
* After 60 files we want a new line so things don't get too wide
*/
Expand Down

0 comments on commit 8611bd2

Please sign in to comment.