| 532 | } |
| 533 | |
| 534 | int read_key_without_echo(struct strbuf *buf) |
| 535 | { |
| 536 | static int warning_displayed; |
| 537 | int ch; |
| 538 | |
| 539 | if (warning_displayed || enable_non_canonical(SAVE_TERM_STDIN) < 0) { |
| 540 | if (!warning_displayed) { |
| 541 | warning("reading single keystrokes not supported on " |
| 542 | "this platform; reading line instead"); |
| 543 | warning_displayed = 1; |
| 544 | } |
| 545 | |
| 546 | return strbuf_getline(buf, stdin); |
| 547 | } |
| 548 | |
| 549 | strbuf_reset(buf); |
| 550 | ch = getchar(); |
| 551 | if (ch == EOF) { |
| 552 | restore_term(); |
| 553 | return EOF; |
| 554 | } |
| 555 | strbuf_addch(buf, ch); |
| 556 | |
| 557 | if (ch == '\033' /* ESC */) { |
| 558 | /* |
| 559 | * We are most likely looking at an Escape sequence. Let's try |
| 560 | * to read more bytes, waiting at most half a second, assuming |
| 561 | * that the sequence is complete if we did not receive any byte |
| 562 | * within that time. |
| 563 | * |
| 564 | * Start by replacing the Escape byte with ^[ */ |
| 565 | strbuf_splice(buf, buf->len - 1, 1, "^[", 2); |
| 566 | |
| 567 | /* |
| 568 | * Query the terminal capabilities once about all the Escape |
| 569 | * sequences it knows about, so that we can avoid waiting for |
| 570 | * half a second when we know that the sequence is complete. |
| 571 | */ |
| 572 | while (!is_known_escape_sequence(buf->buf)) { |
| 573 | ch = getchar_with_timeout(500); |
| 574 | if (ch == EOF) |
| 575 | break; |
| 576 | strbuf_addch(buf, ch); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | restore_term(); |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | #else |
| 585 |
no test coverage detected