Skip to content

Commit 8a9c1ac

Browse files
refactor: update preprocessor directives for Windows compatibility checks
1 parent 9a1a069 commit 8a9c1ac

File tree

1 file changed

+11
-82
lines changed

1 file changed

+11
-82
lines changed

modules/terminal/src/cpp/replxx/src/terminal.cxx

Lines changed: 11 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ char32_t
433433
Terminal::read_char(void)
434434
{
435435
char32_t c(0);
436-
#ifdef _WIN32
436+
#if defined(_MSC_VER)
437437
INPUT_RECORD rec;
438438
DWORD count;
439439
char32_t modifierKeys = 0;
@@ -643,7 +643,7 @@ Terminal::read_char(void)
643643
Terminal::EVENT_TYPE
644644
Terminal::wait_for_input(int long timeout_)
645645
{
646-
#ifdef _WIN32
646+
#if defined(_MSC_VER)
647647
std::array<HANDLE, 2> handles = { _consoleIn, _interrupt };
648648
auto const timeoutEnabled(timeout_ >= 0);
649649
auto const timeoutBudget(
@@ -815,7 +815,7 @@ Terminal::wait_for_input(int long timeout_)
815815
void
816816
Terminal::notify_event(EVENT_TYPE eventType_)
817817
{
818-
#ifdef _WIN32
818+
#if defined(_MSC_VER)
819819
_events.push_back(eventType_);
820820
SetEvent(_interrupt);
821821
#else
@@ -832,7 +832,7 @@ Terminal::notify_event(EVENT_TYPE eventType_)
832832
void
833833
Terminal::clear_screen(CLEAR_SCREEN clearScreen_)
834834
{
835-
#ifdef _WIN32
835+
#if defined(_MSC_VER)
836836
if (_autoEscape) {
837837
#endif
838838
if (clearScreen_ == CLEAR_SCREEN::WHOLE) {
@@ -843,7 +843,7 @@ Terminal::clear_screen(CLEAR_SCREEN clearScreen_)
843843
static_cast<void>(write(_out_fd, clearCode, sizeof(clearCode) - 1) >= 0);
844844
}
845845
return;
846-
#ifdef _WIN32
846+
#if defined(_MSC_VER)
847847
}
848848
COORD coord = { 0, 0 };
849849
CONSOLE_SCREEN_BUFFER_INFO inf;
@@ -872,7 +872,7 @@ Terminal::clear_screen(CLEAR_SCREEN clearScreen_)
872872
void
873873
Terminal::jump_cursor(int xPos_, int yOffset_)
874874
{
875-
#ifdef _WIN32
875+
#if defined(_MSC_VER)
876876
CONSOLE_SCREEN_BUFFER_INFO inf;
877877
GetConsoleScreenBufferInfo(_consoleOut, &inf);
878878
inf.dwCursorPosition.X = xPos_;
@@ -891,22 +891,19 @@ Terminal::jump_cursor(int xPos_, int yOffset_)
891891
#endif
892892
}
893893

894-
#if defined(_WIN32)
895894
bool
896895
Terminal::is_at_beginning_of_the_line(void)
897896
{
897+
#if defined(_MSC_VER)
898+
898899
CONSOLE_SCREEN_BUFFER_INFO inf;
899900
HANDLE consoleOut(
900901
_consoleOut != INVALID_HANDLE_VALUE ? _consoleOut : GetStdHandle(STD_OUTPUT_HANDLE));
901902
if (!GetConsoleScreenBufferInfo(consoleOut, &inf)) {
902903
return false;
903904
}
904905
return (inf.dwCursorPosition.X == 0);
905-
}
906906
#else
907-
bool
908-
Terminal::is_at_beginning_of_the_line(void)
909-
{
910907
// Request cursor position report (DSR) and parse response: ESC [ row ; col R
911908
const char request[] = "\033[6n";
912909
write8(request, sizeof(request) - 1);
@@ -952,90 +949,23 @@ Terminal::is_at_beginning_of_the_line(void)
952949
}
953950
int col = atoi(inside.c_str() + sep + 1);
954951
return (col == 1); // DSR is 1-based
955-
}
956952
#endif
953+
}
957954

958-
#ifdef _WIN32
959955
void
960956
Terminal::set_cursor_visible(bool visible_)
961957
{
958+
#if defined(_MSC_VER)
962959
CONSOLE_CURSOR_INFO cursorInfo;
963960
GetConsoleCursorInfo(_consoleOut, &cursorInfo);
964961
cursorInfo.bVisible = visible_;
965962
SetConsoleCursorInfo(_consoleOut, &cursorInfo);
966963
return;
967-
}
968-
#if defined(_WIN32)
969-
bool
970-
Terminal::is_at_beginning_of_the_line(void)
971-
{
972-
CONSOLE_SCREEN_BUFFER_INFO inf;
973-
HANDLE consoleOut(
974-
_consoleOut != INVALID_HANDLE_VALUE ? _consoleOut : GetStdHandle(STD_OUTPUT_HANDLE));
975-
if (!GetConsoleScreenBufferInfo(consoleOut, &inf)) {
976-
return false;
977-
}
978-
return (inf.dwCursorPosition.X == 0);
979-
}
980964
#else
981-
bool
982-
Terminal::is_at_beginning_of_the_line(void)
983-
{
984-
// Request cursor position report (DSR) and parse response: ESC [ row ; col R
985-
const char request[] = "\033[6n";
986-
write8(request, sizeof(request) - 1);
987-
988-
fd_set rfds;
989-
struct timeval tv;
990-
tv.tv_sec = 0;
991-
tv.tv_usec = 200000; // 200 ms
992-
993-
std::string resp;
994-
int fd = _in_fd;
995-
char buf[32];
996-
997-
while (true) {
998-
FD_ZERO(&rfds);
999-
FD_SET(fd, &rfds);
1000-
int ret = select(fd + 1, &rfds, nullptr, nullptr, &tv);
1001-
if (ret <= 0) {
1002-
break; // timeout or error
1003-
}
1004-
ssize_t n = read(fd, buf, sizeof(buf));
1005-
if (n <= 0) {
1006-
break;
1007-
}
1008-
resp.append(buf, buf + n);
1009-
if (resp.find('R') != std::string::npos) {
1010-
break;
1011-
}
1012-
// small additional delay for more data
1013-
tv.tv_sec = 0;
1014-
tv.tv_usec = 50000; // 50ms
1015-
}
1016-
1017-
size_t p1 = resp.find('[');
1018-
size_t p2 = resp.find('R');
1019-
if (p1 == std::string::npos || p2 == std::string::npos || p2 <= p1) {
1020-
return false;
1021-
}
1022-
std::string inside = resp.substr(p1 + 1, p2 - p1 - 1);
1023-
size_t sep = inside.find(';');
1024-
if (sep == std::string::npos) {
1025-
return false;
1026-
}
1027-
int col = atoi(inside.c_str() + sep + 1);
1028-
return (col == 1); // DSR is 1-based
1029-
}
1030965
#endif
1031-
#else
1032-
void
1033-
Terminal::set_cursor_visible(bool)
1034-
{
1035966
}
1036-
#endif
1037967

1038-
#ifndef _WIN32
968+
#if not defined(_MSC_VER)
1039969
int
1040970
Terminal::read_verbatim(char32_t* buffer_, int size_)
1041971
{
@@ -1068,5 +998,4 @@ Terminal::install_window_change_handler(void)
1068998
return 0;
1069999
}
10701000
#endif
1071-
10721001
}

0 commit comments

Comments
 (0)