File: console.cpp

package info (click to toggle)
llama.cpp 7593%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 71,012 kB
  • sloc: cpp: 329,391; ansic: 48,249; python: 32,103; lisp: 10,053; sh: 6,070; objc: 1,349; javascript: 924; xml: 384; makefile: 233
file content (1137 lines) | stat: -rw-r--r-- 40,520 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
#include "console.h"
#include "log.h"
#include <vector>
#include <iostream>
#include <cassert>
#include <cstddef>
#include <cctype>
#include <cwctype>
#include <cstdint>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <stdarg.h>

#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include <fcntl.h>
#include <io.h>
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif
#else
#include <climits>
#include <sys/ioctl.h>
#include <unistd.h>
#include <wchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <termios.h>
#endif

#define ANSI_COLOR_RED     "\x1b[31m"
#define ANSI_COLOR_GREEN   "\x1b[32m"
#define ANSI_COLOR_YELLOW  "\x1b[33m"
#define ANSI_COLOR_BLUE    "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN    "\x1b[36m"
#define ANSI_COLOR_GRAY    "\x1b[90m"
#define ANSI_COLOR_RESET   "\x1b[0m"
#define ANSI_BOLD          "\x1b[1m"

namespace console {

#if defined (_WIN32)
    namespace {
        // Use private-use unicode values to represent special keys that are not reported
        // as characters (e.g. arrows on Windows). These values should never clash with
        // real input and let the rest of the code handle navigation uniformly.
        static constexpr char32_t KEY_ARROW_LEFT       = 0xE000;
        static constexpr char32_t KEY_ARROW_RIGHT      = 0xE001;
        static constexpr char32_t KEY_ARROW_UP         = 0xE002;
        static constexpr char32_t KEY_ARROW_DOWN       = 0xE003;
        static constexpr char32_t KEY_HOME             = 0xE004;
        static constexpr char32_t KEY_END              = 0xE005;
        static constexpr char32_t KEY_CTRL_ARROW_LEFT  = 0xE006;
        static constexpr char32_t KEY_CTRL_ARROW_RIGHT = 0xE007;
        static constexpr char32_t KEY_DELETE           = 0xE008;
    }

    //
    // Console state
    //
#endif

    static bool         advanced_display = false;
    static bool         simple_io        = true;
    static display_type current_display  = DISPLAY_TYPE_RESET;

    static FILE*        out              = stdout;

#if defined (_WIN32)
    static void*        hConsole;
#else
    static FILE*        tty              = nullptr;
    static termios      initial_state;
#endif

    //
    // Init and cleanup
    //

    void init(bool use_simple_io, bool use_advanced_display) {
        advanced_display = use_advanced_display;
        simple_io = use_simple_io;
#if defined(_WIN32)
        // Windows-specific console initialization
        DWORD dwMode = 0;
        hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        if (hConsole == INVALID_HANDLE_VALUE || !GetConsoleMode(hConsole, &dwMode)) {
            hConsole = GetStdHandle(STD_ERROR_HANDLE);
            if (hConsole != INVALID_HANDLE_VALUE && (!GetConsoleMode(hConsole, &dwMode))) {
                hConsole = nullptr;
                simple_io = true;
            }
        }
        if (hConsole) {
            // Check conditions combined to reduce nesting
            if (advanced_display && !(dwMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) &&
                !SetConsoleMode(hConsole, dwMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)) {
                advanced_display = false;
            }
            // Set console output codepage to UTF8
            SetConsoleOutputCP(CP_UTF8);
        }
        HANDLE hConIn = GetStdHandle(STD_INPUT_HANDLE);
        if (hConIn != INVALID_HANDLE_VALUE && GetConsoleMode(hConIn, &dwMode)) {
            // Set console input codepage to UTF16
            _setmode(_fileno(stdin), _O_WTEXT);

            // Set ICANON (ENABLE_LINE_INPUT) and ECHO (ENABLE_ECHO_INPUT)
            if (simple_io) {
                dwMode |= ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
            } else {
                dwMode &= ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
            }
            if (!SetConsoleMode(hConIn, dwMode)) {
                simple_io = true;
            }
        }
        if (simple_io) {
            _setmode(_fileno(stdin), _O_U8TEXT);
        }
#else
        // POSIX-specific console initialization
        if (!simple_io) {
            struct termios new_termios;
            tcgetattr(STDIN_FILENO, &initial_state);
            new_termios = initial_state;
            new_termios.c_lflag &= ~(ICANON | ECHO);
            new_termios.c_cc[VMIN] = 1;
            new_termios.c_cc[VTIME] = 0;
            tcsetattr(STDIN_FILENO, TCSANOW, &new_termios);

            tty = fopen("/dev/tty", "w+");
            if (tty != nullptr) {
                out = tty;
            }
        }

        setlocale(LC_ALL, "");
#endif
    }

