Created
March 26, 2012 09:31
-
-
Save kbarber/2204167 to your computer and use it in GitHub Desktop.
windows colors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'Win32API' | |
| SetConsoleTextAttribute = Win32API.new('kernel32', 'SetConsoleTextAttribute', 'LL', 'I') | |
| GetConsoleScreenBufferInfo = Win32API.new('kernel32', 'GetConsoleScreenBufferInfo', 'LP', 'I') | |
| GetStdHandle = Win32API.new('kernel32', 'GetStdHandle', 'L', 'L') | |
| def SetConsoleTextAttribute(attribute) | |
| SetConsoleTextAttribute.call(@handle, attribute) != 0 | |
| end | |
| def GetStdHandle(std_handle) | |
| GetStdHandle.call(std_handle) | |
| end | |
| def GetConsoleScreenBufferInfo(buf_info) | |
| GetConsoleScreenBufferInfo.call(@handle, buf_info) != 0 | |
| end | |
| STD_INPUT_HANDLE = -10 | |
| STD_OUTPUT_HANDLE = -11 | |
| STD_ERROR_HANDLE = -12 | |
| RESET = 0x0000 | |
| FOREGROUND_BLUE = 0x0001 | |
| FOREGROUND_GREEN = 0x0002 | |
| FOREGROUND_RED = 0x0004 | |
| FOREGROUND_INTENSITY = 0x0008 | |
| BACKGROUND_BLUE = 0x0010 | |
| BACKGROUND_GREEN = 0x0020 | |
| BACKGROUND_RED = 0x0040 | |
| BACKGROUND_INTENSITY = 0x0080 | |
| @handle = GetStdHandle(STD_OUTPUT_HANDLE) | |
| lpBuffer = ' ' * 22 | |
| GetConsoleScreenBufferInfo(lpBuffer) | |
| info = lpBuffer.unpack('SSSSSssssSS') | |
| # Get existing console attributes | |
| @color_attributes = info[4] | |
| def purple(text) | |
| SetConsoleTextAttribute(FOREGROUND_BLUE|FOREGROUND_RED) | |
| print text | |
| reset | |
| end | |
| def blue(text) | |
| SetConsoleTextAttribute(FOREGROUND_BLUE) | |
| print text | |
| reset | |
| end | |
| def reset | |
| SetConsoleTextAttribute(@color_attributes) | |
| end | |
| purple("foo") | |
| blue("bar\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment