我需要一种方法来检测 PowerShell 当前正在使用哪种字体,这样我就可以从该字体发出某些字符(字形)(如果它处于活动状态)。但是,如果字体未激活,我需要知道,这样我就可以避免从模块中发出这些字形。
问题:有没有办法检测 PowerShell 会话中当前使用的字体,例如在 iTerm2 (Mac)、VSCode 或 Windows Terminal 中运行的字体?
我检查了内置$Host
变量,看看它是否有类似的东西,但除了前景色之外,没有任何与字体相关的属性。
ForegroundColor : Gray
BackgroundColor : Black
CursorPosition : 0,43
WindowPosition : 0,0
CursorSize : 25
BufferSize : 164,44
WindowSize : 164,44
MaxWindowSize : 164,44
MaxPhysicalWindowSize : 3824,132
KeyAvailable : True
WindowTitle : PowerShell
Run Code Online (Sandbox Code Playgroud)
以下脚本可能会有所帮助。
\n\nif ( -not (\'Win32test.ConsoleTest\' -as [type]) ) {\n$defConsoleTest = @\'\nusing System.Runtime.InteropServices;\nusing System;\n\nnamespace Win32test\n{\n public static class ConsoleTest\n {\n [DllImport( "kernel32.dll", \n CharSet = CharSet.Unicode, SetLastError = true)]\n extern static bool GetCurrentConsoleFontEx(\n IntPtr hConsoleOutput,\n bool bMaximumWindow,\n ref CONSOLE_FONT_INFOEX lpConsoleCurrentFont);\n\n private enum StdHandle\n {\n OutputHandle = -11 // The standard output device.\n }\n\n [DllImport("kernel32")]\n private static extern IntPtr GetStdHandle(StdHandle index);\n\n public static string GetFontCsvHeader(){\n return "FaceName,FontFamily,FontWeight,FontSize";\n }\n\n public static string GetFontCsv()\n {\n // Instantiating CONSOLE_FONT_INFOEX and setting cbsize\n CONSOLE_FONT_INFOEX ConsoleFontInfo = new CONSOLE_FONT_INFOEX();\n ConsoleFontInfo.cbSize = (uint)Marshal.SizeOf(ConsoleFontInfo);\n\n GetCurrentConsoleFontEx( GetStdHandle(StdHandle.OutputHandle),\n false, \n ref ConsoleFontInfo);\n\n return ConsoleFontInfo.FaceName + \n "," + ConsoleFontInfo.FontFamily + \n "," + ConsoleFontInfo.FontWeight + \n "," + ConsoleFontInfo.dwFontSize.X + \n "\xc3\x97" + ConsoleFontInfo.dwFontSize.Y;\n }\n\n [StructLayout(LayoutKind.Sequential)]\n private struct COORD\n {\n public short X;\n public short Y;\n\n public COORD(short x, short y)\n {\n X = x;\n Y = y;\n }\n }\n\n // learn.microsoft.com/en-us/windows/console/console-font-infoex\n [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]\n private struct CONSOLE_FONT_INFOEX\n {\n public uint cbSize;\n public uint nFont;\n public COORD dwFontSize;\n public int FontFamily;\n public int FontWeight;\n [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]\n public string FaceName;\n }\n }\n}\n\'@\nAdd-Type -TypeDefinition $defConsoleTest\n}\n\n# convert output to a pscustomobject\n[Win32test.ConsoleTest]::GetFontCsvHeader(), \n[Win32test.ConsoleTest]::GetFontCsv() |\n ConvertFrom-Csv -Delimiter \',\'\n
Run Code Online (Sandbox Code Playgroud)\n\n在Windows 10、Powershell5.1
和 PwSh 7.0.1
(控制台以及 VSCode 终端)中测试输出:
D:\\PShell\\tests\\GetCurrentConsoleFontEx.ps1\n
Run Code Online (Sandbox Code Playgroud)\n\n\n\n\nRun Code Online (Sandbox Code Playgroud)\nFaceName FontFamily FontWeight FontSize\n-------- ---------- ---------- --------\nCourier New 54 400 11\xc3\x9720\n
Windows 的输出相同cmd
。
powershell -nopro -comm "& {D:\\PShell\\tests\\GetCurrentConsoleFontEx.ps1}"\npwsh -nopro -comm "& {D:\\PShell\\tests\\GetCurrentConsoleFontEx.ps1}"\n
Run Code Online (Sandbox Code Playgroud)\n
的注册表值font style
位于
HKCU\Console\%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe
Run Code Online (Sandbox Code Playgroud)
或者
HKCU\Console\%SystemRoot%_SysWOW64_WindowsPowerShell_v1.0_powershell.exe
Run Code Online (Sandbox Code Playgroud)
注册表的组成部分1.你可以在 powershell 中获取这些
(Get-ItemProperty -Path "HKCU:\Console\%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe").facename
#or
(Get-ItemProperty -Path "HKCU:\Console\%SystemRoot%_SysWOW64_WindowsPowerShell_v1.0_powershell.exe").facename
Run Code Online (Sandbox Code Playgroud)
与苹果 iTerm 一样,我不知道注册表是否会是,PSDrive
但它可能......
您可以使用以下命令更改字体
Set-ItemProperty -Path "HKCU:\Console\%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe" -Name "facename" -Value "Font Name" -Type String
#or
Set-ItemProperty -Path "HKCU:\Console\%SystemRoot%_SysWOW64_WindowsPowerShell_v1.0_powershell.exe" -Name "facename" -Value "Font Name" -Type String
Run Code Online (Sandbox Code Playgroud)
还有一个FontFamily
键,但我不知道它是如何工作的,因为它是一个REG_DWORD
键,并且它有特定字体系列的特定数字......
\System32\WindowsPowerShell\v1.0\powershell.exe
是\SysWOW64\WindowsPowerShell\v1.0\powershell.exe
[32 位还是 64 位] Powershell
2我建议也查看https://superuser.com/questions/502340/how-can-i-install-a-new-font-in-powershell-console
归档时间: |
|
查看次数: |
1599 次 |
最近记录: |