| 68 | } |
| 69 | |
| 70 | void CommitConsole() |
| 71 | { |
| 72 | auto sizeWritten = gConsoleWriteBufferIndex; |
| 73 | auto linesWritten = static_cast<SHORT>(sizeWritten / gConsoleWidth); |
| 74 | |
| 75 | // Reset gConsoleTop on the first commit so we don't overwrite any warning |
| 76 | // messages. |
| 77 | auto size = sizeWritten; |
| 78 | if (gConsoleFirstCommit) { |
| 79 | gConsoleFirstCommit = false; |
| 80 | |
| 81 | CONSOLE_SCREEN_BUFFER_INFO info = {}; |
| 82 | GetConsoleScreenBufferInfo(gConsoleHandle, &info); |
| 83 | gConsoleTop = info.dwCursorPosition.Y; |
| 84 | } else { |
| 85 | // Write some extra empty lines to make sure we clear anything from |
| 86 | // last time. |
| 87 | if (size < gConsolePrevWriteBufferSize) { |
| 88 | memset(gConsoleWriteBuffer + size, ' ', gConsolePrevWriteBufferSize - size); |
| 89 | size = gConsolePrevWriteBufferSize; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // If we're at the end of the console buffer, issue some new lines to make |
| 94 | // some space. |
| 95 | auto maxCursorY = gConsoleBufferHeight - linesWritten; |
| 96 | if (gConsoleTop > maxCursorY) { |
| 97 | COORD bottom = { 0, static_cast<SHORT>(gConsoleBufferHeight - 1)}; |
| 98 | SetConsoleCursorPosition(gConsoleHandle, bottom); |
| 99 | printf("\n"); |
| 100 | for (--gConsoleTop; gConsoleTop > maxCursorY; --gConsoleTop) { |
| 101 | printf("\n"); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Write to the console. |
| 106 | DWORD dwCharsWritten = 0; |
| 107 | COORD cursor = { 0, gConsoleTop }; |
| 108 | WriteConsoleOutputCharacterA(gConsoleHandle, gConsoleWriteBuffer, static_cast<DWORD>(size), cursor, &dwCharsWritten); |
| 109 | |
| 110 | // Put the cursor at the end of the written text. |
| 111 | cursor.Y += linesWritten; |
| 112 | SetConsoleCursorPosition(gConsoleHandle, cursor); |
| 113 | |
| 114 | // Update console info in case it was resized. |
| 115 | CONSOLE_SCREEN_BUFFER_INFO info = {}; |
| 116 | GetConsoleScreenBufferInfo(gConsoleHandle, &info); |
| 117 | gConsoleWidth = info.srWindow.Right - info.srWindow.Left + 1; |
| 118 | gConsoleBufferHeight = info.dwSize.Y; |
| 119 | gConsoleWriteBufferIndex = 0; |
| 120 | gConsolePrevWriteBufferSize = sizeWritten; |
| 121 | } |
no outgoing calls
no test coverage detected