    void cleanup() {
        // Reset console display
        set_display(DISPLAY_TYPE_RESET);

#if !defined(_WIN32)
        // Restore settings on POSIX systems
        if (!simple_io) {
            if (tty != nullptr) {
                out = stdout;
                fclose(tty);
                tty = nullptr;
            }
            tcsetattr(STDIN_FILENO, TCSANOW, &initial_state);
        }
#endif
    }

    //
    // Display and IO
    //

    // Keep track of current display and only emit ANSI code if it changes
    void set_display(display_type display) {
        if (advanced_display && current_display != display) {
            common_log_flush(common_log_main());
            switch(display) {
                case DISPLAY_TYPE_RESET:
                    fprintf(out, ANSI_COLOR_RESET);
                    break;
                case DISPLAY_TYPE_INFO:
                    fprintf(out, ANSI_COLOR_MAGENTA);
                    break;
                case DISPLAY_TYPE_PROMPT:
                    fprintf(out, ANSI_COLOR_YELLOW);
                    break;
                case DISPLAY_TYPE_REASONING:
                    fprintf(out, ANSI_COLOR_GRAY);
                    break;
                case DISPLAY_TYPE_USER_INPUT:
                    fprintf(out, ANSI_BOLD ANSI_COLOR_GREEN);
                    break;
                case DISPLAY_TYPE_ERROR:
                    fprintf(out, ANSI_BOLD ANSI_COLOR_RED);
            }
            current_display = display;
            fflush(out);
        }
    }

    static char32_t getchar32() {
#if defined(_WIN32)
        HANDLE hConsole = GetStdHandle(STD_INPUT_HANDLE);
        wchar_t high_surrogate = 0;

        while (true) {
            INPUT_RECORD record;
            DWORD count;
            if (!ReadConsoleInputW(hConsole, &record, 1, &count) || count == 0) {
                return WEOF;
            }

            if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown) {
                wchar_t wc = record.Event.KeyEvent.uChar.UnicodeChar;
                if (wc == 0) {
                    const DWORD ctrl_mask = LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED;
                    const bool ctrl_pressed = (record.Event.KeyEvent.dwControlKeyState & ctrl_mask) != 0;
                    switch (record.Event.KeyEvent.wVirtualKeyCode) {
                        case VK_LEFT:   return ctrl_pressed ? KEY_CTRL_ARROW_LEFT  : KEY_ARROW_LEFT;
                        case VK_RIGHT:  return ctrl_pressed ? KEY_CTRL_ARROW_RIGHT : KEY_ARROW_RIGHT;
                        case VK_UP:     return KEY_ARROW_UP;
                        case VK_DOWN:   return KEY_ARROW_DOWN;
                        case VK_HOME:   return KEY_HOME;
                        case VK_END:    return KEY_END;
                        case VK_DELETE: return KEY_DELETE;
                        default:        continue;
                    }
                }

                if ((wc >= 0xD800) && (wc <= 0xDBFF)) { // Check if wc is a high surrogate
                    high_surrogate = wc;
                    continue;
                }
                if ((wc >= 0xDC00) && (wc <= 0xDFFF)) { // Check if wc is a low surrogate
                    if (high_surrogate != 0) { // Check if we have a high surrogate
                        return ((high_surrogate - 0xD800) << 10) + (wc - 0xDC00) + 0x10000;
                    }
                }

                high_surrogate = 0; // Reset the high surrogate
                return static_cast<char32_t>(wc);
            }
        }
#else
        wchar_t wc = getwchar();
        if (static_cast<wint_t>(wc) == WEOF) {
            return WEOF;
        }

#if WCHAR_MAX == 0xFFFF
        if ((wc >= 0xD800) && (wc <= 0xDBFF)) { // Check if wc is a high surrogate
            wchar_t low_surrogate = getwchar();
            if ((low_surrogate >= 0xDC00) && (low_surrogate <= 0xDFFF)) { // Check if the next wchar is a low surrogate
                return (static_cast<char32_t>(wc & 0x03FF) << 10) + (low_surrogate & 0x03FF) + 0x10000;
            }
        }
        if ((wc >= 0xD800) && (wc <= 0xDFFF)) { // Invalid surrogate pair
            return 0xFFFD; // Return the replacement character U+FFFD
        }
#endif

