Toyomasa Watarai / simple-mbed-cloud-client

Dependents:  

Committer:
MACRUM
Date:
Mon Jul 02 06:30:39 2018 +0000
Revision:
0:276e7a263c35
Initial commit

Who changed what in which revision?

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