-
Notifications
You must be signed in to change notification settings - Fork 3
/
wsWinConsole.pp
232 lines (191 loc) · 5.91 KB
/
wsWinConsole.pp
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
221
222
223
224
225
226
227
228
229
230
231
232
{ Copyright (C) 2020-2021 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}
{$H+}
unit wsWinConsole;
interface
uses
wsWinEnvVar,
Windows;
// Disable Ctrl+C as signal and ignore Ctrl+Break
procedure DisableBreak();
// Enable Ctrl+C as signal and don't ignore Ctrl+Break
procedure EnableBreak();
function WhereX(): LongInt;
function WhereY(): LongInt;
procedure GotoXY(const PosX, PosY: LongInt);
// Moves cursor left, taking into account line wrap
procedure MoveCursorLeft(const NumChars: LongInt);
// Moves cursor right, taking into account line wrap
procedure MoveCursorRight(const NumChars: LongInt);
function GetConsoleCols(): DWORD;
function GetConsoleColsViewport(): DWORD;
function GetConsoleRows(): DWORD;
function GetConsoleRowsViewport(): DWORD;
function IsWindowsTerminal(): Boolean;
implementation
type
THandlerRoutine = function(dwControlType: DWORD): BOOL; stdcall;
var
CONIN, CONOUT: HANDLE;
CSBInfo: CONSOLE_SCREEN_BUFFER_INFO;
ConsoleCols, ConsoleColsViewport, ConsoleRows, ConsoleRowsViewport: DWORD;
// Windows console signal handler routine
function HandlerRoutine(const dwControlType: DWORD): BOOL; stdcall;
begin
result := false; // Pass signal to next handler
case dwControlType of
CTRL_BREAK_EVENT:
begin
result := true; // Signal handled
end;
end; //case
end;
procedure DisableBreak();
var
Mode: DWORD;
begin
GetConsoleMode(CONIN, Mode);
if Mode and ENABLE_PROCESSED_INPUT <> 0 then
begin
SetConsoleMode(CONIN, Mode and (not ENABLE_PROCESSED_INPUT));
SetConsoleCtrlHandler(THandlerRoutine(@HandlerRoutine), true);
end;
end;
procedure EnableBreak();
var
Mode: DWORD;
begin
GetConsoleMode(CONIN, Mode);
if Mode and ENABLE_PROCESSED_INPUT = 0 then
begin
SetConsoleMode(CONIN, Mode or ENABLE_PROCESSED_INPUT);
SetConsoleCtrlHandler(nil, false);
end;
end;
function WhereX(): LongInt;
begin
GetConsoleScreenBufferInfo(CONOUT, CSBInfo);
result := CSBInfo.dwCursorPosition.X + 1;
end;
function WhereY(): LongInt;
begin
GetConsoleScreenBufferInfo(CONOUT, CSBInfo);
result := CSBInfo.dwCursorPosition.Y + 1;
end;
procedure GotoXY(const PosX, PosY: LongInt);
var
CoordCursor: COORD;
begin
if (PosX >= 1) and (PosX <= ConsoleCols) and (PosY >= 1) and (PosY <= ConsoleRows) then
begin
CoordCursor.X := PosX - 1;
CoordCursor.Y := PosY - 1;
SetConsoleCursorPosition(CONOUT, CoordCursor);
end;
end;
procedure MoveCursorLeft(const NumChars: LongInt);
var
X, Y: LongInt;
I: LongInt;
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 := ConsoleCols; // 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: LongInt);
var
X, Y: LongInt;
I: LongInt;
begin
X := WhereX();
Y := WhereY();
for I := 1 to NumChars do
begin
if X = ConsoleCols then
begin
if Y < ConsoleRows 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;
function GetConsoleCols(): DWORD;
begin
result := ConsoleCols;
end;
function GetConsoleColsViewport(): DWORD;
begin
result := ConsoleColsViewport;
end;
function GetConsoleRows(): DWORD;
begin
result := ConsoleRows;
end;
function GetConsoleRowsViewport(): DWORD;
begin
result := ConsoleRowsViewport;
end;
function IsWindowsTerminal(): Boolean;
begin
// https://github.com/microsoft/terminal/pull/897
result := EnvVarExists('WT_SESSION');
end;
procedure InitializeUnit();
var
Access, Mode: DWORD;
begin
Access := GENERIC_READ or GENERIC_WRITE;
CONIN := CreateFile('CONIN$', // LPCTSTR lpFileName
Access, // DWORD dwDesiredAccess
FILE_SHARE_READ, // DWORD dwShareMode
nil, // LPSECURITY_ATTRIBUTES lpSecurityAttributes
OPEN_EXISTING, // DWORD dwCreationDisposition
0, // DWORD dwFlagsAndAttributes
0); // HANDLE hTemplateFile
GetConsoleMode(CONIN, Mode);
CONOUT := CreateFile('CONOUT$', // LPCTSTR lpFileName
Access, // DWORD dwDesiredAccess
FILE_SHARE_READ, // DWORD dwShareMode
nil, // LPSECURITY_ATTRIBUTES lpSecurityAttributes
OPEN_EXISTING, // DWORD dwCreationDisposition
0, // DWORD dwFlagsAndAttributes
0); // HANDLE hTemplateFile
GetConsoleScreenBufferInfo(CONOUT, CSBInfo);
ConsoleCols := CSBInfo.dwSize.X;
ConsoleColsViewport := CSBInfo.srWindow.Right - CSBInfo.srWindow.Left + 1;
ConsoleRows := CSBInfo.dwSize.Y;
ConsoleRowsViewport := CSBInfo.srWindow.Bottom - CSBInfo.srWindow.Top + 1;
end;
initialization
InitializeUnit();
finalization
end.