rsalmei / alive-progress

A new kind of Progress Bar, with real-time throughput, ETA, and very cool animations!
MIT License
5.45k stars 205 forks source link

Newlines end up printed even when only ANSI Escape Codes were printed #232

Closed Illegal-Services closed 1 year ago

Illegal-Services commented 1 year ago

Hello there, first of all this is an awesome project, thanks for it! I'm new to it and I think I've found a bug where I set the title of my script using ANSI escape code.

I just don't want to display any text/newlines at all when I run this code but I keep getting:


[########################################] 10/10 [100%] in 0.0s (4338.66/s)

D:\Downloads\test>

Can someone light me up if I'm doing something wrong or if this really is a problem with alive-progress project please? Here's a minimalistic code snippet, and my Python version if needed? Python 3.11.2:

import time
from alive_progress import alive_it

def set_console_title(title) -> None:
    print(f"\033]0;{title}\007", end="", flush=True)

for i in alive_it(range(10), theme="classic", bar="classic2", enrich_print=False):
    print("\033]2;This is working and not showing up, but a newline get somehow in the stdout output.\007", end="")
    set_console_title(i)
    time.sleep(0.3)
Illegal-Services commented 1 year ago
import ctypes
import time
import sys
from alive_progress import alive_it

def set_console_title(title) -> None:
    sys.stdout.flush()
    ctypes.windll.kernel32.SetConsoleTitleW(f"{title}")

for i in alive_it(range(10), theme="classic", bar="classic2", enrich_print=False):
    set_console_title(i)
    time.sleep(0.3)

Here is a work-around method that does the job alternatively of the "print title" method.

rsalmei commented 1 year ago

Hi @Illegal-Services, thank you!

Well, are you sure you're using the codes correctly? I've never seen \033], just \033[... What are you trying to do?

Illegal-Services commented 1 year ago

"What are you trying to do?"

Just to set the console title

firefox_2023-03-28_15-03

TheTechRobo commented 1 year ago

I'm not sure what ChatGPT is talking about, as [ is the correct way to do it as far as I know. Try it. Works for me, at least...

Don't use the \007 with [ though. That might be what's confusing chatgpt.

EDIT: Nevermind - I thought you were trying to do colours, not set titles. Looks like ] is indeed the correct one for that. Sorry!

rsalmei commented 1 year ago

Ahh, ok, now I got what you're trying to do. That is indeed a different kind of escape code, look:

image

Anyway, I know what is happening. I install an automatic system print hook that captures all bytes sent to stdout until a newline is found. When this happens, I prepare the line header and send the whole text to the actual stdout including a newline, since the bar is refreshing itself very fast, and to see the printed text we need the cursor on a new line. Disabling enrich_print doesn't really do anything here because I still need to clean the line after anything is printed, and move the bar to the next line, or garbage characters from a previous bar refresh would remain there. The fix should be in a layer above, regardless of whether enrich_print is enabled or not.

I'm working on it.