Skip to content

Instantly share code, notes, and snippets.

@btrsco
Last active November 20, 2019 03:57
Show Gist options
  • Save btrsco/5a121705e43b1fff79333234eb59a29e to your computer and use it in GitHub Desktop.
Save btrsco/5a121705e43b1fff79333234eb59a29e to your computer and use it in GitHub Desktop.
Output Styling Helper Functions for Symfony
<?php
/**
* Output Styling Functions
* - - -
* This set of functions is for use alongside Symfony,
* Laravel Artisan or Laravel Zero.
* - - -
* @author: Miguel Batres
*/
/**
* Return an Output Style string
*
* @param string $string
* @param string $foreground
* @param string|null $background
* @param array|null $options
* @return string
*/
function style( string $string, string $foreground = 'default', string $background = null, array $options = [] )
{
$styleString = '';
$styleArray = [];
if ( !is_null( $foreground ) && !empty( $foreground ) ) {
$foreground = strtolower( $foreground );
array_push( $styleArray, "fg=$foreground" );
}
if ( !is_null( $background ) && !empty( $background ) ) {
$background = strtolower( $background );
array_push( $styleArray, "bg=$background" );
}
if ( !is_null( $options ) && !empty( $options ) ) {
$optionString = 'options=';
foreach ( $options as $option ) {
$option = strtolower( $option );
$optionString .= "$option,";
}
array_push( $styleArray, trim( $optionString, ',' ) );
}
foreach ( $styleArray as $style ) {
$styleString .= "$style;";
}
$styleString = trim( $styleString, ';' );
return "<$styleString>$string</>";
}
/**
* Return a Href Output Style string
*
* @param string $string
* @param string $href
* @return string
*/
function href( string $string, string $href )
{
return "<href=$href>$string</>";
}
/**
* Output a Static Sized Alert Based on String Length
*
* Note: Can break awkwardly with long strings, try adding
* new line breaks ("\n") every 50 - 75 characters
*
* @param string $string
* @param string $foreground
* @param string $background
* @param array $options
* @param int $x_padding
* @return string
*/
function alert( string $string, string $foreground = 'black', string $background = 'white', array $options = [], int $x_padding = 2 )
{
$output = '';
$stringParts = explode( "\n", $string );
$longestLineLength = 0;
foreach ( $stringParts as $part ) {
if ( strlen( $part ) > $longestLineLength ) $longestLineLength = strlen( $part );
}
$x_pad = str_repeat( ' ', $x_padding );
$y_pad = str_repeat( ' ', $longestLineLength + ( $x_padding * 2 ) );
$output .= style( "$y_pad", $foreground, $background, $options ) . "\n";
foreach ( $stringParts as $part ) {
$output .= style( $x_pad . str_pad( trim( $part ), $longestLineLength ) . $x_pad, $foreground, $background, $options ) . "\n";
}
$output .= style( "$y_pad", $foreground, $background, $options );
return $output;
}
/**
* Output Black Text Styling
*
* @param string $string
* @param string|null $background
* @param array $options
* @return string
*/
function black( string $string, string $background = null, array $options = [] )
{
return style( $string, 'black', $background, $options );
}
/**
* Output Red Text Styling
*
* @param string $string
* @param string|null $background
* @param array $options
* @return string
*/
function red( string $string, string $background = null, array $options = [] )
{
return style( $string, 'red', $background, $options );
}
/**
* Output Green Text Styling
*
* @param string $string
* @param string|null $background
* @param array $options
* @return string
*/
function green( string $string, string $background = null, array $options = [] )
{
return style( $string, 'green', $background, $options );
}
/**
* Output Yellow Text Styling
*
* @param string $string
* @param string|null $background
* @param array $options
* @return string
*/
function yellow( string $string, string $background = null, array $options = [] )
{
return style( $string, 'yellow', $background, $options );
}
/**
* Output Blue Text Styling
*
* @param string $string
* @param string|null $background
* @param array $options
* @return string
*/
function blue( string $string, string $background = null, array $options = [] )
{
return style( $string, 'blue', $background, $options );
}
/**
* Output Magenta Text Styling
*
* @param string $string
* @param string|null $background
* @param array $options
* @return string
*/
function magenta( string $string, string $background = null, array $options = [] )
{
return style( $string, 'magenta', $background, $options );
}
/**
* Output Cyan Text Styling
*
* @param string $string
* @param string|null $background
* @param array $options
* @return string
*/
function cyan( string $string, string $background = null, array $options = [] )
{
return style( $string, 'cyan', $background, $options );
}
/**
* Output White Text Styling
*
* @param string $string
* @param string|null $background
* @param array $options
* @return string
*/
function white( string $string, string $background = null, array $options = [] )
{
return style( $string, 'white', $background, $options );
}
/**
* Output Classic Error Styling
*
* @param string $string
* @param array $options
* @return string
*/
function error(string $string, array $options = [] )
{
return style( $string, 'white', 'red', $options );
}
/**
* Output Classic Info Styling
*
* @param string $string
* @param array $options
* @return string
*/
function information( string $string, string $background = null, array $options = [] )
{
return style( $string, 'green', $background, $options );
}
/**
* Output Classic Comment Styling
*
* @param string $string
* @param array $options
* @return string
*/
function comment( string $string, string $background = null, array $options = [] )
{
return style( $string, 'yellow', $background, $options );
}
/**
* Output Classic Question Styling
*
* @param string $string
* @param array $options
* @return string
*/
function question( string $string, array $options = [] )
{
return style( $string, 'black', 'cyan', $options );
}
/**
* Output Custom Warning Styling
*
* @param string $string
* @param array $options
* @return string
*/
function warning( string $string, array $options = [] )
{
return style( $string, 'black', 'yellow', $options );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment