pytermgui.enums

This module provides commonly used enumerations for the library. It also has a class implementing Enum-s with default values. All Enums below subclass it, meaning you can use their get_default() methods to get the globally set default value.

To modify defaults, use the defaults dictionary.

  1"""
  2This module provides commonly used enumerations for the library.
  3It also has a class implementing Enum-s with default values. All Enums
  4below subclass it, meaning you can use their `get_default()` methods to get
  5the globally set default value.
  6
  7To modify defaults, use the `defaults` dictionary.
  8"""
  9
 10# This file is an absolute mess to mypy-correctly type.
 11# It is still typed well enough for a human to read, but
 12# I could not make mypy accept it without making the code
 13# horrible to read and edit.
 14#
 15# mypy: ignore-errors
 16
 17from __future__ import annotations
 18
 19from typing import Type
 20from enum import Enum, IntEnum, auto as _auto
 21
 22defaults: dict[IntEnum, Type[IntEnum]] = {}
 23
 24__all__ = [
 25    "SizePolicy",
 26    "CenteringPolicy",
 27    "SizePolicy",
 28    "HorizontalAlignment",
 29    "VerticalAlignment",
 30    "Overflow",
 31]
 32
 33
 34class DefaultEnum(IntEnum):
 35    """An Enum class that can return its default value."""
 36
 37    @classmethod
 38    def get_default(cls) -> IntEnum | None:
 39        """Get default value"""
 40
 41        return defaults.get(cls)
 42
 43
 44class SizePolicy(DefaultEnum):
 45    """Values according to which Widget sizes are assigned."""
 46
 47    FILL = 0
 48    """Inner widget will take up as much width as possible."""
 49
 50    STATIC = 1
 51    """Inner widget will take up an exact amount of width."""
 52
 53    RELATIVE = 2
 54    """Inner widget will take up widget.relative_width * available
 55    space."""
 56
 57
 58class CenteringPolicy(DefaultEnum):
 59    """Policies to center `Container` according to."""
 60
 61    ALL = _auto()
 62    VERTICAL = _auto()
 63    HORIZONTAL = _auto()
 64
 65
 66class HorizontalAlignment(DefaultEnum):
 67    """Policies to align widgets by.
 68
 69    These are applied by the parent object, and are
 70    relative to them."""
 71
 72    LEFT = 0
 73    """Align widget to the left edge."""
 74
 75    CENTER = 1
 76    """Center widget in the available width."""
 77
 78    RIGHT = 2
 79    """Align widget to the right edge."""
 80
 81
 82class VerticalAlignment(DefaultEnum):
 83    """Vertical alignment options for widgets."""
 84
 85    TOP = 0
 86    """Align widgets to the top"""
 87
 88    CENTER = 1
 89    """Align widgets in the center, with equal* padding on the top and bottom.
 90
 91    Note:
 92        When the available height is not divisible by 2, the extra line of padding
 93        is added to the bottom.
 94    """
 95
 96    BOTTOM = 2
 97    """Align widgets to the bottom."""
 98
 99
100class Overflow(DefaultEnum):
101    """Overflow policies implemented by Container."""
102
103    HIDE = 0
104    """Stop gathering lines once there is no room left."""
105
106    SCROLL = 1
107    """Allow scrolling when there is too many lines."""
108
109    RESIZE = 2
110    """Resize parent to fit with the new lines.
111
112    Note:
113        When applied to a window, this prevents resizing its height
114        using the bottom border.
115    """
116
117    # TODO: Implement Overflow.AUTO
118    AUTO = 9999
119    """NotImplemented"""
120
121
122class WidgetChange(Enum):
123    """The type of change that happened within a widget."""
124
125    LINES = _auto()
126    """The result of `get_lines` has changed, but size changes didn't happen."""
127
128    SIZE = _auto()
129    """Both WIDTH and HEIGHT has changed."""
130
131    WIDTH = _auto()
132    """The width of the widget changed, possibly involving LINES type changes."""
133
134    HEIGHT = _auto()
135    """The height of the widget changed, possibly involving LINES type changes."""
136
137
138defaults[SizePolicy] = SizePolicy.FILL
139defaults[CenteringPolicy] = CenteringPolicy.ALL
140defaults[HorizontalAlignment] = HorizontalAlignment.CENTER
141defaults[VerticalAlignment] = VerticalAlignment.CENTER
142defaults[Overflow] = Overflow.RESIZE
class SizePolicy(DefaultEnum):
45class SizePolicy(DefaultEnum):
46    """Values according to which Widget sizes are assigned."""
47
48    FILL = 0
49    """Inner widget will take up as much width as possible."""
50
51    STATIC = 1
52    """Inner widget will take up an exact amount of width."""
53
54    RELATIVE = 2
55    """Inner widget will take up widget.relative_width * available
56    space."""

