]> git.ipfire.org Git - thirdparty/git.git/commit
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)
commita5e46e6b0101b960c131dd39b50999cc0e69ed2b
tree015f926507d78291928b4e6c32c65346b1d98f14
parent9ea416cb511e87f830462b08ae74b8a78fcca223
terminal: add a new function to read a single keystroke

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