        return static_cast<char32_t>(wc);
#endif
    }

    static void pop_cursor() {
#if defined(_WIN32)
        if (hConsole != NULL) {
            CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
            GetConsoleScreenBufferInfo(hConsole, &bufferInfo);

            COORD newCursorPosition = bufferInfo.dwCursorPosition;
            if (newCursorPosition.X == 0) {
                newCursorPosition.X = bufferInfo.dwSize.X - 1;
                newCursorPosition.Y -= 1;
            } else {
                newCursorPosition.X -= 1;
            }

            SetConsoleCursorPosition(hConsole, newCursorPosition);
            return;
        }
#endif
        putc('\b', out);
    }

    static int estimateWidth(char32_t codepoint) {
#if defined(_WIN32)
        (void)codepoint;
        return 1;
#else
        return wcwidth(codepoint);
#endif
    }

    static int put_codepoint(const char* utf8_codepoint, size_t length, int expectedWidth) {
#if defined(_WIN32)
        CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
        if (!GetConsoleScreenBufferInfo(hConsole, &bufferInfo)) {
            // go with the default
            return expectedWidth;
        }
        COORD initialPosition = bufferInfo.dwCursorPosition;
        DWORD nNumberOfChars = length;
        WriteConsole(hConsole, utf8_codepoint, nNumberOfChars, &nNumberOfChars, NULL);

        CONSOLE_SCREEN_BUFFER_INFO newBufferInfo;
        GetConsoleScreenBufferInfo(hConsole, &newBufferInfo);

        // Figure out our real position if we're in the last column
        if (utf8_codepoint[0] != 0x09 && initialPosition.X == newBufferInfo.dwSize.X - 1) {
            DWORD nNumberOfChars;
            WriteConsole(hConsole, &" \b", 2, &nNumberOfChars, NULL);
            GetConsoleScreenBufferInfo(hConsole, &newBufferInfo);
        }

        int width = newBufferInfo.dwCursorPosition.X - initialPosition.X;
        if (width < 0) {
            width += newBufferInfo.dwSize.X;
        }
        return width;
#else
        // We can trust expectedWidth if we've got one
        if (expectedWidth >= 0 || tty == nullptr) {
            fwrite(utf8_codepoint, length, 1, out);
            return expectedWidth;
        }

        fputs("\033[6n", tty); // Query cursor position
        int x1;
        int y1;
        int x2;
        int y2;
        int results = 0;
        results = fscanf(tty, "\033[%d;%dR", &y1, &x1);

        fwrite(utf8_codepoint, length, 1, tty);

        fputs("\033[6n", tty); // Query cursor position
        results += fscanf(tty, "\033[%d;%dR", &y2, &x2);

        if (results != 4) {
            return expectedWidth;
        }

        int width = x2 - x1;
        if (width < 0) {
            // Calculate the width considering text wrapping
            struct winsize w;
            ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
            width += w.ws_col;
        }
        return width;
#endif
    }

    static void replace_last(char ch) {
#if defined(_WIN32)
        pop_cursor();
        put_codepoint(&ch, 1, 1);
#else
        fprintf(out, "\b%c", ch);
#endif
    }

    static char32_t decode_utf8(const std::string & input, size_t pos, size_t & advance) {
        unsigned char c = static_cast<unsigned char>(input[pos]);
        if ((c & 0x80u) == 0u) {
            advance = 1;
            return c;
        }
        if ((c & 0xE0u) == 0xC0u && pos + 1 < input.size()) {
            unsigned char c1 = static_cast<unsigned char>(input[pos + 1]);
            if ((c1 & 0xC0u) != 0x80u) {
                advance = 1;
                return 0xFFFD;
            }
            advance = 2;
            return ((c & 0x1Fu) << 6) | (static_cast<unsigned char>(input[pos + 1]) & 0x3Fu);
        }
        if ((c & 0xF0u) == 0xE0u && pos + 2 < input.size()) {
            unsigned char c1 = static_cast<unsigned char>(input[pos + 1]);
            unsigned char c2 = static_cast<unsigned char>(input[pos + 2]);
            if ((c1 & 0xC0u) != 0x80u || (c2 & 0xC0u) != 0x80u) {
                advance = 1;
                return 0xFFFD;
            }
            advance = 3;
            return ((c & 0x0Fu) << 12) |
                   ((static_cast<unsigned char>(input[pos + 1]) & 0x3Fu) << 6) |
                   (static_cast<unsigned char>(input[pos + 2]) & 0x3Fu);
        }
        if ((c & 0xF8u) == 0xF0u && pos + 3 < input.size()) {
            unsigned char c1 = static_cast<unsigned char>(input[pos + 1]);
            unsigned char c2 = static_cast<unsigned char>(input[pos + 2]);
            unsigned char c3 = static_cast<unsigned char>(input[pos + 3]);
            if ((c1 & 0xC0u) != 0x80u || (c2 & 0xC0u) != 0x80u || (c3 & 0xC0u) != 0x80u) {
                advance = 1;
                return 0xFFFD;
            }
            advance = 4;
            return ((c & 0x07u) << 18) |
                   ((static_cast<unsigned char>(input[pos + 1]) & 0x3Fu) << 12) |
                   ((static_cast<unsigned char>(input[pos + 2]) & 0x3Fu) << 6) |
                   (static_cast<unsigned char>(input[pos + 3]) & 0x3Fu);
        }

        advance = 1;
        return 0xFFFD; // replacement character for invalid input
    }

    static void append_utf8(char32_t ch, std::string & out) {
        if (ch <= 0x7F) {
            out.push_back(static_cast<unsigned char>(ch));
        } else if (ch <= 0x7FF) {
            out.push_back(static_cast<unsigned char>(0xC0 | ((ch >> 6) & 0x1F)));
            out.push_back(static_cast<unsigned char>(0x80 | (ch & 0x3F)));
        } else if (ch <= 0xFFFF) {
            out.push_back(static_cast<unsigned char>(0xE0 | ((ch >> 12) & 0x0F)));
            out.push_back(static_cast<unsigned char>(0x80 | ((ch >> 6) & 0x3F)));
            out.push_back(static_cast<unsigned char>(0x80 | (ch & 0x3F)));
        } else if (ch <= 0x10FFFF) {
            out.push_back(static_cast<unsigned char>(0xF0 | ((ch >> 18) & 0x07)));
            out.push_back(static_cast<unsigned char>(0x80 | ((ch >> 12) & 0x3F)));
            out.push_back(static_cast<unsigned char>(0x80 | ((ch >> 6) & 0x3F)));
            out.push_back(static_cast<unsigned char>(0x80 | (ch & 0x3F)));
        } else {
            // Invalid Unicode code point
        }
    }

    // Helper function to remove the last UTF-8 character from a string
    static size_t prev_utf8_char_pos(const std::string & line, size_t pos) {
        if (pos == 0) return 0;
        pos--;
        while (pos > 0 && (line[pos] & 0xC0) == 0x80) {
            pos--;
        }
        return pos;
    }

    static size_t next_utf8_char_pos(const std::string & line, size_t pos) {
        if (pos >= line.length()) return line.length();
        pos++;
        while (pos < line.length() && (line[pos] & 0xC0) == 0x80) {
            pos++;
        }
        return pos;
    }

    static void move_cursor(int delta);
    static void move_word_left(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths, const std::string & line);
    static void move_word_right(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths, const std::string & line);
    static void move_to_line_start(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths);
    static void move_to_line_end(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths, const std::string & line);

    static void delete_at_cursor(std::string & line, std::vector<int> & widths, size_t & char_pos, size_t & byte_pos) {
        if (char_pos >= widths.size()) {
            return;
        }

        size_t next_pos = next_utf8_char_pos(line, byte_pos);
        int w = widths[char_pos];
        size_t char_len = next_pos - byte_pos;

        line.erase(byte_pos, char_len);
        widths.erase(widths.begin() + char_pos);

        size_t p = byte_pos;
        int tail_width = 0;
        for (size_t i = char_pos; i < widths.size(); ++i) {
            size_t following = next_utf8_char_pos(line, p);
            put_codepoint(line.c_str() + p, following - p, widths[i]);
            tail_width += widths[i];
            p = following;
        }

        for (int i = 0; i < w; ++i) {
            fputc(' ', out);
        }

        move_cursor(-(tail_width + w));
    }

    static void clear_current_line(const std::vector<int> & widths) {
        int total_width = 0;
        for (int w : widths) {
            total_width += (w > 0 ? w : 1);
        }

        if (total_width > 0) {
            std::string spaces(total_width, ' ');
            fwrite(spaces.c_str(), 1, total_width, out);
            move_cursor(-total_width);
        }
    }

    static void set_line_contents(std::string new_line, std::string & line, std::vector<int> & widths, size_t & char_pos,
                                  size_t & byte_pos) {
        move_to_line_start(char_pos, byte_pos, widths);
        clear_current_line(widths);

        line = std::move(new_line);
        widths.clear();
        byte_pos = 0;
        char_pos = 0;

        size_t idx = 0;
        while (idx < line.size()) {
            size_t advance = 0;
            char32_t cp = decode_utf8(line, idx, advance);
            int expected_width = estimateWidth(cp);
            int real_width = put_codepoint(line.c_str() + idx, advance, expected_width);
            if (real_width < 0) real_width = 0;
            widths.push_back(real_width);
            idx += advance;
            ++char_pos;
            byte_pos = idx;
        }
    }

    static void move_to_line_start(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths) {
        int back_width = 0;
        for (size_t i = 0; i < char_pos; ++i) {
            back_width += widths[i];
        }
        move_cursor(-back_width);
        char_pos = 0;
        byte_pos = 0;
    }

    static void move_to_line_end(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths, const std::string & line) {
        int forward_width = 0;
        for (size_t i = char_pos; i < widths.size(); ++i) {
            forward_width += widths[i];
        }
        move_cursor(forward_width);
        char_pos = widths.size();
        byte_pos = line.length();
    }

    static bool has_ctrl_modifier(const std::string & params) {
        size_t start = 0;
        while (start < params.size()) {
            size_t end = params.find(';', start);
            size_t len = (end == std::string::npos) ? params.size() - start : end - start;
            if (len > 0) {
                int value = 0;
                for (size_t i = 0; i < len; ++i) {
                    char ch = params[start + i];
                    if (!std::isdigit(static_cast<unsigned char>(ch))) {
                        value = -1;
                        break;
                    }
                    value = value * 10 + (ch - '0');
                }
                if (value == 5) {
                    return true;
                }
            }

            if (end == std::string::npos) {
                break;
            }
            start = end + 1;
        }
        return false;
    }

    static bool is_space_codepoint(char32_t cp) {
        return std::iswspace(static_cast<wint_t>(cp)) != 0;
    }

    static void move_word_left(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths, const std::string & line) {
        if (char_pos == 0) {
            return;
        }

        size_t new_char_pos = char_pos;
        size_t new_byte_pos = byte_pos;
        int move_width = 0;

        while (new_char_pos > 0) {
            size_t prev_byte = prev_utf8_char_pos(line, new_byte_pos);
            size_t advance = 0;
            char32_t cp = decode_utf8(line, prev_byte, advance);
            if (!is_space_codepoint(cp)) {
                break;
            }
            move_width += widths[new_char_pos - 1];
            new_char_pos--;
            new_byte_pos = prev_byte;
        }

        while (new_char_pos > 0) {
            size_t prev_byte = prev_utf8_char_pos(line, new_byte_pos);
            size_t advance = 0;
            char32_t cp = decode_utf8(line, prev_byte, advance);
            if (is_space_codepoint(cp)) {
                break;
            }
            move_width += widths[new_char_pos - 1];
            new_char_pos--;
            new_byte_pos = prev_byte;
        }

        move_cursor(-move_width);
        char_pos = new_char_pos;
        byte_pos = new_byte_pos;
    }

    static void move_word_right(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths, const std::string & line) {
        if (char_pos >= widths.size()) {
            return;
        }

        size_t new_char_pos = char_pos;
        size_t new_byte_pos = byte_pos;
        int move_width = 0;

        while (new_char_pos < widths.size()) {
            size_t advance = 0;
            char32_t cp = decode_utf8(line, new_byte_pos, advance);
            if (!is_space_codepoint(cp)) {
                break;
            }
            move_width += widths[new_char_pos];
            new_char_pos++;
            new_byte_pos += advance;
        }

        while (new_char_pos < widths.size()) {
            size_t advance = 0;
            char32_t cp = decode_utf8(line, new_byte_pos, advance);
            if (is_space_codepoint(cp)) {
                break;
            }
            move_width += widths[new_char_pos];
            new_char_pos++;
            new_byte_pos += advance;
        }

        while (new_char_pos < widths.size()) {
            size_t advance = 0;
            char32_t cp = decode_utf8(line, new_byte_pos, advance);
            if (!is_space_codepoint(cp)) {
                break;
            }
            move_width += widths[new_char_pos];
            new_char_pos++;
            new_byte_pos += advance;
        }

        move_cursor(move_width);
        char_pos = new_char_pos;
        byte_pos = new_byte_pos;
    }

    static void move_cursor(int delta) {
        if (delta == 0) return;
#if defined(_WIN32)
        if (hConsole != NULL) {
            CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
            GetConsoleScreenBufferInfo(hConsole, &bufferInfo);
            COORD newCursorPosition = bufferInfo.dwCursorPosition;
            int width = bufferInfo.dwSize.X;
            int newX = newCursorPosition.X + delta;
            int newY = newCursorPosition.Y;

            while (newX >= width) {
                newX -= width;
                newY++;
            }
            while (newX < 0) {
                newX += width;
                newY--;
            }

            newCursorPosition.X = newX;
            newCursorPosition.Y = newY;
            SetConsoleCursorPosition(hConsole, newCursorPosition);
        }
#else
        if (delta < 0) {
            for (int i = 0; i < -delta; i++) fprintf(out, "\b");
        } else {
            for (int i = 0; i < delta; i++) fprintf(out, "\033[C");
        }
#endif
    }

    struct history_t {
        std::vector<std::string> entries;
        size_t viewing_idx = SIZE_MAX;
        std::string backup_line; // current line before viewing history
        void add(const std::string & line) {
            if (line.empty()) {
                return;
            }
            // avoid duplicates with the last entry
            if (entries.empty() || entries.back() != line) {
                entries.push_back(line);
            }
            // also clear viewing state
            end_viewing();
        }
        bool prev(std::string & cur_line) {
            if (entries.empty()) {
                return false;
            }
            if (viewing_idx == SIZE_MAX) {
                return false;
            }
            if (viewing_idx > 0) {
                viewing_idx--;
            }
            cur_line = entries[viewing_idx];
            return true;
        }
        bool next(std::string & cur_line) {
            if (entries.empty() || viewing_idx == SIZE_MAX) {
                return false;
            }
            viewing_idx++;
            if (viewing_idx >= entries.size()) {
                cur_line = backup_line;
                end_viewing();
            } else {
                cur_line = entries[viewing_idx];
            }
            return true;
        }
        void begin_viewing(const std::string & line) {
            backup_line = line;
            viewing_idx = entries.size();
        }
        void end_viewing() {
            viewing_idx = SIZE_MAX;
            backup_line.clear();
        }
        bool is_viewing() const {
            return viewing_idx != SIZE_MAX;
        }
    } history;

    static bool readline_advanced(std::string & line, bool multiline_input) {
        if (out != stdout) {
            fflush(stdout);
        }

        line.clear();
        std::vector<int> widths;
        bool is_special_char = false;
        bool end_of_stream = false;

        size_t byte_pos = 0; // current byte index
        size_t char_pos = 0; // current character index (one char can be multiple bytes)

        char32_t input_char;
        while (true) {
            assert(char_pos <= byte_pos);
            assert(char_pos <= widths.size());
            auto history_prev = [&]() {
                if (!history.is_viewing()) {
                    history.begin_viewing(line);
                }
                std::string new_line;
                if (!history.prev(new_line)) {
                    return;
                }
                set_line_contents(new_line, line, widths, char_pos, byte_pos);
            };
            auto history_next = [&]() {
                if (history.is_viewing()) {
                    std::string new_line;
                    if (!history.next(new_line)) {
                        return;
                    }
                    set_line_contents(new_line, line, widths, char_pos, byte_pos);
                }
            };

            fflush(out); // Ensure all output is displayed before waiting for input
            input_char = getchar32();

            if (input_char == '\r' || input_char == '\n') {
                break;
            }

            if (input_char == (char32_t) WEOF || input_char == 0x04 /* Ctrl+D */) {
                end_of_stream = true;
                break;
            }

            if (is_special_char) {
                replace_last(line.back());
                is_special_char = false;
            }

            if (input_char == '\033') { // Escape sequence
                char32_t code = getchar32();
                if (code == '[') {
                    std::string params;
                    while (true) {
                        code = getchar32();
                        if ((code >= 'A' && code <= 'Z') || (code >= 'a' && code <= 'z') || code == '~' || code == (char32_t) WEOF) {
                            break;
                        }
                        params.push_back(static_cast<char>(code));
                    }

                    const bool ctrl_modifier = has_ctrl_modifier(params);

                    if (code == 'D') { // left
                        if (ctrl_modifier) {
                            move_word_left(char_pos, byte_pos, widths, line);
                        } else if (char_pos > 0) {
                            int w = widths[char_pos - 1];
                            move_cursor(-w);
                            char_pos--;
                            byte_pos = prev_utf8_char_pos(line, byte_pos);
                        }
                    } else if (code == 'C') { // right
                        if (ctrl_modifier) {
                            move_word_right(char_pos, byte_pos, widths, line);
                        } else if (char_pos < widths.size()) {
                            int w = widths[char_pos];
                            move_cursor(w);
                            char_pos++;
                            byte_pos = next_utf8_char_pos(line, byte_pos);
                        }
                    } else if (code == 'H') { // home
                        move_to_line_start(char_pos, byte_pos, widths);
                    } else if (code == 'F') { // end
                        move_to_line_end(char_pos, byte_pos, widths, line);
                    } else if (code == 'A' || code == 'B') {
                        // up/down
                        if (code == 'A') {
                            history_prev();
                            is_special_char = false;
                        } else if (code == 'B') {
                            history_next();
                            is_special_char = false;
                        }
                    } else if ((code == '~' || (code >= 'A' && code <= 'Z') || (code >= 'a' && code <= 'z')) && !params.empty()) {
                        std::string digits;
                        for (char ch : params) {
                            if (ch == ';') {
                                break;
                            }
                            if (std::isdigit(static_cast<unsigned char>(ch))) {
                                digits.push_back(ch);
                            }
                        }

                        if (code == '~') {
                            if (digits == "1" || digits == "7") { // home
                                move_to_line_start(char_pos, byte_pos, widths);
                            } else if (digits == "4" || digits == "8") { // end
                                move_to_line_end(char_pos, byte_pos, widths, line);
                            } else if (digits == "3") { // delete
                                delete_at_cursor(line, widths, char_pos, byte_pos);
                            }
                        }
                    }
                } else if (code == 0x1B) {
                    // Discard the rest of the escape sequence
                    while ((code = getchar32()) != (char32_t) WEOF) {
                        if ((code >= 'A' && code <= 'Z') || (code >= 'a' && code <= 'z') || code == '~') {
                            break;
                        }
                    }
                }
#if defined(_WIN32)
            } else if (input_char == KEY_ARROW_LEFT) {
                if (char_pos > 0) {
                    int w = widths[char_pos - 1];
                    move_cursor(-w);
                    char_pos--;
                    byte_pos = prev_utf8_char_pos(line, byte_pos);
                }
            } else if (input_char == KEY_ARROW_RIGHT) {
                if (char_pos < widths.size()) {
                    int w = widths[char_pos];
                    move_cursor(w);
                    char_pos++;
                    byte_pos = next_utf8_char_pos(line, byte_pos);
                }
            } else if (input_char == KEY_CTRL_ARROW_LEFT) {
                move_word_left(char_pos, byte_pos, widths, line);
            } else if (input_char == KEY_CTRL_ARROW_RIGHT) {
                move_word_right(char_pos, byte_pos, widths, line);
            } else if (input_char == KEY_HOME) {
                move_to_line_start(char_pos, byte_pos, widths);
            } else if (input_char == KEY_END) {
                move_to_line_end(char_pos, byte_pos, widths, line);
            } else if (input_char == KEY_DELETE) {
                delete_at_cursor(line, widths, char_pos, byte_pos);
            } else if (input_char == KEY_ARROW_UP || input_char == KEY_ARROW_DOWN) {
                if (input_char == KEY_ARROW_UP) {
                    history_prev();
                    is_special_char = false;
                } else if (input_char == KEY_ARROW_DOWN) {
                    history_next();
                    is_special_char = false;
                }
#endif
            } else if (input_char == 0x08 || input_char == 0x7F) { // Backspace
                if (char_pos > 0) {
                    int w = widths[char_pos - 1];
                    move_cursor(-w);
                    char_pos--;
                    size_t prev_pos = prev_utf8_char_pos(line, byte_pos);
                    size_t char_len = byte_pos - prev_pos;
                    byte_pos = prev_pos;

                    // remove the character
                    line.erase(byte_pos, char_len);
                    widths.erase(widths.begin() + char_pos);

                    // redraw tail
                    size_t p = byte_pos;
                    int tail_width = 0;
                    for (size_t i = char_pos; i < widths.size(); ++i) {
                        size_t next_p = next_utf8_char_pos(line, p);
                        put_codepoint(line.c_str() + p, next_p - p, widths[i]);
                        tail_width += widths[i];
                        p = next_p;
                    }

                    // clear display
                    for (int i = 0; i < w; ++i) {
                        fputc(' ', out);
                    }
                    move_cursor(-(tail_width + w));
                }
            } else {
                // insert character
                std::string new_char_str;
                append_utf8(input_char, new_char_str);
                int w = estimateWidth(input_char);

                if (char_pos == widths.size()) {
                    // insert at the end
                    line += new_char_str;
                    int real_w = put_codepoint(new_char_str.c_str(), new_char_str.length(), w);
                    if (real_w < 0) real_w = 0;
                    widths.push_back(real_w);
                    byte_pos += new_char_str.length();
                    char_pos++;
                } else {
                    // insert in middle
                    line.insert(byte_pos, new_char_str);

                    int real_w = put_codepoint(new_char_str.c_str(), new_char_str.length(), w);
                    if (real_w < 0) real_w = 0;

                    widths.insert(widths.begin() + char_pos, real_w);

                    // print the tail
                    size_t p = byte_pos + new_char_str.length();
                    int tail_width = 0;
                    for (size_t i = char_pos + 1; i < widths.size(); ++i) {
                        size_t next_p = next_utf8_char_pos(line, p);
                        put_codepoint(line.c_str() + p, next_p - p, widths[i]);
                        tail_width += widths[i];
                        p = next_p;
                    }

                    move_cursor(-tail_width);

                    byte_pos += new_char_str.length();
                    char_pos++;
                }
            }

            if (!line.empty() && (line.back() == '\\' || line.back() == '/')) {
                replace_last(line.back());
                is_special_char = true;
            }
        }

        bool has_more = multiline_input;
        if (is_special_char) {
            replace_last(' ');
            pop_cursor();

            char last = line.back();
            line.pop_back();
            if (last == '\\') {
                line += '\n';
                fputc('\n', out);
                has_more = !has_more;
            } else {
                // llama will just eat the single space, it won't act as a space
                if (line.length() == 1 && line.back() == ' ') {
                    line.clear();
                    pop_cursor();
                }
                has_more = false;
            }
        } else {
            if (end_of_stream) {
                has_more = false;
            } else {
                line += '\n';
                fputc('\n', out);
            }
        }

        if (!end_of_stream && !line.empty()) {
            // remove the trailing newline for history storage
            if (!line.empty() && line.back() == '\n') {
                line.pop_back();
            }
            // TODO: maybe support multiline history entries?
            history.add(line);
        }

        fflush(out);
        return has_more;
    }

    static bool readline_simple(std::string & line, bool multiline_input) {
#if defined(_WIN32)
        std::wstring wline;
        if (!std::getline(std::wcin, wline)) {
            // Input stream is bad or EOF received
            line.clear();
            GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);
            return false;
        }

        int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wline[0], (int)wline.size(), NULL, 0, NULL, NULL);
        line.resize(size_needed);
        WideCharToMultiByte(CP_UTF8, 0, &wline[0], (int)wline.size(), &line[0], size_needed, NULL, NULL);
