Move Color to its own module

This commit is contained in:
Sijmen 2018-08-17 16:26:46 +02:00
parent 89c1dd4f73
commit 5d955608f6
4 changed files with 48 additions and 48 deletions

45
color.py Normal file
View file

@ -0,0 +1,45 @@
from typing import Any, Optional
class Color:
BLACK = 0
RED = 1
GREEN = 2
YELLOW = 3
BLUE = 4
MAGENTA = 5
CYAN = 6
WHITE = 7
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:
self.foreground = foreground
self.background = background
def print(self, *args: Any, end: str = '\n') -> None:
print(self.format(*args), end=end)
def format(self, *args: Any) -> str:
colors = ['0']
if self.foreground is not None:
colors.append(f"38;5;{self.foreground}")
if self.background is not None:
colors.append(f"48;5;{self.background}")
color_str = ';'.join(colors)
text = ' '.join(str(a) for a in args)
if not text:
return ''
return f'\033[{color_str}m{text}\033[0m'

View file

@ -1,6 +1,6 @@
import api.projects
from typing import Dict, Any
from util import Color
from color import Color
COLOR_TITLE = Color(Color.YELLOW)
COLOR_HEADER = Color(Color.CYAN)

View file

@ -1,9 +1,8 @@
#!/usr/bin/env python3
from commands import login, projects, stories # noqa
from commands.cli import cli
from config import Config
from commands import login, projects, stories # noqa
if __name__ == '__main__':
Config.read()

46
util.py
View file

@ -1,55 +1,11 @@
import commands.login
import shutil
import textwrap
from typing import Any, Callable, Optional
from typing import Any, Callable
from config import Config
class Color:
BLACK = 0
RED = 1
GREEN = 2
YELLOW = 3
BLUE = 4
MAGENTA = 5
CYAN = 6
WHITE = 7
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:
self.foreground = foreground
self.background = background
def print(self, *args: Any, end: str = '\n') -> None:
print(self.format(*args), end=end)
def format(self, *args: Any) -> str:
colors = ['0']
if self.foreground is not None:
colors.append(f"38;5;{self.foreground}")
if self.background is not None:
colors.append(f"48;5;{self.background}")
color_str = ';'.join(colors)
text = ' '.join(str(a) for a in args)
if not text:
return ''
return f'\033[{color_str}m{text}\033[0m'
def print_wrap(text: str, indent: str = '', end: str = '\n') -> None:
w, _ = shutil.get_terminal_size((80, 20))
if w > 72: