Added console module
This commit is contained in:
@@ -1,11 +0,0 @@
|
||||
from termcolor import colored
|
||||
|
||||
|
||||
class Console:
|
||||
|
||||
@staticmethod
|
||||
def write_line(string: str, color: str = None):
|
||||
if color is not None:
|
||||
print(colored(string, color))
|
||||
else:
|
||||
print(string)
|
3
src/sh_edraft/utils/console/__init__.py
Normal file
3
src/sh_edraft/utils/console/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
# imports:
|
||||
|
||||
from .console import Console
|
76
src/sh_edraft/utils/console/console.py
Normal file
76
src/sh_edraft/utils/console/console.py
Normal file
@@ -0,0 +1,76 @@
|
||||
import os
|
||||
import subprocess
|
||||
from typing import Union
|
||||
from termcolor import colored
|
||||
|
||||
from sh_edraft.utils.console.model.background_color import BackgroundColor
|
||||
from sh_edraft.utils.console.model.foreground_color import ForegroundColor
|
||||
|
||||
|
||||
class Console:
|
||||
_background_color: BackgroundColor = BackgroundColor.default
|
||||
_foreground_color: ForegroundColor = ForegroundColor.default
|
||||
|
||||
@property
|
||||
def background_color(self) -> BackgroundColor:
|
||||
return self._background_color
|
||||
|
||||
@property
|
||||
def foreground_color(self) -> ForegroundColor:
|
||||
return self._foreground_color
|
||||
|
||||
@classmethod
|
||||
def set_background_color(cls, color: Union[BackgroundColor, str]):
|
||||
if type(color) is str:
|
||||
cls._background_color = BackgroundColor[color]
|
||||
else:
|
||||
cls._background_color = color
|
||||
|
||||
@classmethod
|
||||
def set_foreground_color(cls, color: Union[ForegroundColor, str]):
|
||||
if type(color) is str:
|
||||
cls._foreground_color = ForegroundColor[color]
|
||||
else:
|
||||
cls._foreground_color = color
|
||||
|
||||
# useful methods
|
||||
@staticmethod
|
||||
def clear():
|
||||
os.system('cls' if os.name == 'nt' else 'clear')
|
||||
|
||||
@staticmethod
|
||||
def new():
|
||||
if os.name == 'nt':
|
||||
os.system("start /wait cmd")
|
||||
else:
|
||||
p = subprocess.Popen(args=["gnome-terminal"])
|
||||
p.communicate()
|
||||
|
||||
@staticmethod
|
||||
def read(output: str = None) -> str:
|
||||
user_input = input(output if output else '')
|
||||
return user_input[0]
|
||||
|
||||
@staticmethod
|
||||
def read_line(output: str = None) -> str:
|
||||
return input(output if output else '')
|
||||
|
||||
@classmethod
|
||||
def reset(cls):
|
||||
cls._background_color = BackgroundColor.default
|
||||
cls._foreground_color = ForegroundColor.default
|
||||
|
||||
@classmethod
|
||||
def write(cls, string: str):
|
||||
if cls._foreground_color == ForegroundColor.default:
|
||||
print(colored(string), end='')
|
||||
else:
|
||||
print(colored(string, cls._foreground_color.value), end='')
|
||||
|
||||
@classmethod
|
||||
def write_line(cls, *args):
|
||||
string = ' '.join(map(str, args))
|
||||
if cls._foreground_color == ForegroundColor.default:
|
||||
print(colored(string))
|
||||
else:
|
||||
print(colored(string, cls._foreground_color.value))
|
4
src/sh_edraft/utils/console/model/__init__.py
Normal file
4
src/sh_edraft/utils/console/model/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
# imports:
|
||||
|
||||
from .background_color import BackgroundColor
|
||||
from .foreground_color import ForegroundColor
|
14
src/sh_edraft/utils/console/model/background_color.py
Normal file
14
src/sh_edraft/utils/console/model/background_color.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class BackgroundColor(Enum):
|
||||
|
||||
default = 'default'
|
||||
grey = 'grey'
|
||||
red = 'red'
|
||||
green = 'green'
|
||||
yellow = 'yellow'
|
||||
blue = 'blue'
|
||||
magenta = 'magenta'
|
||||
cyan = 'cyan'
|
||||
white = 'white'
|
14
src/sh_edraft/utils/console/model/foreground_color.py
Normal file
14
src/sh_edraft/utils/console/model/foreground_color.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class ForegroundColor(Enum):
|
||||
|
||||
default = 'default'
|
||||
grey = 'grey'
|
||||
red = 'red'
|
||||
green = 'green'
|
||||
yellow = 'yellow'
|
||||
blue = 'blue'
|
||||
magenta = 'magenta'
|
||||
cyan = 'cyan'
|
||||
white = 'white'
|
Reference in New Issue
Block a user