CoolFace
Modelpublic

Felipe97/llama-cpp-compiled

sourceHugging Faceupdated 2d agoView on Hugging Face
0likes1.1kdownloads
console.cpp1167 linesDownload Raw Back to common
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 15#if defined(_WIN32)16#define WIN32_LEAN_AND_MEAN17#ifndef NOMINMAX18#define NOMINMAX19#endif20#include <windows.h>21#include <fcntl.h>22#include <io.h>23#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING24#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x000425#endif26#else27#include <climits>28#include <sys/ioctl.h>29#include <unistd.h>30#include <wchar.h>31#include <stdio.h>32#include <stdlib.h>33#include <signal.h>34#include <termios.h>35#endif36 37#define ANSI_COLOR_RED     "\x1b[31m"38#define ANSI_COLOR_GREEN   "\x1b[32m"39#define ANSI_COLOR_YELLOW  "\x1b[33m"40#define ANSI_COLOR_BLUE    "\x1b[34m"41#define ANSI_COLOR_MAGENTA "\x1b[35m"42#define ANSI_COLOR_CYAN    "\x1b[36m"43#define ANSI_COLOR_GRAY    "\x1b[90m"44#define ANSI_COLOR_RESET   "\x1b[0m"45#define ANSI_BOLD          "\x1b[1m"46 47namespace console {48 49#if defined (_WIN32)50    namespace {51        // Use private-use unicode values to represent special keys that are not reported52        // as characters (e.g. arrows on Windows). These values should never clash with53        // real input and let the rest of the code handle navigation uniformly.54        static constexpr char32_t KEY_ARROW_LEFT       = 0xE000;55        static constexpr char32_t KEY_ARROW_RIGHT      = 0xE001;56        static constexpr char32_t KEY_ARROW_UP         = 0xE002;57        static constexpr char32_t KEY_ARROW_DOWN       = 0xE003;58        static constexpr char32_t KEY_HOME             = 0xE004;59        static constexpr char32_t KEY_END              = 0xE005;60        static constexpr char32_t KEY_CTRL_ARROW_LEFT  = 0xE006;61        static constexpr char32_t KEY_CTRL_ARROW_RIGHT = 0xE007;62        static constexpr char32_t KEY_DELETE           = 0xE008;63    }64 65    //66    // Console state67    //68#endif69 70    static bool         advanced_display = false;71    static bool         simple_io        = true;72    static display_type current_display  = DISPLAY_TYPE_RESET;73 74    static FILE*        out              = stdout;75 76#if defined (_WIN32)77    static void*        hConsole;78#else79    static FILE*        tty              = nullptr;80    static termios      initial_state;81#endif82 83    static completion_callback completion_cb = nullptr;84 85    //86    // Init and cleanup87    //88 89    void init(bool use_simple_io, bool use_advanced_display) {90        advanced_display = use_advanced_display;91        simple_io = use_simple_io;92#if defined(_WIN32)93        // Windows-specific console initialization94        DWORD dwMode = 0;95        hConsole = GetStdHandle(STD_OUTPUT_HANDLE);96        if (hConsole == INVALID_HANDLE_VALUE || !GetConsoleMode(hConsole, &dwMode)) {97            hConsole = GetStdHandle(STD_ERROR_HANDLE);98            if (hConsole != INVALID_HANDLE_VALUE && (!GetConsoleMode(hConsole, &dwMode))) {99                hConsole = nullptr;100                simple_io = true;101            }102        }103        if (hConsole) {104            // Check conditions combined to reduce nesting105            if (advanced_display && !(dwMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) &&106                !SetConsoleMode(hConsole, dwMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)) {107                advanced_display = false;108            }109            // Set console output codepage to UTF8110            SetConsoleOutputCP(CP_UTF8);111        }112        HANDLE hConIn = GetStdHandle(STD_INPUT_HANDLE);113        if (hConIn != INVALID_HANDLE_VALUE && GetConsoleMode(hConIn, &dwMode)) {114            // Set console input codepage to UTF16115            _setmode(_fileno(stdin), _O_WTEXT);116 117            // Set ICANON (ENABLE_LINE_INPUT) and ECHO (ENABLE_ECHO_INPUT)118            if (simple_io) {119                dwMode |= ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;120            } else {121                dwMode &= ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);122            }123            if (!SetConsoleMode(hConIn, dwMode)) {124                simple_io = true;125            }126        }127        if (simple_io) {128            _setmode(_fileno(stdin), _O_U8TEXT);129        }130#else131        // POSIX-specific console initialization132        if (!simple_io) {133            struct termios new_termios;134            tcgetattr(STDIN_FILENO, &initial_state);135            new_termios = initial_state;136            new_termios.c_lflag &= ~(ICANON | ECHO);137            new_termios.c_cc[VMIN] = 1;138            new_termios.c_cc[VTIME] = 0;139            tcsetattr(STDIN_FILENO, TCSANOW, &new_termios);140 141            tty = fopen("/dev/tty", "w+");142            if (tty != nullptr) {143                out = tty;144            }145        }146 147        setlocale(LC_ALL, "");148#endif149    }150 151    void cleanup() {152        // Reset console display153        set_display(DISPLAY_TYPE_RESET);154 155#if !defined(_WIN32)156        // Restore settings on POSIX systems157        if (!simple_io) {158            if (tty != nullptr) {159                out = stdout;160                fclose(tty);161                tty = nullptr;162            }163            tcsetattr(STDIN_FILENO, TCSANOW, &initial_state);164        }165#endif166    }167 168    //169    // Display and IO170    //171 172    // Keep track of current display and only emit ANSI code if it changes173    void set_display(display_type display) {174        if (advanced_display && current_display != display) {175            common_log_flush(common_log_main());176            switch(display) {177                case DISPLAY_TYPE_RESET:178                    fprintf(out, ANSI_COLOR_RESET);179                    break;180                case DISPLAY_TYPE_INFO:181                    fprintf(out, ANSI_COLOR_MAGENTA);182                    break;183                case DISPLAY_TYPE_PROMPT:184                    fprintf(out, ANSI_COLOR_YELLOW);185                    break;186                case DISPLAY_TYPE_REASONING:187                    fprintf(out, ANSI_COLOR_GRAY);188                    break;189                case DISPLAY_TYPE_USER_INPUT:190                    fprintf(out, ANSI_BOLD ANSI_COLOR_GREEN);191                    break;192                case DISPLAY_TYPE_ERROR:193                    fprintf(out, ANSI_BOLD ANSI_COLOR_RED);194            }195            current_display = display;196            fflush(out);197        }198    }199 200    static char32_t getchar32() {201#if defined(_WIN32)202        HANDLE hConsole = GetStdHandle(STD_INPUT_HANDLE);203        wchar_t high_surrogate = 0;204 205        while (true) {206            INPUT_RECORD record;207            DWORD count;208            if (!ReadConsoleInputW(hConsole, &record, 1, &count) || count == 0) {209                return WEOF;210            }211 212            if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown) {213                wchar_t wc = record.Event.KeyEvent.uChar.UnicodeChar;214                if (wc == 0) {215                    const DWORD ctrl_mask = LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED;216                    const bool ctrl_pressed = (record.Event.KeyEvent.dwControlKeyState & ctrl_mask) != 0;217                    switch (record.Event.KeyEvent.wVirtualKeyCode) {218                        case VK_LEFT:   return ctrl_pressed ? KEY_CTRL_ARROW_LEFT  : KEY_ARROW_LEFT;219                        case VK_RIGHT:  return ctrl_pressed ? KEY_CTRL_ARROW_RIGHT : KEY_ARROW_RIGHT;220                        case VK_UP:     return KEY_ARROW_UP;221                        case VK_DOWN:   return KEY_ARROW_DOWN;222                        case VK_HOME:   return KEY_HOME;223                        case VK_END:    return KEY_END;224                        case VK_DELETE: return KEY_DELETE;225                        default:        continue;226                    }227                }228 229                if ((wc >= 0xD800) && (wc <= 0xDBFF)) { // Check if wc is a high surrogate230                    high_surrogate = wc;231                    continue;232                }233                if ((wc >= 0xDC00) && (wc <= 0xDFFF)) { // Check if wc is a low surrogate234                    if (high_surrogate != 0) { // Check if we have a high surrogate235                        return ((high_surrogate - 0xD800) << 10) + (wc - 0xDC00) + 0x10000;236                    }237                }238 239                high_surrogate = 0; // Reset the high surrogate240                return static_cast<char32_t>(wc);241            }242        }243#else244        wchar_t wc = getwchar();245        if (static_cast<wint_t>(wc) == WEOF) {246            return WEOF;247        }248 249#if WCHAR_MAX == 0xFFFF250        if ((wc >= 0xD800) && (wc <= 0xDBFF)) { // Check if wc is a high surrogate251            wchar_t low_surrogate = getwchar();252            if ((low_surrogate >= 0xDC00) && (low_surrogate <= 0xDFFF)) { // Check if the next wchar is a low surrogate253                return (static_cast<char32_t>(wc & 0x03FF) << 10) + (low_surrogate & 0x03FF) + 0x10000;254            }255        }256        if ((wc >= 0xD800) && (wc <= 0xDFFF)) { // Invalid surrogate pair257            return 0xFFFD; // Return the replacement character U+FFFD258        }259#endif260 261        return static_cast<char32_t>(wc);262#endif263    }264 265    static void pop_cursor() {266#if defined(_WIN32)267        if (hConsole != NULL) {268            CONSOLE_SCREEN_BUFFER_INFO bufferInfo;269            GetConsoleScreenBufferInfo(hConsole, &bufferInfo);270 271            COORD newCursorPosition = bufferInfo.dwCursorPosition;272            if (newCursorPosition.X == 0) {273                newCursorPosition.X = bufferInfo.dwSize.X - 1;274                newCursorPosition.Y -= 1;275            } else {276                newCursorPosition.X -= 1;277            }278 279            SetConsoleCursorPosition(hConsole, newCursorPosition);280            return;281        }282#endif283        putc('\b', out);284    }285 286    static int estimateWidth(char32_t codepoint) {287#if defined(_WIN32)288        (void)codepoint;289        return 1;290#else291        return wcwidth(codepoint);292#endif293    }294 295    static int put_codepoint(const char* utf8_codepoint, size_t length, int expectedWidth) {296#if defined(_WIN32)297        CONSOLE_SCREEN_BUFFER_INFO bufferInfo;298        if (!GetConsoleScreenBufferInfo(hConsole, &bufferInfo)) {299            // go with the default300            return expectedWidth;301        }302        COORD initialPosition = bufferInfo.dwCursorPosition;303        DWORD nNumberOfChars = length;304        WriteConsole(hConsole, utf8_codepoint, nNumberOfChars, &nNumberOfChars, NULL);305 306        CONSOLE_SCREEN_BUFFER_INFO newBufferInfo;307        GetConsoleScreenBufferInfo(hConsole, &newBufferInfo);308 309        // Figure out our real position if we're in the last column310        if (utf8_codepoint[0] != 0x09 && initialPosition.X == newBufferInfo.dwSize.X - 1) {311            DWORD nNumberOfChars;312            WriteConsole(hConsole, &" \b", 2, &nNumberOfChars, NULL);313            GetConsoleScreenBufferInfo(hConsole, &newBufferInfo);314        }315 316        int width = newBufferInfo.dwCursorPosition.X - initialPosition.X;317        if (width < 0) {318            width += newBufferInfo.dwSize.X;319        }320        return width;321#else322        // We can trust expectedWidth if we've got one323        if (expectedWidth >= 0 || tty == nullptr) {324            fwrite(utf8_codepoint, length, 1, out);325            return expectedWidth;326        }327 328        fputs("\033[6n", tty); // Query cursor position329        int x1;330        int y1;331        int x2;332        int y2;333        int results = 0;334        results = fscanf(tty, "\033[%d;%dR", &y1, &x1);335 336        fwrite(utf8_codepoint, length, 1, tty);337 338        fputs("\033[6n", tty); // Query cursor position339        results += fscanf(tty, "\033[%d;%dR", &y2, &x2);340 341        if (results != 4) {342            return expectedWidth;343        }344 345        int width = x2 - x1;346        if (width < 0) {347            // Calculate the width considering text wrapping348            struct winsize w;349            ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);350            width += w.ws_col;351        }352        return width;353#endif354    }355 356    static void replace_last(char ch) {357#if defined(_WIN32)358        pop_cursor();359        put_codepoint(&ch, 1, 1);360#else361        fprintf(out, "\b%c", ch);362#endif363    }364 365    static char32_t decode_utf8(const std::string & input, size_t pos, size_t & advance) {366        unsigned char c = static_cast<unsigned char>(input[pos]);367        if ((c & 0x80u) == 0u) {368            advance = 1;369            return c;370        }371        if ((c & 0xE0u) == 0xC0u && pos + 1 < input.size()) {372            unsigned char c1 = static_cast<unsigned char>(input[pos + 1]);373            if ((c1 & 0xC0u) != 0x80u) {374                advance = 1;375                return 0xFFFD;376            }377            advance = 2;378            return ((c & 0x1Fu) << 6) | (static_cast<unsigned char>(input[pos + 1]) & 0x3Fu);379        }380        if ((c & 0xF0u) == 0xE0u && pos + 2 < input.size()) {381            unsigned char c1 = static_cast<unsigned char>(input[pos + 1]);382            unsigned char c2 = static_cast<unsigned char>(input[pos + 2]);383            if ((c1 & 0xC0u) != 0x80u || (c2 & 0xC0u) != 0x80u) {384                advance = 1;385                return 0xFFFD;386            }387            advance = 3;388            return ((c & 0x0Fu) << 12) |389                   ((static_cast<unsigned char>(input[pos + 1]) & 0x3Fu) << 6) |390                   (static_cast<unsigned char>(input[pos + 2]) & 0x3Fu);391        }392        if ((c & 0xF8u) == 0xF0u && pos + 3 < input.size()) {393            unsigned char c1 = static_cast<unsigned char>(input[pos + 1]);394            unsigned char c2 = static_cast<unsigned char>(input[pos + 2]);395            unsigned char c3 = static_cast<unsigned char>(input[pos + 3]);396            if ((c1 & 0xC0u) != 0x80u || (c2 & 0xC0u) != 0x80u || (c3 & 0xC0u) != 0x80u) {397                advance = 1;398                return 0xFFFD;399            }400            advance = 4;401            return ((c & 0x07u) << 18) |402                   ((static_cast<unsigned char>(input[pos + 1]) & 0x3Fu) << 12) |403                   ((static_cast<unsigned char>(input[pos + 2]) & 0x3Fu) << 6) |404                   (static_cast<unsigned char>(input[pos + 3]) & 0x3Fu);405        }406 407        advance = 1;408        return 0xFFFD; // replacement character for invalid input409    }410 411    static void append_utf8(char32_t ch, std::string & out) {412        if (ch <= 0x7F) {413            out.push_back(static_cast<unsigned char>(ch));414        } else if (ch <= 0x7FF) {415            out.push_back(static_cast<unsigned char>(0xC0 | ((ch >> 6) & 0x1F)));416            out.push_back(static_cast<unsigned char>(0x80 | (ch & 0x3F)));417        } else if (ch <= 0xFFFF) {418            out.push_back(static_cast<unsigned char>(0xE0 | ((ch >> 12) & 0x0F)));419            out.push_back(static_cast<unsigned char>(0x80 | ((ch >> 6) & 0x3F)));420            out.push_back(static_cast<unsigned char>(0x80 | (ch & 0x3F)));421        } else if (ch <= 0x10FFFF) {422            out.push_back(static_cast<unsigned char>(0xF0 | ((ch >> 18) & 0x07)));423            out.push_back(static_cast<unsigned char>(0x80 | ((ch >> 12) & 0x3F)));424            out.push_back(static_cast<unsigned char>(0x80 | ((ch >> 6) & 0x3F)));425            out.push_back(static_cast<unsigned char>(0x80 | (ch & 0x3F)));426        } else {427            // Invalid Unicode code point428        }429    }430 431    // Helper function to remove the last UTF-8 character from a string432    static size_t prev_utf8_char_pos(const std::string & line, size_t pos) {433        if (pos == 0) return 0;434        pos--;435        while (pos > 0 && (line[pos] & 0xC0) == 0x80) {436            pos--;437        }438        return pos;439    }440 441    static size_t next_utf8_char_pos(const std::string & line, size_t pos) {442        if (pos >= line.length()) return line.length();443        pos++;444        while (pos < line.length() && (line[pos] & 0xC0) == 0x80) {445            pos++;446        }447        return pos;448    }449 450    static void move_cursor(int delta);451    static void move_word_left(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths, const std::string & line);452    static void move_word_right(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths, const std::string & line);453    static void move_to_line_start(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths);454    static void move_to_line_end(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths, const std::string & line);455 456    static void delete_at_cursor(std::string & line, std::vector<int> & widths, size_t & char_pos, size_t & byte_pos) {457        if (char_pos >= widths.size()) {458            return;459        }460 461        size_t next_pos = next_utf8_char_pos(line, byte_pos);462        int w = widths[char_pos];463        size_t char_len = next_pos - byte_pos;464 465        line.erase(byte_pos, char_len);466        widths.erase(widths.begin() + char_pos);467 468        size_t p = byte_pos;469        int tail_width = 0;470        for (size_t i = char_pos; i < widths.size(); ++i) {471            size_t following = next_utf8_char_pos(line, p);472            put_codepoint(line.c_str() + p, following - p, widths[i]);473            tail_width += widths[i];474            p = following;475        }476 477        for (int i = 0; i < w; ++i) {478            fputc(' ', out);479        }480 481        move_cursor(-(tail_width + w));482    }483 484    static void clear_current_line(const std::vector<int> & widths) {485        int total_width = 0;486        for (int w : widths) {487            total_width += (w > 0 ? w : 1);488        }489 490        if (total_width > 0) {491            std::string spaces(total_width, ' ');492            fwrite(spaces.c_str(), 1, total_width, out);493            move_cursor(-total_width);494        }495    }496 497    static void set_line_contents(std::string new_line, std::string & line, std::vector<int> & widths, size_t & char_pos,498                                  size_t & byte_pos, int cursor_byte_pos = -1) {499        move_to_line_start(char_pos, byte_pos, widths);500        clear_current_line(widths);501 502        line = std::move(new_line);503        widths.clear();504        byte_pos = 0;505        char_pos = 0;506 507        size_t idx = 0;508        int back_width = 0;509        while (idx < line.size()) {510            size_t advance = 0;511            char32_t cp = decode_utf8(line, idx, advance);512            int expected_width = estimateWidth(cp);513            int real_width = put_codepoint(line.c_str() + idx, advance, expected_width);514            if (real_width < 0) real_width = 0;515            widths.push_back(real_width);516            idx += advance;517            if (cursor_byte_pos >= 0 && static_cast<size_t>(cursor_byte_pos) < idx) {518                back_width += real_width;519            } else {520                ++char_pos;521                byte_pos = idx;522            }523        }524        if (cursor_byte_pos >= 0) {525            move_cursor(-back_width);526        }527    }528 529    static void move_to_line_start(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths) {530        int back_width = 0;531        for (size_t i = 0; i < char_pos; ++i) {532            back_width += widths[i];533        }534        move_cursor(-back_width);535        char_pos = 0;536        byte_pos = 0;537    }538 539    static void move_to_line_end(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths, const std::string & line) {540        int forward_width = 0;541        for (size_t i = char_pos; i < widths.size(); ++i) {542            forward_width += widths[i];543        }544        move_cursor(forward_width);545        char_pos = widths.size();546        byte_pos = line.length();547    }548 549    static bool has_ctrl_modifier(const std::string & params) {550        size_t start = 0;551        while (start < params.size()) {552            size_t end = params.find(';', start);553            size_t len = (end == std::string::npos) ? params.size() - start : end - start;554            if (len > 0) {555                int value = 0;556                for (size_t i = 0; i < len; ++i) {557                    char ch = params[start + i];558                    if (!std::isdigit(static_cast<unsigned char>(ch))) {559                        value = -1;560                        break;561                    }562                    value = value * 10 + (ch - '0');563                }564                if (value == 5) {565                    return true;566                }567            }568 569            if (end == std::string::npos) {570                break;571            }572            start = end + 1;573        }574        return false;575    }576 577    static bool is_space_codepoint(char32_t cp) {578        return std::iswspace(static_cast<wint_t>(cp)) != 0;579    }580 581    static void move_word_left(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths, const std::string & line) {582        if (char_pos == 0) {583            return;584        }585 586        size_t new_char_pos = char_pos;587        size_t new_byte_pos = byte_pos;588        int move_width = 0;589 590        while (new_char_pos > 0) {591            size_t prev_byte = prev_utf8_char_pos(line, new_byte_pos);592            size_t advance = 0;593            char32_t cp = decode_utf8(line, prev_byte, advance);594            if (!is_space_codepoint(cp)) {595                break;596            }597            move_width += widths[new_char_pos - 1];598            new_char_pos--;599            new_byte_pos = prev_byte;600        }601 602        while (new_char_pos > 0) {603            size_t prev_byte = prev_utf8_char_pos(line, new_byte_pos);604            size_t advance = 0;605            char32_t cp = decode_utf8(line, prev_byte, advance);606            if (is_space_codepoint(cp)) {607                break;608            }609            move_width += widths[new_char_pos - 1];610            new_char_pos--;611            new_byte_pos = prev_byte;612        }613 614        move_cursor(-move_width);615        char_pos = new_char_pos;616        byte_pos = new_byte_pos;617    }618 619    static void move_word_right(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths, const std::string & line) {620        if (char_pos >= widths.size()) {621            return;622        }623 624        size_t new_char_pos = char_pos;625        size_t new_byte_pos = byte_pos;626        int move_width = 0;627 628        while (new_char_pos < widths.size()) {629            size_t advance = 0;630            char32_t cp = decode_utf8(line, new_byte_pos, advance);631            if (!is_space_codepoint(cp)) {632                break;633            }634            move_width += widths[new_char_pos];635            new_char_pos++;636            new_byte_pos += advance;637        }638 639        while (new_char_pos < widths.size()) {640            size_t advance = 0;641            char32_t cp = decode_utf8(line, new_byte_pos, advance);642            if (is_space_codepoint(cp)) {643                break;644            }645            move_width += widths[new_char_pos];646            new_char_pos++;647            new_byte_pos += advance;648        }649 650        while (new_char_pos < widths.size()) {651            size_t advance = 0;652            char32_t cp = decode_utf8(line, new_byte_pos, advance);653            if (!is_space_codepoint(cp)) {654                break;655            }656            move_width += widths[new_char_pos];657            new_char_pos++;658            new_byte_pos += advance;659        }660 661        move_cursor(move_width);662        char_pos = new_char_pos;663        byte_pos = new_byte_pos;664    }665 666    static void move_cursor(int delta) {667        if (delta == 0) return;668#if defined(_WIN32)669        if (hConsole != NULL) {670            CONSOLE_SCREEN_BUFFER_INFO bufferInfo;671            GetConsoleScreenBufferInfo(hConsole, &bufferInfo);672            COORD newCursorPosition = bufferInfo.dwCursorPosition;673            int width = bufferInfo.dwSize.X;674            int newX = newCursorPosition.X + delta;675            int newY = newCursorPosition.Y;676 677            while (newX >= width) {678                newX -= width;679                newY++;680            }681            while (newX < 0) {682                newX += width;683                newY--;684            }685 686            newCursorPosition.X = newX;687            newCursorPosition.Y = newY;688            SetConsoleCursorPosition(hConsole, newCursorPosition);689        }690#else691        if (delta < 0) {692            for (int i = 0; i < -delta; i++) fprintf(out, "\b");693        } else {694            for (int i = 0; i < delta; i++) fprintf(out, "\033[C");695        }696#endif697    }698 699    struct history_t {700        std::vector<std::string> entries;701        size_t viewing_idx = SIZE_MAX;702        std::string backup_line; // current line before viewing history703        void add(std::string_view line) {704            if (line.empty()) {705                return;706            }707            // avoid duplicates with the last entry708            if (entries.empty() || entries.back() != line) {709                entries.emplace_back(line);710            }711            // also clear viewing state712            end_viewing();713        }714        bool prev(std::string & cur_line) {715            if (entries.empty()) {716                return false;717            }718            if (viewing_idx == SIZE_MAX) {719                return false;720            }721            if (viewing_idx > 0) {722                viewing_idx--;723            }724            cur_line = entries[viewing_idx];725            return true;726        }727        bool next(std::string & cur_line) {728            if (entries.empty() || viewing_idx == SIZE_MAX) {729                return false;730            }731            viewing_idx++;732            if (viewing_idx >= entries.size()) {733                cur_line = backup_line;734                end_viewing();735            } else {736                cur_line = entries[viewing_idx];737            }738            return true;739        }740        void begin_viewing(const std::string & line) {741            backup_line = line;742            viewing_idx = entries.size();743        }744        void end_viewing() {745            viewing_idx = SIZE_MAX;746            backup_line.clear();747        }748        bool is_viewing() const {749            return viewing_idx != SIZE_MAX;750        }751    } history;752 753    static bool readline_advanced(std::string & line, bool multiline_input) {754        if (out != stdout) {755            fflush(stdout);756        }757 758        line.clear();759        std::vector<int> widths;760        bool is_special_char = false;761        bool end_of_stream = false;762 763        size_t byte_pos = 0; // current byte index764        size_t char_pos = 0; // current character index (one char can be multiple bytes)765 766        char32_t input_char;767        while (true) {768            assert(char_pos <= byte_pos);769            assert(char_pos <= widths.size());770            auto history_prev = [&]() {771                if (!history.is_viewing()) {772                    history.begin_viewing(line);773                }774                std::string new_line;775                if (!history.prev(new_line)) {776                    return;777                }778                set_line_contents(new_line, line, widths, char_pos, byte_pos);779            };780            auto history_next = [&]() {781                if (history.is_viewing()) {782                    std::string new_line;783                    if (!history.next(new_line)) {784                        return;785                    }786                    set_line_contents(new_line, line, widths, char_pos, byte_pos);787                }788            };789 790            fflush(out); // Ensure all output is displayed before waiting for input791            input_char = getchar32();792 793            if (input_char == '\r' || input_char == '\n') {794                break;795            }796 797            if (completion_cb && input_char == '\t') {798                auto candidates = completion_cb(line, byte_pos);799 800                if (!candidates.empty()) {801                    if (candidates.size() > 1 || candidates[0].first != line) {802                        // TODO?: Display all candidates803                        set_line_contents(candidates[0].first, line, widths, char_pos, byte_pos, candidates[0].second);804                    } else {805                        // TODO: Move cursor to new byte_pos806                    }807                    continue;808                }809            }810 811            if (input_char == (char32_t) WEOF || input_char == 0x04 /* Ctrl+D */) {812                end_of_stream = true;813                break;814            }815 816            if (is_special_char) {817                replace_last(line.back());818                is_special_char = false;819            }820 821            if (input_char == '\033') { // Escape sequence822                char32_t code = getchar32();823                if (code == '[') {824                    std::string params;825                    while (true) {826                        code = getchar32();827                        if ((code >= 'A' && code <= 'Z') || (code >= 'a' && code <= 'z') || code == '~' || code == (char32_t) WEOF) {828                            break;829                        }830                        params.push_back(static_cast<char>(code));831                    }832 833                    const bool ctrl_modifier = has_ctrl_modifier(params);834 835                    if (code == 'D') { // left836                        if (ctrl_modifier) {837                            move_word_left(char_pos, byte_pos, widths, line);838                        } else if (char_pos > 0) {839                            int w = widths[char_pos - 1];840                            move_cursor(-w);841                            char_pos--;842                            byte_pos = prev_utf8_char_pos(line, byte_pos);843                        }844                    } else if (code == 'C') { // right845                        if (ctrl_modifier) {846                            move_word_right(char_pos, byte_pos, widths, line);847                        } else if (char_pos < widths.size()) {848                            int w = widths[char_pos];849                            move_cursor(w);850                            char_pos++;851                            byte_pos = next_utf8_char_pos(line, byte_pos);852                        }853                    } else if (code == 'H') { // home854                        move_to_line_start(char_pos, byte_pos, widths);855                    } else if (code == 'F') { // end856                        move_to_line_end(char_pos, byte_pos, widths, line);857                    } else if (code == 'A' || code == 'B') {858                        // up/down859                        if (code == 'A') {860                            history_prev();861                            is_special_char = false;862                        } else if (code == 'B') {863                            history_next();864                            is_special_char = false;865                        }866                    } else if ((code == '~' || (code >= 'A' && code <= 'Z') || (code >= 'a' && code <= 'z')) && !params.empty()) {867                        std::string digits;868                        for (char ch : params) {869                            if (ch == ';') {870                                break;871                            }872                            if (std::isdigit(static_cast<unsigned char>(ch))) {873                                digits.push_back(ch);874                            }875                        }876 877                        if (code == '~') {878                            if (digits == "1" || digits == "7") { // home879                                move_to_line_start(char_pos, byte_pos, widths);880                            } else if (digits == "4" || digits == "8") { // end881                                move_to_line_end(char_pos, byte_pos, widths, line);882                            } else if (digits == "3") { // delete883                                delete_at_cursor(line, widths, char_pos, byte_pos);884                            }885                        }886                    }887                } else if (code == 0x1B) {888                    // Discard the rest of the escape sequence889                    while ((code = getchar32()) != (char32_t) WEOF) {890                        if ((code >= 'A' && code <= 'Z') || (code >= 'a' && code <= 'z') || code == '~') {891                            break;892                        }893                    }894                }895#if defined(_WIN32)896            } else if (input_char == KEY_ARROW_LEFT) {897                if (char_pos > 0) {898                    int w = widths[char_pos - 1];899                    move_cursor(-w);900                    char_pos--;901                    byte_pos = prev_utf8_char_pos(line, byte_pos);902                }903            } else if (input_char == KEY_ARROW_RIGHT) {904                if (char_pos < widths.size()) {905                    int w = widths[char_pos];906                    move_cursor(w);907                    char_pos++;908                    byte_pos = next_utf8_char_pos(line, byte_pos);909                }910            } else if (input_char == KEY_CTRL_ARROW_LEFT) {911                move_word_left(char_pos, byte_pos, widths, line);912            } else if (input_char == KEY_CTRL_ARROW_RIGHT) {913                move_word_right(char_pos, byte_pos, widths, line);914            } else if (input_char == KEY_HOME) {915                move_to_line_start(char_pos, byte_pos, widths);916            } else if (input_char == KEY_END) {917                move_to_line_end(char_pos, byte_pos, widths, line);918            } else if (input_char == KEY_DELETE) {919                delete_at_cursor(line, widths, char_pos, byte_pos);920            } else if (input_char == KEY_ARROW_UP || input_char == KEY_ARROW_DOWN) {921                if (input_char == KEY_ARROW_UP) {922                    history_prev();923                    is_special_char = false;924                } else if (input_char == KEY_ARROW_DOWN) {925                    history_next();926                    is_special_char = false;927                }928#endif929            } else if (input_char == 0x08 || input_char == 0x7F) { // Backspace930                if (char_pos > 0) {931                    int w = widths[char_pos - 1];932                    move_cursor(-w);933                    char_pos--;934                    size_t prev_pos = prev_utf8_char_pos(line, byte_pos);935                    size_t char_len = byte_pos - prev_pos;936                    byte_pos = prev_pos;937 938                    // remove the character939                    line.erase(byte_pos, char_len);940                    widths.erase(widths.begin() + char_pos);941 942                    // redraw tail943                    size_t p = byte_pos;944                    int tail_width = 0;945                    for (size_t i = char_pos; i < widths.size(); ++i) {946                        size_t next_p = next_utf8_char_pos(line, p);947                        put_codepoint(line.c_str() + p, next_p - p, widths[i]);948                        tail_width += widths[i];949                        p = next_p;950                    }951 952                    // clear display953                    for (int i = 0; i < w; ++i) {954                        fputc(' ', out);955                    }956                    move_cursor(-(tail_width + w));957                }958            } else {959                // insert character960                std::string new_char_str;961                append_utf8(input_char, new_char_str);962                int w = estimateWidth(input_char);963 964                if (char_pos == widths.size()) {965                    // insert at the end966                    line += new_char_str;967                    int real_w = put_codepoint(new_char_str.c_str(), new_char_str.length(), w);968                    if (real_w < 0) real_w = 0;969                    widths.push_back(real_w);970                    byte_pos += new_char_str.length();971                    char_pos++;972                } else {973                    // insert in middle974                    line.insert(byte_pos, new_char_str);975 976                    int real_w = put_codepoint(new_char_str.c_str(), new_char_str.length(), w);977                    if (real_w < 0) real_w = 0;978 979                    widths.insert(widths.begin() + char_pos, real_w);980 981                    // print the tail982                    size_t p = byte_pos + new_char_str.length();983                    int tail_width = 0;984                    for (size_t i = char_pos + 1; i < widths.size(); ++i) {985                        size_t next_p = next_utf8_char_pos(line, p);986                        put_codepoint(line.c_str() + p, next_p - p, widths[i]);987                        tail_width += widths[i];988                        p = next_p;989                    }990 991                    move_cursor(-tail_width);992 993                    byte_pos += new_char_str.length();994                    char_pos++;995                }996            }997 998            if (!line.empty() && (line.back() == '\\' || line.back() == '/')) {999                replace_last(line.back());1000                is_special_char = true;1001            }1002        }1003 1004        bool has_more = multiline_input;1005        if (is_special_char) {1006            replace_last(' ');1007            pop_cursor();1008 1009            char last = line.back();1010            line.pop_back();1011            if (last == '\\') {1012                line += '\n';1013                fputc('\n', out);1014                has_more = !has_more;1015            } else {1016                // llama will just eat the single space, it won't act as a space1017                if (line.length() == 1 && line.back() == ' ') {1018                    line.clear();1019                    pop_cursor();1020                }1021                has_more = false;1022            }1023        } else {1024            if (end_of_stream) {1025                has_more = false;1026            } else {1027                line += '\n';1028                fputc('\n', out);1029            }1030        }1031 1032        if (!end_of_stream && !line.empty()) {1033            // remove the trailing newline for history storage1034            std::string_view hline = line;1035            if (!line.empty() && line.back() == '\n') {1036                hline.remove_suffix(1);1037            }1038            // TODO: maybe support multiline history entries?1039            history.add(hline);1040        }1041 1042        fflush(out);1043        return has_more;1044    }1045 1046    static bool readline_simple(std::string & line, bool multiline_input) {1047#if defined(_WIN32)1048        std::wstring wline;1049        if (!std::getline(std::wcin, wline)) {1050            // Input stream is bad or EOF received1051            line.clear();1052            GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);1053            return false;1054        }1055 1056        int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wline[0], (int)wline.size(), NULL, 0, NULL, NULL);1057        line.resize(size_needed);1058        WideCharToMultiByte(CP_UTF8, 0, &wline[0], (int)wline.size(), &line[0], size_needed, NULL, NULL);1059#else1060        if (!std::getline(std::cin, line)) {1061            // Input stream is bad or EOF received1062            line.clear();1063            return false;1064        }1065#endif1066        if (!line.empty()) {1067            char last = line.back();1068            if (last == '/') { // Always return control on '/' symbol1069                line.pop_back();1070                return false;1071            }1072            if (last == '\\') { // '\\' changes the default action1073                line.pop_back();1074                multiline_input = !multiline_input;1075            }1076        }1077        line += '\n';1078 1079        // By default, continue input if multiline_input is set1080        return multiline_input;1081    }1082 1083    bool readline(std::string & line, bool multiline_input) {1084        if (simple_io) {1085            return readline_simple(line, multiline_input);1086        }1087        return readline_advanced(line, multiline_input);1088    }1089 1090    void set_completion_callback(completion_callback cb) {1091        completion_cb = cb;1092    }1093 1094    namespace spinner {1095        static const char LOADING_CHARS[] = {'|', '/', '-', '\\'};1096        static std::condition_variable cv_stop;1097        static std::thread th;1098        static size_t frame = 0; // only modified by one thread1099        static bool running = false;1100        static std::mutex mtx;1101        static auto wait_time = std::chrono::milliseconds(100);1102        static void draw_next_frame() {1103            // don't need lock because only one thread modifies running1104            frame = (frame + 1) % sizeof(LOADING_CHARS);1105            replace_last(LOADING_CHARS[frame]);1106            fflush(out);1107        }1108        void start() {1109            std::unique_lock<std::mutex> lock(mtx);1110            if (simple_io || running) {1111                return;1112            }1113            common_log_flush(common_log_main());1114            fprintf(out, "%c", LOADING_CHARS[0]);1115            fflush(out);1116            frame = 1;1117            running = true;1118            th = std::thread([]() {1119                std::unique_lock<std::mutex> lock(mtx);1120                while (true) {1121                    if (cv_stop.wait_for(lock, wait_time, []{ return !running; })) {1122                        break;1123                    }1124                    draw_next_frame();1125                }1126            });1127        }1128        void stop() {1129            {1130                std::unique_lock<std::mutex> lock(mtx);1131                if (simple_io || !running) {1132                    return;1133                }1134                running = false;1135                cv_stop.notify_all();1136            }1137            if (th.joinable()) {1138                th.join();1139            }1140            replace_last(' ');1141            pop_cursor();1142            fflush(out);1143        }1144    }1145 1146    void log(const char * fmt, ...) {1147        va_list args;1148        va_start(args, fmt);1149        vfprintf(out, fmt, args);1150        va_end(args);1151    }1152 1153    void error(const char * fmt, ...) {1154        va_list args;1155        va_start(args, fmt);1156        display_type cur = current_display;1157        set_display(DISPLAY_TYPE_ERROR);1158        vfprintf(out, fmt, args);1159        set_display(cur); // restore previous color1160        va_end(args);1161    }1162 1163    void flush() {1164        fflush(out);1165    }1166}1167