fix terminal output when terminal is put into interactive mode

This commit is contained in:
Jörg Thalheim
2025-05-02 15:13:06 +02:00
parent 7abb8bb662
commit 82949237b7

View File

@@ -2,6 +2,7 @@ import inspect
import logging
import os
import sys
import termios
from pathlib import Path
from typing import Any
@@ -74,7 +75,16 @@ class PrefixFormatter(logging.Formatter):
if self.trace_prints:
format_str += f"\nSource: {filepath}:%(lineno)d::%(funcName)s\n"
return logging.Formatter(format_str).format(record)
line = logging.Formatter(format_str).format(record)
try:
# Programs like sudo can set the terminal into raw mode.
# This means newlines are no longer translated to include a carriage return to the end.
# https://unix.stackexchange.com/questions/151916/why-is-this-binary-file-transferred-over-ssh-t-being-changed/151963#15196
if not termios.tcgetattr(sys.stdout.fileno())[3] & termios.ECHO:
line += "\r"
except Exception: # not a tty or mocked sys.stdout
pass
return line
def hostname_colorcode(self, hostname: str) -> tuple[int, int, int]:
colorcodes = RgbColor.list_values()