Repository navigation
Expand file tree
/
Copy pathWinConsole.pp
More file actions
220 lines (187 loc) · 6.17 KB
/
Copy pathWinConsole.pp
File metadata and controls
220 lines (187 loc) · 6.17 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
{ Copyright (C) 2020-2026 by Bill Stewart (bstewart at iname.com)
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License
along with this program. If not, see https://www.gnu.org/licenses/.
}
{$MODE OBJFPC}
{$MODESWITCH UNICODESTRINGS}
unit WinConsole;
interface
// Outputs string to console
procedure OutStr(const S: string);
function GetConsoleCols(): Integer;
function GetConsoleRows(): Integer;
function WhereX(): Integer;
function WhereY(): Integer;
procedure GotoXY(const PosX, PosY: Integer);
// Moves cursor left, taking into account line wrap
procedure MoveCursorLeft(const NumChars: Integer);
// Moves cursor right, taking into account line wrap
procedure MoveCursorRight(const NumChars: Integer);
// Enable: Enables the ENABLE_VIRTUAL_TERMINAL_PROCESSING bit for ConOut
// Disable: Disables the bit
procedure SetConsoleVTMode(const Enable: Boolean);
implementation
uses
windows,
WinProcess;
var
ConOut: HANDLE;
WindowsTerminal: Boolean;
procedure OutStr(const S: string);
begin
WriteConsoleW(ConOut, // HANDLE hConsoleOutput
PChar(S), // VOID *lpBuffer
Length(S), // DWORD nNumberOfCharsToWrite
nil, // LPDWORD lpNumberOfCharsWritten
nil); // LPVOID lpReserved
end;
function GetConsoleCols(): Integer;
var
CSBInfo: CONSOLE_SCREEN_BUFFER_INFO;
begin
GetConsoleScreenBufferInfo(ConOut, // HANDLE hConsoleOutput
CSBInfo); // PCONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo
if WindowsTerminal then
result := CSBInfo.srWindow.Right - CSBInfo.srWindow.Left + 1
else
result := CSBInfo.dwSize.X;
end;
function GetConsoleRows(): Integer;
var
CSBInfo: CONSOLE_SCREEN_BUFFER_INFO;
begin
GetConsoleScreenBufferInfo(ConOut, // HANDLE hConsoleOutput
CSBInfo); // PCONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo
if WindowsTerminal then
result := CSBInfo.srWindow.Bottom - CSBInfo.srWindow.Top + 1
else
result := CSBInfo.dwSize.Y;
end;
function WhereX(): Integer;
var
CSBInfo: CONSOLE_SCREEN_BUFFER_INFO;
begin
GetConsoleScreenBufferInfo(ConOut, // HANDLE hConsoleOutput
CSBInfo); // PCONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo
result := CSBInfo.dwCursorPosition.X + 1;
end;
function WhereY(): Integer;
var
CSBInfo: CONSOLE_SCREEN_BUFFER_INFO;
begin
GetConsoleScreenBufferInfo(ConOut, // HANDLE hConsoleOutput
CSBInfo); // PCONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo
result := CSBInfo.dwCursorPosition.Y + 1;
end;
procedure GotoXY(const PosX, PosY: Integer);
var
CoordCursor: COORD;
begin
if (PosX >= 1) and (PosX <= GetConsoleCols()) and (PosY >= 1) and (PosY <= GetConsoleRows()) then
begin
CoordCursor.X := PosX - 1;
CoordCursor.Y := PosY - 1;
SetConsoleCursorPosition(ConOut, // HANDLE hConsoleOutput
CoordCursor); // COORD dwCursorPosition
end;
end;
procedure MoveCursorLeft(const NumChars: Integer);
var
X, Y, I: Integer;
begin
X := WhereX();
Y := WhereY();
for I := 1 to NumChars do
begin
if X = 1 then // at column 1
begin
if Y > 1 then // if we're past row 1
begin
X := GetConsoleCols(); // move to last column
Dec(Y); // on previous row
end;
end
else // not at column 1
Dec(X); // move left one column
end;
GotoXY(X, Y);
end;
procedure MoveCursorRight(const NumChars: Integer);
var
X, Y, I: Integer;
begin
X := WhereX();
Y := WhereY();
for I := 1 to NumChars do
begin
if X = GetConsoleCols() then
begin
if Y < GetConsoleRows() then
begin
X := 1; // move to first column
Inc(Y); // on next row
end;
end
else // we're not at the right-most column
Inc(X); // move right one column
end;
GotoXY(X, Y);
end;
procedure SetConsoleVTMode(const Enable: Boolean);
var
OldMode, NewMode: DWORD;
begin
GetConsoleMode(ConOut, // HANDLE hConsoleHandle
NewMode); // LPDWORD lpMode
OldMode := NewMode;
if Enable then
begin
NewMode := NewMode or ENABLE_VIRTUAL_TERMINAL_PROCESSING;
end
else
begin
NewMode := NewMode and (not ENABLE_VIRTUAL_TERMINAL_PROCESSING);
end;
if OldMode <> NewMode then
SetConsoleMode(ConOut, // HANDLE hConsoleHandle
NewMode); // DWORD dwMode
end;
procedure InitializeUnit();
var
ProcessID: DWORD;
begin
ProcessID := GetCurrentProcessId();
if not IsParentProcessName(ProcessID, 'powershell_ise.exe') then
begin
ConOut := CreateFile('CONOUT$', // LPCTSTR lpFileName
GENERIC_READ or GENERIC_WRITE, // DWORD dwDesiredAccess
FILE_SHARE_READ, // DWORD dwShareMode
nil, // LPSECURITY_ATTRIBUTES lpSecurityAttributes
OPEN_EXISTING, // DWORD dwCreationDisposition
0, // DWORD dwFlagsAndAttributes
0); // HANDLE hTemplateFile
end
else
ConOut := StdOutputHandle;
// See this for info on why we need to know if WT is running:
// https://github.com/microsoft/terminal/issues/10191#issuecomment-897345862
// Basically in WT you can't move "backward" into the scrollback bufffer
WindowsTerminal := IsProcessInTree(ProcessID, 'windowsterminal.exe');
end;
procedure FinalizeUnit();
begin
ConOut := StdOutputHandle;
end;
initialization
InitializeUnit();
finalization
FinalizeUnit();
end.