Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Color enum #20

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions dashing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@

__version__ = "0.1.0"

import abc
import contextlib
import itertools
from collections import deque, namedtuple
from typing import Literal, Optional, Tuple
from enum import IntEnum
from typing import Literal, Optional, Tuple, Union

from blessed import Terminal

Expand All @@ -82,21 +84,40 @@
braille_r_left = (0x04, 0x02, 0x01)
braille_r_right = (0x20, 0x10, 0x08)


class Color(IntEnum):
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(object):
def __init__(self, title: str = None, border_color: Color = None, color: Color = 0):
class Tile(abc.ABC):
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
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):
"""
Expand Down Expand Up @@ -227,7 +248,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
Expand Down Expand Up @@ -269,7 +290,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
Expand Down Expand Up @@ -306,7 +329,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

Expand Down