From 82949237b7527e565c7c8ef914adff6586d9381e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Fri, 2 May 2025 15:13:06 +0200 Subject: [PATCH] fix terminal output when terminal is put into interactive mode --- pkgs/clan-cli/clan_cli/custom_logger.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkgs/clan-cli/clan_cli/custom_logger.py b/pkgs/clan-cli/clan_cli/custom_logger.py index beebdcef0..9d2698e34 100644 --- a/pkgs/clan-cli/clan_cli/custom_logger.py +++ b/pkgs/clan-cli/clan_cli/custom_logger.py @@ -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()