ESP32-basiertes Kraftort-Suchger�t mit GPS, LED-Ring und PlatformIO-Firmware.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.4KB

  1. # =========================================================================
  2. # Unity - A Test Framework for C
  3. # ThrowTheSwitch.org
  4. # Copyright (c) 2007-25 Mike Karlesky, Mark VanderVoord, & Greg Williams
  5. # SPDX-License-Identifier: MIT
  6. # =========================================================================
  7. if 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
  20. end
  21. class ColourCommandLine
  22. def initialize
  23. return unless RUBY_PLATFORM =~ /(win|w)32$/
  24. get_std_handle = Win32API.new('kernel32', 'GetStdHandle', ['L'], 'L')
  25. @set_console_txt_attrb =
  26. Win32API.new('kernel32', 'SetConsoleTextAttribute', %w[L N], 'I')
  27. @hout = get_std_handle.call(-11)
  28. end
  29. def change_to(new_colour)
  30. if RUBY_PLATFORM =~ /(win|w)32$/
  31. @set_console_txt_attrb.call(@hout, 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
  105. def colour_puts(role, str)
  106. ColourCommandLine.new.out_c(:puts, role, str)
  107. end
  108. def colour_print(role, str)
  109. ColourCommandLine.new.out_c(:print, role, str)
  110. end