Last active
September 18, 2015 00:01
-
-
Save matugm/a713eafefb2ffb3b1c2d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#coding: utf-8 | |
class HighLine | |
module BuiltinStyles | |
def self.included(base) | |
base.extend ClassMethods | |
end | |
STYLES = { | |
erase_line: "\e[K", | |
erase_char: "\e[P", | |
clear: "\e[0m", | |
reset: "\e[0m", | |
bold: "\e[1m", | |
dark: "\e[2m", | |
underline: "\e[4m", | |
underscore: "\e[4m", | |
blink: "\e[5m", | |
reverse: "\e[7m", | |
concealed: "\e[8m" | |
} | |
STYLES.each do |style_name, code| | |
style = String(style_name).upcase | |
const_set style, code | |
const_set style + "_STYLE", Style.new(style_name: style_name, code: code, builtin: true) | |
end | |
# These RGB colors are approximate; see http://en.wikipedia.org/wiki/ANSI_escape_code | |
BLACK_STYLE = Style.new(:name=>:black, :builtin=>true, :code=>"\e[30m", :rgb=>[ 0, 0, 0]) | |
RED_STYLE = Style.new(:name=>:red, :builtin=>true, :code=>"\e[31m", :rgb=>[128, 0, 0]) | |
GREEN_STYLE = Style.new(:name=>:green, :builtin=>true, :code=>"\e[32m", :rgb=>[ 0,128, 0]) | |
BLUE_STYLE = Style.new(:name=>:blue, :builtin=>true, :code=>"\e[34m", :rgb=>[ 0, 0,128]) | |
YELLOW_STYLE = Style.new(:name=>:yellow, :builtin=>true, :code=>"\e[33m", :rgb=>[128,128, 0]) | |
MAGENTA_STYLE = Style.new(:name=>:magenta, :builtin=>true, :code=>"\e[35m", :rgb=>[128, 0,128]) | |
CYAN_STYLE = Style.new(:name=>:cyan, :builtin=>true, :code=>"\e[36m", :rgb=>[ 0,128,128]) | |
# On Mac OSX Terminal, white is actually gray | |
WHITE_STYLE = Style.new(:name=>:white, :builtin=>true, :code=>"\e[37m", :rgb=>[192,192,192]) | |
# Alias for WHITE, since WHITE is actually a light gray on Macs | |
GRAY_STYLE = Style.new(:name=>:gray, :builtin=>true, :code=>"\e[37m", :rgb=>[192,192,192]) | |
GREY_STYLE = Style.new(:name=>:grey, :builtin=>true, :code=>"\e[37m", :rgb=>[192,192,192]) | |
# On Mac OSX Terminal, this is black foreground, or bright white background. | |
# Also used as base for RGB colors, if available | |
NONE_STYLE = Style.new(:name=>:none, :builtin=>true, :code=>"\e[38m", :rgb=>[ 0, 0, 0]) | |
BASIC_COLORS = %w{BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE GRAY GREY NONE} | |
colors = BASIC_COLORS.dup | |
BASIC_COLORS.each do |color| | |
bright_color = "BRIGHT_#{color}" | |
colors << bright_color | |
const_set bright_color + '_STYLE', const_get(color + '_STYLE').bright | |
light_color = "LIGHT_#{color}" | |
colors << light_color | |
const_set light_color + '_STYLE', const_get(color + '_STYLE').light | |
end | |
COLORS = colors | |
colors.each do |color| | |
const_set color, const_get("#{color}_STYLE").code | |
const_set "ON_#{color}_STYLE", const_get("#{color}_STYLE").on | |
const_set "ON_#{color}", const_get("ON_#{color}_STYLE").code | |
end | |
ON_NONE_STYLE.rgb = [255,255,255] # Override; white background | |
module ClassMethods | |
RGB_COLOR = /^(ON_)?(RGB_)([A-F0-9]{6})(_STYLE)?$/ | |
# For RGB colors: - add example here - | |
def const_missing(name) | |
if name.to_s =~ RGB_COLOR | |
on = $1 | |
suffix = $4 | |
if suffix | |
code_name = $1.to_s + $2 + $3 | |
else | |
code_name = name.to_s | |
end | |
style_name = code_name + '_STYLE' | |
style = Style.rgb($3) | |
style = style.on if on | |
const_set(style_name, style) | |
const_set(code_name, style.code) | |
suffix ? style : style.code | |
else | |
raise NameError, "Bad color or uninitialized constant #{name}" | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment