Skip to content

Commit 04ba74a

Browse files
committed
Terminal: enableAnsi
1 parent 9814748 commit 04ba74a

4 files changed

Lines changed: 32 additions & 10 deletions

File tree

modules/Terminal/Canvas.mpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export namespace CppUtils::Terminal
3333
{
3434
if (m_size.width() == 0 or m_size.height() == 0)
3535
return;
36-
std::print("{}", std::string(m_size.height() - 1, '\n'));
36+
std::print("{}", std::string(m_size.height(), '\n'));
3737
update();
3838
}
3939

@@ -101,10 +101,13 @@ export namespace CppUtils::Terminal
101101
using Area::draw;
102102

103103
public:
104-
inline Canvas(const Container::Size2& size = getTerminalSize(), const std::optional<Viewport>& viewportOpt = std::nullopt):
105-
Area{size, viewportOpt.value_or(Viewport{size, {0, 0}})},
104+
inline Canvas(const Container::Size2& size = getTerminalSize(), const std::optional<Viewport>& optionalViewport = std::nullopt):
105+
Area{size, optionalViewport.value_or(Viewport{size, {0, 0}})},
106106
m_previousBuffer{size}
107-
{}
107+
{
108+
setConsoleOutputUTF8();
109+
enableAnsi();
110+
}
108111

109112
inline auto applyDifferences() noexcept -> void
110113
{
@@ -125,7 +128,7 @@ export namespace CppUtils::Terminal
125128
if (needUpdateText or needUpdateTextStyle or needUpdateTextColor or needUpdateBackgroundColor)
126129
{
127130
if (std::empty(stringBuffer))
128-
setCursorPosition(Container::Size2{position.x(), terminalSize.height() - m_viewport.getSize().height() + position.y() - 1});
131+
setCursorPosition({position.x(), terminalSize.height() - m_viewport.getSize().height() + position.y()});
129132
if (needUpdateTextStyle)
130133
{
131134
stringBuffer += TextStyle::getTextStyleCode(currentChar.textStyle);
@@ -160,7 +163,7 @@ export namespace CppUtils::Terminal
160163
}
161164
if (needFlush)
162165
std::fflush(stdout);
163-
setCursorPosition(getTerminalSize());
166+
setCursorPosition({0, getTerminalSize().height()});
164167
}
165168

166169
inline auto print() noexcept -> void
@@ -190,7 +193,7 @@ export namespace CppUtils::Terminal
190193
m_stopEvent.wait();
191194
}
192195

193-
inline auto stop() noexcept -> void
196+
inline auto close() noexcept -> void
194197
{
195198
m_stopEvent.notify();
196199
}

modules/Terminal/Cursor.mpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export namespace CppUtils::Terminal
3333
const auto response = rawTerminal.read('R');
3434
auto rows = 0, columns = 0;
3535
if (std::sscanf(response.c_str(), "\x1b[%d;%d", std::addressof(rows), std::addressof(columns)) == 2)
36-
return Container::Size2{columns - 1, rows - 1};
36+
return Container::Size2{columns, rows};
3737
return std::unexpected{"Cursor position retrieval failure"sv};
3838
}
3939
#endif
@@ -46,7 +46,7 @@ export namespace CppUtils::Terminal
4646
#elif defined(OS_LINUX) or defined(OS_MACOS)
4747
inline auto setCursorPosition([[maybe_unused]] Container::Size2 position) -> void
4848
{
49-
std::print("\x1b[{};{}H", position.y() + 1, position.x() + 1);
49+
std::print("\x1b[{};{}H", position.y(), position.x());
5050
}
5151
#endif
5252
}

modules/Terminal/Utility.mpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,31 @@ module;
44

55
export module CppUtils.Terminal.Utility;
66

7+
import CppUtils.System.Type;
8+
import CppUtils.System.Error;
9+
import CppUtils.Terminal.Handle;
10+
711
export namespace CppUtils::Terminal
812
{
913
inline auto setConsoleOutputUTF8() -> void
1014
{
1115
#if defined(OS_WINDOWS)
1216
SetConsoleOutputCP(CP_UTF8);
17+
#endif
18+
}
19+
20+
inline auto enableAnsi() -> void
21+
{
22+
#if defined(OS_WINDOWS)
23+
auto outputHandle = getTerminalHandle(stdout);
24+
if (outputHandle == System::InvalidHandle)
25+
return;
26+
27+
auto mode = DWORD{0};
28+
if (not GetConsoleMode(outputHandle, std::addressof(mode)))
29+
throwLastError("CppUtils::Terminal::enableAnsi: GetConsoleMode failed");
30+
31+
SetConsoleMode(outputHandle, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
1332
#endif
1433
}
1534
}

tests/Terminal/Canvas.mpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export namespace CppUtils::UnitTest::Terminal::Canvas
5555
frame = 0;
5656
if (++loop == 5)
5757
{
58-
canvas.stop();
58+
canvas.close();
5959
return;
6060
}
6161
}

0 commit comments

Comments
 (0)