mirror of
https://github.com/RYDE-WORK/ballistica.git
synced 2026-01-28 18:15:45 +08:00
Added elegant way to print colored terminal output
This commit is contained in:
parent
0180274d75
commit
35edfbd375
26
.idea/dictionaries/ericf.xml
generated
26
.idea/dictionaries/ericf.xml
generated
@ -151,15 +151,19 @@
|
||||
<w>basew</w>
|
||||
<w>bastd</w>
|
||||
<w>batools</w>
|
||||
<w>bblk</w>
|
||||
<w>bblu</w>
|
||||
<w>bbot</w>
|
||||
<w>bbtn</w>
|
||||
<w>bcppcompiler</w>
|
||||
<w>bcyn</w>
|
||||
<w>bdfl</w>
|
||||
<w>belarussian</w>
|
||||
<w>benboncan</w>
|
||||
<w>bfiledir</w>
|
||||
<w>bfiles</w>
|
||||
<w>bgmodel</w>
|
||||
<w>bgrn</w>
|
||||
<w>bgterrain</w>
|
||||
<w>bgtex</w>
|
||||
<w>bgthread</w>
|
||||
@ -175,6 +179,7 @@
|
||||
<w>bldtp</w>
|
||||
<w>blocksize</w>
|
||||
<w>bluetooth</w>
|
||||
<w>bmag</w>
|
||||
<w>bname</w>
|
||||
<w>bndl</w>
|
||||
<w>boffs</w>
|
||||
@ -217,6 +222,8 @@
|
||||
<w>buttondown</w>
|
||||
<w>buttonwidget</w>
|
||||
<w>bval</w>
|
||||
<w>bwht</w>
|
||||
<w>bylw</w>
|
||||
<w>bytecount</w>
|
||||
<w>byteswap</w>
|
||||
<w>cachable</w>
|
||||
@ -536,6 +543,7 @@
|
||||
<w>enumtype</w>
|
||||
<w>enumval</w>
|
||||
<w>envhash</w>
|
||||
<w>envval</w>
|
||||
<w>epath</w>
|
||||
<w>epicfail</w>
|
||||
<w>ericf</w>
|
||||
@ -883,6 +891,7 @@
|
||||
<w>ipaddress</w>
|
||||
<w>ipos</w>
|
||||
<w>iprof</w>
|
||||
<w>isatty</w>
|
||||
<w>iscale</w>
|
||||
<w>iserverget</w>
|
||||
<w>iserverput</w>
|
||||
@ -1528,8 +1537,18 @@
|
||||
<w>sandboxing</w>
|
||||
<w>sandyrb</w>
|
||||
<w>saxutils</w>
|
||||
<w>sbblk</w>
|
||||
<w>sbblu</w>
|
||||
<w>sbcyn</w>
|
||||
<w>sbgrn</w>
|
||||
<w>sblk</w>
|
||||
<w>sblu</w>
|
||||
<w>sbmag</w>
|
||||
<w>sbpos</w>
|
||||
<w>sbred</w>
|
||||
<w>sbtn</w>
|
||||
<w>sbwht</w>
|
||||
<w>sbylw</w>
|
||||
<w>scenefile</w>
|
||||
<w>scenefiles</w>
|
||||
<w>scenename</w>
|
||||
@ -1550,6 +1569,7 @@
|
||||
<w>scrlw</w>
|
||||
<w>scrollw</w>
|
||||
<w>scrollwidget</w>
|
||||
<w>scyn</w>
|
||||
<w>sdata</w>
|
||||
<w>sdkutils</w>
|
||||
<w>sdtk</w>
|
||||
@ -1576,6 +1596,7 @@
|
||||
<w>setmusic</w>
|
||||
<w>settingname</w>
|
||||
<w>setversion</w>
|
||||
<w>sgrn</w>
|
||||
<w>shakaharu</w>
|
||||
<w>sharedctypes</w>
|
||||
<w>sharedobj</w>
|
||||
@ -1595,6 +1616,7 @@
|
||||
<w>skey</w>
|
||||
<w>sline</w>
|
||||
<w>slval</w>
|
||||
<w>smag</w>
|
||||
<w>smlh</w>
|
||||
<w>smtpd</w>
|
||||
<w>smtplib</w>
|
||||
@ -1644,6 +1666,7 @@
|
||||
<w>srcpydata</w>
|
||||
<w>srcstr</w>
|
||||
<w>srctxt</w>
|
||||
<w>sred</w>
|
||||
<w>sshd</w>
|
||||
<w>sslproto</w>
|
||||
<w>stackstr</w>
|
||||
@ -1703,8 +1726,10 @@
|
||||
<w>sver</w>
|
||||
<w>svne</w>
|
||||
<w>svvv</w>
|
||||
<w>swht</w>
|
||||
<w>swip</w>
|
||||
<w>swipsound</w>
|
||||
<w>sylw</w>
|
||||
<w>symlinked</w>
|
||||
<w>syncable</w>
|
||||
<w>syncall</w>
|
||||
@ -1749,6 +1774,7 @@
|
||||
<w>tempfile</w>
|
||||
<w>tempfilepath</w>
|
||||
<w>templatecb</w>
|
||||
<w>termcolors</w>
|
||||
<w>termios</w>
|
||||
<w>testbuffer</w>
|
||||
<w>testcall</w>
|
||||
|
||||
@ -456,6 +456,7 @@
|
||||
"ba_data/python/efro/__pycache__/dataclassutils.cpython-37.opt-1.pyc",
|
||||
"ba_data/python/efro/__pycache__/executils.cpython-37.opt-1.pyc",
|
||||
"ba_data/python/efro/__pycache__/jsonutils.cpython-37.opt-1.pyc",
|
||||
"ba_data/python/efro/__pycache__/terminal.cpython-37.opt-1.pyc",
|
||||
"ba_data/python/efro/__pycache__/util.cpython-37.opt-1.pyc",
|
||||
"ba_data/python/efro/dataclassutils.py",
|
||||
"ba_data/python/efro/entity/__init__.py",
|
||||
@ -474,6 +475,7 @@
|
||||
"ba_data/python/efro/entity/util.py",
|
||||
"ba_data/python/efro/executils.py",
|
||||
"ba_data/python/efro/jsonutils.py",
|
||||
"ba_data/python/efro/terminal.py",
|
||||
"ba_data/python/efro/util.py",
|
||||
"server/__pycache__/ballisticacore_server.cpython-37.opt-1.pyc",
|
||||
"server/ballisticacore_server.py"
|
||||
|
||||
@ -145,6 +145,7 @@ SCRIPT_TARGETS_PY_1 = \
|
||||
build/server/ballisticacore_server.py \
|
||||
build/ba_data/python/efro/executils.py \
|
||||
build/ba_data/python/efro/dataclassutils.py \
|
||||
build/ba_data/python/efro/terminal.py \
|
||||
build/ba_data/python/efro/util.py \
|
||||
build/ba_data/python/efro/__init__.py \
|
||||
build/ba_data/python/efro/jsonutils.py \
|
||||
@ -386,6 +387,7 @@ SCRIPT_TARGETS_PYC_1 = \
|
||||
build/server/__pycache__/ballisticacore_server.cpython-37.opt-1.pyc \
|
||||
build/ba_data/python/efro/__pycache__/executils.cpython-37.opt-1.pyc \
|
||||
build/ba_data/python/efro/__pycache__/dataclassutils.cpython-37.opt-1.pyc \
|
||||
build/ba_data/python/efro/__pycache__/terminal.cpython-37.opt-1.pyc \
|
||||
build/ba_data/python/efro/__pycache__/util.cpython-37.opt-1.pyc \
|
||||
build/ba_data/python/efro/__pycache__/__init__.cpython-37.opt-1.pyc \
|
||||
build/ba_data/python/efro/__pycache__/jsonutils.cpython-37.opt-1.pyc \
|
||||
@ -651,6 +653,11 @@ build/ba_data/python/efro/__pycache__/dataclassutils.cpython-37.opt-1.pyc: \
|
||||
@echo Compiling script: $^
|
||||
@rm -rf $@ && $(TOOLS_DIR)/snippets compile_python_files $^ && chmod 444 $@
|
||||
|
||||
build/ba_data/python/efro/__pycache__/terminal.cpython-37.opt-1.pyc: \
|
||||
build/ba_data/python/efro/terminal.py
|
||||
@echo Compiling script: $^
|
||||
@rm -rf $@ && $(TOOLS_DIR)/snippets compile_python_files $^ && chmod 444 $@
|
||||
|
||||
build/ba_data/python/efro/__pycache__/util.cpython-37.opt-1.pyc: \
|
||||
build/ba_data/python/efro/util.py
|
||||
@echo Compiling script: $^
|
||||
|
||||
189
assets/src/ba_data/python/efro/terminal.py
Normal file
189
assets/src/ba_data/python/efro/terminal.py
Normal file
@ -0,0 +1,189 @@
|
||||
# Copyright (c) 2011-2020 Eric Froemling
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# -----------------------------------------------------------------------------
|
||||
"""Functionality related to terminal IO."""
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
import os
|
||||
from enum import Enum, unique
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
pass
|
||||
|
||||
|
||||
@unique
|
||||
class TerminalColor(Enum):
|
||||
"""Color codes for printing to terminals."""
|
||||
|
||||
# Styles
|
||||
RESET = '\033[0m'
|
||||
BOLD = '\033[1m'
|
||||
UNDERLINE = '\033[4m'
|
||||
INVERSE = '\033[7m'
|
||||
|
||||
# Normal foreground colors
|
||||
BLACK = '\033[30m'
|
||||
RED = '\033[31m'
|
||||
GREEN = '\033[32m'
|
||||
YELLOW = '\033[33m'
|
||||
BLUE = '\033[34m'
|
||||
MAGENTA = '\033[35m'
|
||||
CYAN = '\033[36m'
|
||||
WHITE = '\033[37m'
|
||||
|
||||
# Normal background colors.
|
||||
BG_BLACK = '\033[40m'
|
||||
BG_RED = '\033[41m'
|
||||
BG_GREEN = '\033[42m'
|
||||
BG_YELLOW = '\033[43m'
|
||||
BG_BLUE = '\033[44m'
|
||||
BG_MAGENTA = '\033[45m'
|
||||
BG_CYAN = '\033[46m'
|
||||
BG_WHITE = '\033[47m'
|
||||
|
||||
# Strong foreground colors
|
||||
STRONG_BLACK = '\033[90m'
|
||||
STRONG_RED = '\033[91m'
|
||||
STRONG_GREEN = '\033[92m'
|
||||
STRONG_YELLOW = '\033[93m'
|
||||
STRONG_BLUE = '\033[94m'
|
||||
STRONG_MAGENTA = '\033[95m'
|
||||
STRONG_CYAN = '\033[96m'
|
||||
STRONG_WHITE = '\033[97m'
|
||||
|
||||
# Strong background colors.
|
||||
STRONG_BG_BLACK = '\033[100m'
|
||||
STRONG_BG_RED = '\033[101m'
|
||||
STRONG_BG_GREEN = '\033[102m'
|
||||
STRONG_BG_YELLOW = '\033[103m'
|
||||
STRONG_BG_BLUE = '\033[104m'
|
||||
STRONG_BG_MAGENTA = '\033[105m'
|
||||
STRONG_BG_CYAN = '\033[106m'
|
||||
STRONG_BG_WHITE = '\033[107m'
|
||||
|
||||
|
||||
class Clr:
|
||||
"""Convenience class for color terminal output.
|
||||
|
||||
These will be set to ANSI color escape sequences if the current process
|
||||
seems to be an interactive terminal (sys.__stdout__.isatty()), otherwise
|
||||
they will be empty strings.
|
||||
If the environment variable EFRO_TERMCOLORS is set to 0 or 1, that
|
||||
value will be used instead.
|
||||
"""
|
||||
_envval = os.environ.get('EFRO_TERMCOLORS')
|
||||
color_enabled = (True if _envval == '1' else
|
||||
False if _envval == '0' else sys.__stdout__.isatty())
|
||||
if color_enabled:
|
||||
|
||||
# Styles
|
||||
RST = TerminalColor.RESET.value
|
||||
BLD = TerminalColor.BOLD.value
|
||||
UND = TerminalColor.UNDERLINE.value
|
||||
INV = TerminalColor.INVERSE.value
|
||||
|
||||
# Normal foreground colors
|
||||
BLK = TerminalColor.BLACK.value
|
||||
RED = TerminalColor.RED.value
|
||||
GRN = TerminalColor.GREEN.value
|
||||
YLW = TerminalColor.YELLOW.value
|
||||
BLU = TerminalColor.BLUE.value
|
||||
MAG = TerminalColor.MAGENTA.value
|
||||
CYN = TerminalColor.CYAN.value
|
||||
WHT = TerminalColor.WHITE.value
|
||||
|
||||
# Normal background colors.
|
||||
BBLK = TerminalColor.BG_BLACK.value
|
||||
BRED = TerminalColor.BG_RED.value
|
||||
BGRN = TerminalColor.BG_GREEN.value
|
||||
BYLW = TerminalColor.BG_YELLOW.value
|
||||
BBLU = TerminalColor.BG_BLUE.value
|
||||
BMAG = TerminalColor.BG_MAGENTA.value
|
||||
BCYN = TerminalColor.BG_CYAN.value
|
||||
BWHT = TerminalColor.BG_WHITE.value
|
||||
|
||||
# Strong foreground colors
|
||||
SBLK = TerminalColor.STRONG_BLACK.value
|
||||
SRED = TerminalColor.STRONG_RED.value
|
||||
SGRN = TerminalColor.STRONG_GREEN.value
|
||||
SYLW = TerminalColor.STRONG_YELLOW.value
|
||||
SBLU = TerminalColor.STRONG_BLUE.value
|
||||
SMAG = TerminalColor.STRONG_MAGENTA.value
|
||||
SCYN = TerminalColor.STRONG_CYAN.value
|
||||
SWHT = TerminalColor.STRONG_WHITE.value
|
||||
|
||||
# Strong background colors.
|
||||
SBBLK = TerminalColor.STRONG_BG_BLACK.value
|
||||
SBRED = TerminalColor.STRONG_BG_RED.value
|
||||
SBGRN = TerminalColor.STRONG_BG_GREEN.value
|
||||
SBYLW = TerminalColor.STRONG_BG_YELLOW.value
|
||||
SBBLU = TerminalColor.STRONG_BG_BLUE.value
|
||||
SBMAG = TerminalColor.STRONG_BG_MAGENTA.value
|
||||
SBCYN = TerminalColor.STRONG_BG_CYAN.value
|
||||
SBWHT = TerminalColor.STRONG_BG_WHITE.value
|
||||
|
||||
else:
|
||||
# Styles
|
||||
RST = ''
|
||||
BLD = ''
|
||||
UND = ''
|
||||
INV = ''
|
||||
|
||||
# Normal foreground colors
|
||||
BLK = ''
|
||||
RED = ''
|
||||
GRN = ''
|
||||
YLW = ''
|
||||
BLU = ''
|
||||
MAG = ''
|
||||
CYN = ''
|
||||
WHT = ''
|
||||
|
||||
# Normal background colors.
|
||||
BBLK = ''
|
||||
BRED = ''
|
||||
BGRN = ''
|
||||
BYLW = ''
|
||||
BBLU = ''
|
||||
BMAG = ''
|
||||
BCYN = ''
|
||||
BWHT = ''
|
||||
|
||||
# Strong foreground colors
|
||||
SBLK = ''
|
||||
SRED = ''
|
||||
SGRN = ''
|
||||
SYLW = ''
|
||||
SBLU = ''
|
||||
SMAG = ''
|
||||
SCYN = ''
|
||||
SWHT = ''
|
||||
|
||||
# Strong background colors.
|
||||
SBBLK = ''
|
||||
SBRED = ''
|
||||
SBGRN = ''
|
||||
SBYLW = ''
|
||||
SBBLU = ''
|
||||
SBMAG = ''
|
||||
SBCYN = ''
|
||||
SBWHT = ''
|
||||
@ -67,7 +67,7 @@ class ServerManagerApp:
|
||||
|
||||
@property
|
||||
def config(self) -> ServerConfig:
|
||||
"""The current config settings for the app."""
|
||||
"""The current config for the app."""
|
||||
return self._config
|
||||
|
||||
@config.setter
|
||||
|
||||
@ -30,13 +30,11 @@ import subprocess
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from efro.terminal import Clr
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing import List, Sequence, Optional
|
||||
|
||||
CLRBLU = '\033[94m' # Blue.
|
||||
CLRHDR = '\033[95m' # Header.
|
||||
CLREND = '\033[0m' # End.
|
||||
|
||||
|
||||
# Python pip packages we require for this project.
|
||||
@dataclass
|
||||
@ -113,8 +111,8 @@ def _lazybuild_check_paths(inpaths: List[str], category: SourceCategory,
|
||||
def _testpath(path: str) -> bool:
|
||||
# Now see this path is newer than our target..
|
||||
if mtime is None or os.path.getmtime(path) >= mtime:
|
||||
print(f'{CLRHDR}Build of {tnamepretty} triggered by'
|
||||
f' {path}{CLREND}')
|
||||
print(f'{Clr.SMAG}Build of {tnamepretty} triggered by'
|
||||
f' {path}{Clr.RST}')
|
||||
return True
|
||||
return False
|
||||
|
||||
@ -159,8 +157,8 @@ def _lazybuild_check_paths(inpaths: List[str], category: SourceCategory,
|
||||
if _testpath(fpath):
|
||||
return True
|
||||
unchanged_count += 1
|
||||
print(f'{CLRBLU}Lazybuild: skipping "{tnamepretty}"'
|
||||
f' ({unchanged_count} inputs unchanged).{CLREND}')
|
||||
print(f'{Clr.SBLU}Lazybuild: skipping "{tnamepretty}"'
|
||||
f' ({unchanged_count} inputs unchanged).{Clr.RST}')
|
||||
return False
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user