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