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

Function readline_advanced

smallthinker/common/console.cpp:355–456  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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