_termui_impl.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. """
  2. This module contains implementations for the termui module. To keep the
  3. import time of Click down, some infrequently used functionality is
  4. placed in this module and only imported as needed.
  5. """
  6. from __future__ import annotations
  7. import collections.abc as cabc
  8. import contextlib
  9. import math
  10. import os
  11. import shlex
  12. import sys
  13. import time
  14. import typing as t
  15. from gettext import gettext as _
  16. from io import StringIO
  17. from pathlib import Path
  18. from types import TracebackType
  19. from ._compat import _default_text_stdout
  20. from ._compat import CYGWIN
  21. from ._compat import get_best_encoding
  22. from ._compat import isatty
  23. from ._compat import open_stream
  24. from ._compat import strip_ansi
  25. from ._compat import term_len
  26. from ._compat import WIN
  27. from .exceptions import ClickException
  28. from .utils import echo
  29. V = t.TypeVar("V")
  30. if os.name == "nt":
  31. BEFORE_BAR = "\r"
  32. AFTER_BAR = "\n"
  33. else:
  34. BEFORE_BAR = "\r\033[?25l"
  35. AFTER_BAR = "\033[?25h\n"
  36. class ProgressBar(t.Generic[V]):
  37. def __init__(
  38. self,
  39. iterable: cabc.Iterable[V] | None,
  40. length: int | None = None,
  41. fill_char: str = "#",
  42. empty_char: str = " ",
  43. bar_template: str = "%(bar)s",
  44. info_sep: str = " ",
  45. hidden: bool = False,
  46. show_eta: bool = True,
  47. show_percent: bool | None = None,
  48. show_pos: bool = False,
  49. item_show_func: t.Callable[[V | None], str | None] | None = None,
  50. label: str | None = None,
  51. file: t.TextIO | None = None,
  52. color: bool | None = None,
  53. update_min_steps: int = 1,
  54. width: int = 30,
  55. ) -> None:
  56. self.fill_char = fill_char
  57. self.empty_char = empty_char
  58. self.bar_template = bar_template
  59. self.info_sep = info_sep
  60. self.hidden = hidden
  61. self.show_eta = show_eta
  62. self.show_percent = show_percent
  63. self.show_pos = show_pos
  64. self.item_show_func = item_show_func
  65. self.label: str = label or ""
  66. if file is None:
  67. file = _default_text_stdout()
  68. # There are no standard streams attached to write to. For example,
  69. # pythonw on Windows.
  70. if file is None:
  71. file = StringIO()
  72. self.file = file
  73. self.color = color
  74. self.update_min_steps = update_min_steps
  75. self._completed_intervals = 0
  76. self.width: int = width
  77. self.autowidth: bool = width == 0
  78. if length is None:
  79. from operator import length_hint
  80. length = length_hint(iterable, -1)
  81. if length == -1:
  82. length = None
  83. if iterable is None:
  84. if length is None:
  85. raise TypeError("iterable or length is required")
  86. iterable = t.cast("cabc.Iterable[V]", range(length))
  87. self.iter: cabc.Iterable[V] = iter(iterable)
  88. self.length = length
  89. self.pos: int = 0
  90. self.avg: list[float] = []
  91. self.last_eta: float
  92. self.start: float
  93. self.start = self.last_eta = time.time()
  94. self.eta_known: bool = False
  95. self.finished: bool = False
  96. self.max_width: int | None = None
  97. self.entered: bool = False
  98. self.current_item: V | None = None
  99. self._is_atty = isatty(self.file)
  100. self._last_line: str | None = None
  101. def __enter__(self) -> ProgressBar[V]:
  102. self.entered = True
  103. self.render_progress()
  104. return self
  105. def __exit__(
  106. self,
  107. exc_type: type[BaseException] | None,
  108. exc_value: BaseException | None,
  109. tb: TracebackType | None,
  110. ) -> None:
  111. self.render_finish()
  112. def __iter__(self) -> cabc.Iterator[V]:
  113. if not self.entered:
  114. raise RuntimeError("You need to use progress bars in a with block.")
  115. self.render_progress()
  116. return self.generator()
  117. def __next__(self) -> V:
  118. # Iteration is defined in terms of a generator function,
  119. # returned by iter(self); use that to define next(). This works
  120. # because `self.iter` is an iterable consumed by that generator,
  121. # so it is re-entry safe. Calling `next(self.generator())`
  122. # twice works and does "what you want".
  123. return next(iter(self))
  124. def render_finish(self) -> None:
  125. if self.hidden or not self._is_atty:
  126. return
  127. self.file.write(AFTER_BAR)
  128. self.file.flush()
  129. @property
  130. def pct(self) -> float:
  131. if self.finished:
  132. return 1.0
  133. return min(self.pos / (float(self.length or 1) or 1), 1.0)
  134. @property
  135. def time_per_iteration(self) -> float:
  136. if not self.avg:
  137. return 0.0
  138. return sum(self.avg) / float(len(self.avg))
  139. @property
  140. def eta(self) -> float:
  141. if self.length is not None and not self.finished:
  142. return self.time_per_iteration * (self.length - self.pos)
  143. return 0.0
  144. def format_eta(self) -> str:
  145. if self.eta_known:
  146. t = int(self.eta)
  147. seconds = t % 60
  148. t //= 60
  149. minutes = t % 60
  150. t //= 60
  151. hours = t % 24
  152. t //= 24
  153. if t > 0:
  154. return f"{t}d {hours:02}:{minutes:02}:{seconds:02}"
  155. else:
  156. return f"{hours:02}:{minutes:02}:{seconds:02}"
  157. return ""
  158. def format_pos(self) -> str:
  159. pos = str(self.pos)
  160. if self.length is not None:
  161. pos += f"/{self.length}"
  162. return pos
  163. def format_pct(self) -> str:
  164. return f"{int(self.pct * 100): 4}%"[1:]
  165. def format_bar(self) -> str:
  166. if self.length is not None:
  167. bar_length = int(self.pct * self.width)
  168. bar = self.fill_char * bar_length
  169. bar += self.empty_char * (self.width - bar_length)
  170. elif self.finished:
  171. bar = self.fill_char * self.width
  172. else:
  173. chars = list(self.empty_char * (self.width or 1))
  174. if self.time_per_iteration != 0:
  175. chars[
  176. int(
  177. (math.cos(self.pos * self.time_per_iteration) / 2.0 + 0.5)
  178. * self.width
  179. )
  180. ] = self.fill_char
  181. bar = "".join(chars)
  182. return bar
  183. def format_progress_line(self) -> str:
  184. show_percent = self.show_percent
  185. info_bits = []
  186. if self.length is not None and show_percent is None:
  187. show_percent = not self.show_pos
  188. if self.show_pos:
  189. info_bits.append(self.format_pos())
  190. if show_percent:
  191. info_bits.append(self.format_pct())
  192. if self.show_eta and self.eta_known and not self.finished:
  193. info_bits.append(self.format_eta())
  194. if self.item_show_func is not None:
  195. item_info = self.item_show_func(self.current_item)
  196. if item_info is not None:
  197. info_bits.append(item_info)
  198. return (
  199. self.bar_template
  200. % {
  201. "label": self.label,
  202. "bar": self.format_bar(),
  203. "info": self.info_sep.join(info_bits),
  204. }
  205. ).rstrip()
  206. def render_progress(self) -> None:
  207. if self.hidden:
  208. return
  209. if not self._is_atty:
  210. # Only output the label once if the output is not a TTY.
  211. if self._last_line != self.label:
  212. self._last_line = self.label
  213. echo(self.label, file=self.file, color=self.color)
  214. return
  215. buf = []
  216. # Update width in case the terminal has been resized
  217. if self.autowidth:
  218. import shutil
  219. old_width = self.width
  220. self.width = 0
  221. clutter_length = term_len(self.format_progress_line())
  222. new_width = max(0, shutil.get_terminal_size().columns - clutter_length)
  223. if new_width < old_width and self.max_width is not None:
  224. buf.append(BEFORE_BAR)
  225. buf.append(" " * self.max_width)
  226. self.max_width = new_width
  227. self.width = new_width
  228. clear_width = self.width
  229. if self.max_width is not None:
  230. clear_width = self.max_width
  231. buf.append(BEFORE_BAR)
  232. line = self.format_progress_line()
  233. line_len = term_len(line)
  234. if self.max_width is None or self.max_width < line_len:
  235. self.max_width = line_len
  236. buf.append(line)
  237. buf.append(" " * (clear_width - line_len))
  238. line = "".join(buf)
  239. # Render the line only if it changed.
  240. if line != self._last_line:
  241. self._last_line = line
  242. echo(line, file=self.file, color=self.color, nl=False)
  243. self.file.flush()
  244. def make_step(self, n_steps: int) -> None:
  245. self.pos += n_steps
  246. if self.length is not None and self.pos >= self.length:
  247. self.finished = True
  248. if (time.time() - self.last_eta) < 1.0:
  249. return
  250. self.last_eta = time.time()
  251. # self.avg is a rolling list of length <= 7 of steps where steps are
  252. # defined as time elapsed divided by the total progress through
  253. # self.length.
  254. if self.pos:
  255. step = (time.time() - self.start) / self.pos
  256. else:
  257. step = time.time() - self.start
  258. self.avg = self.avg[-6:] + [step]
  259. self.eta_known = self.length is not None
  260. def update(self, n_steps: int, current_item: V | None = None) -> None:
  261. """Update the progress bar by advancing a specified number of
  262. steps, and optionally set the ``current_item`` for this new
  263. position.
  264. :param n_steps: Number of steps to advance.
  265. :param current_item: Optional item to set as ``current_item``
  266. for the updated position.
  267. .. versionchanged:: 8.0
  268. Added the ``current_item`` optional parameter.
  269. .. versionchanged:: 8.0
  270. Only render when the number of steps meets the
  271. ``update_min_steps`` threshold.
  272. """
  273. if current_item is not None:
  274. self.current_item = current_item
  275. self._completed_intervals += n_steps
  276. if self._completed_intervals >= self.update_min_steps:
  277. self.make_step(self._completed_intervals)
  278. self.render_progress()
  279. self._completed_intervals = 0
  280. def finish(self) -> None:
  281. self.eta_known = False
  282. self.current_item = None
  283. self.finished = True
  284. def generator(self) -> cabc.Iterator[V]:
  285. """Return a generator which yields the items added to the bar
  286. during construction, and updates the progress bar *after* the
  287. yielded block returns.
  288. """
  289. # WARNING: the iterator interface for `ProgressBar` relies on
  290. # this and only works because this is a simple generator which
  291. # doesn't create or manage additional state. If this function
  292. # changes, the impact should be evaluated both against
  293. # `iter(bar)` and `next(bar)`. `next()` in particular may call
  294. # `self.generator()` repeatedly, and this must remain safe in
  295. # order for that interface to work.
  296. if not self.entered:
  297. raise RuntimeError("You need to use progress bars in a with block.")
  298. if not self._is_atty:
  299. yield from self.iter
  300. else:
  301. for rv in self.iter:
  302. self.current_item = rv
  303. # This allows show_item_func to be updated before the
  304. # item is processed. Only trigger at the beginning of
  305. # the update interval.
  306. if self._completed_intervals == 0:
  307. self.render_progress()
  308. yield rv
  309. self.update(1)
  310. self.finish()
  311. self.render_progress()
  312. def pager(generator: cabc.Iterable[str], color: bool | None = None) -> None:
  313. """Decide what method to use for paging through text."""
  314. stdout = _default_text_stdout()
  315. # There are no standard streams attached to write to. For example,
  316. # pythonw on Windows.
  317. if stdout is None:
  318. stdout = StringIO()
  319. if not isatty(sys.stdin) or not isatty(stdout):
  320. return _nullpager(stdout, generator, color)
  321. # Split and normalize the pager command into parts.
  322. pager_cmd_parts = shlex.split(os.environ.get("PAGER", ""), posix=False)
  323. if pager_cmd_parts:
  324. if WIN:
  325. if _tempfilepager(generator, pager_cmd_parts, color):
  326. return
  327. elif _pipepager(generator, pager_cmd_parts, color):
  328. return
  329. if os.environ.get("TERM") in ("dumb", "emacs"):
  330. return _nullpager(stdout, generator, color)
  331. if (WIN or sys.platform.startswith("os2")) and _tempfilepager(
  332. generator, ["more"], color
  333. ):
  334. return
  335. if _pipepager(generator, ["less"], color):
  336. return
  337. import tempfile
  338. fd, filename = tempfile.mkstemp()
  339. os.close(fd)
  340. try:
  341. if _pipepager(generator, ["more"], color):
  342. return
  343. return _nullpager(stdout, generator, color)
  344. finally:
  345. os.unlink(filename)
  346. def _pipepager(
  347. generator: cabc.Iterable[str], cmd_parts: list[str], color: bool | None
  348. ) -> bool:
  349. """Page through text by feeding it to another program. Invoking a
  350. pager through this might support colors.
  351. Returns `True` if the command was found, `False` otherwise and thus another
  352. pager should be attempted.
  353. """
  354. # Split the command into the invoked CLI and its parameters.
  355. if not cmd_parts:
  356. return False
  357. import shutil
  358. cmd = cmd_parts[0]
  359. cmd_params = cmd_parts[1:]
  360. cmd_filepath = shutil.which(cmd)
  361. if not cmd_filepath:
  362. return False
  363. # Resolves symlinks and produces a normalized absolute path string.
  364. cmd_path = Path(cmd_filepath).resolve()
  365. cmd_name = cmd_path.name
  366. import subprocess
  367. # Make a local copy of the environment to not affect the global one.
  368. env = dict(os.environ)
  369. # If we're piping to less and the user hasn't decided on colors, we enable
  370. # them by default we find the -R flag in the command line arguments.
  371. if color is None and cmd_name == "less":
  372. less_flags = f"{os.environ.get('LESS', '')}{' '.join(cmd_params)}"
  373. if not less_flags:
  374. env["LESS"] = "-R"
  375. color = True
  376. elif "r" in less_flags or "R" in less_flags:
  377. color = True
  378. c = subprocess.Popen(
  379. [str(cmd_path)] + cmd_params,
  380. shell=True,
  381. stdin=subprocess.PIPE,
  382. env=env,
  383. errors="replace",
  384. text=True,
  385. )
  386. assert c.stdin is not None
  387. try:
  388. for text in generator:
  389. if not color:
  390. text = strip_ansi(text)
  391. c.stdin.write(text)
  392. except BrokenPipeError:
  393. # In case the pager exited unexpectedly, ignore the broken pipe error.
  394. pass
  395. except Exception as e:
  396. # In case there is an exception we want to close the pager immediately
  397. # and let the caller handle it.
  398. # Otherwise the pager will keep running, and the user may not notice
  399. # the error message, or worse yet it may leave the terminal in a broken state.
  400. c.terminate()
  401. raise e
  402. finally:
  403. # We must close stdin and wait for the pager to exit before we continue
  404. try:
  405. c.stdin.close()
  406. # Close implies flush, so it might throw a BrokenPipeError if the pager
  407. # process exited already.
  408. except BrokenPipeError:
  409. pass
  410. # Less doesn't respect ^C, but catches it for its own UI purposes (aborting
  411. # search or other commands inside less).
  412. #
  413. # That means when the user hits ^C, the parent process (click) terminates,
  414. # but less is still alive, paging the output and messing up the terminal.
  415. #
  416. # If the user wants to make the pager exit on ^C, they should set
  417. # `LESS='-K'`. It's not our decision to make.
  418. while True:
  419. try:
  420. c.wait()
  421. except KeyboardInterrupt:
  422. pass
  423. else:
  424. break
  425. return True
  426. def _tempfilepager(
  427. generator: cabc.Iterable[str], cmd_parts: list[str], color: bool | None
  428. ) -> bool:
  429. """Page through text by invoking a program on a temporary file.
  430. Returns `True` if the command was found, `False` otherwise and thus another
  431. pager should be attempted.
  432. """
  433. # Split the command into the invoked CLI and its parameters.
  434. if not cmd_parts:
  435. return False
  436. import shutil
  437. cmd = cmd_parts[0]
  438. cmd_filepath = shutil.which(cmd)
  439. if not cmd_filepath:
  440. return False
  441. # Resolves symlinks and produces a normalized absolute path string.
  442. cmd_path = Path(cmd_filepath).resolve()
  443. import subprocess
  444. import tempfile
  445. fd, filename = tempfile.mkstemp()
  446. # TODO: This never terminates if the passed generator never terminates.
  447. text = "".join(generator)
  448. if not color:
  449. text = strip_ansi(text)
  450. encoding = get_best_encoding(sys.stdout)
  451. with open_stream(filename, "wb")[0] as f:
  452. f.write(text.encode(encoding))
  453. try:
  454. subprocess.call([str(cmd_path), filename])
  455. except OSError:
  456. # Command not found
  457. pass
  458. finally:
  459. os.close(fd)
  460. os.unlink(filename)
  461. return True
  462. def _nullpager(
  463. stream: t.TextIO, generator: cabc.Iterable[str], color: bool | None
  464. ) -> None:
  465. """Simply print unformatted text. This is the ultimate fallback."""
  466. for text in generator:
  467. if not color:
  468. text = strip_ansi(text)
  469. stream.write(text)
  470. class Editor:
  471. def __init__(
  472. self,
  473. editor: str | None = None,
  474. env: cabc.Mapping[str, str] | None = None,
  475. require_save: bool = True,
  476. extension: str = ".txt",
  477. ) -> None:
  478. self.editor = editor
  479. self.env = env
  480. self.require_save = require_save
  481. self.extension = extension
  482. def get_editor(self) -> str:
  483. if self.editor is not None:
  484. return self.editor
  485. for key in "VISUAL", "EDITOR":
  486. rv = os.environ.get(key)
  487. if rv:
  488. return rv
  489. if WIN:
  490. return "notepad"
  491. from shutil import which
  492. for editor in "sensible-editor", "vim", "nano":
  493. if which(editor) is not None:
  494. return editor
  495. return "vi"
  496. def edit_files(self, filenames: cabc.Iterable[str]) -> None:
  497. import subprocess
  498. editor = self.get_editor()
  499. environ: dict[str, str] | None = None
  500. if self.env:
  501. environ = os.environ.copy()
  502. environ.update(self.env)
  503. exc_filename = " ".join(f'"{filename}"' for filename in filenames)
  504. try:
  505. c = subprocess.Popen(
  506. args=f"{editor} {exc_filename}", env=environ, shell=True
  507. )
  508. exit_code = c.wait()
  509. if exit_code != 0:
  510. raise ClickException(
  511. _("{editor}: Editing failed").format(editor=editor)
  512. )
  513. except OSError as e:
  514. raise ClickException(
  515. _("{editor}: Editing failed: {e}").format(editor=editor, e=e)
  516. ) from e
  517. @t.overload
  518. def edit(self, text: bytes | bytearray) -> bytes | None: ...
  519. # We cannot know whether or not the type expected is str or bytes when None
  520. # is passed, so str is returned as that was what was done before.
  521. @t.overload
  522. def edit(self, text: str | None) -> str | None: ...
  523. def edit(self, text: str | bytes | bytearray | None) -> str | bytes | None:
  524. import tempfile
  525. if text is None:
  526. data: bytes | bytearray = b""
  527. elif isinstance(text, (bytes, bytearray)):
  528. data = text
  529. else:
  530. if text and not text.endswith("\n"):
  531. text += "\n"
  532. if WIN:
  533. data = text.replace("\n", "\r\n").encode("utf-8-sig")
  534. else:
  535. data = text.encode("utf-8")
  536. fd, name = tempfile.mkstemp(prefix="editor-", suffix=self.extension)
  537. f: t.BinaryIO
  538. try:
  539. with os.fdopen(fd, "wb") as f:
  540. f.write(data)
  541. # If the filesystem resolution is 1 second, like Mac OS
  542. # 10.12 Extended, or 2 seconds, like FAT32, and the editor
  543. # closes very fast, require_save can fail. Set the modified
  544. # time to be 2 seconds in the past to work around this.
  545. os.utime(name, (os.path.getatime(name), os.path.getmtime(name) - 2))
  546. # Depending on the resolution, the exact value might not be
  547. # recorded, so get the new recorded value.
  548. timestamp = os.path.getmtime(name)
  549. self.edit_files((name,))
  550. if self.require_save and os.path.getmtime(name) == timestamp:
  551. return None
  552. with open(name, "rb") as f:
  553. rv = f.read()
  554. if isinstance(text, (bytes, bytearray)):
  555. return rv
  556. return rv.decode("utf-8-sig").replace("\r\n", "\n")
  557. finally:
  558. os.unlink(name)
  559. def open_url(url: str, wait: bool = False, locate: bool = False) -> int:
  560. import subprocess
  561. def _unquote_file(url: str) -> str:
  562. from urllib.parse import unquote
  563. if url.startswith("file://"):
  564. url = unquote(url[7:])
  565. return url
  566. if sys.platform == "darwin":
  567. args = ["open"]
  568. if wait:
  569. args.append("-W")
  570. if locate:
  571. args.append("-R")
  572. args.append(_unquote_file(url))
  573. null = open("/dev/null", "w")
  574. try:
  575. return subprocess.Popen(args, stderr=null).wait()
  576. finally:
  577. null.close()
  578. elif WIN:
  579. if locate:
  580. url = _unquote_file(url)
  581. args = ["explorer", f"/select,{url}"]
  582. else:
  583. args = ["start"]
  584. if wait:
  585. args.append("/WAIT")
  586. args.append("")
  587. args.append(url)
  588. try:
  589. return subprocess.call(args)
  590. except OSError:
  591. # Command not found
  592. return 127
  593. elif CYGWIN:
  594. if locate:
  595. url = _unquote_file(url)
  596. args = ["cygstart", os.path.dirname(url)]
  597. else:
  598. args = ["cygstart"]
  599. if wait:
  600. args.append("-w")
  601. args.append(url)
  602. try:
  603. return subprocess.call(args)
  604. except OSError:
  605. # Command not found
  606. return 127
  607. try:
  608. if locate:
  609. url = os.path.dirname(_unquote_file(url)) or "."
  610. else:
  611. url = _unquote_file(url)
  612. c = subprocess.Popen(["xdg-open", url])
  613. if wait:
  614. return c.wait()
  615. return 0
  616. except OSError:
  617. if url.startswith(("http://", "https://")) and not locate and not wait:
  618. import webbrowser
  619. webbrowser.open(url)
  620. return 0
  621. return 1
  622. def _translate_ch_to_exc(ch: str) -> None:
  623. if ch == "\x03":
  624. raise KeyboardInterrupt()
  625. if ch == "\x04" and not WIN: # Unix-like, Ctrl+D
  626. raise EOFError()
  627. if ch == "\x1a" and WIN: # Windows, Ctrl+Z
  628. raise EOFError()
  629. return None
  630. if sys.platform == "win32":
  631. import msvcrt
  632. @contextlib.contextmanager
  633. def raw_terminal() -> cabc.Iterator[int]:
  634. yield -1
  635. def getchar(echo: bool) -> str:
  636. # The function `getch` will return a bytes object corresponding to
  637. # the pressed character. Since Windows 10 build 1803, it will also
  638. # return \x00 when called a second time after pressing a regular key.
  639. #
  640. # `getwch` does not share this probably-bugged behavior. Moreover, it
  641. # returns a Unicode object by default, which is what we want.
  642. #
  643. # Either of these functions will return \x00 or \xe0 to indicate
  644. # a special key, and you need to call the same function again to get
  645. # the "rest" of the code. The fun part is that \u00e0 is
  646. # "latin small letter a with grave", so if you type that on a French
  647. # keyboard, you _also_ get a \xe0.
  648. # E.g., consider the Up arrow. This returns \xe0 and then \x48. The
  649. # resulting Unicode string reads as "a with grave" + "capital H".
  650. # This is indistinguishable from when the user actually types
  651. # "a with grave" and then "capital H".
  652. #
  653. # When \xe0 is returned, we assume it's part of a special-key sequence
  654. # and call `getwch` again, but that means that when the user types
  655. # the \u00e0 character, `getchar` doesn't return until a second
  656. # character is typed.
  657. # The alternative is returning immediately, but that would mess up
  658. # cross-platform handling of arrow keys and others that start with
  659. # \xe0. Another option is using `getch`, but then we can't reliably
  660. # read non-ASCII characters, because return values of `getch` are
  661. # limited to the current 8-bit codepage.
  662. #
  663. # Anyway, Click doesn't claim to do this Right(tm), and using `getwch`
  664. # is doing the right thing in more situations than with `getch`.
  665. if echo:
  666. func = t.cast(t.Callable[[], str], msvcrt.getwche)
  667. else:
  668. func = t.cast(t.Callable[[], str], msvcrt.getwch)
  669. rv = func()
  670. if rv in ("\x00", "\xe0"):
  671. # \x00 and \xe0 are control characters that indicate special key,
  672. # see above.
  673. rv += func()
  674. _translate_ch_to_exc(rv)
  675. return rv
  676. else:
  677. import termios
  678. import tty
  679. @contextlib.contextmanager
  680. def raw_terminal() -> cabc.Iterator[int]:
  681. f: t.TextIO | None
  682. fd: int
  683. if not isatty(sys.stdin):
  684. f = open("/dev/tty")
  685. fd = f.fileno()
  686. else:
  687. fd = sys.stdin.fileno()
  688. f = None
  689. try:
  690. old_settings = termios.tcgetattr(fd)
  691. try:
  692. tty.setraw(fd)
  693. yield fd
  694. finally:
  695. termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
  696. sys.stdout.flush()
  697. if f is not None:
  698. f.close()
  699. except termios.error:
  700. pass
  701. def getchar(echo: bool) -> str:
  702. with raw_terminal() as fd:
  703. ch = os.read(fd, 32).decode(get_best_encoding(sys.stdin), "replace")
  704. if echo and isatty(sys.stdout):
  705. sys.stdout.write(ch)
  706. _translate_ch_to_exc(ch)
  707. return ch