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