Last active
August 29, 2015 14:19
-
-
Save zwetan/a905af90ef5ed64bd443 to your computer and use it in GitHub Desktop.
ANSI colors in your AS3 scripts ;)
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
#!/usr/bin/as3shebang -- | |
// see http://en.wikipedia.org/wiki/ANSI_escape_code | |
package ansi | |
{ | |
import flash.utils.describeType; | |
import shell.Program; | |
public class controls | |
{ | |
public static var reset:String = "\x1b[0m"; | |
public static var bold:String = "\x1b[1m"; | |
public static var underline:String = "\x1b[4m"; | |
public static var flash:String = "\x1b[5m"; | |
public static var invert:String = "\x1b[7m"; | |
public static var conceal:String = "\x1b[8m"; | |
public function controls() | |
{ | |
super(); | |
} | |
} | |
public class colors | |
{ | |
//colors | |
public static var black:String = "\x1b[30m"; | |
public static var red:String = "\x1b[31m"; | |
public static var green:String = "\x1b[32m"; | |
public static var yellow:String = "\x1b[33m"; | |
public static var blue:String = "\x1b[34m"; | |
public static var magenta:String = "\x1b[35m"; | |
public static var cyan:String = "\x1b[36m"; | |
public static var white:String = "\x1b[37m"; | |
//extra colors | |
public static var brightBlack:String = "\x1b[90m"; | |
public static var brightRed:String = "\x1b[91m"; | |
public static var brightGreen:String = "\x1b[92m"; | |
public static var brightYellow:String = "\x1b[93m"; | |
public static var brightBlue:String = "\x1b[94m"; | |
public static var brightMagenta:String = "\x1b[95m"; | |
public static var brightCyan:String = "\x1b[96m"; | |
public static var brightWhite:String = "\x1b[97m"; | |
public static function all():Array | |
{ | |
var _class:XML = describeType( colors ); | |
var categories:Array = []; | |
for each( var member:XML in _class.variable ) | |
{ | |
categories.push( member.@name ); | |
} | |
Program.disposeXML( _class ); | |
return categories; | |
} | |
public function colors() | |
{ | |
super(); | |
} | |
} | |
public class backgrounds | |
{ | |
//backgrounds | |
public static var black:String = "\x1b[40m"; | |
public static var red:String = "\x1b[41m"; | |
public static var green:String = "\x1b[42m"; | |
public static var yellow:String = "\x1b[43m"; | |
public static var blue:String = "\x1b[44m"; | |
public static var magenta:String = "\x1b[45m"; | |
public static var cyan:String = "\x1b[46m"; | |
public static var white:String = "\x1b[47m"; | |
//extra backgrounds | |
public static var brightBlack:String = "\x1b[100m"; | |
public static var brightRed:String = "\x1b[101m"; | |
public static var brightGreen:String = "\x1b[102m"; | |
public static var brightYellow:String = "\x1b[103m"; | |
public static var brightBlue:String = "\x1b[104m"; | |
public static var brightMagenta:String = "\x1b[105m"; | |
public static var brightCyan:String = "\x1b[106m"; | |
public static var brightWhite:String = "\x1b[107m"; | |
public static function all():Array | |
{ | |
var _class:XML = describeType( backgrounds ); | |
var categories:Array = []; | |
for each( var member:XML in _class.variable ) | |
{ | |
categories.push( member.@name ); | |
} | |
Program.disposeXML( _class ); | |
return categories; | |
} | |
public function backgrounds() | |
{ | |
super(); | |
} | |
} | |
public function colorize( str:String, ...c ):String | |
{ | |
var s:String = ""; | |
if( c.length > 0 ) | |
{ | |
for( var i:uint = 0; i < c.length; i++ ) | |
{ | |
s += c[i]; | |
} | |
return s + str + controls.reset; | |
} | |
return str; | |
} | |
} | |
import ansi.*; | |
function testColors():void | |
{ | |
var name:String; | |
var names:Array = colors.all(); | |
for( var i:uint = 0; i < names.length; i++ ) | |
{ | |
name = names[i]; | |
if( name == "black" ) | |
{ | |
trace( colorize( name, colors[ name ], backgrounds.white ) ); | |
continue; | |
} | |
trace( colorize( name, colors[ name ] ) ); | |
} | |
} | |
function testBackgrounds():void | |
{ | |
var name:String; | |
var names:Array = backgrounds.all(); | |
for( var i:uint = 0; i < names.length; i++ ) | |
{ | |
name = names[i]; | |
if( name == "black" ) | |
{ | |
trace( colorize( name, backgrounds[ name ], colors.white ) ); | |
continue; | |
} | |
trace( colorize( name, backgrounds[ name ], colors.black ) ); | |
} | |
} | |
trace( "hello world" ); | |
trace( "" ); | |
trace( "----" ); | |
trace( "colors:" ); | |
testColors( "hello world" ); | |
trace( "----" ); | |
trace( "" ); | |
trace( "----" ); | |
trace( "backgrounds:" ); | |
testBackgrounds( "hello world" ); | |
trace( "----" ); | |
trace( "" ); | |
var logo:String = ""; | |
var msg:String = ""; | |
logo += "\n"; | |
logo += " █ \n"; | |
logo += " █████ █ ██ █ █ ██ █ \n"; | |
logo += " ██ ██ █ ██ ███████ ██ █ █ █ ██ \n"; | |
logo += " █ ██ ██ █████ █████ ███████ ██████ █ ████ ██████ █ ████ ██ ████ \n"; | |
logo += " ███ █ ██ ██ ██ ███ █ ██ ███ █████ █ █ ██ ████ █ ████ ██ \n"; | |
logo += " ███████ ███████ █ ██ █ ██ ██ ██ ███ ████ ██ ██ █ ██ █ \n"; | |
logo += " ████ ████ █ ██ █ ██ ██ ██ █ ████ ██ ██ █ ██ █ ██ \n"; | |
logo += " ██ ███ ████ █ ██████ ██ ███████ ██ █ █ ██ ██████ █ █ ██ ██ \n"; | |
logo += " █ ██ ████ █████ █ ██ ████ █ █ █ ██ ███ █ █ █ █ ██ \n"; | |
logo += " █ "; | |
msg += " ★ hello world ★"; | |
trace( " " ); | |
trace( colorize( logo, colors.brightRed, controls.bold, backgrounds.red ) ); | |
trace( colorize( msg, colors.yellow, controls.flash ) ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
also demo a bit of code reflection with
flash.utils.describeType