consolekit.terminal_colours
Functions for adding ANSI character codes to terminal print statements.
Classes:
|
ANSI Colour Codes for foreground colour. |
|
ANSI Colour Codes for background colour. |
|
ANSI Colour Codes for text style. |
Provides methods to control the cursor. |
|
alias of |
|
alias of |
|
|
Abstract base class for ANSI Codes. |
|
An ANSI escape sequence representing a colour. |
Data:
Functions:
|
Helper to get the default value of the color flag. |
|
Strip ANSI colour codes from the given string to return a plaintext output. |
-
class
AnsiFore
(*args, **kwargs)[source] Bases:
AnsiCodes
ANSI Colour Codes for foreground colour.
The colours can be used as a context manager, a string, or a function.
Valid values are:
BLACK
RED
GREEN
YELLOW
BLUE
MAGENTA
CYAN
WHITE
RESET
LIGHTBLACK_EX
LIGHTRED_EX
LIGHTGREEN_EX
LIGHTYELLOW_EX
LIGHTBLUE_EX
LIGHTMAGENTA_EX
LIGHTCYAN_EX
LIGHTWHITE_EX
This class is also available under the shorter alias
Fore
.
-
class
AnsiBack
(*args, **kwargs)[source] Bases:
AnsiCodes
ANSI Colour Codes for background colour.
The colours can be used as a context manager, a string, or a function.
Valid values are:
BLACK
RED
GREEN
YELLOW
BLUE
MAGENTA
CYAN
WHITE
RESET
LIGHTBLACK_EX
LIGHTRED_EX
LIGHTGREEN_EX
LIGHTYELLOW_EX
LIGHTBLUE_EX
LIGHTMAGENTA_EX
LIGHTCYAN_EX
LIGHTWHITE_EX
This class is also available under the shorter alias
Back
.
-
class
AnsiStyle
(*args, **kwargs)[source] Bases:
AnsiCodes
ANSI Colour Codes for text style.
Valid values are:
BRIGHT
DIM
NORMAL
Additionally,
AnsiStyle.RESET_ALL
can be used to reset the foreground and background colours as well as the text style.This class is also available under the shorter alias
Style
.
-
class
AnsiCursor
[source] Bases:
object
Provides methods to control the cursor.
Methods:
UP
([n])Moves the cursor up
n
lines.DOWN
([n])Moves the cursor down
n
lines.FORWARD
([n])Moves the cursor forward (right)
n
lines.BACK
([n])Moves the cursor backward (left)
n
lines.POS
([x, y])Moves the cursor to the given position.
HIDE
()Hides the cursor.
SHOW
()Shows the cursor.
-
Fore
alias of
consolekit.terminal_colours.AnsiFore
-
Back
alias of
consolekit.terminal_colours.AnsiBack
-
Style
-
class
Colour
(style: str, stack: Union[Deque[str], List[str]], reset: str)[source] Bases:
str
An ANSI escape sequence representing a colour.
The colour can be used as a context manager, a string, or a function.
- Parameters
Methods:
__call__
(text)Returns the given text in this colour.
from_code
(code[, background])Returns a
Colour
to create coloured text.from_256_code
(code[, background])Returns a
Colour
to create 256-colour text.from_rgb
(r, g, b[, background])Returns a
Colour
to create 24-bit coloured text.from_hex
(hex_colour[, background])Returns a
Colour
to create 24-bit coloured text.-
classmethod
from_code
(code, background=False)[source] Returns a
Colour
to create coloured text.The colour can be reset using
Fore.RESET
orBack.RESET
.New in version 0.9.0.
- Parameters
- Return type
Note
The
background
option only influences the reset value and the stack used. It will not handle conversion of foreground codes to background codes.
-
classmethod
from_256_code
(code, background=False)[source] Returns a
Colour
to create 256-colour text.The colour can be reset using
Fore.RESET
orBack.RESET
.New in version 0.9.0.
Note
Not all terminals support 256-colour mode.
- Parameters
- Return type
Note
The
background
option only influences the reset value and the stack used. It will not handle conversion of foreground codes to background codes.
-
classmethod
from_rgb
(r, g, b, background=False)[source] Returns a
Colour
to create 24-bit coloured text.The colour can be reset using
Fore.RESET
orBack.RESET
.New in version 0.9.0.
Note
Not all terminals support 24-bit colours.
- Parameters
- Return type
Note
The
background
option only influences the reset value and the stack used. It will not handle conversion of foreground codes to background codes.
-
classmethod
from_hex
(hex_colour, background=False)[source] Returns a
Colour
to create 24-bit coloured text.The colour can be reset using
Fore.RESET
orBack.RESET
.New in version 0.9.0.
Note
Not all terminals support 24-bit colours.
- Parameters
- Return type
Note
The
background
option only influences the reset value and the stack used. It will not handle conversion of foreground codes to background codes.
-
resolve_color_default
(color=None)[source] Helper to get the default value of the color flag.
If a value is passed it is returned unchanged, otherwise it’s looked up from the current context.
If the environment variable
PYCHARM_HOSTED
is1
(which is the case if running in PyCharm) the output will be coloured by default.If the environment variable
NO_COLOR
is1
the output will not be coloured by default. See https://no-color.org/ for more details. This variable takes precedence overPYCHARM_HOSTED
.If no value is passed in, there is no context, and neither environment variable is set,
None
is returned.Changed in version 1.3.0:Added support for the
NO_COLOR
environment variable.Only uses a value from the click context (
Context.color
) if it is notNone
. Otherwise falls back to checking the environment variables.