Skip to content

Commit

Permalink
Merge pull request #49 from Yesser-Studios/no-weird-print
Browse files Browse the repository at this point in the history
Fix output being printed incorrectly when amount of outdated packages is greater than the height of the terminal window
  • Loading branch information
yesseruser authored Feb 15, 2024
2 parents d2a1fe4 + 81f88ed commit 8c4c173
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/ypkgupgr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ async def start_updates(lines: list[str]):
package_info = line.split()
package_name = package_info[0]

line_length[line_no] = 0

logger.debug(f"Starting to update {package_name}")
tasks.append(asyncio.create_task(update(package_name, line_no)))

Expand All @@ -138,6 +136,7 @@ def update_packages():

progress_ring(progress=0, intermediate=True)

# Remember to change string in misc.current_lines if changing this
print("Getting outdated pip packages...")
logger.info("Getting outdated packages.")

Expand All @@ -161,6 +160,7 @@ def update_packages():

clear_screen()

# Remember to change string in misc.current_lines if changing this
print("Updating packages using pip...")

logger.info("Starting to update packages...")
Expand Down
29 changes: 20 additions & 9 deletions src/ypkgupgr/graphics.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import io

from .logs import log_debug
from .colors import Colors
from .misc import failed, line_length
from .misc import failed, current_lines


def clear_screen():
Expand All @@ -13,9 +15,6 @@ def progress_ring(progress, complete=False, intermediate=False):
Updates Windows Terminal's progress ring.
"""

global outdated_count
global finished_count

# Checks if the WT_SESSION variable is set to prevent printing on consoles where this isn't supported.
if "WT_SESSION" not in os.environ:
log_debug("Progress ring not shown because WT_SESSION key not present.")
Expand All @@ -32,14 +31,26 @@ def progress_ring(progress, complete=False, intermediate=False):
else:
state = 2

# Prints the progress string according to https://github.com/MicrosoftDocs/terminal/blob/main/TerminalDocs/tutorials/progress-bar-sequences.md
# Show progress https://github.com/MicrosoftDocs/terminal/blob/main/TerminalDocs/tutorials/progress-bar-sequences.md
print(f"{chr(27)}]9;4;{state};{progress}{chr(7)}", end="")

log_debug(f"Progress ring updated with the following data: State: {state}; Progress: {progress}")


def progress_update(line: int, text: str):
for i in range(line_length[line] + 1):
text += " "
print(f"\033[{line};1H" + Colors.RESET + text)
line_length[line] = len(text)
if line < len(current_lines):
current_lines[line] = Colors.RESET + text
elif line == len(current_lines):
current_lines.append(Colors.RESET + text)
else:
for i in range(0, line - (len(current_lines))):
current_lines.append("")
current_lines.append(Colors.RESET + text)

buffer = io.StringIO() # To show processed output all at once
for string in current_lines:
buffer.write(string + "\n")
buffer.flush()

clear_screen()
print(buffer.getvalue())
6 changes: 4 additions & 2 deletions src/ypkgupgr/misc.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from .colors import Colors

failed = ""
outdated_count = 0
finished_count = 0
Expand All @@ -6,5 +8,5 @@

ran_from_script = False

line_length = dict()
line_count = 0
line_count = 0
current_lines = [Colors.RESET + "Getting outdated pip packages...", Colors.RESET + "Updating packages using pip..."]

0 comments on commit 8c4c173

Please sign in to comment.