console.cpp 40 KB

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