MCPcopy Create free account
hub / github.com/LagPixelLOL/ChatGPTCLIBot / read_raw

Method read_raw

main/interface/cpp-terminal/platforms/input.cpp:27–75  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

25}
26
27bool Term::Platform::read_raw(char* s) {
28 //Do nothing when TTY is not connected.
29 if (!is_stdin_a_tty()) {
30 return false;
31 }
32#ifdef _WIN32
33 DWORD n_read = 0;
34 GetNumberOfConsoleInputEvents(GetStdHandle(STD_INPUT_HANDLE), &n_read);
35 if (n_read >= 1) {
36 INPUT_RECORD buf;
37 if (!ReadConsoleInputW(GetStdHandle(STD_INPUT_HANDLE), &buf, 1, &n_read)) {
38 throw Exception("ReadConsoleInput() failed.");
39 }
40 if (n_read == 1) {
41 switch (buf.EventType) {
42 case KEY_EVENT: {
43 WORD skip = buf.Event.KeyEvent.wVirtualKeyCode; //Skip them for now.
44 if (skip == VK_SHIFT || skip == VK_LWIN || skip == VK_RWIN || skip == VK_APPS
45 || skip == VK_CONTROL || skip == VK_MENU || skip == VK_CAPITAL) {
46 return false;
47 }
48 if (buf.Event.KeyEvent.bKeyDown) {
49 *s = buf.Event.KeyEvent.uChar.AsciiChar;
50 return true;
51 } else {
52 return false;
53 }
54 }
55 case FOCUS_EVENT:
56 case MENU_EVENT:
57 case MOUSE_EVENT:
58 case WINDOW_BUFFER_SIZE_EVENT:
59 default:
60 return false;
61 }
62 } else {
63 throw Exception("kbhit() and ReadConsoleInput() inconsistent.");
64 }
65 } else {
66 return false;
67 }
68#else
69 ::ssize_t nread = ::read(0, s, 1);
70 if (nread == -1 && errno != EAGAIN) {
71 throw Term::Exception("read() failed");
72 }
73 return nread == 1;
74#endif
75}

Callers

nothing calls this directly

Calls 1

ExceptionClass · 0.85

Tested by

no test coverage detected