#else
        if (!std::getline(std::cin, line)) {
            // Input stream is bad or EOF received
            line.clear();
            return false;
        }
#endif
        if (!line.empty()) {
            char last = line.back();
            if (last == '/') { // Always return control on '/' symbol
                line.pop_back();
                return false;
            }
            if (last == '\\') { // '\\' changes the default action
                line.pop_back();
                multiline_input = !multiline_input;
            }
        }
        line += '\n';

        // By default, continue input if multiline_input is set
        return multiline_input;
    }

    bool readline(std::string & line, bool multiline_input) {
        if (simple_io) {
            return readline_simple(line, multiline_input);
        }
        return readline_advanced(line, multiline_input);
    }

    namespace spinner {
        static const char LOADING_CHARS[] = {'|', '/', '-', '\\'};
        static std::condition_variable cv_stop;
        static std::thread th;
        static size_t frame = 0; // only modified by one thread
        static bool running = false;
        static std::mutex mtx;
        static auto wait_time = std::chrono::milliseconds(100);
        static void draw_next_frame() {
            // don't need lock because only one thread modifies running
            frame = (frame + 1) % sizeof(LOADING_CHARS);
            replace_last(LOADING_CHARS[frame]);
            fflush(out);
        }
        void start() {
            std::unique_lock<std::mutex> lock(mtx);
            if (simple_io || running) {
                return;
            }
            common_log_flush(common_log_main());
            fprintf(out, "%c", LOADING_CHARS[0]);
            fflush(out);
            frame = 1;
            running = true;
            th = std::thread([]() {
                std::unique_lock<std::mutex> lock(mtx);
                while (true) {
                    if (cv_stop.wait_for(lock, wait_time, []{ return !running; })) {
                        break;
                    }
                    draw_next_frame();
                }
            });
        }
        void stop() {
            {
                std::unique_lock<std::mutex> lock(mtx);
                if (simple_io || !running) {
                    return;
                }
                running = false;
                cv_stop.notify_all();
            }
            if (th.joinable()) {
                th.join();
            }
            replace_last(' ');
            pop_cursor();
            fflush(out);
        }
    }

    void log(const char * fmt, ...) {
        va_list args;
        va_start(args, fmt);
        vfprintf(out, fmt, args);
        va_end(args);
    }

    void error(const char * fmt, ...) {
        va_list args;
        va_start(args, fmt);
        display_type cur = current_display;
        set_display(DISPLAY_TYPE_ERROR);
        vfprintf(out, fmt, args);
        set_display(cur); // restore previous color
        va_end(args);
    }

    void flush() {
        fflush(out);
    }
}