Repository navigation
feat(config): add RAII guard for raw mode #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
861c2e9
feat(config): add RAII guard for raw mode
Aintdev 415fcd9
fix(rawmodeguard): change file encoding to utf8
Aintdev 44484e3
fix(rawmodeguard): check console-mode retrieval and fix VT-processing…
Aintdev 72e0305
feat(raw_mode_guard): added to umbrella header
Aintdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| #include "pch.h" | ||
|
|
||
| #include "ctui_raw_mode.h" | ||
|
|
||
|
|
||
| namespace ctui | ||
| { | ||
| RawModeGuard::RawModeGuard() | ||
| { | ||
| #ifdef _WIN32 | ||
| _stdin = GetStdHandle(STD_INPUT_HANDLE); | ||
| _stdout = GetStdHandle(STD_OUTPUT_HANDLE); | ||
|
|
||
| // GetConsoleMode fails if the handle is invalid or redirected (e.g. stdin/stdout | ||
| // piped to a file). Without this check, _original_*_mode stays uninitialized and | ||
| // we'd later restore garbage in the destructor. | ||
| if (!GetConsoleMode(_stdin, &_original_stdin_mode)) | ||
| throw std::system_error(GetLastError(), std::system_category(), "GetConsoleMode (stdin) failed"); | ||
| if (!GetConsoleMode(_stdout, &_original_stdout_mode)) | ||
| throw std::system_error(GetLastError(), std::system_category(), "GetConsoleMode (stdout) failed"); | ||
|
|
||
| DWORD stdin_mode = _original_stdin_mode; | ||
| stdin_mode &= ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT); | ||
| SetConsoleMode(_stdin, stdin_mode); | ||
|
|
||
| DWORD stdout_mode = _original_stdout_mode; | ||
| stdout_mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; | ||
| SetConsoleMode(_stdout, stdout_mode); | ||
| #else | ||
| // Same reasoning as GetConsoleMode above: tcgetattr fails for non-TTY stdin | ||
| // (redirected/piped), and leaves _original untouched on failure. | ||
| if (tcgetattr(STDIN_FILENO, &_original) != 0) | ||
| throw std::system_error(errno, std::generic_category(), "tcgetattr failed"); | ||
|
|
||
| termios raw = _original; | ||
| raw.c_lflag &= ~(ICANON | ECHO); | ||
| raw.c_iflag &= ~IXON; | ||
|
|
||
| tcsetattr(STDIN_FILENO, TCSANOW, &raw); | ||
| #endif | ||
|
|
||
| // TODO: Move code to main loop as soon as it exists. | ||
| std::ios::sync_with_stdio(false); | ||
| std::cout << "\033[?1049h\033[2J\033[H"; | ||
| } | ||
|
|
||
| RawModeGuard::~RawModeGuard() | ||
| { | ||
| // Must run *before* restoring the console mode below: if the original stdout | ||
| // mode didn't have ENABLE_VIRTUAL_TERMINAL_PROCESSING (the common case), the | ||
| // escape sequence would no longer be interpreted once we restore it, and the | ||
| // terminal would be stuck in the alternate screen buffer. | ||
| std::cout << "\033[?1049l"; // TODO: move to main loop | ||
|
|
||
| #ifdef _WIN32 | ||
| SetConsoleMode(_stdin, _original_stdin_mode); | ||
| SetConsoleMode(_stdout, _original_stdout_mode); | ||
| #else | ||
| tcsetattr(STDIN_FILENO, TCSANOW, &_original); | ||
| #endif | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| #pragma once | ||
|
|
||
| #ifdef _WIN32 | ||
| #include <windows.h> | ||
| #else | ||
| #include <termios.h> | ||
| #include <unistd.h> | ||
| #endif | ||
|
|
||
| namespace ctui | ||
| { | ||
| class RawModeGuard | ||
|
Aintdev marked this conversation as resolved.
Resolved
|
||
| { | ||
| // Original Terminal State | ||
| #ifdef _WIN32 | ||
| HANDLE _stdin; | ||
| HANDLE _stdout; | ||
|
|
||
| DWORD _original_stdin_mode; | ||
| DWORD _original_stdout_mode; | ||
| #else | ||
| termios _original; | ||
| #endif | ||
| public: | ||
| RawModeGuard(); | ||
| ~RawModeGuard(); | ||
|
|
||
| RawModeGuard(const RawModeGuard&) = delete; | ||
| RawModeGuard& operator=(const RawModeGuard&) = delete; | ||
| RawModeGuard(RawModeGuard&&) = delete; | ||
| RawModeGuard& operator=(RawModeGuard&&) = delete; | ||
| }; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.