Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 50
Output pretty error if possible#399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b05b319
Output pretty error if possible
fantix 17c6997
OS-specific line separator
fantix 0595db9
Safe lazy color initialization
fantix 89399c1
Able to turn off error hint by EDGEDB_ERROR_HINT=off
fantix 0a5acae
CRF: use consistent env var values
fantix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this is an approximation that will overestimate the length of strings using combining characters that don't normalize away.
This means we can get stuff like
https://zalgo.org/ has a great generator for pathological unicode strings.
Probably this is almost always fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CLI's error formatter does seem to handle this one right. I'm not sure exactly what algorithm it uses.
I think to some extent anything we do is an approximation because it depends on assumptions about how the terminal will render it.
Unicode does have a notion of "grapheme clusters", and there are python libraries for handling them (https://github.com/alvinlindstam/grapheme). I'm not sure that this is the best approach either, though, since the width of those clusters can vary and I don't know if there is good way to guess that well. (Consider
🇨🇦, which depending on your terminal will show as either a flag or the letters CA, but either way will probably take two characters, but is only one grapheme cluster.)Maybe the best approach on our side would be basically just to drop combining characters.
There is a potentially infinitely deep rabbit hole here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rust uses de facto standard in Rust: https://crates.io/crates/unicode-width
Which uses Unicode Standard Annex 11 for calculating width.
Note: unicode width != number of graphemes. As width of a grapheme can be two chars.
And you're right that in generic case it's impossible to calculate width. Because some characters can have width not just depending on the terminal but also on the specific font (if there is a character for a specific set of emojis).
unicode-width's frontpage gives an example.Dropping Zero-width Joiners
xsel -bmanually).Note: zalgo-generated stuff looks like plain
fooin all three terminals (although those unicode tricks are present in the output)The summary of this: dropping zero-width joiners is safe to do, although looks unnecessary in my setup. If that's not true for some popular terminals or on other systems it could be done.
Fixup
For my examples this function works:
But I'm not sure if this is good enough. The libraries doing this (rustic unicode-width, and pythonic urwid) have their own width tables.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in #404