MCPcopy
hub / github.com/pallets/click / getchar

Function getchar

src/click/_termui_impl.py:860–904  ·  view source on GitHub ↗
(echo: bool)

Source from the content-addressed store, hash-verified

858 yield -1
859
860 def getchar(echo: bool) -> str:
861 # The function `getch` will return a bytes object corresponding to
862 # the pressed character. Since Windows 10 build 1803, it will also
863 # return \x00 when called a second time after pressing a regular key.
864 #
865 # `getwch` does not share this probably-bugged behavior. Moreover, it
866 # returns a Unicode object by default, which is what we want.
867 #
868 # Either of these functions will return \x00 or \xe0 to indicate
869 # a special key, and you need to call the same function again to get
870 # the "rest" of the code. The fun part is that \u00e0 is
871 # "latin small letter a with grave", so if you type that on a French
872 # keyboard, you _also_ get a \xe0.
873 # E.g., consider the Up arrow. This returns \xe0 and then \x48. The
874 # resulting Unicode string reads as "a with grave" + "capital H".
875 # This is indistinguishable from when the user actually types
876 # "a with grave" and then "capital H".
877 #
878 # When \xe0 is returned, we assume it's part of a special-key sequence
879 # and call `getwch` again, but that means that when the user types
880 # the \u00e0 character, `getchar` doesn't return until a second
881 # character is typed.
882 # The alternative is returning immediately, but that would mess up
883 # cross-platform handling of arrow keys and others that start with
884 # \xe0. Another option is using `getch`, but then we can't reliably
885 # read non-ASCII characters, because return values of `getch` are
886 # limited to the current 8-bit codepage.
887 #
888 # Anyway, Click doesn't claim to do this Right(tm), and using `getwch`
889 # is doing the right thing in more situations than with `getch`.
890
891 if echo:
892 func = t.cast(t.Callable[[], str], msvcrt.getwche)
893 else:
894 func = t.cast(t.Callable[[], str], msvcrt.getwch)
895
896 rv = func()
897
898 if rv in ("\x00", "\xe0"):
899 # \x00 and \xe0 are control characters that indicate special key,
900 # see above.
901 rv += func()
902
903 _translate_ch_to_exc(rv)
904 return rv
905
906else:
907 import termios

Callers

nothing calls this directly

Calls 7

funcFunction · 0.85
_translate_ch_to_excFunction · 0.85
get_best_encodingFunction · 0.85
isattyFunction · 0.85
readMethod · 0.80
raw_terminalFunction · 0.70
writeMethod · 0.45

Tested by

no test coverage detected