/* * Please use the PSR autoloader instead of include() when possible! */ include dirname(__FILE__) . '/../src/SimpleAnsiEscape.php'; use SimpleAnsiEscape\SimpleAnsiEscape as esc; echo "\n"; $printAll = true; $testText = 'Just after exclaiming how delicious it looked, A.B. quickly ate his very sizable piece of cake.'; echo str_pad('Escape Alias', 15, ' ', STR_PAD_LEFT) . " | Demo Typeset Text\n"; echo ' ' . str_repeat('-', 15) . '|' . str_repeat('-', strlen($testText)) . "\n"; $sortedMap = esc::$ansiMap; // Sort disregarding special characters uksort($sortedMap, function ($a, $b) { if (substr($a, 0, 1) == '~') { $negate = 1; } else { $negate = 0; } $a = preg_replace('/^[~]+/', '', $a); $b = preg_replace('/^[~]+/', '', $b); return strcasecmp($a, $b) + $negate; }); $alreadyPrinted = []; foreach ($sortedMap as $escapeAlias => $escapeInteger) { // Just prevents aliases from being printed more than once, has nothing to do with ansi escape codes particularly. if ($printAll == true || !isset($alreadyPrinted[$escapeInteger])) { $alreadyPrinted[$escapeInteger] = true; echo str_pad($escapeAlias, 15, ' ', STR_PAD_LEFT) . ' | '; echo esc::ansiEscape($escapeAlias) . $testText . esc::ansiEscape() . "\n"; } }
// Finally, let's print this buffer we've been working on: echo $buffer; // Still, we have 'open ended' terminal escapes: white text, blue background, // and now bold and underlined. Pretty gaudy if you ask me. Let's reset our // terminal back to default. We could have just as easily added this to our // buffer before we 'echo'd it out. Calling ansiEscape() with no parameters is // synonymous to ansiEscape('reset'). echo esc::ansiEscape() . "\n\n"; /* * Additional Examples: * -------------------- */ // A good use case for the one-parameter variant is when we do not get text // returned and instead output directly, we can ue the one-parameter to negate // the need for output buffering, ex.: echo esc::ansiEscape('faint'); // set our format // var_dump() is a good example of a function that does not return it's output var_dump("Don't forget to run printEscAliases.php for a full list of aliases!"); // But of course don't forget to reset afterwards, or we'll bleed into our shell! echo esc::ansiEscape(); // resets the terminal echo "\n"; /* * Demonstrations of one-paramter variants: */ echo esc::ansiEscape("bold, color/blue", "Simple ANSI Escape: http://github.com/abcarroll/simple-ansi-escape") . "\n"; echo esc::ansiEscape('text/pink', "Many thanks to the Wikipedia Foundation & The Contributers to the " . "Wikipedia ANSI Escape Codes Wiki Page") . "\n"; /* * End */