Skip to content

Instantly share code, notes, and snippets.

@AbePralle
Last active May 20, 2020 20:32
Show Gist options
  • Select an option

  • Save AbePralle/68a97ec7bdacccee987e993446415e93 to your computer and use it in GitHub Desktop.

Select an option

Save AbePralle/68a97ec7bdacccee987e993446415e93 to your computer and use it in GitHub Desktop.
Rogue ConsoleStyle
class ConsoleStyle
# USAGE
# println "$Hello World!$" (ConsoleStyle().fg_white.bg_red, ConsoleStyle().reset)
#
# OR
# ConsoleStyle.print( ConsoleStyle.FG_WHITE )
# ConsoleStyle.print( ConsoleStyle.BG_RED )
# print( "Hello World!" )
# ConsoleStyle.print( ConsoleStyle.RESET )
# println
GLOBAL PROPERTIES
RESET = 0
BOLD = 1
DIM = 2
UNDERLINE = 4
BLINK = 5
INVERSE = 7
HIDDEN = 8
BOLD_OFF = 21
DIM_OFF = 22
UNDERLINE_OFF = 24
BLINK_OFF = 25
INVERSE_OFF = 27
HIDDEN_OFF = 28
FG_DEFAULT = 39
FG_BLACK = 30
FG_RED = 31
FG_GREEN = 32
FG_YELLOW = 33
FG_BLUE = 34
FG_MAGENTA = 35
FG_CYAN = 36
FG_LIGHT_GRAY = 37
FG_DARK_GRAY = 90
FG_LIGHT_RED = 91
FG_LIGHT_GREEN = 92
FG_LIGHT_YELLOW = 93
FG_LIGHT_BLUE = 94
FG_LIGHT_MAGENTA = 95
FG_LIGHT_CYAN = 96
FG_WHITE = 97
BG_DEFAULT = 49
BG_BLACK = 40
BG_RED = 41
BG_GREEN = 42
BG_YELLOW = 43
BG_BLUE = 44
BG_MAGENTA = 45
BG_CYAN = 46
BG_LIGHT_GRAY = 47
BG_DARK_GRAY = 100
BG_LIGHT_RED = 101
BG_LIGHT_GREEN = 102
BG_LIGHT_YELLOW = 103
BG_LIGHT_BLUE = 104
BG_LIGHT_MAGENTA = 105
BG_LIGHT_CYAN = 106
BG_WHITE = 107
GLOBAL METHODS
method print( code:Int32 )
Global.print( "\e[" )
Global.print( code )
Global.print( "m" )
method print( code:Int32, builder:StringBuilder )
builder.print( "\e[" ).print( code ).print( "m" )
PROPERTIES
codes = Int32[]
METHODS
method to->String
if (codes.is_empty) return ""
return "\e[$m" (codes.join(";"))
endClass
# Use metacode to generate a modifier method for each code.
# For example, for FG_RED the following method is generated:
# method fg_red->this
# codes.add( FG_RED )
# return this
$metacode<ConsoleStyle.organize_methods>
forEach (p in global_list)
sourceln "method $->this" (p.name.to_lowercase)
sourceln " codes.add( $ )" (p.name)
sourceln " return this"
injectMethod
endForEach
$endMetacode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment