• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
7if 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
20end
21
22class ColourCommandLine
23  def initialize
24    return unless RUBY_PLATFORM =~ /(win|w)32$/
25    get_std_handle = Win32API.new('kernel32', 'GetStdHandle', ['L'], 'L')
26    @set_console_txt_attrb =
27      Win32API.new('kernel32', 'SetConsoleTextAttribute', %w(L N), 'I')
28    @hout = get_std_handle.call(-11)
29  end
30
31  def change_to(new_colour)
32    if RUBY_PLATFORM =~ /(win|w)32$/
33      @set_console_txt_attrb.call(@hout, win32_colour(new_colour))
34    else
35      "\033[30;#{posix_colour(new_colour)};22m"
36    end
37  end
38
39  def win32_colour(colour)
40    case colour
41    when :black then 0
42    when :dark_blue then 1
43    when :dark_green then 2
44    when :dark_cyan then 3
45    when :dark_red then 4
46    when :dark_purple then 5
47    when :dark_yellow, :narrative then 6
48    when :default_white, :default, :dark_white then 7
49    when :silver then 8
50    when :blue then 9
51    when :green, :success then 10
52    when :cyan, :output then 11
53    when :red, :failure then 12
54    when :purple then 13
55    when :yellow then 14
56    when :white then 15
57    else
58      0
59    end
60  end
61
62  def posix_colour(colour)
63    # ANSI Escape Codes - Foreground colors
64    # | Code | Color                     |
65    # | 39   | Default foreground color  |
66    # | 30   | Black                     |
67    # | 31   | Red                       |
68    # | 32   | Green                     |
69    # | 33   | Yellow                    |
70    # | 34   | Blue                      |
71    # | 35   | Magenta                   |
72    # | 36   | Cyan                      |
73    # | 37   | Light gray                |
74    # | 90   | Dark gray                 |
75    # | 91   | Light red                 |
76    # | 92   | Light green               |
77    # | 93   | Light yellow              |
78    # | 94   | Light blue                |
79    # | 95   | Light magenta             |
80    # | 96   | Light cyan                |
81    # | 97   | White                     |
82
83    case colour
84    when :black then 30
85    when :red, :failure then 31
86    when :green, :success then 32
87    when :yellow then 33
88    when :blue, :narrative then 34
89    when :purple, :magenta then 35
90    when :cyan, :output then 36
91    when :white, :default_white then 37
92    when :default then 39
93    else
94      39
95    end
96  end
97
98  def out_c(mode, colour, str)
99    case RUBY_PLATFORM
100    when /(win|w)32$/
101      change_to(colour)
102      $stdout.puts str if mode == :puts
103      $stdout.print str if mode == :print
104      change_to(:default_white)
105    else
106      $stdout.puts("#{change_to(colour)}#{str}\033[0m") if mode == :puts
107      $stdout.print("#{change_to(colour)}#{str}\033[0m") if mode == :print
108    end
109  end
110end # ColourCommandLine
111
112def colour_puts(role, str)
113  ColourCommandLine.new.out_c(:puts, role, str)
114end
115
116def colour_print(role, str)
117  ColourCommandLine.new.out_c(:print, role, str)
118end
119