-
Notifications
You must be signed in to change notification settings - Fork 175
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
Improved routine for terminal dimensions. Works on windows too. #127
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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 |
---|---|---|
|
@@ -18,9 +18,9 @@ | |
"""Simple terminal related routines.""" | ||
|
||
import getopt | ||
import os | ||
import os # needed for testing | ||
import re | ||
import struct | ||
import shutil | ||
import sys | ||
import time | ||
|
||
|
@@ -32,8 +32,6 @@ | |
except (ImportError, ModuleNotFoundError): | ||
pass | ||
|
||
__version__ = '0.1.1' | ||
|
||
# ANSI, ISO/IEC 6429 escape sequences, SGR (Select Graphic Rendition) subset. | ||
SGR = { | ||
'reset': 0, | ||
|
@@ -169,18 +167,8 @@ def EncloseAnsiText(text): | |
|
||
|
||
def TerminalSize(): | ||
"""Returns terminal length and width as a tuple.""" | ||
try: | ||
with open(os.ctermid()) as tty_instance: | ||
length_width = struct.unpack( | ||
'hh', fcntl.ioctl(tty_instance.fileno(), termios.TIOCGWINSZ, '1234') | ||
) | ||
except (IOError, OSError, NameError): | ||
try: | ||
length_width = (int(os.environ['LINES']), int(os.environ['COLUMNS'])) | ||
except (ValueError, KeyError): | ||
length_width = (24, 80) | ||
return length_width | ||
"""Returns terminal width and length as a tuple.""" | ||
harro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return shutil.get_terminal_size(fallback=(80, 24)) | ||
|
||
|
||
def LineWrap(text, omit_sgr=False): | ||
|
@@ -194,7 +182,7 @@ def LineWrap(text, omit_sgr=False): | |
Text with additional line wraps inserted for lines grater than the width. | ||
""" | ||
|
||
def _SplitWithSgr(text_line): | ||
def _SplitWithSgr(text_line, width): | ||
"""Tokenise the line so that the sgr sequences can be omitted.""" | ||
token_list = sgr_re.split(text_line) | ||
text_line_list = [] | ||
|
@@ -226,20 +214,20 @@ def _SplitWithSgr(text_line): | |
|
||
# We don't use textwrap library here as it insists on removing | ||
# trailing/leading whitespace (pre 2.6). | ||
(_, width) = TerminalSize() | ||
(term_width, _) = TerminalSize() | ||
text = str(text) | ||
text_multiline = [] | ||
for text_line in text.splitlines(): | ||
# Is this a line that needs splitting? | ||
while (omit_sgr and (len(StripAnsiText(text_line)) > width)) or ( | ||
len(text_line) > width | ||
while (omit_sgr and (len(StripAnsiText(text_line)) > term_width)) or ( | ||
len(text_line) > term_width | ||
): | ||
# If there are no sgr escape characters then do a straight split. | ||
if not omit_sgr: | ||
text_multiline.append(text_line[:width]) | ||
text_line = text_line[width:] | ||
text_multiline.append(text_line[:term_width]) | ||
text_line = text_line[term_width:] | ||
else: | ||
(multiline_line, text_line) = _SplitWithSgr(text_line) | ||
(multiline_line, text_line) = _SplitWithSgr(text_line, term_width) | ||
text_multiline.append(multiline_line) | ||
if text_line: | ||
text_multiline.append(text_line) | ||
|
@@ -318,7 +306,7 @@ def SetLines(self, lines): | |
ValueError, TypeError: Not a valid integer representation. | ||
""" | ||
|
||
(self._cli_lines, self._cli_cols) = TerminalSize() | ||
(self._cli_cols, self._cli_lines) = TerminalSize() | ||
|
||
if lines: | ||
self._cli_lines = int(lines) | ||
|
@@ -472,7 +460,7 @@ def main(argv=None): | |
# Prints the size of the terminal and returns. | ||
# Mutually exclusive to the paging of text and overrides that behaviour. | ||
if opt in ('-s', '--size'): | ||
print('Length: %d, Width: %d' % TerminalSize()) | ||
print('Width: %d, Length: %d' % TerminalSize()) | ||
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 is the only user facing change. |
||
return 0 | ||
elif opt in ('-d', '--delay'): | ||
isdelay = True | ||
|
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.
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.
Removed as this was a legacy of terminal.py being its own package.