From 8038964564e7930dfc61f3e528fce228761ae75b Mon Sep 17 00:00:00 2001 From: Robin De Schepper Date: Tue, 12 Mar 2024 22:55:55 +0100 Subject: [PATCH 1/4] more py2 --- dashing/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dashing/__init__.py b/dashing/__init__.py index 59afc40..f7ed3c6 100644 --- a/dashing/__init__.py +++ b/dashing/__init__.py @@ -87,7 +87,7 @@ Colormap = Tuple[Tuple[float, Color], ...] -class Tile(object): +class Tile: def __init__(self, title: str = None, border_color: Color = None, color: Color = 0): self.title = title self.color = color From d9801e6dea0e9c9a33cdb068451b48e4882f8dc3 Mon Sep 17 00:00:00 2001 From: Robin De Schepper Date: Wed, 13 Mar 2024 11:45:27 +0100 Subject: [PATCH 2/4] change `Color` to an `Enum` for human readable labels, still accept ints --- dashing/__init__.py | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/dashing/__init__.py b/dashing/__init__.py index f7ed3c6..fe87d84 100644 --- a/dashing/__init__.py +++ b/dashing/__init__.py @@ -63,7 +63,8 @@ import contextlib import itertools from collections import deque, namedtuple -from typing import Literal, Optional, Tuple +from enum import Enum +from typing import Literal, Optional, Tuple, Union from blessed import Terminal @@ -82,13 +83,31 @@ braille_r_left = (0x04, 0x02, 0x01) braille_r_right = (0x20, 0x10, 0x08) + +class Color(Enum): + Black = 0 + Red = 1 + Green = 2 + Yellow = 3 + Blue = 4 + Magenta = 5 + Cyan = 6 + White = 7 + + +ColorLiteral = Literal[0, 1, 2, 3, 4, 5, 6, 7] +ColorValue = Union[Color, ColorLiteral] +Colormap = Tuple[Tuple[float, ColorValue], ...] TBox = namedtuple("TBox", "t x y w h") -Color = Literal[0, 1, 2, 3, 4, 5, 6, 7] -Colormap = Tuple[Tuple[float, Color], ...] class Tile: - def __init__(self, title: str = None, border_color: Color = None, color: Color = 0): + def __init__( + self, + title: str = None, + border_color: ColorValue = None, + color: ColorValue = Color.Black, + ): self.title = title self.color = color self.border_color = border_color @@ -227,7 +246,7 @@ class Text(Tile): """ - def __init__(self, text: str, color: Color = 0, **kw): + def __init__(self, text: str, color: ColorValue = Color.Black, **kw): super().__init__(**kw) self.text: str = text self.color = color @@ -269,7 +288,9 @@ def append(self, msg: str): class HGauge(Tile): """Horizontal gauge""" - def __init__(self, label: str = None, val=100, color: Color = 2, **kw): + def __init__( + self, label: str = None, val=100, color: ColorValue = Color.Green, **kw + ): super().__init__(color=color, **kw) self.value = val self.label = label @@ -306,7 +327,7 @@ def _display(self, tbox: TBox, parent: Optional[Tile]): class VGauge(Tile): """Vertical gauge""" - def __init__(self, val=100, color: Color = 2, **kw): + def __init__(self, val=100, color: ColorValue = Color.Green, **kw): super().__init__(color=color, **kw) self.value = val From 60d59818deced21a6c63b32f0967146aa1f90b2f Mon Sep 17 00:00:00 2001 From: Robin De Schepper Date: Wed, 13 Mar 2024 11:47:07 +0100 Subject: [PATCH 3/4] use an abstract base class to enforce `_display` interface --- dashing/__init__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dashing/__init__.py b/dashing/__init__.py index fe87d84..9cc11f3 100644 --- a/dashing/__init__.py +++ b/dashing/__init__.py @@ -60,6 +60,7 @@ __version__ = "0.1.0" +import abc import contextlib import itertools from collections import deque, namedtuple @@ -101,7 +102,7 @@ class Color(Enum): TBox = namedtuple("TBox", "t x y w h") -class Tile: +class Tile(abc.ABC): def __init__( self, title: str = None, @@ -113,9 +114,10 @@ def __init__( self.border_color = border_color self._terminal: Optional[Terminal] = None + @abc.abstractmethod def _display(self, tbox: TBox, parent: Optional["Tile"]): """Render current tile""" - raise NotImplementedError + pass def _draw_borders_and_title(self, tbox: TBox): """ From 32f1dec26bcf0cc0aa114d1034f56a1c330268bd Mon Sep 17 00:00:00 2001 From: Robin De Schepper Date: Fri, 22 Mar 2024 17:17:56 +0100 Subject: [PATCH 4/4] use intenum --- dashing/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dashing/__init__.py b/dashing/__init__.py index 9cc11f3..a33446f 100644 --- a/dashing/__init__.py +++ b/dashing/__init__.py @@ -64,7 +64,7 @@ import contextlib import itertools from collections import deque, namedtuple -from enum import Enum +from enum import IntEnum from typing import Literal, Optional, Tuple, Union from blessed import Terminal @@ -85,7 +85,7 @@ braille_r_right = (0x20, 0x10, 0x08) -class Color(Enum): +class Color(IntEnum): Black = 0 Red = 1 Green = 2