termui.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. from __future__ import annotations
  2. import collections.abc as cabc
  3. import inspect
  4. import io
  5. import itertools
  6. import sys
  7. import typing as t
  8. from contextlib import AbstractContextManager
  9. from gettext import gettext as _
  10. from ._compat import isatty
  11. from ._compat import strip_ansi
  12. from .exceptions import Abort
  13. from .exceptions import UsageError
  14. from .globals import resolve_color_default
  15. from .types import Choice
  16. from .types import convert_type
  17. from .types import ParamType
  18. from .utils import echo
  19. from .utils import LazyFile
  20. if t.TYPE_CHECKING:
  21. from ._termui_impl import ProgressBar
  22. V = t.TypeVar("V")
  23. # The prompt functions to use. The doc tools currently override these
  24. # functions to customize how they work.
  25. visible_prompt_func: t.Callable[[str], str] = input
  26. _ansi_colors = {
  27. "black": 30,
  28. "red": 31,
  29. "green": 32,
  30. "yellow": 33,
  31. "blue": 34,
  32. "magenta": 35,
  33. "cyan": 36,
  34. "white": 37,
  35. "reset": 39,
  36. "bright_black": 90,
  37. "bright_red": 91,
  38. "bright_green": 92,
  39. "bright_yellow": 93,
  40. "bright_blue": 94,
  41. "bright_magenta": 95,
  42. "bright_cyan": 96,
  43. "bright_white": 97,
  44. }
  45. _ansi_reset_all = "\033[0m"
  46. def hidden_prompt_func(prompt: str) -> str:
  47. import getpass
  48. return getpass.getpass(prompt)
  49. def _build_prompt(
  50. text: str,
  51. suffix: str,
  52. show_default: bool = False,
  53. default: t.Any | None = None,
  54. show_choices: bool = True,
  55. type: ParamType | None = None,
  56. ) -> str:
  57. prompt = text
  58. if type is not None and show_choices and isinstance(type, Choice):
  59. prompt += f" ({', '.join(map(str, type.choices))})"
  60. if default is not None and show_default:
  61. prompt = f"{prompt} [{_format_default(default)}]"
  62. return f"{prompt}{suffix}"
  63. def _format_default(default: t.Any) -> t.Any:
  64. if isinstance(default, (io.IOBase, LazyFile)) and hasattr(default, "name"):
  65. return default.name
  66. return default
  67. def prompt(
  68. text: str,
  69. default: t.Any | None = None,
  70. hide_input: bool = False,
  71. confirmation_prompt: bool | str = False,
  72. type: ParamType | t.Any | None = None,
  73. value_proc: t.Callable[[str], t.Any] | None = None,
  74. prompt_suffix: str = ": ",
  75. show_default: bool = True,
  76. err: bool = False,
  77. show_choices: bool = True,
  78. ) -> t.Any:
  79. """Prompts a user for input. This is a convenience function that can
  80. be used to prompt a user for input later.
  81. If the user aborts the input by sending an interrupt signal, this
  82. function will catch it and raise a :exc:`Abort` exception.
  83. :param text: the text to show for the prompt.
  84. :param default: the default value to use if no input happens. If this
  85. is not given it will prompt until it's aborted.
  86. :param hide_input: if this is set to true then the input value will
  87. be hidden.
  88. :param confirmation_prompt: Prompt a second time to confirm the
  89. value. Can be set to a string instead of ``True`` to customize
  90. the message.
  91. :param type: the type to use to check the value against.
  92. :param value_proc: if this parameter is provided it's a function that
  93. is invoked instead of the type conversion to
  94. convert a value.
  95. :param prompt_suffix: a suffix that should be added to the prompt.
  96. :param show_default: shows or hides the default value in the prompt.
  97. :param err: if set to true the file defaults to ``stderr`` instead of
  98. ``stdout``, the same as with echo.
  99. :param show_choices: Show or hide choices if the passed type is a Choice.
  100. For example if type is a Choice of either day or week,
  101. show_choices is true and text is "Group by" then the
  102. prompt will be "Group by (day, week): ".
  103. .. versionchanged:: 8.3.1
  104. A space is no longer appended to the prompt.
  105. .. versionadded:: 8.0
  106. ``confirmation_prompt`` can be a custom string.
  107. .. versionadded:: 7.0
  108. Added the ``show_choices`` parameter.
  109. .. versionadded:: 6.0
  110. Added unicode support for cmd.exe on Windows.
  111. .. versionadded:: 4.0
  112. Added the `err` parameter.
  113. """
  114. def prompt_func(text: str) -> str:
  115. f = hidden_prompt_func if hide_input else visible_prompt_func
  116. try:
  117. # Write the prompt separately so that we get nice
  118. # coloring through colorama on Windows
  119. echo(text[:-1], nl=False, err=err)
  120. # Echo the last character to stdout to work around an issue where
  121. # readline causes backspace to clear the whole line.
  122. return f(text[-1:])
  123. except (KeyboardInterrupt, EOFError):
  124. # getpass doesn't print a newline if the user aborts input with ^C.
  125. # Allegedly this behavior is inherited from getpass(3).
  126. # A doc bug has been filed at https://bugs.python.org/issue24711
  127. if hide_input:
  128. echo(None, err=err)
  129. raise Abort() from None
  130. if value_proc is None:
  131. value_proc = convert_type(type, default)
  132. prompt = _build_prompt(
  133. text, prompt_suffix, show_default, default, show_choices, type
  134. )
  135. if confirmation_prompt:
  136. if confirmation_prompt is True:
  137. confirmation_prompt = _("Repeat for confirmation")
  138. confirmation_prompt = _build_prompt(confirmation_prompt, prompt_suffix)
  139. while True:
  140. while True:
  141. value = prompt_func(prompt)
  142. if value:
  143. break
  144. elif default is not None:
  145. value = default
  146. break
  147. try:
  148. result = value_proc(value)
  149. except UsageError as e:
  150. if hide_input:
  151. echo(_("Error: The value you entered was invalid."), err=err)
  152. else:
  153. echo(_("Error: {e.message}").format(e=e), err=err)
  154. continue
  155. if not confirmation_prompt:
  156. return result
  157. while True:
  158. value2 = prompt_func(confirmation_prompt)
  159. is_empty = not value and not value2
  160. if value2 or is_empty:
  161. break
  162. if value == value2:
  163. return result
  164. echo(_("Error: The two entered values do not match."), err=err)
  165. def confirm(
  166. text: str,
  167. default: bool | None = False,
  168. abort: bool = False,
  169. prompt_suffix: str = ": ",
  170. show_default: bool = True,
  171. err: bool = False,
  172. ) -> bool:
  173. """Prompts for confirmation (yes/no question).
  174. If the user aborts the input by sending a interrupt signal this
  175. function will catch it and raise a :exc:`Abort` exception.
  176. :param text: the question to ask.
  177. :param default: The default value to use when no input is given. If
  178. ``None``, repeat until input is given.
  179. :param abort: if this is set to `True` a negative answer aborts the
  180. exception by raising :exc:`Abort`.
  181. :param prompt_suffix: a suffix that should be added to the prompt.
  182. :param show_default: shows or hides the default value in the prompt.
  183. :param err: if set to true the file defaults to ``stderr`` instead of
  184. ``stdout``, the same as with echo.
  185. .. versionchanged:: 8.3.1
  186. A space is no longer appended to the prompt.
  187. .. versionchanged:: 8.0
  188. Repeat until input is given if ``default`` is ``None``.
  189. .. versionadded:: 4.0
  190. Added the ``err`` parameter.
  191. """
  192. prompt = _build_prompt(
  193. text,
  194. prompt_suffix,
  195. show_default,
  196. "y/n" if default is None else ("Y/n" if default else "y/N"),
  197. )
  198. while True:
  199. try:
  200. # Write the prompt separately so that we get nice
  201. # coloring through colorama on Windows
  202. echo(prompt[:-1], nl=False, err=err)
  203. # Echo the last character to stdout to work around an issue where
  204. # readline causes backspace to clear the whole line.
  205. value = visible_prompt_func(prompt[-1:]).lower().strip()
  206. except (KeyboardInterrupt, EOFError):
  207. raise Abort() from None
  208. if value in ("y", "yes"):
  209. rv = True
  210. elif value in ("n", "no"):
  211. rv = False
  212. elif default is not None and value == "":
  213. rv = default
  214. else:
  215. echo(_("Error: invalid input"), err=err)
  216. continue
  217. break
  218. if abort and not rv:
  219. raise Abort()
  220. return rv
  221. def echo_via_pager(
  222. text_or_generator: cabc.Iterable[str] | t.Callable[[], cabc.Iterable[str]] | str,
  223. color: bool | None = None,
  224. ) -> None:
  225. """This function takes a text and shows it via an environment specific
  226. pager on stdout.
  227. .. versionchanged:: 3.0
  228. Added the `color` flag.
  229. :param text_or_generator: the text to page, or alternatively, a
  230. generator emitting the text to page.
  231. :param color: controls if the pager supports ANSI colors or not. The
  232. default is autodetection.
  233. """
  234. color = resolve_color_default(color)
  235. if inspect.isgeneratorfunction(text_or_generator):
  236. i = t.cast("t.Callable[[], cabc.Iterable[str]]", text_or_generator)()
  237. elif isinstance(text_or_generator, str):
  238. i = [text_or_generator]
  239. else:
  240. i = iter(t.cast("cabc.Iterable[str]", text_or_generator))
  241. # convert every element of i to a text type if necessary
  242. text_generator = (el if isinstance(el, str) else str(el) for el in i)
  243. from ._termui_impl import pager
  244. return pager(itertools.chain(text_generator, "\n"), color)
  245. @t.overload
  246. def progressbar(
  247. *,
  248. length: int,
  249. label: str | None = None,
  250. hidden: bool = False,
  251. show_eta: bool = True,
  252. show_percent: bool | None = None,
  253. show_pos: bool = False,
  254. fill_char: str = "#",
  255. empty_char: str = "-",
  256. bar_template: str = "%(label)s [%(bar)s] %(info)s",
  257. info_sep: str = " ",
  258. width: int = 36,
  259. file: t.TextIO | None = None,
  260. color: bool | None = None,
  261. update_min_steps: int = 1,
  262. ) -> ProgressBar[int]: ...
  263. @t.overload
  264. def progressbar(
  265. iterable: cabc.Iterable[V] | None = None,
  266. length: int | None = None,
  267. label: str | None = None,
  268. hidden: bool = False,
  269. show_eta: bool = True,
  270. show_percent: bool | None = None,
  271. show_pos: bool = False,
  272. item_show_func: t.Callable[[V | None], str | None] | None = None,
  273. fill_char: str = "#",
  274. empty_char: str = "-",
  275. bar_template: str = "%(label)s [%(bar)s] %(info)s",
  276. info_sep: str = " ",
  277. width: int = 36,
  278. file: t.TextIO | None = None,
  279. color: bool | None = None,
  280. update_min_steps: int = 1,
  281. ) -> ProgressBar[V]: ...
  282. def progressbar(
  283. iterable: cabc.Iterable[V] | None = None,
  284. length: int | None = None,
  285. label: str | None = None,
  286. hidden: bool = False,
  287. show_eta: bool = True,
  288. show_percent: bool | None = None,
  289. show_pos: bool = False,
  290. item_show_func: t.Callable[[V | None], str | None] | None = None,
  291. fill_char: str = "#",
  292. empty_char: str = "-",
  293. bar_template: str = "%(label)s [%(bar)s] %(info)s",
  294. info_sep: str = " ",
  295. width: int = 36,
  296. file: t.TextIO | None = None,
  297. color: bool | None = None,
  298. update_min_steps: int = 1,
  299. ) -> ProgressBar[V]:
  300. """This function creates an iterable context manager that can be used
  301. to iterate over something while showing a progress bar. It will
  302. either iterate over the `iterable` or `length` items (that are counted
  303. up). While iteration happens, this function will print a rendered
  304. progress bar to the given `file` (defaults to stdout) and will attempt
  305. to calculate remaining time and more. By default, this progress bar
  306. will not be rendered if the file is not a terminal.
  307. The context manager creates the progress bar. When the context
  308. manager is entered the progress bar is already created. With every
  309. iteration over the progress bar, the iterable passed to the bar is
  310. advanced and the bar is updated. When the context manager exits,
  311. a newline is printed and the progress bar is finalized on screen.
  312. Note: The progress bar is currently designed for use cases where the
  313. total progress can be expected to take at least several seconds.
  314. Because of this, the ProgressBar class object won't display
  315. progress that is considered too fast, and progress where the time
  316. between steps is less than a second.
  317. No printing must happen or the progress bar will be unintentionally
  318. destroyed.
  319. Example usage::
  320. with progressbar(items) as bar:
  321. for item in bar:
  322. do_something_with(item)
  323. Alternatively, if no iterable is specified, one can manually update the
  324. progress bar through the `update()` method instead of directly
  325. iterating over the progress bar. The update method accepts the number
  326. of steps to increment the bar with::
  327. with progressbar(length=chunks.total_bytes) as bar:
  328. for chunk in chunks:
  329. process_chunk(chunk)
  330. bar.update(chunks.bytes)
  331. The ``update()`` method also takes an optional value specifying the
  332. ``current_item`` at the new position. This is useful when used
  333. together with ``item_show_func`` to customize the output for each
  334. manual step::
  335. with click.progressbar(
  336. length=total_size,
  337. label='Unzipping archive',
  338. item_show_func=lambda a: a.filename
  339. ) as bar:
  340. for archive in zip_file:
  341. archive.extract()
  342. bar.update(archive.size, archive)
  343. :param iterable: an iterable to iterate over. If not provided the length
  344. is required.
  345. :param length: the number of items to iterate over. By default the
  346. progressbar will attempt to ask the iterator about its
  347. length, which might or might not work. If an iterable is
  348. also provided this parameter can be used to override the
  349. length. If an iterable is not provided the progress bar
  350. will iterate over a range of that length.
  351. :param label: the label to show next to the progress bar.
  352. :param hidden: hide the progressbar. Defaults to ``False``. When no tty is
  353. detected, it will only print the progressbar label. Setting this to
  354. ``False`` also disables that.
  355. :param show_eta: enables or disables the estimated time display. This is
  356. automatically disabled if the length cannot be
  357. determined.
  358. :param show_percent: enables or disables the percentage display. The
  359. default is `True` if the iterable has a length or
  360. `False` if not.
  361. :param show_pos: enables or disables the absolute position display. The
  362. default is `False`.
  363. :param item_show_func: A function called with the current item which
  364. can return a string to show next to the progress bar. If the
  365. function returns ``None`` nothing is shown. The current item can
  366. be ``None``, such as when entering and exiting the bar.
  367. :param fill_char: the character to use to show the filled part of the
  368. progress bar.
  369. :param empty_char: the character to use to show the non-filled part of
  370. the progress bar.
  371. :param bar_template: the format string to use as template for the bar.
  372. The parameters in it are ``label`` for the label,
  373. ``bar`` for the progress bar and ``info`` for the
  374. info section.
  375. :param info_sep: the separator between multiple info items (eta etc.)
  376. :param width: the width of the progress bar in characters, 0 means full
  377. terminal width
  378. :param file: The file to write to. If this is not a terminal then
  379. only the label is printed.
  380. :param color: controls if the terminal supports ANSI colors or not. The
  381. default is autodetection. This is only needed if ANSI
  382. codes are included anywhere in the progress bar output
  383. which is not the case by default.
  384. :param update_min_steps: Render only when this many updates have
  385. completed. This allows tuning for very fast iterators.
  386. .. versionadded:: 8.2
  387. The ``hidden`` argument.
  388. .. versionchanged:: 8.0
  389. Output is shown even if execution time is less than 0.5 seconds.
  390. .. versionchanged:: 8.0
  391. ``item_show_func`` shows the current item, not the previous one.
  392. .. versionchanged:: 8.0
  393. Labels are echoed if the output is not a TTY. Reverts a change
  394. in 7.0 that removed all output.
  395. .. versionadded:: 8.0
  396. The ``update_min_steps`` parameter.
  397. .. versionadded:: 4.0
  398. The ``color`` parameter and ``update`` method.
  399. .. versionadded:: 2.0
  400. """
  401. from ._termui_impl import ProgressBar
  402. color = resolve_color_default(color)
  403. return ProgressBar(
  404. iterable=iterable,
  405. length=length,
  406. hidden=hidden,
  407. show_eta=show_eta,
  408. show_percent=show_percent,
  409. show_pos=show_pos,
  410. item_show_func=item_show_func,
  411. fill_char=fill_char,
  412. empty_char=empty_char,
  413. bar_template=bar_template,
  414. info_sep=info_sep,
  415. file=file,
  416. label=label,
  417. width=width,
  418. color=color,
  419. update_min_steps=update_min_steps,
  420. )
  421. def clear() -> None:
  422. """Clears the terminal screen. This will have the effect of clearing
  423. the whole visible space of the terminal and moving the cursor to the
  424. top left. This does not do anything if not connected to a terminal.
  425. .. versionadded:: 2.0
  426. """
  427. if not isatty(sys.stdout):
  428. return
  429. # ANSI escape \033[2J clears the screen, \033[1;1H moves the cursor
  430. echo("\033[2J\033[1;1H", nl=False)
  431. def _interpret_color(color: int | tuple[int, int, int] | str, offset: int = 0) -> str:
  432. if isinstance(color, int):
  433. return f"{38 + offset};5;{color:d}"
  434. if isinstance(color, (tuple, list)):
  435. r, g, b = color
  436. return f"{38 + offset};2;{r:d};{g:d};{b:d}"
  437. return str(_ansi_colors[color] + offset)
  438. def style(
  439. text: t.Any,
  440. fg: int | tuple[int, int, int] | str | None = None,
  441. bg: int | tuple[int, int, int] | str | None = None,
  442. bold: bool | None = None,
  443. dim: bool | None = None,
  444. underline: bool | None = None,
  445. overline: bool | None = None,
  446. italic: bool | None = None,
  447. blink: bool | None = None,
  448. reverse: bool | None = None,
  449. strikethrough: bool | None = None,
  450. reset: bool = True,
  451. ) -> str:
  452. """Styles a text with ANSI styles and returns the new string. By
  453. default the styling is self contained which means that at the end
  454. of the string a reset code is issued. This can be prevented by
  455. passing ``reset=False``.
  456. Examples::
  457. click.echo(click.style('Hello World!', fg='green'))
  458. click.echo(click.style('ATTENTION!', blink=True))
  459. click.echo(click.style('Some things', reverse=True, fg='cyan'))
  460. click.echo(click.style('More colors', fg=(255, 12, 128), bg=117))
  461. Supported color names:
  462. * ``black`` (might be a gray)
  463. * ``red``
  464. * ``green``
  465. * ``yellow`` (might be an orange)
  466. * ``blue``
  467. * ``magenta``
  468. * ``cyan``
  469. * ``white`` (might be light gray)
  470. * ``bright_black``
  471. * ``bright_red``
  472. * ``bright_green``
  473. * ``bright_yellow``
  474. * ``bright_blue``
  475. * ``bright_magenta``
  476. * ``bright_cyan``
  477. * ``bright_white``
  478. * ``reset`` (reset the color code only)
  479. If the terminal supports it, color may also be specified as:
  480. - An integer in the interval [0, 255]. The terminal must support
  481. 8-bit/256-color mode.
  482. - An RGB tuple of three integers in [0, 255]. The terminal must
  483. support 24-bit/true-color mode.
  484. See https://en.wikipedia.org/wiki/ANSI_color and
  485. https://gist.github.com/XVilka/8346728 for more information.
  486. :param text: the string to style with ansi codes.
  487. :param fg: if provided this will become the foreground color.
  488. :param bg: if provided this will become the background color.
  489. :param bold: if provided this will enable or disable bold mode.
  490. :param dim: if provided this will enable or disable dim mode. This is
  491. badly supported.
  492. :param underline: if provided this will enable or disable underline.
  493. :param overline: if provided this will enable or disable overline.
  494. :param italic: if provided this will enable or disable italic.
  495. :param blink: if provided this will enable or disable blinking.
  496. :param reverse: if provided this will enable or disable inverse
  497. rendering (foreground becomes background and the
  498. other way round).
  499. :param strikethrough: if provided this will enable or disable
  500. striking through text.
  501. :param reset: by default a reset-all code is added at the end of the
  502. string which means that styles do not carry over. This
  503. can be disabled to compose styles.
  504. .. versionchanged:: 8.0
  505. A non-string ``message`` is converted to a string.
  506. .. versionchanged:: 8.0
  507. Added support for 256 and RGB color codes.
  508. .. versionchanged:: 8.0
  509. Added the ``strikethrough``, ``italic``, and ``overline``
  510. parameters.
  511. .. versionchanged:: 7.0
  512. Added support for bright colors.
  513. .. versionadded:: 2.0
  514. """
  515. if not isinstance(text, str):
  516. text = str(text)
  517. bits = []
  518. if fg:
  519. try:
  520. bits.append(f"\033[{_interpret_color(fg)}m")
  521. except KeyError:
  522. raise TypeError(f"Unknown color {fg!r}") from None
  523. if bg:
  524. try:
  525. bits.append(f"\033[{_interpret_color(bg, 10)}m")
  526. except KeyError:
  527. raise TypeError(f"Unknown color {bg!r}") from None
  528. if bold is not None:
  529. bits.append(f"\033[{1 if bold else 22}m")
  530. if dim is not None:
  531. bits.append(f"\033[{2 if dim else 22}m")
  532. if underline is not None:
  533. bits.append(f"\033[{4 if underline else 24}m")
  534. if overline is not None:
  535. bits.append(f"\033[{53 if overline else 55}m")
  536. if italic is not None:
  537. bits.append(f"\033[{3 if italic else 23}m")
  538. if blink is not None:
  539. bits.append(f"\033[{5 if blink else 25}m")
  540. if reverse is not None:
  541. bits.append(f"\033[{7 if reverse else 27}m")
  542. if strikethrough is not None:
  543. bits.append(f"\033[{9 if strikethrough else 29}m")
  544. bits.append(text)
  545. if reset:
  546. bits.append(_ansi_reset_all)
  547. return "".join(bits)
  548. def unstyle(text: str) -> str:
  549. """Removes ANSI styling information from a string. Usually it's not
  550. necessary to use this function as Click's echo function will
  551. automatically remove styling if necessary.
  552. .. versionadded:: 2.0
  553. :param text: the text to remove style information from.
  554. """
  555. return strip_ansi(text)
  556. def secho(
  557. message: t.Any | None = None,
  558. file: t.IO[t.AnyStr] | None = None,
  559. nl: bool = True,
  560. err: bool = False,
  561. color: bool | None = None,
  562. **styles: t.Any,
  563. ) -> None:
  564. """This function combines :func:`echo` and :func:`style` into one
  565. call. As such the following two calls are the same::
  566. click.secho('Hello World!', fg='green')
  567. click.echo(click.style('Hello World!', fg='green'))
  568. All keyword arguments are forwarded to the underlying functions
  569. depending on which one they go with.
  570. Non-string types will be converted to :class:`str`. However,
  571. :class:`bytes` are passed directly to :meth:`echo` without applying
  572. style. If you want to style bytes that represent text, call
  573. :meth:`bytes.decode` first.
  574. .. versionchanged:: 8.0
  575. A non-string ``message`` is converted to a string. Bytes are
  576. passed through without style applied.
  577. .. versionadded:: 2.0
  578. """
  579. if message is not None and not isinstance(message, (bytes, bytearray)):
  580. message = style(message, **styles)
  581. return echo(message, file=file, nl=nl, err=err, color=color)
  582. @t.overload
  583. def edit(
  584. text: bytes | bytearray,
  585. editor: str | None = None,
  586. env: cabc.Mapping[str, str] | None = None,
  587. require_save: bool = False,
  588. extension: str = ".txt",
  589. ) -> bytes | None: ...
  590. @t.overload
  591. def edit(
  592. text: str,
  593. editor: str | None = None,
  594. env: cabc.Mapping[str, str] | None = None,
  595. require_save: bool = True,
  596. extension: str = ".txt",
  597. ) -> str | None: ...
  598. @t.overload
  599. def edit(
  600. text: None = None,
  601. editor: str | None = None,
  602. env: cabc.Mapping[str, str] | None = None,
  603. require_save: bool = True,
  604. extension: str = ".txt",
  605. filename: str | cabc.Iterable[str] | None = None,
  606. ) -> None: ...
  607. def edit(
  608. text: str | bytes | bytearray | None = None,
  609. editor: str | None = None,
  610. env: cabc.Mapping[str, str] | None = None,
  611. require_save: bool = True,
  612. extension: str = ".txt",
  613. filename: str | cabc.Iterable[str] | None = None,
  614. ) -> str | bytes | bytearray | None:
  615. r"""Edits the given text in the defined editor. If an editor is given
  616. (should be the full path to the executable but the regular operating
  617. system search path is used for finding the executable) it overrides
  618. the detected editor. Optionally, some environment variables can be
  619. used. If the editor is closed without changes, `None` is returned. In
  620. case a file is edited directly the return value is always `None` and
  621. `require_save` and `extension` are ignored.
  622. If the editor cannot be opened a :exc:`UsageError` is raised.
  623. Note for Windows: to simplify cross-platform usage, the newlines are
  624. automatically converted from POSIX to Windows and vice versa. As such,
  625. the message here will have ``\n`` as newline markers.
  626. :param text: the text to edit.
  627. :param editor: optionally the editor to use. Defaults to automatic
  628. detection.
  629. :param env: environment variables to forward to the editor.
  630. :param require_save: if this is true, then not saving in the editor
  631. will make the return value become `None`.
  632. :param extension: the extension to tell the editor about. This defaults
  633. to `.txt` but changing this might change syntax
  634. highlighting.
  635. :param filename: if provided it will edit this file instead of the
  636. provided text contents. It will not use a temporary
  637. file as an indirection in that case. If the editor supports
  638. editing multiple files at once, a sequence of files may be
  639. passed as well. Invoke `click.file` once per file instead
  640. if multiple files cannot be managed at once or editing the
  641. files serially is desired.
  642. .. versionchanged:: 8.2.0
  643. ``filename`` now accepts any ``Iterable[str]`` in addition to a ``str``
  644. if the ``editor`` supports editing multiple files at once.
  645. """
  646. from ._termui_impl import Editor
  647. ed = Editor(editor=editor, env=env, require_save=require_save, extension=extension)
  648. if filename is None:
  649. return ed.edit(text)
  650. if isinstance(filename, str):
  651. filename = (filename,)
  652. ed.edit_files(filenames=filename)
  653. return None
  654. def launch(url: str, wait: bool = False, locate: bool = False) -> int:
  655. """This function launches the given URL (or filename) in the default
  656. viewer application for this file type. If this is an executable, it
  657. might launch the executable in a new session. The return value is
  658. the exit code of the launched application. Usually, ``0`` indicates
  659. success.
  660. Examples::
  661. click.launch('https://click.palletsprojects.com/')
  662. click.launch('/my/downloaded/file', locate=True)
  663. .. versionadded:: 2.0
  664. :param url: URL or filename of the thing to launch.
  665. :param wait: Wait for the program to exit before returning. This
  666. only works if the launched program blocks. In particular,
  667. ``xdg-open`` on Linux does not block.
  668. :param locate: if this is set to `True` then instead of launching the
  669. application associated with the URL it will attempt to
  670. launch a file manager with the file located. This
  671. might have weird effects if the URL does not point to
  672. the filesystem.
  673. """
  674. from ._termui_impl import open_url
  675. return open_url(url, wait=wait, locate=locate)
  676. # If this is provided, getchar() calls into this instead. This is used
  677. # for unittesting purposes.
  678. _getchar: t.Callable[[bool], str] | None = None
  679. def getchar(echo: bool = False) -> str:
  680. """Fetches a single character from the terminal and returns it. This
  681. will always return a unicode character and under certain rare
  682. circumstances this might return more than one character. The
  683. situations which more than one character is returned is when for
  684. whatever reason multiple characters end up in the terminal buffer or
  685. standard input was not actually a terminal.
  686. Note that this will always read from the terminal, even if something
  687. is piped into the standard input.
  688. Note for Windows: in rare cases when typing non-ASCII characters, this
  689. function might wait for a second character and then return both at once.
  690. This is because certain Unicode characters look like special-key markers.
  691. .. versionadded:: 2.0
  692. :param echo: if set to `True`, the character read will also show up on
  693. the terminal. The default is to not show it.
  694. """
  695. global _getchar
  696. if _getchar is None:
  697. from ._termui_impl import getchar as f
  698. _getchar = f
  699. return _getchar(echo)
  700. def raw_terminal() -> AbstractContextManager[int]:
  701. from ._termui_impl import raw_terminal as f
  702. return f()
  703. def pause(info: str | None = None, err: bool = False) -> None:
  704. """This command stops execution and waits for the user to press any
  705. key to continue. This is similar to the Windows batch "pause"
  706. command. If the program is not run through a terminal, this command
  707. will instead do nothing.
  708. .. versionadded:: 2.0
  709. .. versionadded:: 4.0
  710. Added the `err` parameter.
  711. :param info: The message to print before pausing. Defaults to
  712. ``"Press any key to continue..."``.
  713. :param err: if set to message goes to ``stderr`` instead of
  714. ``stdout``, the same as with echo.
  715. """
  716. if not isatty(sys.stdin) or not isatty(sys.stdout):
  717. return
  718. if info is None:
  719. info = _("Press any key to continue...")
  720. try:
  721. if info:
  722. echo(info, nl=False, err=err)
  723. try:
  724. getchar()
  725. except (KeyboardInterrupt, EOFError):
  726. pass
  727. finally:
  728. if info:
  729. echo(err=err)