Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log support for console apps that use \r to update line (e.g.: progress bars) #462

Open
fishnux opened this issue Aug 20, 2023 · 1 comment
Labels
feature request New feature or request

Comments

@fishnux
Copy link
Contributor

fishnux commented Aug 20, 2023

Log support for console apps that use \r to update line (e.g.: progress bars)

Some console apps use \r to move the cursor to the beginning of the current line in order to update progress (e.g.: progress bar). In Dagu, a \r doesn't work, and a new line is printed instead, so the log file is spammed with progress updates.

In order to fix this in Dagu, there would be a need of a buffer to hold the last printed line, so it can be manipulated (with \r) before being written to a file. The web interface should show this last line, even if not written to the log file already (so we can see the progress).

Example script to be used for testing:

#!/usr/bin/env python3
import time
import sys

def simulate_progress_bar(total_steps):
    bar_size = 20  # The size of the progress bar in characters
    for step in range(total_steps + 1):
        progress = step / total_steps  # Calculate the progress as a fraction
        completed_chars = int(bar_size * progress)  # Calculate how many characters to fill based on progress
        # Create the progress bar string: [=====>        ]
        bar = '[' + '=' * completed_chars + ' ' * (bar_size - completed_chars) + ']'
        # Use \r to overwrite the current line with the updated progress bar and percentage
        sys.stdout.write('\r' + bar + f' {progress:.1%}')
        sys.stdout.flush()  # Flush the output to ensure it's immediately visible
        time.sleep(0.1)  # Simulate a process delay for demonstration purposes
    sys.stdout.write('\n')  # Print a newline when the progress is complete

if __name__ == "__main__":
    total_steps = 50  # Total number of steps for the progress
    simulate_progress_bar(total_steps)
@fishnux
Copy link
Contributor Author

fishnux commented Aug 27, 2023

This is more complex than I thought. Some apps won't output progress bars if the terminal is not a "TTY". In some cases progress bars are useful to be logged as they're the only way to keep track of a long running task, but not more than once a second (and in reality a progress bar may be updated many times per second, potentially spamming the log file [like I mentioned in the first post])

@yohamta yohamta added the feature request New feature or request label Dec 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants