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