8000 fix decode error on py2; allow console width to be set via COLUMNS en… · simonsystem/python-for-android@1833ae9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1833ae9

Browse files
committed
fix decode error on py2; allow console width to be set via COLUMNS env var
1 parent 3a4869b commit 1833ae9

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

pythonforandroid/logger.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,27 @@ def shorten_string(string, max_width):
9292
return string
9393
visible = max_width - 16 - int(log10(string_len))
9494
# expected suffix len "...(and XXXXX more)"
95-
return ''.join((string[:visible], '...(and ', str(string_len - visible),
96-
' more)'))
95+
return u''.join((string[:visible], u'...(and ', str(string_len - visible),
96+
u' more)'))
97+
98+
99+
def get_console_width():
100+
try:
101+
cols = int(os.environ['COLUMNS'])
102+
except (KeyError, ValueError):
103+
pass
104+
else:
105+
if cols >= 25:
106+
return cols
107+
108+
try:
109+
cols = max(25, int(os.popen('stty size', 'r').read().split()[1]))
110+
except Exception:
111+
pass
112+
else:
113+
return cols
114+
115+
return 100
97116

98117

99118
def shprint(command, *args, **kwargs):
@@ -109,10 +128,7 @@ def shprint(command, *args, **kwargs):
109128
filter_out = kwargs.pop('_filterout', None)
110129
if len(logger.handlers) > 1:
111130
logger.removeHandler(logger.handlers[1])
112-
try:
113-
columns = max(25, int(os.popen('stty size', 'r').read().split()[1]))
114-
except:
115-
columns = 100
131+
columns = get_console_width()
116132
command_path = str(command).split('/')
117133
command_string = command_path[-1]
118134
string = ' '.join(['running', command_string] + list(args))

0 commit comments

Comments
 (0)
0