Skip to content

Commit 6f274b7

Browse files
authored
Implement console resizing handler thread
1 parent f333f8c commit 6f274b7

1 file changed

Lines changed: 33 additions & 2 deletions

File tree

main.c

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ typedef struct {
4040
HANDLE events[2];
4141
HANDLE inputHandler;
4242
DWORD originalConsoleMode;
43+
HPCON hpcon;
4344
} Context;
4445

4546
static void ParseArgs(int argc, const wchar_t** wargv, char** argv, Context* ctx);
@@ -49,6 +50,7 @@ static HRESULT InitializeStartupInfoAttachedToPseudoConsole(STARTUPINFOEXW* star
4950
HPCON hpcon);
5051
static void __cdecl PipeListener(LPVOID);
5152
static void __cdecl InputHandlerThread(LPVOID);
53+
static void __cdecl ResizeHandlerThread(LPVOID);
5254
static void CloseInputHandler(Context* ctx);
5355

5456
static wchar_t* ToUtf16(const char* utf8) {
@@ -147,6 +149,7 @@ int wmain(int argc, const wchar_t* argv[]) {
147149

148150
hr = CreatePseudoConsoleAndPipes(&hpcon, &ctx);
149151
if (S_OK == hr) {
152+
ctx.hpcon = hpcon;
150153
HANDLE pipeListener = (HANDLE) _beginthread(PipeListener, 0, &ctx);
151154

152155
STARTUPINFOEXW startupInfo = {0};
@@ -162,6 +165,7 @@ int wmain(int argc, const wchar_t* argv[]) {
162165
ctx.events[1] = cmdProc.hThread;
163166

164167
ctx.inputHandler = (HANDLE) _beginthread(InputHandlerThread, 0, &ctx);
168+
_beginthread(ResizeHandlerThread, 0, &ctx);
165169

166170
WaitForMultipleObjects(sizeof(ctx.events) / sizeof(HANDLE), ctx.events, FALSE,
167171
INFINITE);
@@ -344,6 +348,7 @@ typedef enum { INIT, VERIFY, EXEC, END } State;
344348

345349
static State ProcessOutput(Context* ctx, const char* buffer, DWORD len, State state) {
346350
State nextState;
351+
DWORD written;
347352
switch (state) {
348353
case INIT: {
349354
if (!IsWaitInputPass(ctx, buffer, len)) {
@@ -358,12 +363,12 @@ static State ProcessOutput(Context* ctx, const char* buffer, DWORD len, State st
358363
fprintf(stderr, "Password is error!");
359364
nextState = END;
360365
} else {
361-
fprintf(stdout, "%s", buffer);
366+
WriteFile(ctx->stdOut, buffer, len, &written, NULL);
362367
nextState = EXEC;
363368
}
364369
} break;
365370
case EXEC: {
366-
fprintf(stdout, "%s", buffer);
371+
WriteFile(ctx->stdOut, buffer, len, &written, NULL);
367372
nextState = EXEC;
368373
} break;
369374
case END: {
@@ -488,3 +493,29 @@ static void CloseInputHandler(Context* ctx) {
488493

489494
SetConsoleMode(hStdin, ctx->originalConsoleMode);
490495
}
496+
497+
static void __cdecl ResizeHandlerThread(LPVOID arg) {
498+
Context* ctx = arg;
499+
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
500+
COORD currentSize = {0, 0};
501+
CONSOLE_SCREEN_BUFFER_INFO csbi;
502+
503+
if (GetConsoleScreenBufferInfo(hConsole, &csbi)) {
504+
currentSize.X = csbi.srWindow.Right - csbi.srWindow.Left + 1;
505+
currentSize.Y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
506+
}
507+
508+
while (WaitForMultipleObjects(sizeof(ctx->events) / sizeof(HANDLE), ctx->events, FALSE, 100) ==
509+
WAIT_TIMEOUT) {
510+
if (GetConsoleScreenBufferInfo(hConsole, &csbi)) {
511+
COORD newSize;
512+
newSize.X = csbi.srWindow.Right - csbi.srWindow.Left + 1;
513+
newSize.Y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
514+
515+
if (newSize.X != currentSize.X || newSize.Y != currentSize.Y) {
516+
currentSize = newSize;
517+
ResizePseudoConsole(ctx->hpcon, currentSize);
518+
}
519+
}
520+
}
521+
}

0 commit comments

Comments
 (0)