1284990Scy# ========================================== 2284990Scy# Unity Project - A Test Framework for C 3284990Scy# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams 4284990Scy# [Released under MIT License. Please refer to license.txt for details] 5284990Scy# ========================================== 6284990Scy 7284990Scyif RUBY_PLATFORM =~/(win|w)32$/ 8284990Scy begin 9284990Scy require 'Win32API' 10284990Scy rescue LoadError 11284990Scy puts "ERROR! \"Win32API\" library not found" 12284990Scy puts "\"Win32API\" is required for colour on a windows machine" 13284990Scy puts " try => \"gem install Win32API\" on the command line" 14284990Scy puts 15284990Scy end 16284990Scy # puts 17284990Scy # puts 'Windows Environment Detected...' 18284990Scy # puts 'Win32API Library Found.' 19284990Scy # puts 20284990Scyend 21284990Scy 22284990Scyclass ColourCommandLine 23284990Scy def initialize 24284990Scy if RUBY_PLATFORM =~/(win|w)32$/ 25284990Scy get_std_handle = Win32API.new("kernel32", "GetStdHandle", ['L'], 'L') 26284990Scy @set_console_txt_attrb = 27284990Scy Win32API.new("kernel32","SetConsoleTextAttribute",['L','N'], 'I') 28284990Scy @hout = get_std_handle.call(-11) 29284990Scy end 30284990Scy end 31284990Scy 32284990Scy def change_to(new_colour) 33284990Scy if RUBY_PLATFORM =~/(win|w)32$/ 34284990Scy @set_console_txt_attrb.call(@hout,self.win32_colour(new_colour)) 35284990Scy else 36284990Scy "\033[30;#{posix_colour(new_colour)};22m" 37284990Scy end 38284990Scy end 39284990Scy 40284990Scy def win32_colour(colour) 41284990Scy case colour 42284990Scy when :black then 0 43284990Scy when :dark_blue then 1 44284990Scy when :dark_green then 2 45284990Scy when :dark_cyan then 3 46284990Scy when :dark_red then 4 47284990Scy when :dark_purple then 5 48284990Scy when :dark_yellow, :narrative then 6 49284990Scy when :default_white, :default, :dark_white then 7 50284990Scy when :silver then 8 51284990Scy when :blue then 9 52284990Scy when :green, :success then 10 53284990Scy when :cyan, :output then 11 54284990Scy when :red, :failure then 12 55284990Scy when :purple then 13 56284990Scy when :yellow then 14 57284990Scy when :white then 15 58284990Scy else 59284990Scy 0 60284990Scy end 61284990Scy end 62284990Scy 63284990Scy def posix_colour(colour) 64284990Scy case colour 65284990Scy when :black then 30 66284990Scy when :red, :failure then 31 67284990Scy when :green, :success then 32 68284990Scy when :yellow then 33 69284990Scy when :blue, :narrative then 34 70284990Scy when :purple, :magenta then 35 71284990Scy when :cyan, :output then 36 72284990Scy when :white, :default_white, :default then 37 73284990Scy else 74284990Scy 30 75284990Scy end 76284990Scy end 77284990Scy 78284990Scy def out_c(mode, colour, str) 79284990Scy case RUBY_PLATFORM 80284990Scy when /(win|w)32$/ 81284990Scy change_to(colour) 82284990Scy $stdout.puts str if mode == :puts 83284990Scy $stdout.print str if mode == :print 84284990Scy change_to(:default_white) 85284990Scy else 86284990Scy $stdout.puts("#{change_to(colour)}#{str}\033[0m") if mode == :puts 87284990Scy $stdout.print("#{change_to(colour)}#{str}\033[0m") if mode == :print 88284990Scy end 89284990Scy end 90284990Scyend # ColourCommandLine 91284990Scy 92284990Scydef colour_puts(role,str) ColourCommandLine.new.out_c(:puts, role, str) end 93284990Scydef colour_print(role,str) ColourCommandLine.new.out_c(:print, role, str) end 94284990Scy 95