-
Notifications
You must be signed in to change notification settings - Fork 222
Initial version of auto-detection of terminal width. #110
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
Open
dawngerpony
wants to merge
12
commits into
gruns:master
Choose a base branch
from
dawngerpony:dawngerpony/auto-columns
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e191743
Initial version of auto-detection of terminal width.
dawngerpony 42f96c7
Move parameter detection out of __call__ in case it causes a performa…
dawngerpony 7e3256b
Add a test (skipped in 2.x)
dawngerpony d47c5f7
Remove redundant commented-out line.
dawngerpony 8f3658f
Refactor implementation; add another test.
dawngerpony e26249f
Update tests/test_icecream.py
dawngerpony 661e58e
Update tests/test_icecream.py
dawngerpony 78b0ae9
Implement most of the latest code review suggestions.
dawngerpony c10907d
Update icecream/icecream.py
dawngerpony 2236996
Support for configureOutput().
dawngerpony 9490fa3
Default to not detecting terminal width.
dawngerpony 4a8ab87
Update icecream/icecream.py
dawngerpony File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
|
|
||
| import ast | ||
| import inspect | ||
| import os | ||
| import pprint | ||
| import sys | ||
| from datetime import datetime | ||
|
|
@@ -33,6 +34,15 @@ | |
|
|
||
| from .coloring import SolarizedDark | ||
|
|
||
| try: | ||
| from shutil import get_terminal_size | ||
| except ImportError: | ||
| try: | ||
| from backports.shutil_get_terminal_size import get_terminal_size | ||
| except ImportError: | ||
| def get_terminal_size(): | ||
| return os.environ['COLUMNS'] | ||
|
|
||
|
|
||
| PYTHON2 = (sys.version_info[0] == 2) | ||
|
|
||
|
|
@@ -154,15 +164,38 @@ def format_pair(prefix, arg, value): | |
| return '\n'.join(lines) | ||
|
|
||
|
|
||
| def argumentToString(obj): | ||
| s = DEFAULT_ARG_TO_STRING_FUNCTION(obj) | ||
| def argumentToString(obj, width=DEFAULT_LINE_WRAP_WIDTH): | ||
| s = DEFAULT_ARG_TO_STRING_FUNCTION(obj, width=width) | ||
| s = s.replace('\\n', '\n') # Preserve string newlines in output. | ||
| return s | ||
|
|
||
|
|
||
| def detect_terminal_width(prefix, default=DEFAULT_LINE_WRAP_WIDTH): | ||
| """ Returns the number of columns that this terminal can handle. """ | ||
| width = default | ||
| try: | ||
| # We need to pass a terminal height in the tuple so we pass the default | ||
| # of 25 lines but it's not used for anything. | ||
| width = get_terminal_size((default, 25)).columns | ||
| except Exception: # Not in TTY or something else went wrong | ||
| pass | ||
| # TODO account for argPrefix() | ||
| # TODO make sure we support configureOutput() | ||
| return width - len(prefix) | ||
|
|
||
|
|
||
| def supports_param(fn, param="width"): | ||
| """ Returns True if the function supports that parameter. """ | ||
| try: | ||
| from inspect import signature | ||
| return param in signature(fn).parameters | ||
| except ImportError: # Python 2.x | ||
| from inspect import getargspec | ||
| return param in getargspec(fn).args | ||
|
|
||
|
|
||
| class IceCreamDebugger: | ||
| _pairDelimiter = ', ' # Used by the tests in tests/. | ||
| lineWrapWidth = DEFAULT_LINE_WRAP_WIDTH | ||
| contextDelimiter = DEFAULT_CONTEXT_DELIMITER | ||
|
|
||
| def __init__(self, prefix=DEFAULT_PREFIX, | ||
|
|
@@ -173,6 +206,8 @@ def __init__(self, prefix=DEFAULT_PREFIX, | |
| self.includeContext = includeContext | ||
| self.outputFunction = outputFunction | ||
| self.argToStringFunction = argToStringFunction | ||
| self.passWidthParam = supports_param(self.argToStringFunction) | ||
| self.lineWrapWidth = detect_terminal_width(self.prefix, DEFAULT_LINE_WRAP_WIDTH) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This still doesn't account for the width of
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, will fix. |
||
|
|
||
| def __call__(self, *args): | ||
| if self.enabled: | ||
|
|
@@ -232,7 +267,8 @@ def _constructArgumentOutput(self, prefix, context, pairs): | |
| def argPrefix(arg): | ||
| return '%s: ' % arg | ||
|
|
||
| pairs = [(arg, self.argToStringFunction(val)) for arg, val in pairs] | ||
| kwargs = {"width": self.lineWrapWidth} if self.passWidthParam else {} | ||
| pairs = [(arg, self.argToStringFunction(val, **kwargs)) for arg, val in pairs] | ||
| # For cleaner output, if <arg> is a literal, eg 3, "string", b'bytes', | ||
| # etc, only output the value, not the argument and the value, as the | ||
| # argument and the value will be identical or nigh identical. Ex: with | ||
|
|
@@ -325,6 +361,7 @@ def configureOutput(self, prefix=_absent, outputFunction=_absent, | |
|
|
||
| if argToStringFunction is not _absent: | ||
| self.argToStringFunction = argToStringFunction | ||
| self.passWidthParam = supports_param(self.argToStringFunction) | ||
|
|
||
| if includeContext is not _absent: | ||
| self.includeContext = includeContext | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.