colour_prompt.rb 3.4 KB

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