Use ansi escape code to clear line

This commit is contained in:
Ivan Habunek 2024-05-31 12:57:51 +02:00
parent 3fe1faa18e
commit 38636e2b21
No known key found for this signature in database
GPG Key ID: F5F0623FF5EBCB3D
2 changed files with 9 additions and 4 deletions

View File

@ -1,4 +1,5 @@
import json import json
import sys
from itertools import islice from itertools import islice
from typing import Any, Callable, Generator, List, Optional, TypeVar from typing import Any, Callable, Generator, List, Optional, TypeVar
@ -10,6 +11,11 @@ from twitchdl.twitch import Clip, Video
T = TypeVar("T") T = TypeVar("T")
def clear_line():
sys.stdout.write("\033[1K")
sys.stdout.write("\r")
def truncate(string: str, length: int) -> str: def truncate(string: str, length: int) -> str:
if len(string) > length: if len(string) > length:
return string[: length - 1] + "" return string[: length - 1] + ""

View File

@ -7,7 +7,7 @@ from typing import Deque, Dict, NamedTuple, Optional
import click import click
from twitchdl.output import blue from twitchdl.output import blue, clear_line
from twitchdl.utils import format_size, format_time from twitchdl.utils import format_size, format_time
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -127,7 +127,8 @@ class Progress:
self._recalculate() self._recalculate()
click.echo(f"\rDownloaded {self.vod_downloaded_count}/{self.vod_count} VODs", nl=False) clear_line()
click.echo(f"Downloaded {self.vod_downloaded_count}/{self.vod_count} VODs", nl=False)
click.secho(f" {self.progress_perc}%", fg="blue", nl=False) click.secho(f" {self.progress_perc}%", fg="blue", nl=False)
if self.estimated_total is not None: if self.estimated_total is not None:
@ -141,6 +142,4 @@ class Progress:
if self.remaining_time is not None: if self.remaining_time is not None:
click.echo(f" ETA {blue(format_time(self.remaining_time))}", nl=False) click.echo(f" ETA {blue(format_time(self.remaining_time))}", nl=False)
click.echo(" ", nl=False)
self.last_printed = now self.last_printed = now