Skip to content

Instantly share code, notes, and snippets.

@Frago9876543210
Last active December 30, 2018 17:04
Show Gist options
  • Save Frago9876543210/a52a6c688fa6434f5403b56bc212dd4f to your computer and use it in GitHub Desktop.
Save Frago9876543210/a52a6c688fa6434f5403b56bc212dd4f to your computer and use it in GitHub Desktop.
Console frames generator.
<?php
declare(strict_types=1);
interface Color{
const BOLD = "\x1b[1m";
const OBFUSCATED = "";
const ITALIC = "\x1b[3m";
const UNDERLINE = "\x1b[4m";
const STRIKETHROUGH = "\x1b[9m";
const RESET = "\x1b[m";
const BLACK = "\x1b[38;5;16m";
const DARK_BLUE = "\x1b[38;5;19m";
const DARK_GREEN = "\x1b[38;5;34m";
const DARK_AQUA = "\x1b[38;5;37m";
const DARK_RED = "\x1b[38;5;124m";
const PURPLE = "\x1b[38;5;127m";
const GOLD = "\x1b[38;5;214m";
const GRAY = "\x1b[38;5;145m";
const DARK_GRAY = "\x1b[38;5;59m";
const BLUE = "\x1b[38;5;63m";
const GREEN = "\x1b[38;5;83m";
const AQUA = "\x1b[38;5;87m";
const RED = "\x1b[38;5;203m";
const LIGHT_PURPLE = "\x1b[38;5;207m";
const YELLOW = "\x1b[38;5;227m";
const WHITE = "\x1b[38;5;231m";
const BLOCK = "\xe2\x96\x88";
}
class Frames{
const HORIZONTAL_WALL = "\xe2\x94\x80";
const VERTICAL_WALL = "\xe2\x94\x82";
const RIGHT_UP_ARC = "\xe2\x95\xad";
const LEFT_UP_ARC = "\xe2\x95\xae";
const RIGHT_DOWN_ARC = "\xe2\x95\xb0";
const LEFT_DOWN_ARC = "\xe2\x95\xaf";
const EOL = "\n";
/** @var string $output */
private $output;
/**
* Frames constructor.
* @param int $width
* @param int $height
* @param string[] $lines
* @param string $border
* @param string $space
*/
public function __construct(int $width, int $height, array $lines = [], string $border = "", string $space = " "){
$width *= 2;
$horizontal = str_repeat(self::HORIZONTAL_WALL, $width);
$this->output .= $border . self::RIGHT_UP_ARC . $horizontal . self::LEFT_UP_ARC . self::EOL;
for($i = 0; $i < $height; $i++){
if(isset($lines[$i])){
$text = $lines[$i];
if(($len = mb_strlen(preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $text))) < $width){
$text .= str_repeat($space, $width - $len);
}
}else{
$text = str_repeat($space, $width);
}
$this->output .= $border . self::VERTICAL_WALL . Color::RESET . $text . $border . self::VERTICAL_WALL . Color::RESET . self::EOL;
}
$this->output .= $border . self::RIGHT_DOWN_ARC . $horizontal . self::LEFT_DOWN_ARC . Color::RESET . self::EOL;
}
public function __toString(){
return $this->output;
}
}
$text = [
Color::RED . "test string" . Color::RESET,
Color::AQUA . "next line!" . Color::RESET
];
//empty frame
echo new Frames(16, 8);
//with colored text
echo new Frames(16, 8, $text);
//with colored border
echo new Frames(16, 8, $text, Color::GOLD);
//with colored stuffing
echo new Frames(16, 8, [], Color::GOLD, Color::DARK_AQUA . Color::BLOCK);
//nice text
$space = Color::DARK_GRAY . Color::BLOCK;
echo new Frames(16, 8, [
"",
"",
"",
str_repeat($space, 10) . Color::YELLOW . "Cool effect!" . Color::RESET . str_repeat($space, 10)
], Color::RED, $space);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment