-
-
Notifications
You must be signed in to change notification settings - Fork 745
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
Allow user to force terminal size used by the CLI formatters by setting environment variable #4242
Changes from 12 commits
99e4f62
df5afde
c9ad392
e59291d
921fe11
208b593
56f9cec
471b5ef
a034c06
cf50187
62c61cc
9530889
e2d2c8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,58 +14,80 @@ | |
# limitations under the License. | ||
|
||
from __future__ import absolute_import | ||
|
||
import os | ||
import struct | ||
import subprocess | ||
import sys | ||
|
||
from st2client.utils.color import format_status | ||
|
||
DEFAULT_TERMINAL_SIZE_LINES = 80 | ||
DEFAULT_TERMINAL_SIZE_COLUMNS = 150 | ||
|
||
__all__ = [ | ||
'get_terminal_size' | ||
'get_terminal_size_columns' | ||
] | ||
|
||
|
||
def get_terminal_size(default=(80, 20)): | ||
def get_terminal_size_columns(default=DEFAULT_TERMINAL_SIZE_COLUMNS): | ||
""" | ||
:return: (lines, cols) | ||
Try to retrieve COLUMNS value of terminal size using various system specific approaches. | ||
|
||
If terminal size can't be retrieved, default value is returned. | ||
|
||
NOTE 1: COLUMNS environment variable is checked first, if the value is not set / available, | ||
other methods are tried. | ||
|
||
:rtype: ``int`` | ||
:return: columns | ||
""" | ||
# 1. Try COLUMNS environment variable first like in upstream Python 3 method - | ||
# https://github.com/python/cpython/blob/master/Lib/shutil.py#L1203 | ||
# This way it's consistent with upstream implementation. In the past, our implementation | ||
# checked those variables at the end as a fall back. | ||
try: | ||
columns = os.environ['COLUMNS'] | ||
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. 👍 on approach, but I think original implementation is safer on corner cases here: For example, when only 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. Yeah, agreed - our implementation requires both variables to be set (but we actually only use columns and ignore lines all together). Original implementation allows you to specify those independently. I was thinking of making that change as well, but it's a lot more involved than just doing what I do right now (and previous code also requires both to be specified). 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. Setting only one var and expect it to take precedence is a valid behavior with LINES vs COLUMNS. It would be a surprise that only 2 alltogether would work. We're following the established standards here. Ideally if we won't surprise user in a bad way :) We can use a waterfall of fallbacks as in original .py implementation: If we change the implementation to read columns only, |
||
return int(columns) | ||
except (KeyError, ValueError): | ||
pass | ||
|
||
def ioctl_GWINSZ(fd): | ||
import fcntl | ||
import termios | ||
# Return a tuple (lines, columns) | ||
return struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234')) | ||
# try stdin, stdout, stderr | ||
|
||
# 2. try stdin, stdout, stderr | ||
for fd in (0, 1, 2): | ||
try: | ||
return ioctl_GWINSZ(fd) | ||
except: | ||
return ioctl_GWINSZ(fd)[1] | ||
except Exception: | ||
pass | ||
# try os.ctermid() | ||
|
||
# 3. try os.ctermid() | ||
try: | ||
fd = os.open(os.ctermid(), os.O_RDONLY) | ||
try: | ||
return ioctl_GWINSZ(fd) | ||
return ioctl_GWINSZ(fd)[1] | ||
finally: | ||
os.close(fd) | ||
except: | ||
except Exception: | ||
pass | ||
# try `stty size` | ||
|
||
# 4. try `stty size` | ||
try: | ||
process = subprocess.Popen(['stty', 'size'], | ||
shell=False, | ||
stdout=subprocess.PIPE, | ||
stderr=open(os.devnull, 'w')) | ||
result = process.communicate() | ||
if process.returncode == 0: | ||
return tuple(int(x) for x in result[0].split()) | ||
except: | ||
return tuple(int(x) for x in result[0].split())[1] | ||
except Exception: | ||
pass | ||
# try environment variables | ||
try: | ||
return tuple(int(os.getenv(var)) for var in ('LINES', 'COLUMNS')) | ||
except: | ||
pass | ||
# return default. | ||
|
||
# 5. return default fallback value | ||
return default | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Licensed to the StackStorm, Inc ('StackStorm') under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import absolute_import | ||
|
||
import os | ||
|
||
import unittest2 | ||
import mock | ||
|
||
from st2client.utils.terminal import DEFAULT_TERMINAL_SIZE_COLUMNS | ||
from st2client.utils.terminal import get_terminal_size_columns | ||
|
||
__all__ = [ | ||
'TerminalUtilsTestCase' | ||
] | ||
|
||
|
||
class TerminalUtilsTestCase(unittest2.TestCase): | ||
@mock.patch.dict(os.environ, {'LINES': '111', 'COLUMNS': '222'}) | ||
def test_get_terminal_size_columns_columns_environment_variable_has_precedence(self): | ||
# Verify that COLUMNS environment variables has precedence over other approaches | ||
columns = get_terminal_size_columns() | ||
|
||
self.assertEqual(columns, 222) | ||
|
||
@mock.patch('struct.unpack', mock.Mock(return_value=(333, 444))) | ||
def test_get_terminal_size_columns_stdout_is_used(self): | ||
columns = get_terminal_size_columns() | ||
self.assertEqual(columns, 444) | ||
|
||
@mock.patch('struct.unpack', mock.Mock(side_effect=Exception('a'))) | ||
@mock.patch('subprocess.Popen') | ||
def test_get_terminal_size_subprocess_popen_is_used(self, mock_popen): | ||
mock_communicate = mock.Mock(return_value=['555 666']) | ||
|
||
mock_process = mock.Mock() | ||
mock_process.returncode = 0 | ||
mock_process.communicate = mock_communicate | ||
|
||
mock_popen.return_value = mock_process | ||
|
||
columns = get_terminal_size_columns() | ||
self.assertEqual(columns, 666) | ||
|
||
@mock.patch('struct.unpack', mock.Mock(side_effect=Exception('a'))) | ||
@mock.patch('subprocess.Popen', mock.Mock(side_effect=Exception('b'))) | ||
def test_get_terminal_size_default_values_are_used(self): | ||
columns = get_terminal_size_columns() | ||
|
||
self.assertEqual(columns, DEFAULT_TERMINAL_SIZE_COLUMNS) |
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.
Can remove as never needed/used