summary refs log tree commit diff
path: root/dot_ipython/profile_default/startup/94_devtools.py
blob: bffd2e8168cf35a2b973b3775c6dcec1b138078b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""Devtools utilities."""


def _setup():
    try:
        import devtools
    except ModuleNotFoundError:
        return
    import logging
    import time
    import fcntl
    import struct
    import termios
    import threading

    class DevtoolsResizer(threading.Thread):
        """Devtools width resizer depending on tty size."""

        def __init__(self, max_rows=200):
            self.logger = logging.getLogger("startup.devtools")
            self._run = False
            super().__init__(name=type(self).__name__, daemon=True)

        @staticmethod
        def _width():
            ioctl = fcntl.ioctl(
                0, termios.TIOCGWINSZ, struct.pack("HHHH", 0, 0, 0, 0)
            )
            th, tw, hp, wp = struct.unpack("HHHH", ioctl)
            return tw

        def run(self):
            self.logger.info("Devtools resizer running")
            self._run = True
            while self._run:
                width = self._width()
                if devtools.pformat._width != width:
                    self.logger.debug("Resized devtools to %d", devtools.pformat._width)
                    devtools.pformat._width = width
                time.sleep(0.2)

        def stop(self):
            self._run = False

    DevtoolsResizer().start()


_setup()

del _setup

_summarise_startup()