python-imprastorage/impra/w32color.py

72 lines
3.1 KiB
Python
Raw Normal View History

2012-10-01 20:20:56 +00:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# software : ImpraStorage <http://imprastorage.sourceforge.net/> #
# version : 0.7 #
# date : 2012 #
# licence : GPLv3.0 <http://www.gnu.org/licenses/> #
# author : a-Sansara <http://www.a-sansara.net/> #
# copyright : pluie.org <http://www.pluie.org/> #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# This file is part of ImpraStorage.
#
# ImpraStorage is free software (free as in speech) : you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# ImpraStorage is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License
# along with ImpraStorage. If not, see <http://www.gnu.org/licenses/>.
2012-09-20 21:55:37 +00:00
"""
Colors text in console mode application (win32).
Uses ctypes and Win32 methods SetConsoleTextAttribute and
GetConsoleScreenBufferInfo.
$Id: color_console.py 534 2009-05-10 04:00:59Z andre $
"""
2012-10-01 20:20:56 +00:00
from ctypes import windll, Structure as Struct, c_short as SHORT, c_ushort as WORD, byref
2012-09-20 21:55:37 +00:00
2012-10-01 20:20:56 +00:00
class Coord(Struct):
2012-09-20 21:55:37 +00:00
"""struct in wincon.h."""
2012-09-24 17:11:20 +00:00
_fields_ = [("X", SHORT),("Y", SHORT)]
2012-09-20 21:55:37 +00:00
2012-10-01 20:20:56 +00:00
class SmallRect(Struct):
2012-09-20 21:55:37 +00:00
"""struct in wincon.h."""
2012-09-24 17:11:20 +00:00
_fields_ = [("Left", SHORT),("Top", SHORT),("Right", SHORT),("Bottom", SHORT)]
2012-09-20 21:55:37 +00:00
2012-10-01 20:20:56 +00:00
class ConsoleScreenBufferInfo(Struct):
2012-09-20 21:55:37 +00:00
"""struct in wincon.h."""
2012-10-01 20:20:56 +00:00
_fields_ = [("dwSize", Coord),("dwCursorPosition", Coord),("wAttributes", WORD),("srWindow", SmallRect),("dwMaximumWindowSize", Coord)]
2012-09-20 21:55:37 +00:00
# winbase.h
2012-09-24 17:11:20 +00:00
STD_INPUT_HANDLE = -10
2012-09-20 21:55:37 +00:00
STD_OUTPUT_HANDLE = -11
2012-09-24 17:11:20 +00:00
STD_ERROR_HANDLE = -12
2012-09-20 21:55:37 +00:00
stdout_handle = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
SetConsoleTextAttribute = windll.kernel32.SetConsoleTextAttribute
GetConsoleScreenBufferInfo = windll.kernel32.GetConsoleScreenBufferInfo
def get_text_attr():
"""Returns the character attributes (colors) of the console screen
buffer."""
2012-10-01 20:20:56 +00:00
csbi = ConsoleScreenBufferInfo()
2012-09-20 21:55:37 +00:00
GetConsoleScreenBufferInfo(stdout_handle, byref(csbi))
return csbi.wAttributes
def set_text_attr(color):
"""Sets the character attributes (colors) of the console screen
buffer. Color is a combination of foreground and background color,
foreground and background intensity."""
SetConsoleTextAttribute(stdout_handle, color)