]> git.ipfire.org Git - thirdparty/git.git/commitdiff
terminal: add a new function to read a single keystroke
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Tue, 14 Jan 2020 18:43:49 +0000 (18:43 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 15 Jan 2020 20:06:17 +0000 (12:06 -0800)
Typically, input on the command-line is line-based. It is actually not
really easy to get single characters (or better put: keystrokes).

We provide two implementations here:

- One that handles `/dev/tty` based systems as well as native Windows.
  The former uses the `tcsetattr()` function to put the terminal into
  "raw mode", which allows us to read individual keystrokes, one by one.
  The latter uses `stty.exe` to do the same, falling back to direct
  Win32 Console access.

  Thanks to the refactoring leading up to this commit, this is a single
  function, with the platform-specific details hidden away in
  conditionally-compiled code blocks.

- A fall-back which simply punts and reads back an entire line.

Note that the function writes the keystroke into an `strbuf` rather than
a `char`, in preparation for reading Escape sequences (e.g. when the
user hit an arrow key). This is also required for UTF-8 sequences in
case the keystroke corresponds to a non-ASCII letter.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
compat/terminal.c
compat/terminal.h

index 16e9949da10e5d9af64cc251595eb8861888b8ff..1b2564042ac60c1f2a2e6b7f7ebf85b992b59a3e 100644 (file)
@@ -60,6 +60,11 @@ static int disable_echo(void)
        return disable_bits(ECHO);
 }
 
+static int enable_non_canonical(void)
+{
+       return disable_bits(ICANON | ECHO);
+}
+
 #elif defined(GIT_WINDOWS_NATIVE)
 
 #define INPUT_PATH "CONIN$"
@@ -151,6 +156,10 @@ static int disable_echo(void)
        return disable_bits(ENABLE_ECHO_INPUT);
 }
 
+static int enable_non_canonical(void)
+{
+       return disable_bits(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
+}
 
 #endif
 
@@ -198,6 +207,33 @@ char *git_terminal_prompt(const char *prompt, int echo)
        return buf.buf;
 }
 
+int read_key_without_echo(struct strbuf *buf)
+{
+       static int warning_displayed;
+       int ch;
+
+       if (warning_displayed || enable_non_canonical() < 0) {
+               if (!warning_displayed) {
+                       warning("reading single keystrokes not supported on "
+                               "this platform; reading line instead");
+                       warning_displayed = 1;
+               }
+
+               return strbuf_getline(buf, stdin);
+       }
+
+       strbuf_reset(buf);
+       ch = getchar();
+       if (ch == EOF) {
+               restore_term();
+               return EOF;
+       }
+
+       strbuf_addch(buf, ch);
+       restore_term();
+       return 0;
+}
+
 #else
 
 char *git_terminal_prompt(const char *prompt, int echo)
@@ -205,4 +241,23 @@ char *git_terminal_prompt(const char *prompt, int echo)
        return getpass(prompt);
 }
 
+int read_key_without_echo(struct strbuf *buf)
+{
+       static int warning_displayed;
+       const char *res;
+
+       if (!warning_displayed) {
+               warning("reading single keystrokes not supported on this "
+                       "platform; reading line instead");
+               warning_displayed = 1;
+       }
+
+       res = getpass("");
+       strbuf_reset(buf);
+       if (!res)
+               return EOF;
+       strbuf_addstr(buf, res);
+       return 0;
+}
+
 #endif
index 97db7cd69d65fc1a03ffb25e4b53ad6d296016e5..a9d52b8464e2f6e3c39bc107078cb5b2a36aa5d5 100644 (file)
@@ -3,4 +3,7 @@
 
 char *git_terminal_prompt(const char *prompt, int echo);
 
+/* Read a single keystroke, without echoing it to the terminal */
+int read_key_without_echo(struct strbuf *buf);
+
 #endif /* COMPAT_TERMINAL_H */