Terminal progress reporting plugin using OSC 9;4 ANSI sequences. Emits OSC 9;4 sequences to indicate test progress to terminal tabs/windows/etc. Not all terminal emulators support this feature. Ref: https://conemu.github.io/en/AnsiEscapeCodes.html#ConEmu_specific_OSC
| 1698 | |
| 1699 | |
| 1700 | class TerminalProgressPlugin: |
| 1701 | """Terminal progress reporting plugin using OSC 9;4 ANSI sequences. |
| 1702 | |
| 1703 | Emits OSC 9;4 sequences to indicate test progress to terminal |
| 1704 | tabs/windows/etc. |
| 1705 | |
| 1706 | Not all terminal emulators support this feature. |
| 1707 | |
| 1708 | Ref: https://conemu.github.io/en/AnsiEscapeCodes.html#ConEmu_specific_OSC |
| 1709 | """ |
| 1710 | |
| 1711 | def __init__(self, tr: TerminalReporter) -> None: |
| 1712 | self._tr = tr |
| 1713 | self._session: Session | None = None |
| 1714 | self._has_failures = False |
| 1715 | |
| 1716 | def _emit_progress( |
| 1717 | self, |
| 1718 | state: Literal["remove", "normal", "error", "indeterminate", "paused"], |
| 1719 | progress: int | None = None, |
| 1720 | ) -> None: |
| 1721 | """Emit OSC 9;4 sequence for indicating progress to the terminal. |
| 1722 | |
| 1723 | :param state: |
| 1724 | Progress state to set. |
| 1725 | :param progress: |
| 1726 | Progress value 0-100. Required for "normal", optional for "error" |
| 1727 | and "paused", otherwise ignored. |
| 1728 | """ |
| 1729 | assert progress is None or 0 <= progress <= 100 |
| 1730 | |
| 1731 | # OSC 9;4 sequence: ESC ] 9 ; 4 ; state ; progress ST |
| 1732 | # ST can be ESC \ or BEL. ESC \ seems better supported. |
| 1733 | match state: |
| 1734 | case "remove": |
| 1735 | sequence = "\x1b]9;4;0;\x1b\\" |
| 1736 | case "normal": |
| 1737 | assert progress is not None |
| 1738 | sequence = f"\x1b]9;4;1;{progress}\x1b\\" |
| 1739 | case "error": |
| 1740 | if progress is not None: |
| 1741 | sequence = f"\x1b]9;4;2;{progress}\x1b\\" |
| 1742 | else: |
| 1743 | sequence = "\x1b]9;4;2;\x1b\\" |
| 1744 | case "indeterminate": |
| 1745 | sequence = "\x1b]9;4;3;\x1b\\" |
| 1746 | case "paused": |
| 1747 | if progress is not None: |
| 1748 | sequence = f"\x1b]9;4;4;{progress}\x1b\\" |
| 1749 | else: |
| 1750 | sequence = "\x1b]9;4;4;\x1b\\" |
| 1751 | |
| 1752 | self._tr.write_raw(sequence, flush=True) |
| 1753 | |
| 1754 | @hookimpl |
| 1755 | def pytest_sessionstart(self, session: Session) -> None: |
| 1756 | self._session = session |
| 1757 | # Show indeterminate progress during collection. |
no outgoing calls