From 38151f9aec69e4ff7f6a1604c5ef46c0135a1c7d Mon Sep 17 00:00:00 2001 From: jesko Date: Mon, 1 Apr 2024 12:10:21 +0200 Subject: [PATCH] ensure terminating separator line when chunks are discarded --- refinery/units/sinks/peek.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/refinery/units/sinks/peek.py b/refinery/units/sinks/peek.py index 86ff708097..de9db888d3 100644 --- a/refinery/units/sinks/peek.py +++ b/refinery/units/sinks/peek.py @@ -6,11 +6,13 @@ import os import textwrap import codecs +import itertools +import collections from refinery.units.sinks import Arg, HexViewer from refinery.lib.meta import ByteStringWrapper, metavars, CustomStringRepresentation, SizeInt from refinery.lib.types import INF -from refinery.lib.tools import get_terminal_size, isbuffer, lookahead +from refinery.lib.tools import get_terminal_size, isbuffer from refinery.lib.environment import environment @@ -291,11 +293,26 @@ def filter(self, chunks): except ImportError: pass discarded = 0 - for final, item in lookahead(chunks): - item.temp = final - if not item.visible and self.isatty: + it = iter(chunks) + buffer = collections.deque(itertools.islice(it, 0, 2)) + buffer.reverse() + + while buffer: + if self.isatty and not buffer[0].visible: + buffer.popleft() discarded += 1 else: - yield item + item = buffer.pop() + last = not bool(buffer) + item.temp = last + if not item.visible and self.isatty: + discarded += 1 + else: + yield item + try: + buffer.appendleft(next(it)) + except StopIteration: + pass + if discarded: self.log_warn(F'discarded {discarded} invisible chunks to prevent them from leaking into the terminal.')