Refactor colors

This commit is contained in:
Sijmen 2018-08-17 14:55:27 +02:00
parent 87789805fc
commit 0068ec8d50
2 changed files with 36 additions and 27 deletions

View file

@ -1,5 +1,5 @@
import argparse
import click
import math
from collections import defaultdict
from datetime import datetime, timedelta
from typing import Any, DefaultDict, Dict, List, Sequence, Tuple, Optional
@ -10,7 +10,7 @@ import tabulate
import api.projects
import api.stories
from config import Config
from util import require_login
from util import require_login, Color
from . import COLOR_HEADER, _format_state, _get_persons
from ._stories_info import stories_info
@ -22,6 +22,16 @@ STATES = 'unstarted', 'planned', 'started', 'finished', 'delivered', 'accepted'
Persons = Dict[int, Dict[str, Any]]
Totals = DefaultDict[int, Dict[str, int]]
COLOR_PLANNED = Color(Color.BRIGHT_BLACK)
COLOR_STARTED = Color(226)
COLOR_FINISHED = Color(Color.BRIGHT_BLUE)
COLOR_DELIVERED = Color(208)
COLOR_ACCEPTED = Color(Color.BRIGHT_GREEN)
def __ceil(value: float) -> int:
return int(math.ceil(value))
def __get_row(item: Tuple[int, Dict[str, int]], persons: Persons,
show_accepted: bool) -> Sequence:
@ -31,13 +41,13 @@ def __get_row(item: Tuple[int, Dict[str, int]], persons: Persons,
estimates = [points[state] for state in STATES]
progress = '['
progress += f'\033[90m' + 'P' * round(points['planned'])
progress += f'\033[38;5;226m' + 'S' * round(points['started'])
progress += f'\033[94m' + 'F' * round(points['finished'])
progress += f'\033[38;5;208m' + 'D' * round(points['delivered'])
progress += COLOR_PLANNED.format('P' * __ceil(points['planned']))
progress += COLOR_STARTED.format('S' * __ceil(points['started']))
progress += COLOR_FINISHED.format('F' * __ceil(points['finished']))
progress += COLOR_DELIVERED.format('D' * __ceil(points['delivered']))
if show_accepted:
progress += f'\033[92m' + 'A' * round(points['accepted'])
progress += '\033[0m]'
progress += COLOR_ACCEPTED.format('A' * __ceil(points['accepted']))
progress += ']'
return name, (*estimates), sum(estimates), progress

37
util.py
View file

@ -7,24 +7,23 @@ from config import Config
class Color:
BLACK = 30
RED = 31
GREEN = 32
YELLOW = 33
BLUE = 34
MAGENTA = 35
CYAN = 36
WHITE = 37
BRIGHT_BLACK = 90
BRIGHT_RED = 91
BRIGHT_GREEN = 92
BRIGHT_YELLOW = 93
BRIGHT_BLUE = 94
BRIGHT_MAGENTA = 95
BRIGHT_CYAN = 96
BRIGHT_WHITE = 97
BLACK = 0
RED = 1
GREEN = 2
YELLOW = 3
BLUE = 4
MAGENTA = 5
CYAN = 6
WHITE = 7
RESET = 0
BRIGHT_BLACK = 8
BRIGHT_RED = 9
BRIGHT_GREEN = 10
BRIGHT_YELLOW = 11
BRIGHT_BLUE = 12
BRIGHT_MAGENTA = 13
BRIGHT_CYAN = 14
BRIGHT_WHITE = 15
def __init__(self, foreground: Optional[int],
background: Optional[int] = None) -> None:
@ -38,10 +37,10 @@ class Color:
colors = ['0']
if self.foreground is not None:
colors.append(str(self.foreground))
colors.append(f"38;5;{self.foreground}")
if self.background is not None:
colors.append(str(self.background + 10))
colors.append(f"48;5;{self.background}")
color_str = ';'.join(colors)
text = ' '.join(str(a) for a in args)