MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / readline_advanced

Function readline_advanced

common/console.cpp:352–453  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

350 }
351
352 static bool readline_advanced(std::string & line, bool multiline_input) {
353 if (out != stdout) {
354 fflush(stdout);
355 }
356
357 line.clear();
358 std::vector<int> widths;
359 bool is_special_char = false;
360 bool end_of_stream = false;
361
362 char32_t input_char;
363 while (true) {
364 fflush(out); // Ensure all output is displayed before waiting for input
365 input_char = getchar32();
366
367 if (input_char == '\r' || input_char == '\n') {
368 break;
369 }
370
371 if (input_char == (char32_t) WEOF || input_char == 0x04 /* Ctrl+D*/) {
372 end_of_stream = true;
373 break;
374 }
375
376 if (is_special_char) {
377 set_display(user_input);
378 replace_last(line.back());
379 is_special_char = false;
380 }
381
382 if (input_char == '\033') { // Escape sequence
383 char32_t code = getchar32();
384 if (code == '[' || code == 0x1B) {
385 // Discard the rest of the escape sequence
386 while ((code = getchar32()) != (char32_t) WEOF) {
387 if ((code >= 'A' && code <= 'Z') || (code >= 'a' && code <= 'z') || code == '~') {
388 break;
389 }
390 }
391 }
392 } else if (input_char == 0x08 || input_char == 0x7F) { // Backspace
393 if (!widths.empty()) {
394 int count;
395 do {
396 count = widths.back();
397 widths.pop_back();
398 // Move cursor back, print space, and move cursor back again
399 for (int i = 0; i < count; i++) {
400 replace_last(' ');
401 pop_cursor();
402 }
403 pop_back_utf8_char(line);
404 } while (count == 0 && !widths.empty());
405 }
406 } else {
407 int offset = line.length();
408 append_utf8(input_char, line);
409 int width = put_codepoint(line.c_str() + offset, line.length() - offset, estimateWidth(input_char));

Callers 1

readlineFunction · 0.70

Calls 13

lengthMethod · 0.80
getchar32Function · 0.70
set_displayFunction · 0.70
replace_lastFunction · 0.70
pop_cursorFunction · 0.70
pop_back_utf8_charFunction · 0.70
append_utf8Function · 0.70
put_codepointFunction · 0.70
estimateWidthFunction · 0.70
clearMethod · 0.45
emptyMethod · 0.45
c_strMethod · 0.45

Tested by

no test coverage detected