Skip to content

Commit

Permalink
Scheduler: Handle SIGWINCH for JobStatusDisplay
Browse files Browse the repository at this point in the history
  • Loading branch information
zmedico committed Dec 7, 2024
1 parent b39f9f8 commit 102b725
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
13 changes: 8 additions & 5 deletions lib/_emerge/JobStatusDisplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ def __init__(self, quiet=False, xterm_titles=True):
if not isinstance(v, str):
self._term_codes[k] = v.decode(encoding, "replace")

if self._isatty:
width = portage.output.get_term_size()[1]
else:
width = self.max_display_width
self._set_width(width)
self.sigwinch()

def _set_width(self, width):
if width == getattr(self, "width", None):
Expand All @@ -79,6 +75,13 @@ def _set_width(self, width):
object.__setattr__(self, "width", width)
object.__setattr__(self, "_jobs_column_width", width - 32)

def sigwinch(self):
if self._isatty:
width = portage.output.get_term_size()[1]
else:
width = self.max_display_width
self._set_width(width)

@property
def out(self):
"""Use a lazy reference to sys.stdout, in case the API consumer has
Expand Down
11 changes: 11 additions & 0 deletions lib/_emerge/Scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,9 @@ def sighandler(signum, frame):
)
signal.siginterrupt(signal.SIGCONT, False)

earlier_sigwinch_handler = signal.signal(
signal.SIGWINCH, self._sigwinch_handler
)
try:
rval = self._merge()
finally:
Expand All @@ -1194,6 +1197,11 @@ def sighandler(signum, frame):
else:
signal.signal(signal.SIGCONT, signal.SIG_DFL)

if earlier_sigwinch_handler is not None:
signal.signal(signal.SIGWINCH, earlier_sigwinch_handler)
else:
signal.signal(signal.SIGWINCH, signal.SIG_DFL)

self._termination_check()
if received_signal:
sys.exit(received_signal[0])
Expand Down Expand Up @@ -1909,6 +1917,9 @@ def _schedule_merge_wakeup(self, future):
def _sigcont_handler(self, signum, frame):
self._sigcont_time = time.time()

def _sigwinch_handler(self, signum, frame):
self._status_display.sigwinch()

def _job_delay(self):
"""
@rtype: bool
Expand Down
12 changes: 2 additions & 10 deletions lib/portage/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,16 +522,8 @@ def get_term_size(fd=None):
fd = sys.stdout
if not hasattr(fd, "isatty") or not fd.isatty():
return (0, 0)
try:
import curses

try:
curses.setupterm(term=os.environ.get("TERM", "unknown"), fd=fd.fileno())
return curses.tigetnum("lines"), curses.tigetnum("cols")
except curses.error:
pass
except ImportError:
pass

# Do not use curses because it returns stale values after terminal resize.

try:
proc = subprocess.Popen(["stty", "size"], stdout=subprocess.PIPE, stderr=fd)
Expand Down

0 comments on commit 102b725

Please sign in to comment.