-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSetConsoleFont.ps1
More file actions
138 lines (111 loc) · 5.57 KB
/
SetConsoleFont.ps1
File metadata and controls
138 lines (111 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<#
DESCRIPTION
This script sets the current console font. The module WindowsConsoleFont by Jaykul
could (and probably should) be used here instead. That's not currently published
anywhere so I put together a minimal version to avoid needing to compile that project
on every machine I use.
#>
[CmdletBinding()]
param([string] $Family, [int] $Size)
end {
# If we're not in conhost don't try to set font family.
if ($env:TERM_PROGRAM -match 'vscode' -or $env:WT_SESSION) {
return
}
$assemblyPath = "$PSScriptRoot\FontSetter.dll"
if (-not (Test-Path $assemblyPath)) {
Add-Type -OutputType Library -OutputAssembly $assemblyPath -TypeDefinition '
using System;
using System.ComponentModel;
using System.Management.Automation;
using System.Runtime.InteropServices;
namespace ProfileUtility
{
[EditorBrowsable(EditorBrowsableState.Never)]
public static class FontSetter
{
private static IntPtr s_outputHandle;
[Hidden, EditorBrowsable(EditorBrowsableState.Never)]
public static void SetConsoleFont(string familyName, short size = 0)
{
var info = new Interop.CONSOLE_FONT_INFOEX();
info.FaceName = new char[Interop.LF_FACESIZE];
info.cbSize = (uint)Marshal.SizeOf<Interop.CONSOLE_FONT_INFOEX>();
int result = Interop.GetCurrentConsoleFontEx(
GetOutputHandle(),
false,
ref info);
if (result == 0)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
Array.Clear(info.FaceName, 0, info.FaceName.Length);
familyName.CopyTo(0, info.FaceName, 0, Math.Min(familyName.Length, Interop.LF_FACESIZE));
if (size > 0)
{
info.dwFontSize.Y = size;
}
result = Interop.SetCurrentConsoleFontEx(
GetOutputHandle(),
false,
ref info);
if (result == 0)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
private static IntPtr GetOutputHandle()
{
if (s_outputHandle != Interop.INVALID_HANDLE_VALUE && s_outputHandle != IntPtr.Zero)
{
return s_outputHandle;
}
IntPtr outputHandle = Interop.GetStdHandle(Interop.STD_OUTPUT_HANDLE);
if (outputHandle == Interop.INVALID_HANDLE_VALUE)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
return s_outputHandle = outputHandle;
}
private static class Interop
{
public const int LF_FACESIZE = 32;
public const int STD_OUTPUT_HANDLE = -11;
public static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
private const string Kernel32DllName = "kernel32";
[DllImport(Kernel32DllName, SetLastError = true)]
public static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport(Kernel32DllName, SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int GetCurrentConsoleFontEx(
IntPtr hConsoleOutput,
bool bMaximumWindow,
ref CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx);
[DllImport(Kernel32DllName, SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int SetCurrentConsoleFontEx(
IntPtr hConsoleOutput,
bool bMaximumWindow,
ref CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx);
[StructLayout(LayoutKind.Sequential)]
public struct COORD
{
public short X;
public short Y;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct CONSOLE_FONT_INFOEX
{
public uint cbSize;
public int nFont;
public COORD dwFontSize;
public uint FontFamily;
public uint FontWeight;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = LF_FACESIZE)]
public char[] FaceName;
}
}
}
}'
}
Add-Type -Path $assemblyPath -ErrorAction Stop
[ProfileUtility.FontSetter]::SetConsoleFont($Family, $Size)
}