Created
December 30, 2015 16:26
-
-
Save ignasbernotas/84499ff9394b12baa0ff to your computer and use it in GitHub Desktop.
Christmas tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Ask santa for less fugly code next year | |
* | |
* Happy new year from Ignas:) | |
*/ | |
//Open alternate window | |
fwrite(STDOUT, "\0337\033[?47h"); | |
$top = " | |
| | |
\\ ' / | |
"; | |
$base = " | |
>*< | |
>O<@< | |
>>>@<<* | |
>@>*<O<<< | |
>*>>@<<<@<< | |
>@>>O<<<*<<@< | |
>*>>O<<@<<<@<<< | |
>@>>*<<@<>*<<O<*< | |
>O>>*<<@<>O><<*<@<< | |
>*>>@><O<<*>>@><*<O<< | |
>@>>O<*<O>>@<<O<<<*<@<< | |
>O>>*<<@>O><*<O><@<<<O<*< | |
| | | |
| | | |
/___\ | |
"; | |
/** | |
* Add sparkling top | |
* | |
* @param $top | |
* @param int $size | |
* @return string | |
*/ | |
function sparkleTop($top, $size = 1) | |
{ | |
$topLight = '- (@) -'; | |
$topLight = str_repeat(' ', 5 - $size) . preg_replace('/-{1,}/', str_repeat('-', $size), $topLight); | |
$top = $top . $topLight; | |
return $top; | |
} | |
/** | |
* Switch toys to add animation | |
* | |
* @param $tree | |
* @param bool|false $reverse | |
* @return string | |
*/ | |
function switchToys($tree, $reverse = false) | |
{ | |
$replace = ["*" => "@", "@" => "*"]; | |
if ($reverse) { | |
$replace = array_flip($replace); | |
} | |
$tree = strtr($tree, $replace); | |
return $tree; | |
} | |
/** | |
* Color text | |
* | |
* @param $text | |
* @param $color | |
* @return string | |
*/ | |
function color($text, $color) | |
{ | |
$colors = [ | |
'red' => "\033[31m", | |
'green' => "\033[32m", | |
'yellow' => "\033[33m" | |
]; | |
$end = "\033[0m"; | |
$text = $colors[$color] . $text . $end; | |
return $text; | |
} | |
// color all <> symbols green | |
$base = str_replace( | |
['<', '>'], | |
[ | |
color('<', 'green'), | |
color('>', 'green') | |
], | |
$base | |
); | |
$reverse = true; | |
$iteration = 0; | |
// animate it all | |
while (true) { | |
$reverse = !$reverse; | |
// clear screen | |
fwrite(STDOUT, "\033[H\033[2J"); | |
$output = $base; | |
if ($iteration % 3 === 0) { | |
$base = switchToys($base, $reverse); | |
$iteration = 0; | |
} | |
// color toys | |
$output = str_replace('*', color('*', 'red'), $base); | |
$output = str_replace('@', color('@', 'yellow'), $output); | |
$topping = sparkleTop($top, $iteration); | |
$output = $topping . $output; | |
$iteration++; | |
echo $output; | |
usleep(500000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment