diff --git a/core/textinput/src/textinput/TerminalDisplayWin.cpp b/core/textinput/src/textinput/TerminalDisplayWin.cpp index fbb4615f198e4a12032f900f9d9a83d5869460c6..dd28c4bd4f485253f705aa3d6b4b8fa891d4455f 100644 --- a/core/textinput/src/textinput/TerminalDisplayWin.cpp +++ b/core/textinput/src/textinput/TerminalDisplayWin.cpp @@ -15,34 +15,38 @@ #ifdef _WIN32 #include "textinput/TerminalDisplayWin.h" - #include "textinput/Color.h" +#include <assert.h> + namespace textinput { TerminalDisplayWin::TerminalDisplayWin(): TerminalDisplay(false), fStartLine(0), fIsAttached(false), fDefaultAttributes(0) { + DWORD mode; + SetIsTTY(::GetConsoleMode(::GetStdHandle(STD_INPUT_HANDLE), &mode) != 0); + fOut = ::GetStdHandle(STD_OUTPUT_HANDLE); bool isConsole = ::GetConsoleMode(fOut, &fOldMode) != 0; - SetIsTTY(isConsole); - if (isConsole) { + if (!isConsole) { // Prevent redirection from stealing our console handle, // simply open our own. fOut = ::CreateFileA("CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); ::GetConsoleMode(fOut, &fOldMode); - fMyMode = fOldMode | ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT; CONSOLE_SCREEN_BUFFER_INFO csbi; ::GetConsoleScreenBufferInfo(fOut, &csbi); fDefaultAttributes = csbi.wAttributes; + assert(fDefaultAttributes != 0 && "~TerminalDisplayWin broken"); } + fMyMode = fOldMode | ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT; HandleResizeEvent(); } TerminalDisplayWin::~TerminalDisplayWin() { - if (IsTTY()) { + if (fDefaultAttributes) { ::SetConsoleTextAttribute(fOut, fDefaultAttributes); // We allocated CONOUT$: CloseHandle(fOut); @@ -80,6 +84,7 @@ namespace textinput { void TerminalDisplayWin::CheckCursorPos() { + if (!IsTTY()) return; // Did something print something on the screen? // I.e. did the cursor move? CONSOLE_SCREEN_BUFFER_INFO CSI; @@ -108,8 +113,10 @@ namespace textinput { void TerminalDisplayWin::MoveInternal(Pos P) { - COORD C = {P.fCol, P.fLine + fStartLine}; - ::SetConsoleCursorPosition(fOut, C); + if (IsTTY()) { + COORD C = {P.fCol, P.fLine + fStartLine}; + ::SetConsoleCursorPosition(fOut, C); + } } void @@ -174,21 +181,19 @@ namespace textinput { void TerminalDisplayWin::Attach() { // set to noecho - if (fIsAttached) return; - if (IsTTY() && !::SetConsoleMode(fOut, fMyMode)) { + if (fIsAttached || !IsTTY()) return; + if (!::SetConsoleMode(fOut, fMyMode)) { ShowError("attaching to console output"); } CONSOLE_SCREEN_BUFFER_INFO Info; - if (IsTTY()) { - if (!::GetConsoleScreenBufferInfo(fOut, &Info)) { - ShowError("attaching / getting console info"); - } else { - fStartLine = Info.dwCursorPosition.Y; - if (Info.dwCursorPosition.X) { - // Whooa - where are we?! Newline and cross fingers: - WriteRawString("\n", 1); - ++fStartLine; - } + if (!::GetConsoleScreenBufferInfo(fOut, &Info)) { + ShowError("attaching / getting console info"); + } else { + fStartLine = Info.dwCursorPosition.Y; + if (Info.dwCursorPosition.X) { + // Whooa - where are we?! Newline and cross fingers: + WriteRawString("\n", 1); + ++fStartLine; } } fIsAttached = true; @@ -196,8 +201,8 @@ namespace textinput { void TerminalDisplayWin::Detach() { - if (!fIsAttached) return; - if (IsTTY() && !SetConsoleMode(fOut, fOldMode)) { + if (!fIsAttached || !IsTTY()) return; + if (!SetConsoleMode(fOut, fOldMode)) { ShowError("detaching to console output"); } TerminalDisplay::Detach();