Skip to content

Commit 73d5ef9

Browse files
committed
control: fix streaming text issue in _logs
The text was coming out colorized before. That's why we need to use secho. Also the text would display with odd linebreaks. `iter_lines_from_stream` fixes this.
1 parent 3ca5df2 commit 73d5ef9

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/warnet/control.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,15 +369,31 @@ def _logs(pod_name: str, follow: bool):
369369
return # cancelled by user
370370

371371
try:
372-
stream = pod_log(pod_name, container_name=None, follow=follow)
373-
for line in stream.stream():
374-
print(line.decode("utf-8"), end=None)
372+
stream = pod_log(pod_name, container_name=None, follow=follow, namespace=pod_namespace)
373+
for line in iter_lines_from_stream(stream):
374+
click.secho(line)
375375
except Exception as e:
376376
print(e)
377377
except KeyboardInterrupt:
378378
print("Interrupted streaming log!")
379379

380380

381+
def iter_lines_from_stream(log_stream, encoding="utf-8"):
382+
decoder = codecs.getincrementaldecoder(encoding)()
383+
buffer = ""
384+
for chunk in log_stream.stream():
385+
# Decode the chunk incrementally
386+
text = decoder.decode(chunk)
387+
buffer += text
388+
# Split the buffer into lines
389+
lines = buffer.split("\n")
390+
buffer = lines.pop() # Last item is incomplete line or empty
391+
yield from lines
392+
# Yield any remaining text in the buffer
393+
if buffer:
394+
yield buffer
395+
396+
381397
@click.command()
382398
@click.argument("tank_name", required=False)
383399
@click.option("--all", "-a", "snapshot_all", is_flag=True, help="Snapshot all running tanks")

0 commit comments

Comments
 (0)