Don't guess terinal size.

This commit is contained in:
Julien Palard 2021-01-19 16:24:22 +01:00
parent ad27b7ce70
commit f3527b905d

View File

@ -2,6 +2,7 @@ import os
import asyncio import asyncio
from typing import Tuple from typing import Tuple
from time import perf_counter from time import perf_counter
from shutil import get_terminal_size
import subprocess import subprocess
@ -82,10 +83,10 @@ DISABLE_CURSOR = "\033[?25l"
ENABLE_CURSOR = "\033[?25h" ENABLE_CURSOR = "\033[?25h"
def truncate(string, max_width=120): def truncate(string, max_width):
if len(string) < max_width: if len(string) <= max_width:
return string return string
return string[:max_width] + "" return string[: max_width - 1] + ""
async def show_progression(results): async def show_progression(results):
@ -95,6 +96,7 @@ async def show_progression(results):
currently_running = [] currently_running = []
frameno = 0 frameno = 0
print(DISABLE_CURSOR, end="") print(DISABLE_CURSOR, end="")
columns, _ = get_terminal_size()
try: try:
while True: while True:
result = await results.get() result = await results.get()
@ -113,14 +115,14 @@ async def show_progression(results):
if msgtype in ("CHANGED", "FAILED", "UNREACHABLE"): if msgtype in ("CHANGED", "FAILED", "UNREACHABLE"):
print(msg) print(msg)
status_line = ( status_line = (
FRAMES[frameno % len(FRAMES)] + " "
f"{len(currently_running)} playbook{'s' if len(currently_running) > 1 else ''} running: " f"{len(currently_running)} playbook{'s' if len(currently_running) > 1 else ''} running: "
f"{truncate(', '.join(currently_running), max_width=100)}" f"{', '.join(currently_running)}"
) )
print( print(
"\033[0J", # ED (Erase In Display) with parameter 0: "\033[0J", # ED (Erase In Display) with parameter 0:
# Erase from the active position to the end of the screen. # Erase from the active position to the end of the screen.
FRAMES[frameno % len(FRAMES)], truncate(status_line, max_width=columns),
f"{status_line:126}",
end="\r", end="\r",
) )
finally: finally: