Source code for userinterfaces.terminal_interface_win32
"""User interface for Win32 terminals."""## (C) Pywikibot team, 2003-2024## Distributed under the terms of the MIT license.#from__future__importannotationsimportctypesfrompywikibot.userinterfacesimportterminal_interface_basewindowsColors={'default':7,'black':0,'blue':1,'green':2,'aqua':3,'red':4,'purple':5,'yellow':6,'lightgray':7,'gray':8,'lightblue':9,'lightgreen':10,'lightaqua':11,'lightred':12,'lightpurple':13,'lightyellow':14,'white':15,}
[docs]classWin32UI(terminal_interface_base.UI):"""User interface for Win32 terminals."""
[docs]defsupport_color(self,target_stream):"""Return whether the target stream supports actually color."""returntarget_stream.isatty()
[docs]defencounter_color(self,color,target_stream)->None:# pragma: no cover"""Set the new color."""fg,bg=self.divide_color(color)windows_color=windowsColors[fg]# Merge foreground/backgroung color if needed.ifbgisnotNone:windows_color=windowsColors[bg]<<4|windows_coloriftarget_stream==self.stdin:addr=-10eliftarget_stream==self.stdout:addr=-11eliftarget_stream==self.stderr:addr=-12else:super().encounter_color(color,target_stream)fromctypes.wintypesimportDWORD,HANDLEget_handle=ctypes.WINFUNCTYPE(HANDLE,DWORD)(('GetStdHandle',ctypes.windll.kernel32))handle=get_handle(DWORD(addr))ctypes.windll.kernel32.SetConsoleTextAttribute(handle,windows_color)
def_raw_input(self):# pragma: no coverdata=self.stdin.readline()if'\x1a'indata:raiseEOFErrorreturndata.strip()