Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
Inline & Padded Box
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximilian Schmidt committed Oct 23, 2018
1 parent 03ce851 commit 6a07ad9
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
72 changes: 71 additions & 1 deletion Classes/Utility/IO.php
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ public function advanceProgress(int $step, string $label = null)
*
* NOTE: This requires to start a progress bar first!
*
* @param string|null An optional label, describing the finalization
* @param string|null $label An optional label, describing the finalization
* @return void
* @throws \InvalidArgumentException
*/
Expand All @@ -638,6 +638,76 @@ public function finishProgress(string $label = null)
$this->currentProgressTotal = null;
}

// Drawing

/**
* Print a padded message box to the terminal.
*
* @param string $message The message to print
* @param int $padding The padding to use horizontally
* @param int $margin The margin to the left edge of the terminal
* @param string $color The foreground color to use
* @param string $background The background color to use
* @return void
*/
public function paddedBox(
string $message,
int $padding = 2,
int $margin = 0,
string $color = null,
string $background = null
) {
$lines = explode(PHP_EOL, $message);

$maxLength = 0;
foreach ($lines as $line) {
if (($length = strlen($line)) > $maxLength) {
$maxLength = $length;
}
}

$maxLength += $padding * 2;

if ($margin !== null && $margin > 0) {
$tmp = '';
for ($i=0; $i < $margin; $i++) {
$tmp += ' ';
}
$margin = $tmp;
} else {
$margin = '';
}

if ($padding > 2) {
$tmp = '';
for ($i=0; $i < $padding; $i++) {
$tmp += ' ';
}
$padding = $tmp;
} else {
$padding = ' ';
}

$this->inline($margin);
for ($i=0; $i < $maxLength; $i++) {
$this->inline(' ', $context, $color, $background);
}
$this->inline(PHP_EOL);

foreach ($lines as $line) {
$this->inline($margin);
$this->inline($padding, [], $color, $background);
$this->out($line, $context, $color, $background);
$this->inline($padding, [], $color, $background);
}

$this->inline($margin);
for ($i = 0; $i < $maxLength; $i++) {
$this->inline(' ', [], $color, $background);
}
$this->inline(PHP_EOL);
}

// Override parent methods with more complex functionality

/**
Expand Down
16 changes: 16 additions & 0 deletions Classes/Utility/RawIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,22 @@ public function out(string $message, array $context = [], string $color = null,
);
}

/**
* Output an inline message to the terminal.
*
* @param string $message The message to print
* @param array|null $context Additional context variables that shall be interpolated into $message
* @param string|null $color The foreground color of the message
* @param string|null $background The background color of the message
* @return void
*/
public function inline(string $message, array $context = [], string $color = null, string $background = null)
{
$this->climate($color, $background)->inline(
$this->interpolate($message, $context)
);
}

/**
* Print a success-message to the terminal.
*
Expand Down

0 comments on commit 6a07ad9

Please sign in to comment.