console.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. #include "console.h"
  2. #include <vector>
  3. #include <iostream>
  4. #include <cassert>
  5. #include <cstddef>
  6. #include <cctype>
  7. #include <cwctype>
  8. #include <cstdint>
  9. #if defined(_WIN32)
  10. #define WIN32_LEAN_AND_MEAN
  11. #ifndef NOMINMAX
  12. #define NOMINMAX
  13. #endif
  14. #include <windows.h>
  15. #include <fcntl.h>
  16. #include <io.h>
  17. #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
  18. #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
  19. #endif
  20. #else
  21. #include <climits>
  22. #include <sys/ioctl.h>
  23. #include <unistd.h>
  24. #include <wchar.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <signal.h>
  28. #include <termios.h>
  29. #endif
  30. #define ANSI_COLOR_RED "\x1b[31m"
  31. #define ANSI_COLOR_GREEN "\x1b[32m"
  32. #define ANSI_COLOR_YELLOW "\x1b[33m"
  33. #define ANSI_COLOR_BLUE "\x1b[34m"
  34. #define ANSI_COLOR_MAGENTA "\x1b[35m"
  35. #define ANSI_COLOR_CYAN "\x1b[36m"
  36. #define ANSI_COLOR_RESET "\x1b[0m"
  37. #define ANSI_BOLD "\x1b[1m"
  38. namespace console {
  39. #if defined (_WIN32)
  40. namespace {
  41. // Use private-use unicode values to represent special keys that are not reported
  42. // as characters (e.g. arrows on Windows). These values should never clash with
  43. // real input and let the rest of the code handle navigation uniformly.
  44. static constexpr char32_t KEY_ARROW_LEFT = 0xE000;
  45. static constexpr char32_t KEY_ARROW_RIGHT = 0xE001;
  46. static constexpr char32_t KEY_ARROW_UP = 0xE002;
  47. static constexpr char32_t KEY_ARROW_DOWN = 0xE003;
  48. static constexpr char32_t KEY_HOME = 0xE004;
  49. static constexpr char32_t KEY_END = 0xE005;
  50. static constexpr char32_t KEY_CTRL_ARROW_LEFT = 0xE006;
  51. static constexpr char32_t KEY_CTRL_ARROW_RIGHT = 0xE007;
  52. static constexpr char32_t KEY_DELETE = 0xE008;
  53. }
  54. //
  55. // Console state
  56. //
  57. #endif
  58. static bool advanced_display = false;
  59. static bool simple_io = true;
  60. static display_t current_display = reset;
  61. static FILE* out = stdout;
  62. #if defined (_WIN32)
  63. static void* hConsole;
  64. #else
  65. static FILE* tty = nullptr;
  66. static termios initial_state;
  67. #endif
  68. //
  69. // Init and cleanup
  70. //
  71. void init(bool use_simple_io, bool use_advanced_display) {
  72. advanced_display = use_advanced_display;
  73. simple_io = use_simple_io;
  74. #if defined(_WIN32)
  75. // Windows-specific console initialization
  76. DWORD dwMode = 0;
  77. hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  78. if (hConsole == INVALID_HANDLE_VALUE || !GetConsoleMode(hConsole, &dwMode)) {
  79. hConsole = GetStdHandle(STD_ERROR_HANDLE);
  80. if (hConsole != INVALID_HANDLE_VALUE && (!GetConsoleMode(hConsole, &dwMode))) {
  81. hConsole = nullptr;
  82. simple_io = true;
  83. }
  84. }
  85. if (hConsole) {
  86. // Check conditions combined to reduce nesting
  87. if (advanced_display && !(dwMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) &&
  88. !SetConsoleMode(hConsole, dwMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)) {
  89. advanced_display = false;
  90. }
  91. // Set console output codepage to UTF8
  92. SetConsoleOutputCP(CP_UTF8);
  93. }
  94. HANDLE hConIn = GetStdHandle(STD_INPUT_HANDLE);
  95. if (hConIn != INVALID_HANDLE_VALUE && GetConsoleMode(hConIn, &dwMode)) {
  96. // Set console input codepage to UTF16
  97. _setmode(_fileno(stdin), _O_WTEXT);
  98. // Set ICANON (ENABLE_LINE_INPUT) and ECHO (ENABLE_ECHO_INPUT)
  99. if (simple_io) {
  100. dwMode |= ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
  101. } else {
  102. dwMode &= ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
  103. }
  104. if (!SetConsoleMode(hConIn, dwMode)) {
  105. simple_io = true;
  106. }
  107. }
  108. if (simple_io) {
  109. _setmode(_fileno(stdin), _O_U8TEXT);
  110. }
  111. #else
  112. // POSIX-specific console initialization
  113. if (!simple_io) {
  114. struct termios new_termios;
  115. tcgetattr(STDIN_FILENO, &initial_state);
  116. new_termios = initial_state;
  117. new_termios.c_lflag &= ~(ICANON | ECHO);
  118. new_termios.c_cc[VMIN] = 1;
  119. new_termios.c_cc[VTIME] = 0;
  120. tcsetattr(STDIN_FILENO, TCSANOW, &new_termios);
  121. tty = fopen("/dev/tty", "w+");
  122. if (tty != nullptr) {
  123. out = tty;
  124. }
  125. }
  126. setlocale(LC_ALL, "");
  127. #endif
  128. }
  129. void cleanup() {
  130. // Reset console display
  131. set_display(reset);
  132. #if !defined(_WIN32)
  133. // Restore settings on POSIX systems
  134. if (!simple_io) {
  135. if (tty != nullptr) {
  136. out = stdout;
  137. fclose(tty);
  138. tty = nullptr;
  139. }
  140. tcsetattr(STDIN_FILENO, TCSANOW, &initial_state);
  141. }
  142. #endif
  143. }
  144. //
  145. // Display and IO
  146. //
  147. // Keep track of current display and only emit ANSI code if it changes
  148. void set_display(display_t display) {
  149. if (advanced_display && current_display != display) {
  150. fflush(stdout);
  151. switch(display) {
  152. case reset:
  153. fprintf(out, ANSI_COLOR_RESET);
  154. break;
  155. case prompt:
  156. fprintf(out, ANSI_COLOR_YELLOW);
  157. break;
  158. case user_input:
  159. fprintf(out, ANSI_BOLD ANSI_COLOR_GREEN);
  160. break;
  161. case error:
  162. fprintf(out, ANSI_BOLD ANSI_COLOR_RED);
  163. }
  164. current_display = display;
  165. fflush(out);
  166. }
  167. }
  168. static char32_t getchar32() {
  169. #if defined(_WIN32)
  170. HANDLE hConsole = GetStdHandle(STD_INPUT_HANDLE);
  171. wchar_t high_surrogate = 0;
  172. while (true) {
  173. INPUT_RECORD record;
  174. DWORD count;
  175. if (!ReadConsoleInputW(hConsole, &record, 1, &count) || count == 0) {
  176. return WEOF;
  177. }
  178. if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown) {
  179. wchar_t wc = record.Event.KeyEvent.uChar.UnicodeChar;
  180. if (wc == 0) {
  181. const DWORD ctrl_mask = LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED;
  182. const bool ctrl_pressed = (record.Event.KeyEvent.dwControlKeyState & ctrl_mask) != 0;
  183. switch (record.Event.KeyEvent.wVirtualKeyCode) {
  184. case VK_LEFT: return ctrl_pressed ? KEY_CTRL_ARROW_LEFT : KEY_ARROW_LEFT;
  185. case VK_RIGHT: return ctrl_pressed ? KEY_CTRL_ARROW_RIGHT : KEY_ARROW_RIGHT;
  186. case VK_UP: return KEY_ARROW_UP;
  187. case VK_DOWN: return KEY_ARROW_DOWN;
  188. case VK_HOME: return KEY_HOME;
  189. case VK_END: return KEY_END;
  190. case VK_DELETE: return KEY_DELETE;
  191. default: continue;
  192. }
  193. }
  194. if ((wc >= 0xD800) && (wc <= 0xDBFF)) { // Check if wc is a high surrogate
  195. high_surrogate = wc;
  196. continue;
  197. }
  198. if ((wc >= 0xDC00) && (wc <= 0xDFFF)) { // Check if wc is a low surrogate
  199. if (high_surrogate != 0) { // Check if we have a high surrogate
  200. return ((high_surrogate - 0xD800) << 10) + (wc - 0xDC00) + 0x10000;
  201. }
  202. }
  203. high_surrogate = 0; // Reset the high surrogate
  204. return static_cast<char32_t>(wc);
  205. }
  206. }
  207. #else
  208. wchar_t wc = getwchar();
  209. if (static_cast<wint_t>(wc) == WEOF) {
  210. return WEOF;
  211. }
  212. #if WCHAR_MAX == 0xFFFF
  213. if ((wc >= 0xD800) && (wc <= 0xDBFF)) { // Check if wc is a high surrogate
  214. wchar_t low_surrogate = getwchar();
  215. if ((low_surrogate >= 0xDC00) && (low_surrogate <= 0xDFFF)) { // Check if the next wchar is a low surrogate
  216. return (static_cast<char32_t>(wc & 0x03FF) << 10) + (low_surrogate & 0x03FF) + 0x10000;
  217. }
  218. }
  219. if ((wc >= 0xD800) && (wc <= 0xDFFF)) { // Invalid surrogate pair
  220. return 0xFFFD; // Return the replacement character U+FFFD
  221. }
  222. #endif
  223. return static_cast<char32_t>(wc);
  224. #endif
  225. }
  226. static void pop_cursor() {
  227. #if defined(_WIN32)
  228. if (hConsole != NULL) {
  229. CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
  230. GetConsoleScreenBufferInfo(hConsole, &bufferInfo);
  231. COORD newCursorPosition = bufferInfo.dwCursorPosition;
  232. if (newCursorPosition.X == 0) {
  233. newCursorPosition.X = bufferInfo.dwSize.X - 1;
  234. newCursorPosition.Y -= 1;
  235. } else {
  236. newCursorPosition.X -= 1;
  237. }
  238. SetConsoleCursorPosition(hConsole, newCursorPosition);
  239. return;
  240. }
  241. #endif
  242. putc('\b', out);
  243. }
  244. static int estimateWidth(char32_t codepoint) {
  245. #if defined(_WIN32)
  246. (void)codepoint;
  247. return 1;
  248. #else
  249. return wcwidth(codepoint);
  250. #endif
  251. }
  252. static int put_codepoint(const char* utf8_codepoint, size_t length, int expectedWidth) {
  253. #if defined(_WIN32)
  254. CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
  255. if (!GetConsoleScreenBufferInfo(hConsole, &bufferInfo)) {
  256. // go with the default
  257. return expectedWidth;
  258. }
  259. COORD initialPosition = bufferInfo.dwCursorPosition;
  260. DWORD nNumberOfChars = length;
  261. WriteConsole(hConsole, utf8_codepoint, nNumberOfChars, &nNumberOfChars, NULL);
  262. CONSOLE_SCREEN_BUFFER_INFO newBufferInfo;
  263. GetConsoleScreenBufferInfo(hConsole, &newBufferInfo);
  264. // Figure out our real position if we're in the last column
  265. if (utf8_codepoint[0] != 0x09 && initialPosition.X == newBufferInfo.dwSize.X - 1) {
  266. DWORD nNumberOfChars;
  267. WriteConsole(hConsole, &" \b", 2, &nNumberOfChars, NULL);
  268. GetConsoleScreenBufferInfo(hConsole, &newBufferInfo);
  269. }
  270. int width = newBufferInfo.dwCursorPosition.X - initialPosition.X;
  271. if (width < 0) {
  272. width += newBufferInfo.dwSize.X;
  273. }
  274. return width;
  275. #else
  276. // We can trust expectedWidth if we've got one
  277. if (expectedWidth >= 0 || tty == nullptr) {
  278. fwrite(utf8_codepoint, length, 1, out);
  279. return expectedWidth;
  280. }
  281. fputs("\033[6n", tty); // Query cursor position
  282. int x1;
  283. int y1;
  284. int x2;
  285. int y2;
  286. int results = 0;
  287. results = fscanf(tty, "\033[%d;%dR", &y1, &x1);
  288. fwrite(utf8_codepoint, length, 1, tty);
  289. fputs("\033[6n", tty); // Query cursor position
  290. results += fscanf(tty, "\033[%d;%dR", &y2, &x2);
  291. if (results != 4) {
  292. return expectedWidth;
  293. }
  294. int width = x2 - x1;
  295. if (width < 0) {
  296. // Calculate the width considering text wrapping
  297. struct winsize w;
  298. ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
  299. width += w.ws_col;
  300. }
  301. return width;
  302. #endif
  303. }
  304. static void replace_last(char ch) {
  305. #if defined(_WIN32)
  306. pop_cursor();
  307. put_codepoint(&ch, 1, 1);
  308. #else
  309. fprintf(out, "\b%c", ch);
  310. #endif
  311. }
  312. static char32_t decode_utf8(const std::string & input, size_t pos, size_t & advance) {
  313. unsigned char c = static_cast<unsigned char>(input[pos]);
  314. if ((c & 0x80u) == 0u) {
  315. advance = 1;
  316. return c;
  317. }
  318. if ((c & 0xE0u) == 0xC0u && pos + 1 < input.size()) {
  319. unsigned char c1 = static_cast<unsigned char>(input[pos + 1]);
  320. if ((c1 & 0xC0u) != 0x80u) {
  321. advance = 1;
  322. return 0xFFFD;
  323. }
  324. advance = 2;
  325. return ((c & 0x1Fu) << 6) | (static_cast<unsigned char>(input[pos + 1]) & 0x3Fu);
  326. }
  327. if ((c & 0xF0u) == 0xE0u && pos + 2 < input.size()) {
  328. unsigned char c1 = static_cast<unsigned char>(input[pos + 1]);
  329. unsigned char c2 = static_cast<unsigned char>(input[pos + 2]);
  330. if ((c1 & 0xC0u) != 0x80u || (c2 & 0xC0u) != 0x80u) {
  331. advance = 1;
  332. return 0xFFFD;
  333. }
  334. advance = 3;
  335. return ((c & 0x0Fu) << 12) |
  336. ((static_cast<unsigned char>(input[pos + 1]) & 0x3Fu) << 6) |
  337. (static_cast<unsigned char>(input[pos + 2]) & 0x3Fu);
  338. }
  339. if ((c & 0xF8u) == 0xF0u && pos + 3 < input.size()) {
  340. unsigned char c1 = static_cast<unsigned char>(input[pos + 1]);
  341. unsigned char c2 = static_cast<unsigned char>(input[pos + 2]);
  342. unsigned char c3 = static_cast<unsigned char>(input[pos + 3]);
  343. if ((c1 & 0xC0u) != 0x80u || (c2 & 0xC0u) != 0x80u || (c3 & 0xC0u) != 0x80u) {
  344. advance = 1;
  345. return 0xFFFD;
  346. }
  347. advance = 4;
  348. return ((c & 0x07u) << 18) |
  349. ((static_cast<unsigned char>(input[pos + 1]) & 0x3Fu) << 12) |
  350. ((static_cast<unsigned char>(input[pos + 2]) & 0x3Fu) << 6) |
  351. (static_cast<unsigned char>(input[pos + 3]) & 0x3Fu);
  352. }
  353. advance = 1;
  354. return 0xFFFD; // replacement character for invalid input
  355. }
  356. static void append_utf8(char32_t ch, std::string & out) {
  357. if (ch <= 0x7F) {
  358. out.push_back(static_cast<unsigned char>(ch));
  359. } else if (ch <= 0x7FF) {
  360. out.push_back(static_cast<unsigned char>(0xC0 | ((ch >> 6) & 0x1F)));
  361. out.push_back(static_cast<unsigned char>(0x80 | (ch & 0x3F)));
  362. } else if (ch <= 0xFFFF) {
  363. out.push_back(static_cast<unsigned char>(0xE0 | ((ch >> 12) & 0x0F)));
  364. out.push_back(static_cast<unsigned char>(0x80 | ((ch >> 6) & 0x3F)));
  365. out.push_back(static_cast<unsigned char>(0x80 | (ch & 0x3F)));
  366. } else if (ch <= 0x10FFFF) {
  367. out.push_back(static_cast<unsigned char>(0xF0 | ((ch >> 18) & 0x07)));
  368. out.push_back(static_cast<unsigned char>(0x80 | ((ch >> 12) & 0x3F)));
  369. out.push_back(static_cast<unsigned char>(0x80 | ((ch >> 6) & 0x3F)));
  370. out.push_back(static_cast<unsigned char>(0x80 | (ch & 0x3F)));
  371. } else {
  372. // Invalid Unicode code point
  373. }
  374. }
  375. // Helper function to remove the last UTF-8 character from a string
  376. static size_t prev_utf8_char_pos(const std::string & line, size_t pos) {
  377. if (pos == 0) return 0;
  378. pos--;
  379. while (pos > 0 && (line[pos] & 0xC0) == 0x80) {
  380. pos--;
  381. }
  382. return pos;
  383. }
  384. static size_t next_utf8_char_pos(const std::string & line, size_t pos) {
  385. if (pos >= line.length()) return line.length();
  386. pos++;
  387. while (pos < line.length() && (line[pos] & 0xC0) == 0x80) {
  388. pos++;
  389. }
  390. return pos;
  391. }
  392. static void move_cursor(int delta);
  393. static void move_word_left(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths, const std::string & line);
  394. static void move_word_right(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths, const std::string & line);
  395. static void move_to_line_start(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths);
  396. static void move_to_line_end(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths, const std::string & line);
  397. static void delete_at_cursor(std::string & line, std::vector<int> & widths, size_t & char_pos, size_t & byte_pos) {
  398. if (char_pos >= widths.size()) {
  399. return;
  400. }
  401. size_t next_pos = next_utf8_char_pos(line, byte_pos);
  402. int w = widths[char_pos];
  403. size_t char_len = next_pos - byte_pos;
  404. line.erase(byte_pos, char_len);
  405. widths.erase(widths.begin() + char_pos);
  406. size_t p = byte_pos;
  407. int tail_width = 0;
  408. for (size_t i = char_pos; i < widths.size(); ++i) {
  409. size_t following = next_utf8_char_pos(line, p);
  410. put_codepoint(line.c_str() + p, following - p, widths[i]);
  411. tail_width += widths[i];
  412. p = following;
  413. }
  414. for (int i = 0; i < w; ++i) {
  415. fputc(' ', out);
  416. }
  417. move_cursor(-(tail_width + w));
  418. }
  419. static void clear_current_line(const std::vector<int> & widths) {
  420. int total_width = 0;
  421. for (int w : widths) {
  422. total_width += (w > 0 ? w : 1);
  423. }
  424. if (total_width > 0) {
  425. std::string spaces(total_width, ' ');
  426. fwrite(spaces.c_str(), 1, total_width, out);
  427. move_cursor(-total_width);
  428. }
  429. }
  430. static void set_line_contents(std::string new_line, std::string & line, std::vector<int> & widths, size_t & char_pos,
  431. size_t & byte_pos) {
  432. move_to_line_start(char_pos, byte_pos, widths);
  433. clear_current_line(widths);
  434. line = std::move(new_line);
  435. widths.clear();
  436. byte_pos = 0;
  437. char_pos = 0;
  438. size_t idx = 0;
  439. while (idx < line.size()) {
  440. size_t advance = 0;
  441. char32_t cp = decode_utf8(line, idx, advance);
  442. int expected_width = estimateWidth(cp);
  443. int real_width = put_codepoint(line.c_str() + idx, advance, expected_width);
  444. if (real_width < 0) real_width = 0;
  445. widths.push_back(real_width);
  446. idx += advance;
  447. ++char_pos;
  448. byte_pos = idx;
  449. }
  450. }
  451. static void move_to_line_start(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths) {
  452. int back_width = 0;
  453. for (size_t i = 0; i < char_pos; ++i) {
  454. back_width += widths[i];
  455. }
  456. move_cursor(-back_width);
  457. char_pos = 0;
  458. byte_pos = 0;
  459. }
  460. static void move_to_line_end(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths, const std::string & line) {
  461. int forward_width = 0;
  462. for (size_t i = char_pos; i < widths.size(); ++i) {
  463. forward_width += widths[i];
  464. }
  465. move_cursor(forward_width);
  466. char_pos = widths.size();
  467. byte_pos = line.length();
  468. }
  469. static bool has_ctrl_modifier(const std::string & params) {
  470. size_t start = 0;
  471. while (start < params.size()) {
  472. size_t end = params.find(';', start);
  473. size_t len = (end == std::string::npos) ? params.size() - start : end - start;
  474. if (len > 0) {
  475. int value = 0;
  476. for (size_t i = 0; i < len; ++i) {
  477. char ch = params[start + i];
  478. if (!std::isdigit(static_cast<unsigned char>(ch))) {
  479. value = -1;
  480. break;
  481. }
  482. value = value * 10 + (ch - '0');
  483. }
  484. if (value == 5) {
  485. return true;
  486. }
  487. }
  488. if (end == std::string::npos) {
  489. break;
  490. }
  491. start = end + 1;
  492. }
  493. return false;
  494. }
  495. static bool is_space_codepoint(char32_t cp) {
  496. return std::iswspace(static_cast<wint_t>(cp)) != 0;
  497. }
  498. static void move_word_left(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths, const std::string & line) {
  499. if (char_pos == 0) {
  500. return;
  501. }
  502. size_t new_char_pos = char_pos;
  503. size_t new_byte_pos = byte_pos;
  504. int move_width = 0;
  505. while (new_char_pos > 0) {
  506. size_t prev_byte = prev_utf8_char_pos(line, new_byte_pos);
  507. size_t advance = 0;
  508. char32_t cp = decode_utf8(line, prev_byte, advance);
  509. if (!is_space_codepoint(cp)) {
  510. break;
  511. }
  512. move_width += widths[new_char_pos - 1];
  513. new_char_pos--;
  514. new_byte_pos = prev_byte;
  515. }
  516. while (new_char_pos > 0) {
  517. size_t prev_byte = prev_utf8_char_pos(line, new_byte_pos);
  518. size_t advance = 0;
  519. char32_t cp = decode_utf8(line, prev_byte, advance);
  520. if (is_space_codepoint(cp)) {
  521. break;
  522. }
  523. move_width += widths[new_char_pos - 1];
  524. new_char_pos--;
  525. new_byte_pos = prev_byte;
  526. }
  527. move_cursor(-move_width);
  528. char_pos = new_char_pos;
  529. byte_pos = new_byte_pos;
  530. }
  531. static void move_word_right(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths, const std::string & line) {
  532. if (char_pos >= widths.size()) {
  533. return;
  534. }
  535. size_t new_char_pos = char_pos;
  536. size_t new_byte_pos = byte_pos;
  537. int move_width = 0;
  538. while (new_char_pos < widths.size()) {
  539. size_t advance = 0;
  540. char32_t cp = decode_utf8(line, new_byte_pos, advance);
  541. if (!is_space_codepoint(cp)) {
  542. break;
  543. }
  544. move_width += widths[new_char_pos];
  545. new_char_pos++;
  546. new_byte_pos += advance;
  547. }
  548. while (new_char_pos < widths.size()) {
  549. size_t advance = 0;
  550. char32_t cp = decode_utf8(line, new_byte_pos, advance);
  551. if (is_space_codepoint(cp)) {
  552. break;
  553. }
  554. move_width += widths[new_char_pos];
  555. new_char_pos++;
  556. new_byte_pos += advance;
  557. }
  558. while (new_char_pos < widths.size()) {
  559. size_t advance = 0;
  560. char32_t cp = decode_utf8(line, new_byte_pos, advance);
  561. if (!is_space_codepoint(cp)) {
  562. break;
  563. }
  564. move_width += widths[new_char_pos];
  565. new_char_pos++;
  566. new_byte_pos += advance;
  567. }
  568. move_cursor(move_width);
  569. char_pos = new_char_pos;
  570. byte_pos = new_byte_pos;
  571. }
  572. static void move_cursor(int delta) {
  573. if (delta == 0) return;
  574. #if defined(_WIN32)
  575. if (hConsole != NULL) {
  576. CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
  577. GetConsoleScreenBufferInfo(hConsole, &bufferInfo);
  578. COORD newCursorPosition = bufferInfo.dwCursorPosition;
  579. int width = bufferInfo.dwSize.X;
  580. int newX = newCursorPosition.X + delta;
  581. int newY = newCursorPosition.Y;
  582. while (newX >= width) {
  583. newX -= width;
  584. newY++;
  585. }
  586. while (newX < 0) {
  587. newX += width;
  588. newY--;
  589. }
  590. newCursorPosition.X = newX;
  591. newCursorPosition.Y = newY;
  592. SetConsoleCursorPosition(hConsole, newCursorPosition);
  593. }
  594. #else
  595. if (delta < 0) {
  596. for (int i = 0; i < -delta; i++) fprintf(out, "\b");
  597. } else {
  598. for (int i = 0; i < delta; i++) fprintf(out, "\033[C");
  599. }
  600. #endif
  601. }
  602. struct history_t {
  603. std::vector<std::string> entries;
  604. size_t viewing_idx = SIZE_MAX;
  605. std::string backup_line; // current line before viewing history
  606. void add(const std::string & line) {
  607. if (line.empty()) {
  608. return;
  609. }
  610. // avoid duplicates with the last entry
  611. if (entries.empty() || entries.back() != line) {
  612. entries.push_back(line);
  613. }
  614. // also clear viewing state
  615. end_viewing();
  616. }
  617. bool prev(std::string & cur_line) {
  618. if (entries.empty()) {
  619. return false;
  620. }
  621. if (viewing_idx == SIZE_MAX) {
  622. return false;
  623. }
  624. if (viewing_idx > 0) {
  625. viewing_idx--;
  626. }
  627. cur_line = entries[viewing_idx];
  628. return true;
  629. }
  630. bool next(std::string & cur_line) {
  631. if (entries.empty() || viewing_idx == SIZE_MAX) {
  632. return false;
  633. }
  634. viewing_idx++;
  635. if (viewing_idx >= entries.size()) {
  636. cur_line = backup_line;
  637. end_viewing();
  638. } else {
  639. cur_line = entries[viewing_idx];
  640. }
  641. return true;
  642. }
  643. void begin_viewing(const std::string & line) {
  644. backup_line = line;
  645. viewing_idx = entries.size();
  646. }
  647. void end_viewing() {
  648. viewing_idx = SIZE_MAX;
  649. backup_line.clear();
  650. }
  651. bool is_viewing() const {
  652. return viewing_idx != SIZE_MAX;
  653. }
  654. } history;
  655. static bool readline_advanced(std::string & line, bool multiline_input) {
  656. if (out != stdout) {
  657. fflush(stdout);
  658. }
  659. line.clear();
  660. std::vector<int> widths;
  661. bool is_special_char = false;
  662. bool end_of_stream = false;
  663. size_t byte_pos = 0; // current byte index
  664. size_t char_pos = 0; // current character index (one char can be multiple bytes)
  665. char32_t input_char;
  666. while (true) {
  667. assert(char_pos <= byte_pos);
  668. assert(char_pos <= widths.size());
  669. auto history_prev = [&]() {
  670. if (!history.is_viewing()) {
  671. history.begin_viewing(line);
  672. }
  673. std::string new_line;
  674. if (!history.prev(new_line)) {
  675. return;
  676. }
  677. set_line_contents(new_line, line, widths, char_pos, byte_pos);
  678. };
  679. auto history_next = [&]() {
  680. if (history.is_viewing()) {
  681. std::string new_line;
  682. if (!history.next(new_line)) {
  683. return;
  684. }
  685. set_line_contents(new_line, line, widths, char_pos, byte_pos);
  686. }
  687. };
  688. fflush(out); // Ensure all output is displayed before waiting for input
  689. input_char = getchar32();
  690. if (input_char == '\r' || input_char == '\n') {
  691. break;
  692. }
  693. if (input_char == (char32_t) WEOF || input_char == 0x04 /* Ctrl+D */) {
  694. end_of_stream = true;
  695. break;
  696. }
  697. if (is_special_char) {
  698. set_display(user_input);
  699. replace_last(line.back());
  700. is_special_char = false;
  701. }
  702. if (input_char == '\033') { // Escape sequence
  703. char32_t code = getchar32();
  704. if (code == '[') {
  705. std::string params;
  706. while (true) {
  707. code = getchar32();
  708. if ((code >= 'A' && code <= 'Z') || (code >= 'a' && code <= 'z') || code == '~' || code == (char32_t) WEOF) {
  709. break;
  710. }
  711. params.push_back(static_cast<char>(code));
  712. }
  713. const bool ctrl_modifier = has_ctrl_modifier(params);
  714. if (code == 'D') { // left
  715. if (ctrl_modifier) {
  716. move_word_left(char_pos, byte_pos, widths, line);
  717. } else if (char_pos > 0) {
  718. int w = widths[char_pos - 1];
  719. move_cursor(-w);
  720. char_pos--;
  721. byte_pos = prev_utf8_char_pos(line, byte_pos);
  722. }
  723. } else if (code == 'C') { // right
  724. if (ctrl_modifier) {
  725. move_word_right(char_pos, byte_pos, widths, line);
  726. } else if (char_pos < widths.size()) {
  727. int w = widths[char_pos];
  728. move_cursor(w);
  729. char_pos++;
  730. byte_pos = next_utf8_char_pos(line, byte_pos);
  731. }
  732. } else if (code == 'H') { // home
  733. move_to_line_start(char_pos, byte_pos, widths);
  734. } else if (code == 'F') { // end
  735. move_to_line_end(char_pos, byte_pos, widths, line);
  736. } else if (code == 'A' || code == 'B') {
  737. // up/down
  738. if (code == 'A') {
  739. history_prev();
  740. is_special_char = false;
  741. } else if (code == 'B') {
  742. history_next();
  743. is_special_char = false;
  744. }
  745. } else if ((code == '~' || (code >= 'A' && code <= 'Z') || (code >= 'a' && code <= 'z')) && !params.empty()) {
  746. std::string digits;
  747. for (char ch : params) {
  748. if (ch == ';') {
  749. break;
  750. }
  751. if (std::isdigit(static_cast<unsigned char>(ch))) {
  752. digits.push_back(ch);
  753. }
  754. }
  755. if (code == '~') {
  756. if (digits == "1" || digits == "7") { // home
  757. move_to_line_start(char_pos, byte_pos, widths);
  758. } else if (digits == "4" || digits == "8") { // end
  759. move_to_line_end(char_pos, byte_pos, widths, line);
  760. } else if (digits == "3") { // delete
  761. delete_at_cursor(line, widths, char_pos, byte_pos);
  762. }
  763. }
  764. }
  765. } else if (code == 0x1B) {
  766. // Discard the rest of the escape sequence
  767. while ((code = getchar32()) != (char32_t) WEOF) {
  768. if ((code >= 'A' && code <= 'Z') || (code >= 'a' && code <= 'z') || code == '~') {
  769. break;
  770. }
  771. }
  772. }
  773. #if defined(_WIN32)
  774. } else if (input_char == KEY_ARROW_LEFT) {
  775. if (char_pos > 0) {
  776. int w = widths[char_pos - 1];
  777. move_cursor(-w);
  778. char_pos--;
  779. byte_pos = prev_utf8_char_pos(line, byte_pos);
  780. }
  781. } else if (input_char == KEY_ARROW_RIGHT) {
  782. if (char_pos < widths.size()) {
  783. int w = widths[char_pos];
  784. move_cursor(w);
  785. char_pos++;
  786. byte_pos = next_utf8_char_pos(line, byte_pos);
  787. }
  788. } else if (input_char == KEY_CTRL_ARROW_LEFT) {
  789. move_word_left(char_pos, byte_pos, widths, line);
  790. } else if (input_char == KEY_CTRL_ARROW_RIGHT) {
  791. move_word_right(char_pos, byte_pos, widths, line);
  792. } else if (input_char == KEY_HOME) {
  793. move_to_line_start(char_pos, byte_pos, widths);
  794. } else if (input_char == KEY_END) {
  795. move_to_line_end(char_pos, byte_pos, widths, line);
  796. } else if (input_char == KEY_DELETE) {
  797. delete_at_cursor(line, widths, char_pos, byte_pos);
  798. } else if (input_char == KEY_ARROW_UP || input_char == KEY_ARROW_DOWN) {
  799. if (input_char == KEY_ARROW_UP) {
  800. history_prev();
  801. is_special_char = false;
  802. } else if (input_char == KEY_ARROW_DOWN) {
  803. history_next();
  804. is_special_char = false;
  805. }
  806. #endif
  807. } else if (input_char == 0x08 || input_char == 0x7F) { // Backspace
  808. if (char_pos > 0) {
  809. int w = widths[char_pos - 1];
  810. move_cursor(-w);
  811. char_pos--;
  812. size_t prev_pos = prev_utf8_char_pos(line, byte_pos);
  813. size_t char_len = byte_pos - prev_pos;
  814. byte_pos = prev_pos;
  815. // remove the character
  816. line.erase(byte_pos, char_len);
  817. widths.erase(widths.begin() + char_pos);
  818. // redraw tail
  819. size_t p = byte_pos;
  820. int tail_width = 0;
  821. for (size_t i = char_pos; i < widths.size(); ++i) {
  822. size_t next_p = next_utf8_char_pos(line, p);
  823. put_codepoint(line.c_str() + p, next_p - p, widths[i]);
  824. tail_width += widths[i];
  825. p = next_p;
  826. }
  827. // clear display
  828. for (int i = 0; i < w; ++i) {
  829. fputc(' ', out);
  830. }
  831. move_cursor(-(tail_width + w));
  832. }
  833. } else {
  834. // insert character
  835. std::string new_char_str;
  836. append_utf8(input_char, new_char_str);
  837. int w = estimateWidth(input_char);
  838. if (char_pos == widths.size()) {
  839. // insert at the end
  840. line += new_char_str;
  841. int real_w = put_codepoint(new_char_str.c_str(), new_char_str.length(), w);
  842. if (real_w < 0) real_w = 0;
  843. widths.push_back(real_w);
  844. byte_pos += new_char_str.length();
  845. char_pos++;
  846. } else {
  847. // insert in middle
  848. line.insert(byte_pos, new_char_str);
  849. int real_w = put_codepoint(new_char_str.c_str(), new_char_str.length(), w);
  850. if (real_w < 0) real_w = 0;
  851. widths.insert(widths.begin() + char_pos, real_w);
  852. // print the tail
  853. size_t p = byte_pos + new_char_str.length();
  854. int tail_width = 0;
  855. for (size_t i = char_pos + 1; i < widths.size(); ++i) {
  856. size_t next_p = next_utf8_char_pos(line, p);
  857. put_codepoint(line.c_str() + p, next_p - p, widths[i]);
  858. tail_width += widths[i];
  859. p = next_p;
  860. }
  861. move_cursor(-tail_width);
  862. byte_pos += new_char_str.length();
  863. char_pos++;
  864. }
  865. }
  866. if (!line.empty() && (line.back() == '\\' || line.back() == '/')) {
  867. set_display(prompt);
  868. replace_last(line.back());
  869. is_special_char = true;
  870. }
  871. }
  872. bool has_more = multiline_input;
  873. if (is_special_char) {
  874. replace_last(' ');
  875. pop_cursor();
  876. char last = line.back();
  877. line.pop_back();
  878. if (last == '\\') {
  879. line += '\n';
  880. fputc('\n', out);
  881. has_more = !has_more;
  882. } else {
  883. // llama will just eat the single space, it won't act as a space
  884. if (line.length() == 1 && line.back() == ' ') {
  885. line.clear();
  886. pop_cursor();
  887. }
  888. has_more = false;
  889. }
  890. } else {
  891. if (end_of_stream) {
  892. has_more = false;
  893. } else {
  894. line += '\n';
  895. fputc('\n', out);
  896. }
  897. }
  898. if (!end_of_stream && !line.empty()) {
  899. // remove the trailing newline for history storage
  900. if (!line.empty() && line.back() == '\n') {
  901. line.pop_back();
  902. }
  903. // TODO: maybe support multiline history entries?
  904. history.add(line);
  905. }
  906. fflush(out);
  907. return has_more;
  908. }
  909. static bool readline_simple(std::string & line, bool multiline_input) {
  910. #if defined(_WIN32)
  911. std::wstring wline;
  912. if (!std::getline(std::wcin, wline)) {
  913. // Input stream is bad or EOF received
  914. line.clear();
  915. GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);
  916. return false;
  917. }
  918. int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wline[0], (int)wline.size(), NULL, 0, NULL, NULL);
  919. line.resize(size_needed);
  920. WideCharToMultiByte(CP_UTF8, 0, &wline[0], (int)wline.size(), &line[0], size_needed, NULL, NULL);
  921. #else
  922. if (!std::getline(std::cin, line)) {
  923. // Input stream is bad or EOF received
  924. line.clear();
  925. return false;
  926. }
  927. #endif
  928. if (!line.empty()) {
  929. char last = line.back();
  930. if (last == '/') { // Always return control on '/' symbol
  931. line.pop_back();
  932. return false;
  933. }
  934. if (last == '\\') { // '\\' changes the default action
  935. line.pop_back();
  936. multiline_input = !multiline_input;
  937. }
  938. }
  939. line += '\n';
  940. // By default, continue input if multiline_input is set
  941. return multiline_input;
  942. }
  943. bool readline(std::string & line, bool multiline_input) {
  944. set_display(user_input);
  945. if (simple_io) {
  946. return readline_simple(line, multiline_input);
  947. }
  948. return readline_advanced(line, multiline_input);
  949. }
  950. }