colour_prompt.rb 3.4 KB

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