Values according to which Widget sizes are assigned.

FILL = <SizePolicy.FILL: 0>

Inner widget will take up as much width as possible.

STATIC = <SizePolicy.STATIC: 1>

Inner widget will take up an exact amount of width.

RELATIVE = <SizePolicy.RELATIVE: 2>

Inner widget will take up widget.relative_width * available space.

Inherited Members
DefaultEnum
get_default
enum.Enum
name
value
builtins.int
conjugate
bit_length
bit_count
to_bytes
from_bytes
as_integer_ratio
real
imag
numerator
denominator
class CenteringPolicy(DefaultEnum):
59class CenteringPolicy(DefaultEnum):
60    """Policies to center `Container` according to."""
61
62    ALL = _auto()
63    VERTICAL = _auto()
64    HORIZONTAL = _auto()

Policies to center Container according to.

ALL = <CenteringPolicy.ALL: 1>
VERTICAL = <CenteringPolicy.VERTICAL: 2>
HORIZONTAL = <CenteringPolicy.HORIZONTAL: 3>
Inherited Members
DefaultEnum
get_default
enum.Enum
name
value
builtins.int
conjugate
bit_length
bit_count
to_bytes
from_bytes
as_integer_ratio
real
imag
numerator
denominator
class HorizontalAlignment(DefaultEnum):
67class HorizontalAlignment(DefaultEnum):
68    """Policies to align widgets by.
69
70    These are applied by the parent object, and are
71    relative to them."""
72
73    LEFT = 0
74    """Align widget to the left edge."""
75
76    CENTER = 1
77    """Center widget in the available width."""
78
79    RIGHT = 2
80    """Align widget to the right edge."""

Policies to align widgets by.

These are applied by the parent object, and are relative to them.

Align widget to the left edge.

Center widget in the available width.

Align widget to the right edge.

Inherited Members
DefaultEnum
get_default
enum.Enum
name
value
builtins.int
conjugate
bit_length
bit_count
to_bytes
from_bytes
as_integer_ratio
real
imag
numerator
denominator
class VerticalAlignment(DefaultEnum):
83class VerticalAlignment(DefaultEnum):
84    """Vertical alignment options for widgets."""
85
86    TOP = 0
87    """Align widgets to the top"""
88
89    CENTER = 1
90    """Align widgets in the center, with equal* padding on the top and bottom.
91
92    Note:
93        When the available height is not divisible by 2, the extra line of padding
94        is added to the bottom.
95    """
96
97    BOTTOM = 2
98    """Align widgets to the bottom."""

Vertical alignment options for widgets.

Align widgets to the top

CENTER = <VerticalAlignment.CENTER: 1>

Align widgets in the center, with equal* padding on the top and bottom.

Note

When the available height is not divisible by 2, the extra line of padding is added to the bottom.

BOTTOM = <VerticalAlignment.BOTTOM: 2>

Align widgets to the bottom.

Inherited Members
DefaultEnum
get_default
enum.Enum
name
value
builtins.int
conjugate
bit_length
bit_count
to_bytes
from_bytes
as_integer_ratio
real
imag
numerator
denominator
class Overflow(DefaultEnum):
101class Overflow(DefaultEnum):
102    """Overflow policies implemented by Container."""
103
104    HIDE = 0
105    """Stop gathering lines once there is no room left."""
106
107    SCROLL = 1
108    """Allow scrolling when there is too many lines."""
109
110    RESIZE = 2
111    """Resize parent to fit with the new lines.
112
113    Note:
114        When applied to a window, this prevents resizing its height
115        using the bottom border.
116    """
117
118    # TODO: Implement Overflow.AUTO
119    AUTO = 9999
120    """NotImplemented"""

Overflow policies implemented by Container.

HIDE = <Overflow.HIDE: 0>

Stop gathering lines once there is no room left.

SCROLL = <Overflow.SCROLL: 1>

Allow scrolling when there is too many lines.

RESIZE = <Overflow.RESIZE: 2>

Resize parent to fit with the new lines.

Note

When applied to a window, this prevents resizing its height using the bottom border.

AUTO = <Overflow.AUTO: 9999>

NotImplemented

Inherited Members
DefaultEnum
get_default
enum.Enum
name
value
builtins.int
conjugate
bit_length
bit_count
to_bytes
from_bytes
as_integer_ratio
real
imag
numerator
denominator