|
28 | 28 | #
|
29 | 29 |
|
30 | 30 | import os
|
| 31 | +import sys |
31 | 32 | import shlex
|
32 | 33 | import struct
|
33 | 34 | import platform
|
@@ -72,6 +73,46 @@ def entry(text_to_print):
|
72 | 73 | elif pyversion == 2:
|
73 | 74 | return raw_input(text_to_print)
|
74 | 75 |
|
| 76 | + |
| 77 | +def interrupt(): |
| 78 | + """ |
| 79 | + Print what to do to abort the script; dynamic depending on OS |
| 80 | + Useful when you have a long-running script that you might want t interrupt part way through |
| 81 | + """ |
| 82 | + |
| 83 | + print('(Press Ctrl-%s to quit at any time.)' % 'C' if os.name == 'nt' else 'D') |
| 84 | + |
| 85 | + |
| 86 | +def overtype(*objects, sep=' ', end='', file=sys.stdout, flush=False): |
| 87 | + """ |
| 88 | + Print `objects` to the text stream `file`, starting with "\r", separated by `sep` and followed by `end`. |
| 89 | + `sep`, `end`, `file` and `flush`, if present, must be given as keyword arguments |
| 90 | +
|
| 91 | + All non-keyword arguments are converted to strings like ``str()`` does and written to the stream, |
| 92 | + separated by `sep` and followed by `end`. |
| 93 | +
|
| 94 | + If no objects are given, ``overwrite()` will just write "\r". |
| 95 | +
|
| 96 | + Based on the Python print() docs: https://docs.python.org/3/library/functions.html#print |
| 97 | +
|
| 98 | + # This does not currently work in the PyCharm console, at least on Windows |
| 99 | +
|
| 100 | + :param objects: |
| 101 | + :param sep: String to separate the objects with, by default " " |
| 102 | + :type sep: str |
| 103 | + :param end: String to end with, by default nothing |
| 104 | + :type end: str |
| 105 | + :param file: An object with a ``write(string)`` method; default ``sys.stdout`` |
| 106 | + :param flush: If true, the stream is forcibly flushed. |
| 107 | + :type flush: bool |
| 108 | + :return: |
| 109 | + """ |
| 110 | + |
| 111 | + object0 = f"\r{objects[0]}" |
| 112 | + objects = (object0, *objects[1:]) |
| 113 | + print(*objects, sep=sep, end=end, file=file, flush=flush) |
| 114 | + |
| 115 | + |
75 | 116 | def get_terminal_size():
|
76 | 117 | """
|
77 | 118 | Get width and height of console
|
|
0 commit comments