Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 44 additions & 11 deletions pym/bob/tty.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ def setVerbosity(self, verbosity):
def cleanup(self):
pass

def suspend(self):
"""Restore tty settings upon SIGTSTP (terminal stop)"""
self.cleanup()

def resume(self):
"""Resume tty after SIGTSTP"""
pass

def setProgress(self, done, num):
pass

Expand Down Expand Up @@ -250,6 +258,9 @@ def __init__(self, verbosity, maxJobs):
self.__tasksDone = 0
self.__tasksNum = 1

self.__ttyInit()

def __ttyInit(self):
# disable cursor
print("\x1b[?25l")

Expand Down Expand Up @@ -353,6 +364,10 @@ def cleanup(self):
except ImportError:
pass

def resume(self):
self.__ttyInit()
self.__putFooter()

def setProgress(self, done, num):
self.__tasksDone = done
self.__tasksNum = num
Expand Down Expand Up @@ -451,6 +466,9 @@ def __init__(self, verbosity, maxJobs):
self.__tasksDone = 0
self.__tasksNum = 1

self.__ttyInit()

def __ttyInit(self):
# disable cursor
print("\x1b[?25l")

Expand Down Expand Up @@ -541,6 +559,10 @@ def cleanup(self):
except ImportError:
pass

def resume(self):
self.__ttyInit()
self.__putFooter()

def setProgress(self, done, num):
self.__tasksDone = done
self.__tasksNum = num
Expand Down Expand Up @@ -592,24 +614,35 @@ def ttyReinit():
if __onTTY and sys.platform == "win32":
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), __origMode.value | 4)

def handleTerminalStop(signum, frame):
__tui.suspend()
signal.raise_signal(signal.SIGSTOP)
__tui.resume()

# module initialization

__onTTY = (sys.stdout.isatty() and sys.stderr.isatty())
__useColor = False
__tui = SingleTUI(NORMAL)
__parallelTUIThreshold = 16

if __onTTY and sys.platform == "win32":
# Try to set ENABLE_VIRTUAL_TERMINAL_PROCESSING flag. Enables vt100 color
# codes on Windows 10 console. If this fails we inhibit color code usage
# because it will clutter the output.
import ctypes
import ctypes.wintypes
__origMode = ctypes.wintypes.DWORD()
kernel32 = ctypes.windll.kernel32
kernel32.GetConsoleMode(kernel32.GetStdHandle(-11), ctypes.byref(__origMode))
if not kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), __origMode.value | 4):
__onTTY = False
if __onTTY:
if sys.platform == "win32":
# Try to set ENABLE_VIRTUAL_TERMINAL_PROCESSING flag. Enables vt100 color
# codes on Windows 10 console. If this fails we inhibit color code usage
# because it will clutter the output.
import ctypes
import ctypes.wintypes
__origMode = ctypes.wintypes.DWORD()
kernel32 = ctypes.windll.kernel32
kernel32.GetConsoleMode(kernel32.GetStdHandle(-11), ctypes.byref(__origMode))
if not kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), __origMode.value | 4):
__onTTY = False
else:
# Intercept SIGTSTP to leave TTY in a sane state if user presses Ctrl+Z.
import signal
signal.signal(signal.SIGTSTP, handleTerminalStop)


def setColorMode(mode):
global __useColor
Expand Down