-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Output pretty error if possible (#399)
Error position and hint are now included by default if present, overridden by EDGEDB_ERROR_HINT (enabled/disabled). The output is aslo colored if the stderr refers to a terminal, overriden by EDGEDB_COLOR_OUTPUT (auto/enabled/disabled).
- Loading branch information
Showing
3 changed files
with
196 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import os | ||
import sys | ||
import warnings | ||
|
||
COLOR = None | ||
|
||
|
||
class Color: | ||
HEADER = "" | ||
BLUE = "" | ||
CYAN = "" | ||
GREEN = "" | ||
WARNING = "" | ||
FAIL = "" | ||
ENDC = "" | ||
BOLD = "" | ||
UNDERLINE = "" | ||
|
||
|
||
def get_color() -> Color: | ||
global COLOR | ||
|
||
if COLOR is None: | ||
COLOR = Color() | ||
if type(USE_COLOR) is bool: | ||
use_color = USE_COLOR | ||
else: | ||
try: | ||
use_color = USE_COLOR() | ||
except Exception: | ||
use_color = False | ||
if use_color: | ||
COLOR.HEADER = '\033[95m' | ||
COLOR.BLUE = '\033[94m' | ||
COLOR.CYAN = '\033[96m' | ||
COLOR.GREEN = '\033[92m' | ||
COLOR.WARNING = '\033[93m' | ||
COLOR.FAIL = '\033[91m' | ||
COLOR.ENDC = '\033[0m' | ||
COLOR.BOLD = '\033[1m' | ||
COLOR.UNDERLINE = '\033[4m' | ||
|
||
return COLOR | ||
|
||
|
||
try: | ||
USE_COLOR = { | ||
"default": lambda: sys.stderr.isatty(), | ||
"auto": lambda: sys.stderr.isatty(), | ||
"enabled": True, | ||
"disabled": False, | ||
}[ | ||
os.getenv("EDGEDB_COLOR_OUTPUT", "default") | ||
] | ||
except KeyError: | ||
warnings.warn( | ||
"EDGEDB_COLOR_OUTPUT can only be one of: " | ||
"default, auto, enabled or disabled" | ||
) | ||
USE_COLOR = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters