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