Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions PSReadLine/ConsoleLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public static class NativeMethods
public const uint ENABLE_PROCESSED_INPUT = 0x0001;
public const uint ENABLE_LINE_INPUT = 0x0002;

public const int FontTypeMask = 0x06;
public const int TrueTypeFont = 0x04;

internal static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1); // WinBase.h

[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr GetStdHandle(uint handleId);

Expand Down Expand Up @@ -58,6 +63,42 @@ public static extern int ToUnicode(uint uVirtKey, uint uScanCode, byte[] lpKeySt

[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern short VkKeyScan(char @char);

[DllImport("kernel32.dll", SetLastError = false, CharSet = CharSet.Unicode)]
public static extern uint GetConsoleOutputCP();

[DllImport("User32.dll", SetLastError = false, CharSet = CharSet.Unicode)]
public static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr GetConsoleWindow();

[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr GetDC(IntPtr hwnd);

[DllImport("GDI32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool TranslateCharsetInfo(IntPtr src, out CHARSETINFO Cs, uint options);

[DllImport("GDI32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool GetTextMetrics(IntPtr hdc, out TEXTMETRIC tm);

[DllImport("GDI32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool GetCharWidth32(IntPtr hdc, uint first, uint last, out int width);

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr CreateFile
(
string fileName,
uint desiredAccess,
uint ShareModes,
IntPtr securityAttributes,
uint creationDisposition,
uint flagsAndAttributes,
IntPtr templateFileWin32Handle
);

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern bool GetCurrentConsoleFontEx(IntPtr consoleOutput, bool bMaximumWindow, ref CONSOLE_FONT_INFO_EX consoleFontInfo);
}

public delegate bool BreakHandler(ConsoleBreakSignal ConsoleBreakSignal);
Expand All @@ -72,13 +113,45 @@ public enum ConsoleBreakSignal : uint
None = 255,
}

public enum CHAR_INFO_Attributes : ushort
{
COMMON_LVB_LEADING_BYTE = 0x0100,
COMMON_LVB_TRAILING_BYTE = 0x0200
}

public enum StandardHandleId : uint
{
Error = unchecked((uint)-12),
Output = unchecked((uint)-11),
Input = unchecked((uint)-10),
}

[Flags]
public enum AccessQualifiers : uint
{
// From winnt.h
GenericRead = 0x80000000,
GenericWrite = 0x40000000
}

public enum CreationDisposition : uint
{
// From winbase.h
CreateNew = 1,
CreateAlways = 2,
OpenExisting = 3,
OpenAlways = 4,
TruncateExisting = 5
}

[Flags]
public enum ShareModes : uint
{
// From winnt.h
ShareRead = 0x00000001,
ShareWrite = 0x00000002
}

public struct SMALL_RECT
{
public short Left;
Expand Down Expand Up @@ -119,6 +192,69 @@ public override string ToString()
}
}

[StructLayout(LayoutKind.Sequential)]
public struct FONTSIGNATURE
{
//From public\sdk\inc\wingdi.h

// fsUsb*: A 128-bit Unicode subset bitfield (USB) identifying up to 126 Unicode subranges
internal uint fsUsb0;
internal uint fsUsb1;
internal uint fsUsb2;
internal uint fsUsb3;
// fsCsb*: A 64-bit, code-page bitfield (CPB) that identifies a specific character set or code page.
internal uint fsCsb0;
internal uint fsCsb1;
}

[StructLayout(LayoutKind.Sequential)]
public struct CHARSETINFO
{
//From public\sdk\inc\wingdi.h
internal uint ciCharset; // Character set value.
internal uint ciACP; // ANSI code-page identifier.
internal FONTSIGNATURE fs;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct TEXTMETRIC
{
//From public\sdk\inc\wingdi.h
public int tmHeight;
public int tmAscent;
public int tmDescent;
public int tmInternalLeading;
public int tmExternalLeading;
public int tmAveCharWidth;
public int tmMaxCharWidth;
public int tmWeight;
public int tmOverhang;
public int tmDigitizedAspectX;
public int tmDigitizedAspectY;
public char tmFirstChar;
public char tmLastChar;
public char tmDefaultChar;
public char tmBreakChar;
public byte tmItalic;
public byte tmUnderlined;
public byte tmStruckOut;
public byte tmPitchAndFamily;
public byte tmCharSet;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct CONSOLE_FONT_INFO_EX
{
internal int cbSize;
internal int nFont;
internal short FontWidth;
internal short FontHeight;
internal int FontFamily;
internal int FontWeight;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
internal string FontFace;
}

public struct CHAR_INFO
{
public ushort UnicodeChar;
Expand Down
Loading