okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1d0397652
main
1"""What kind of terminal this is, for the few places that must change what they draw. 2 3The classic Windows console window (conhost: the default window of Windows PowerShell and cmd on Windows 10) uses Consolas or Lucida Console, 4which have no glyphs for the eighth-block sparkline characters or the check mark: they show as empty boxes (Consolas has the full, half and 5shade blocks, the box-drawing set and the rounded corners; Lucida Console has no rounded corners and no filled circle). 6Windows Terminal, VS Code's terminal, ConEmu and every Linux/macOS terminal with a modern font have them. `STRATA_GLYPHS=ascii|unicode` 7overrides the guess (`solid` draws the classic console's solid look without touching the window's font). 8 9In the classic console only, `classic_style()` does the best it can with no admin rights and no registry change: 10* Cascadia Mono installed: this window's font is switched to it (the console font API) and the full Unicode look is drawn; 11* otherwise the "solid" look: solid block bars with a thin track, ▄ / █ sparklines, square boxes, the 16 palette colours conhost really has, 12and `•` instead of `●` when the font is not Consolas (Lucida has no `●`). 13Windows Terminal, Linux and macOS never get either: `legacy_console()` is False there. 14""" 15 16from __future__import annotations 17 18import os 19import sys 20 21TIP = "For the full look, use Windows Terminal (built into Windows 11)" 22 23 24def legacy_console ()-> bool : 25"""True for the classic Windows console (no Windows Terminal, no VS Code, no ConEmu, no ssh session into it).""" 26forced = os .environ .get ("STRATA_GLYPHS" ,"" ).strip ().lower () 27if forced in ("ascii" ,"legacy" ,"solid" ): 28return True 29if forced == "unicode" : 30return False 31if os .name != "nt" and sys .platform != "win32" : 32return False 33env = os .environ 34return not (env .get ("WT_SESSION" )or env .get ("TERM_PROGRAM" )or env .get ("ConEmuANSI" )or env .get ("ANSICON" ) 35or env .get ("TERM" ,"" ).startswith (("xterm" ,"screen" ,"tmux" ,"vt" ))) 36 37 38def console_columns ()-> int | None : 39"""The visible width of the Windows console window in columns (not its buffer), or None when it cannot be read.""" 40if os .name != "nt" : 41return None 42try : 43import ctypes 44from ctypes import wintypes 45 46class COORD (ctypes .Structure ): 47_fields_ = [("X" ,ctypes .c_short ), ("Y" ,ctypes .c_short )] 48 49class RECT (ctypes .Structure ): 50_fields_ = [("Left" ,ctypes .c_short ), ("Top" ,ctypes .c_short ), ("Right" ,ctypes .c_short ), ("Bottom" ,ctypes .c_short )] 51 52class INFO (ctypes .Structure ): 53_fields_ = [("size" ,COORD ), ("cursor" ,COORD ), ("attr" ,wintypes .WORD ), ("window" ,RECT ), ("maxsize" ,COORD )] 54 55handle = ctypes .windll .kernel32 .GetStdHandle (- 11 ) 56info = INFO () 57if not ctypes .windll .kernel32 .GetConsoleScreenBufferInfo (handle ,ctypes .byref (info )): 58return None 59return int (info .window .Right - info .window .Left + 1 ) 60except Exception :# noqa: BLE001 61return None 62 63 64# ------------------------------------------------------------------------------------------------ the font of the classic console 65 66def _font_struct (): 67import ctypes 68 69class COORD (ctypes .Structure ): 70_fields_ = [("X" ,ctypes .c_short ), ("Y" ,ctypes .c_short )] 71 72class CONSOLE_FONT_INFOEX (ctypes .Structure ): 73_fields_ = [("cbSize" ,ctypes .c_ulong ), ("nFont" ,ctypes .c_ulong ), ("dwFontSize" ,COORD ), ("FontFamily" ,ctypes .c_uint ), 74 ("FontWeight" ,ctypes .c_uint ), ("FaceName" ,ctypes .c_wchar * 32 )] 75return CONSOLE_FONT_INFOEX 76 77 78def console_font ()-> str | None : 79"""The face name of this console window's font (Windows only), or None.""" 80if os .name != "nt" : 81return None 82try : 83import ctypes 84S = _font_struct () 85info = S () 86info .cbSize = ctypes .sizeof (S ) 87handle = ctypes .windll .kernel32 .GetStdHandle (- 11 ) 88if not ctypes .windll .kernel32 .GetCurrentConsoleFontEx (handle ,False ,ctypes .byref (info )): 89return None 90return info .FaceName or None 91except Exception :# noqa: BLE001 92return None 93 94 95def cascadia_installed ()-> bool : 96"""Is Cascadia Mono installed (it comes with Windows Terminal and Visual Studio, and can be installed alone)?""" 97if os .name != "nt" : 98return False 99try : 100import winreg 101for root in (winreg .HKEY_LOCAL_MACHINE ,winreg .HKEY_CURRENT_USER ): 102try : 103with winreg .OpenKey (root ,"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts" )as key : 104i = 0 105while True : 106name = winreg .EnumValue (key ,i )[0 ] 107if name .lower ().startswith ("cascadia mono" ): 108return True 109i += 1 110except OSError : 111continue 112except Exception :# noqa: BLE001 113return False 114fonts = os .path .join (os .environ .get ("WINDIR" ,"C:\\Windows" ),"Fonts" ) 115return any (os .path .isfile (os .path .join (fonts ,n ))for n in ("CascadiaMono.ttf" ,"CascadiaMonoPL.ttf" )) 116 117 118def switch_font (face :str = "Cascadia Mono" )-> bool : 119"""Set THIS console window's font through the console font API (no admin, nothing saved, gone when the window closes); True if it took.""" 120if os .name != "nt" : 121return False 122try : 123import ctypes 124S = _font_struct () 125info = S () 126info .cbSize = ctypes .sizeof (S ) 127handle = ctypes .windll .kernel32 .GetStdHandle (- 11 ) 128if not ctypes .windll .kernel32 .GetCurrentConsoleFontEx (handle ,False ,ctypes .byref (info )): 129return False 130height = info .dwFontSize .Y or 16 131info .FaceName = face 132info .dwFontSize .X = 0 133info .dwFontSize .Y = height 134info .FontFamily = 54 # TMPF_TRUETYPE | fixed pitch | modern 135info .FontWeight = 400 136if not ctypes .windll .kernel32 .SetCurrentConsoleFontEx (handle ,False ,ctypes .byref (info )): 137return False 138return console_font ()== face 139except Exception :# noqa: BLE001 140return False 141 142 143def decide_style (* ,legacy :bool ,cascadia_ok :bool ,font :str | None )-> dict : 144"""The look for this console (pure: the tests give it the facts). 145 146{"mode": "none"} not the classic console: nothing here changes 147{"mode": "full", "font": "Cascadia Mono", ...} the font was switched to Cascadia Mono: the full Unicode look 148{"mode": "solid", "font": face, "bullet": "●"|"•"} solid blocks and palette colours; the bullet depends on the font 149`tip` is the one line every classic console shows.""" 150if not legacy : 151return {"mode" :"none" ,"bullet" :"●" ,"tip" :None ,"font" :font } 152if cascadia_ok : 153return {"mode" :"full" ,"bullet" :"●" ,"tip" :TIP ,"font" :"Cascadia Mono" } 154bullet = "●" if (font or "" ).lower ().startswith ("consolas" )else "•" # Lucida Console (and unknown fonts) have no ● 155return {"mode" :"solid" ,"bullet" :bullet ,"tip" :TIP ,"font" :font } 156 157 158_cached :dict | None = None 159 160 161def classic_style ()-> dict : 162"""decide_style for this process, switching the font to Cascadia Mono once when that is possible. Cheap after the first call.""" 163global _cached 164if _cached is not None : 165return _cached 166legacy = legacy_console () 167forced = os .environ .get ("STRATA_GLYPHS" ,"" ).strip ().lower ()in ("ascii" ,"legacy" ,"solid" ) 168switched = False 169font = console_font ()if (legacy and forced )else None 170if legacy and not forced and os .name == "nt" : 171font = console_font () 172if font and font .lower ().startswith ("cascadia mono" ): 173switched = True 174elif cascadia_installed (): 175switched = switch_font ("Cascadia Mono" ) 176font = console_font ()if switched else font 177_cached = decide_style (legacy = legacy ,cascadia_ok = switched ,font = font ) 178return _cached 179 180 181def forget_style ()-> None : 182"""For the tests: decide again.""" 183global _cached 184_cached = None