|
| 1 | +""" |
| 2 | +Internal module for console introspection |
| 3 | +""" |
| 4 | + |
| 5 | +import sys |
| 6 | +import locale |
| 7 | +from pandas.util.terminal import get_terminal_size |
| 8 | + |
| 9 | +# ----------------------------------------------------------------------------- |
| 10 | +# Global formatting options |
| 11 | +_initial_defencoding = None |
| 12 | + |
| 13 | + |
| 14 | +def detect_console_encoding(): |
| 15 | + """ |
| 16 | + Try to find the most capable encoding supported by the console. |
| 17 | + slighly modified from the way IPython handles the same issue. |
| 18 | + """ |
| 19 | + global _initial_defencoding |
| 20 | + |
| 21 | + encoding = None |
| 22 | + try: |
| 23 | + encoding = sys.stdout.encoding or sys.stdin.encoding |
| 24 | + except AttributeError: |
| 25 | + pass |
| 26 | + |
| 27 | + # try again for something better |
| 28 | + if not encoding or 'ascii' in encoding.lower(): |
| 29 | + try: |
| 30 | + encoding = locale.getpreferredencoding() |
| 31 | + except Exception: |
| 32 | + pass |
| 33 | + |
| 34 | + # when all else fails. this will usually be "ascii" |
| 35 | + if not encoding or 'ascii' in encoding.lower(): |
| 36 | + encoding = sys.getdefaultencoding() |
| 37 | + |
| 38 | + # GH3360, save the reported defencoding at import time |
| 39 | + # MPL backends may change it. Make available for debugging. |
| 40 | + if not _initial_defencoding: |
| 41 | + _initial_defencoding = sys.getdefaultencoding() |
| 42 | + |
| 43 | + return encoding |
| 44 | + |
| 45 | + |
| 46 | +def get_console_size(): |
| 47 | + """Return console size as tuple = (width, height). |
| 48 | +
|
| 49 | + Returns (None,None) in non-interactive session. |
| 50 | + """ |
| 51 | + from pandas import get_option |
| 52 | + from pandas.core import common as com |
| 53 | + |
| 54 | + display_width = get_option('display.width') |
| 55 | + # deprecated. |
| 56 | + display_height = get_option('display.height', silent=True) |
| 57 | + |
| 58 | + # Consider |
| 59 | + # interactive shell terminal, can detect term size |
| 60 | + # interactive non-shell terminal (ipnb/ipqtconsole), cannot detect term |
| 61 | + # size non-interactive script, should disregard term size |
| 62 | + |
| 63 | + # in addition |
| 64 | + # width,height have default values, but setting to 'None' signals |
| 65 | + # should use Auto-Detection, But only in interactive shell-terminal. |
| 66 | + # Simple. yeah. |
| 67 | + |
| 68 | + if com.in_interactive_session(): |
| 69 | + if com.in_ipython_frontend(): |
| 70 | + # sane defaults for interactive non-shell terminal |
| 71 | + # match default for width,height in config_init |
| 72 | + from pandas.core.config import get_default_val |
| 73 | + terminal_width = get_default_val('display.width') |
| 74 | + terminal_height = get_default_val('display.height') |
| 75 | + else: |
| 76 | + # pure terminal |
| 77 | + terminal_width, terminal_height = get_terminal_size() |
| 78 | + else: |
| 79 | + terminal_width, terminal_height = None, None |
| 80 | + |
| 81 | + # Note if the User sets width/Height to None (auto-detection) |
| 82 | + # and we're in a script (non-inter), this will return (None,None) |
| 83 | + # caller needs to deal. |
| 84 | + return (display_width or terminal_width, display_height or terminal_height) |
0 commit comments