1 // Copyright (c) 2011-2016 Ryan Prichard
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 #ifndef AGENT_WIN32_CONSOLE_BUFFER_H
22 #define AGENT_WIN32_CONSOLE_BUFFER_H
31 #include "SmallRect.h"
33 class ConsoleScreenBufferInfo : public CONSOLE_SCREEN_BUFFER_INFO {
35 ConsoleScreenBufferInfo()
37 memset(this, 0, sizeof(*this));
40 Coord bufferSize() const { return dwSize; }
41 SmallRect windowRect() const { return srWindow; }
42 Coord cursorPosition() const { return dwCursorPosition; }
45 class Win32ConsoleBuffer {
47 Win32ConsoleBuffer(HANDLE conout, bool owned) :
48 m_conout(conout), m_owned(owned)
53 static const int kDefaultAttributes = 7;
55 ~Win32ConsoleBuffer() {
57 CloseHandle(m_conout);
61 static std::unique_ptr<Win32ConsoleBuffer> openStdout();
62 static std::unique_ptr<Win32ConsoleBuffer> openConout();
63 static std::unique_ptr<Win32ConsoleBuffer> createErrorBuffer();
65 Win32ConsoleBuffer(const Win32ConsoleBuffer &other) = delete;
66 Win32ConsoleBuffer &operator=(const Win32ConsoleBuffer &other) = delete;
69 void clearLines(int row, int count, const ConsoleScreenBufferInfo &info);
70 void clearAllLines(const ConsoleScreenBufferInfo &info);
72 // Buffer and window sizes.
73 ConsoleScreenBufferInfo bufferInfo();
75 SmallRect windowRect();
76 void resizeBuffer(const Coord &size);
77 bool resizeBufferRange(const Coord &initialSize, Coord &finalSize);
78 bool resizeBufferRange(const Coord &initialSize) {
80 return resizeBufferRange(initialSize, dummy);
82 void moveWindow(const SmallRect &rect);
85 Coord cursorPosition();
86 void setCursorPosition(const Coord &point);
89 void read(const SmallRect &rect, CHAR_INFO *data);
90 void write(const SmallRect &rect, const CHAR_INFO *data);
92 void setTextAttribute(WORD attributes);
95 HANDLE m_conout = nullptr;
99 #endif // AGENT_WIN32_CONSOLE_BUFFER_H