MCPcopy
hub / github.com/fastapi/typer / style

Function style

typer/_click/termui.py:312–367  ·  view source on GitHub ↗

Styles a text with ANSI styles and returns the new string. By default the styling is self contained which means that at the end of the string a reset code is issued. This can be prevented by passing ``reset=False``.

(
    text: Any,
    fg: int | tuple[int, int, int] | str | None = None,
    bg: int | tuple[int, int, int] | str | None = None,
    bold: bool | None = None,
    dim: bool | None = None,
    underline: bool | None = None,
    overline: bool | None = None,
    italic: bool | None = None,
    blink: bool | None = None,
    reverse: bool | None = None,
    strikethrough: bool | None = None,
    reset: bool = True,
)

Source from the content-addressed store, hash-verified

310
311
312def style(
313 text: Any,
314 fg: int | tuple[int, int, int] | str | None = None,
315 bg: int | tuple[int, int, int] | str | None = None,
316 bold: bool | None = None,
317 dim: bool | None = None,
318 underline: bool | None = None,
319 overline: bool | None = None,
320 italic: bool | None = None,
321 blink: bool | None = None,
322 reverse: bool | None = None,
323 strikethrough: bool | None = None,
324 reset: bool = True,
325) -> str:
326 """Styles a text with ANSI styles and returns the new string. By
327 default the styling is self contained which means that at the end
328 of the string a reset code is issued. This can be prevented by
329 passing ``reset=False``.
330 """
331 if not isinstance(text, str):
332 text = str(text)
333
334 bits = []
335
336 if fg:
337 try:
338 bits.append(f"\033[{_interpret_color(fg)}m")
339 except KeyError:
340 raise TypeError(f"Unknown color {fg!r}") from None
341
342 if bg:
343 try:
344 bits.append(f"\033[{_interpret_color(bg, 10)}m")
345 except KeyError:
346 raise TypeError(f"Unknown color {bg!r}") from None
347
348 if bold is not None:
349 bits.append(f"\033[{1 if bold else 22}m")
350 if dim is not None:
351 bits.append(f"\033[{2 if dim else 22}m")
352 if underline is not None:
353 bits.append(f"\033[{4 if underline else 24}m")
354 if overline is not None:
355 bits.append(f"\033[{53 if overline else 55}m")
356 if italic is not None:
357 bits.append(f"\033[{3 if italic else 23}m")
358 if blink is not None:
359 bits.append(f"\033[{5 if blink else 25}m")
360 if reverse is not None:
361 bits.append(f"\033[{7 if reverse else 27}m")
362 if strikethrough is not None:
363 bits.append(f"\033[{9 if strikethrough else 29}m")
364 bits.append(text)
365 if reset:
366 bits.append(_ansi_reset_all)
367 return "".join(bits)
368
369

Callers 2

invokeMethod · 0.85
sechoFunction · 0.85

Calls 1

_interpret_colorFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…