MCPcopy Create free account
hub / github.com/git/git / read_key_without_echo

Function read_key_without_echo

compat/terminal.c:534–582  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

532}
533
534int 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

Callers 1

read_single_characterFunction · 0.85

Calls 9

enable_non_canonicalFunction · 0.85
warningFunction · 0.85
strbuf_getlineFunction · 0.85
restore_termFunction · 0.85
strbuf_addchFunction · 0.85
strbuf_spliceFunction · 0.85
is_known_escape_sequenceFunction · 0.85
getchar_with_timeoutFunction · 0.85
strbuf_addstrFunction · 0.85

Tested by

no test coverage detected