| 1 |
# ========================================== |
| 2 |
# Unity Project - A Test Framework for C |
| 3 |
# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams |
| 4 |
# [Released under MIT License. Please refer to license.txt for details] |
| 5 |
# ========================================== |
| 6 |
|
| 7 |
if RUBY_PLATFORM =~ /(win|w)32$/ |
| 8 |
begin |
| 9 |
require 'Win32API' |
| 10 |
rescue LoadError |
| 11 |
puts 'ERROR! "Win32API" library not found' |
| 12 |
puts '"Win32API" is required for colour on a windows machine' |
| 13 |
puts ' try => "gem install Win32API" on the command line' |
| 14 |
puts |
| 15 |
end |
| 16 |
# puts |
| 17 |
# puts 'Windows Environment Detected...' |
| 18 |
# puts 'Win32API Library Found.' |
| 19 |
# puts |
| 20 |
end |
| 21 |
|
| 22 |
class ColourCommandLine |
| 23 |
def initialize |
| 24 |
return unless RUBY_PLATFORM =~ /(win|w)32$/ |
| 25 |
|
| 26 |
get_std_handle = Win32API.new('kernel32', 'GetStdHandle', ['L'], 'L') |
| 27 |
@set_console_txt_attrb = |
| 28 |
Win32API.new('kernel32', 'SetConsoleTextAttribute', %w[L N], 'I') |
| 29 |
@hout = get_std_handle.call(-11) |
| 30 |
end |
| 31 |
|
| 32 |
def change_to(new_colour) |
| 33 |
if RUBY_PLATFORM =~ /(win|w)32$/ |
| 34 |
@set_console_txt_attrb.call(@hout, win32_colour(new_colour)) |
| 35 |
else |
| 36 |
"\033[30;#{posix_colour(new_colour)};22m" |
| 37 |
end |
| 38 |
end |
| 39 |
|
| 40 |
def win32_colour(colour) |
| 41 |
case colour |
| 42 |
when :black then 0 |
| 43 |
when :dark_blue then 1 |
| 44 |
when :dark_green then 2 |
| 45 |
when :dark_cyan then 3 |
| 46 |
when :dark_red then 4 |
| 47 |
when :dark_purple then 5 |
| 48 |
when :dark_yellow, :narrative then 6 |
| 49 |
when :default_white, :default, :dark_white then 7 |
| 50 |
when :silver then 8 |
| 51 |
when :blue then 9 |
| 52 |
when :green, :success then 10 |
| 53 |
when :cyan, :output then 11 |
| 54 |
when :red, :failure then 12 |
| 55 |
when :purple then 13 |
| 56 |
when :yellow then 14 |
| 57 |
when :white then 15 |
| 58 |
else |
| 59 |
0 |
| 60 |
end |
| 61 |
end |
| 62 |
|
| 63 |
def posix_colour(colour) |
| 64 |
# ANSI Escape Codes - Foreground colors |
| 65 |
# | Code | Color | |
| 66 |
# | 39 | Default foreground color | |
| 67 |
# | 30 | Black | |
| 68 |
# | 31 | Red | |
| 69 |
# | 32 | Green | |
| 70 |
# | 33 | Yellow | |
| 71 |
# | 34 | Blue | |
| 72 |
# | 35 | Magenta | |
| 73 |
# | 36 | Cyan | |
| 74 |
# | 37 | Light gray | |
| 75 |
# | 90 | Dark gray | |
| 76 |
# | 91 | Light red | |
| 77 |
# | 92 | Light green | |
| 78 |
# | 93 | Light yellow | |
| 79 |
# | 94 | Light blue | |
| 80 |
# | 95 | Light magenta | |
| 81 |
# | 96 | Light cyan | |
| 82 |
# | 97 | White | |
| 83 |
|
| 84 |
case colour |
| 85 |
when :black then 30 |
| 86 |
when :red, :failure then 31 |
| 87 |
when :green, :success then 32 |
| 88 |
when :yellow then 33 |
| 89 |
when :blue, :narrative then 34 |
| 90 |
when :purple, :magenta then 35 |
| 91 |
when :cyan, :output then 36 |
| 92 |
when :white, :default_white then 37 |
| 93 |
when :default then 39 |
| 94 |
else |
| 95 |
39 |
| 96 |
end |
| 97 |
end |
| 98 |
|
| 99 |
def out_c(mode, colour, str) |
| 100 |
case RUBY_PLATFORM |
| 101 |
when /(win|w)32$/ |
| 102 |
change_to(colour) |
| 103 |
$stdout.puts str if mode == :puts |
| 104 |
$stdout.print str if mode == :print |
| 105 |
change_to(:default_white) |
| 106 |
else |
| 107 |
$stdout.puts("#{change_to(colour)}#{str}\033[0m") if mode == :puts |
| 108 |
$stdout.print("#{change_to(colour)}#{str}\033[0m") if mode == :print |
| 109 |
end |
| 110 |
end |
| 111 |
end |
| 112 |
|
| 113 |
def colour_puts(role, str) |
| 114 |
ColourCommandLine.new.out_c(:puts, role, str) |
| 115 |
end |
| 116 |
|
| 117 |
def colour_print(role, str) |
| 118 |
ColourCommandLine.new.out_c(:print, role, str) |
| 119 |
end |