Skip to content

Commit

Permalink
Merge pull request #46 from solvebio/feature/ipython-notebook-and-iss…
Browse files Browse the repository at this point in the history
…ues-21-43

Address issue #43 and possibly #21
  • Loading branch information
davecap committed Oct 31, 2014
2 parents 61144fd + 9d9dd8a commit f3beaa5
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions solvebio/utils/printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,37 @@

logger = logging.getLogger('solvebio')

std_handles = [sys.stdin, sys.stdout, sys.stderr]
try:
# Switch from the default input ASCII encoding to the default locale.
# The Python runtime will use this when it has to decode a
# string buffer to unicode. This is not needed in Python3.

# However reload(sys), used below resets stdin, stdout, and stderr
# which is bad if they've already been reassigned. An ipython
# notebook shell, for example, sets up its own stdout.
# See github issue #43 and #21.
reload(sys).setdefaultencoding(locale.getdefaultlocale()[1])
locale.setlocale(locale.LC_ALL, '')
except:
pass
finally:
sys.stdin, sys.stdout, sys.stderr = std_handles


# Set rows and columns and colors


def set_from_env(name, default_value):
try:
return int(os.environ[name])
except:
return default_value


TTY_ROWS = set_from_env('LINES', 24)
TTY_COLS = set_from_env('COLUMNS', 80)

TTY_ROWS = 24
TTY_COLS = 80
TTY_COLORS = True

if sys.stdout.isatty():
Expand All @@ -26,7 +49,8 @@
TTY_ROWS = int(rows)
TTY_COLS = int(cols)
except:
logger.warn('Cannot detect terminal column width')
logger.warn('Cannot detect terminal column width.\nUsing value '
'from environment variables and/or internal defaults.')
else:
TTY_COLORS = False

Expand Down

0 comments on commit f3beaa5

Please sign in to comment.